Hongyao Shi / Mbed 2 deprecated Fianl_test

Dependencies:   4DGL-uLCD-SE EthernetInterface HTTPClient mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers board.cpp Source File

board.cpp

00001 #include "stdio.h"
00002 #include "stdlib.h"
00003 #include "board.h"
00004 
00005 
00006 
00007 Board::Board(void)
00008 {
00009     board = new int*[3];
00010     for(int i=0; i<3; i++)
00011     {
00012         board[i] = new int[3];
00013         for(int j=0; j<3; j++)
00014             board[i][j]=0;
00015     }
00016 }
00017 
00018 int ** Board::get_board()
00019 {
00020     return board;
00021 }
00022 
00023 void Board::set_board(int col, int row, int playerID)
00024 {
00025     if(board[col][row]!=0 && turn == playerID)
00026     {}
00027     else
00028     {
00029         board[col][row] = playerID;
00030     }
00031 }
00032 
00033 bool Board::updateBoard(int turn, int printout)
00034 {
00035     int location_x = printout/3;
00036     int location_y = printout%3;
00037     if(board[location_x][location_y] == 0)
00038     {
00039         board[location_x][location_y] = turn;
00040         return true;
00041     }
00042     else
00043         return false;
00044 }
00045 
00046 int Board::updateBoard_output(int turn, int printout)
00047 {
00048     int location_x = printout/3;
00049     int location_y = printout%3;
00050     return board[location_x][location_y];
00051 }
00052 
00053 
00054 int Board::check_victory(int playerID)
00055 {
00056     if(board[0][0] == playerID && board[0][1] == playerID && board[0][2] == playerID
00057     ||board[0][0] == playerID && board[1][1] == playerID && board[2][2] == playerID
00058     ||board[0][0] == playerID && board[1][0] == playerID && board[2][0] == playerID
00059     ||board[1][0] == playerID && board[1][1] == playerID && board[1][2] == playerID
00060     ||board[2][0] == playerID && board[2][1] == playerID && board[2][2] == playerID
00061     ||board[0][1] == playerID && board[1][1] == playerID && board[1][2] == playerID
00062     ||board[0][1] == playerID && board[2][1] == playerID && board[2][2] == playerID
00063     ||board[0][2] == playerID && board[1][1] == playerID && board[2][0] == playerID)
00064         return playerID;
00065     else
00066         return 0;
00067 }
00068     
00069