#include using namespace std; int winner(int boardposition[][3]); //prototypes int horizontalwin(int boardposition[][3]); int verticalwin(int boardposition[][3]); int diagonalwin(int boardposition[][3]); bool boardfull(int boardposition[][3]); int main() { int boardposition[3][3]; int x, y; for ( x = 1; x <= 3; x++) { for ( y = 1; y <= 3; y++) { cout << "Value for x " << x << " y " << y << "?" << endl; cin >> boardposition[x][y]; } } cout << boardposition[1][1] << boardposition[2][1] << boardposition[3][1] << endl; cout << boardposition[1][2] << boardposition[2][2] << boardposition[3][2] << endl; cout << boardposition[1][3] << boardposition[2][3] << boardposition[3][3] << endl; cout << "winner returns: " << winner(boardposition) << endl; cout << "horizontalwin returns: " << horizontalwin(boardposition) << endl; cout << "verticalwin returns: " << verticalwin(boardposition) << endl; cout << "diagonalwin returns: " << diagonalwin(boardposition) << endl; cout << "boardfull returns: " << boardfull(boardposition) << endl; return 0; } int winner(int boardposition[][3]) { // binds everything else together... // returns one of 4 states // 0--no winner // 1--X win // 2--O win // 3--Cat's Game switch (horizontalwin(boardposition)) { case 0 : break; case 1 : return boardposition[1][1]; case 2 : return boardposition[1][2]; case 3 : return boardposition[1][3]; } switch (verticalwin(boardposition)) { case 0 : break; case 1 : return boardposition[1][1]; case 2 : return boardposition[2][1]; case 3 : return boardposition[3][1]; } switch (diagonalwin(boardposition)) { case 0 : break; case 1 : return boardposition[1][1]; case 2 : return boardposition[1][3]; } if (boardfull(boardposition)==true) { return 4; } return 0; } int horizontalwin(int boardposition[][3]) { //will return one of 4 states: // 0--no horizontal winner // 1--top row winner // 2--middle row winner // 3--bottom row winner if ( boardposition[1][1] == boardposition[2][1] == boardposition[3][1] && boardposition[1][1] != 0 ) //we don't want this reported at //the very beginning of the game { return 1; } if ( boardposition[1][2] == boardposition[2][2] == boardposition[3][2] && boardposition[1][2] != 0 ) //we don't want this reported at //the very beginning of the game { return 2; } if ( boardposition[1][3] == boardposition[2][3] == boardposition[3][3] && boardposition[1][3] != 0 ) //we don't want this reported at //the very beginning of the game { return 3; } return 0; } int verticalwin(int boardposition[][3]) { //will return one of 4 states: // 0--no vertical winner // 1--left column winner // 2--middle column winner // 3--right column winner if ( boardposition[1][1] == boardposition[1][2] == boardposition[1][3] && boardposition[1][1] != 0 ) //we don't want this reported at //the very beginning of the game { return 1; } if ( boardposition[2][1] == boardposition[2][2] == boardposition[2][3] && boardposition[2][1] != 0 ) //we don't want this reported at //the very beginning of the game { cout << boardposition[1][1] << boardposition[2][1] << boardposition[3][1] << endl; cout << boardposition[1][2] << boardposition[2][2] << boardposition[3][2] << endl; cout << boardposition[1][3] << boardposition[2][3] << boardposition[3][3] << endl;Z return 2; } if ( boardposition[3][1] == boardposition[3][2] == boardposition[3][3] && boardposition[3][1] != 0 ) //we don't want this reported at //the very beginning of the game { return 3; } return 0; } int diagonalwin(int boardposition[][3]) { int horizontalwin(int boardposition[][3]); //will return one of these numbers: // 0--no diagonal winner // 1--top-left to bottom-right winner // 2--bottom-left to top-right winner if ( boardposition[1][1] == boardposition[2][2] == boardposition[3][3] && boardposition[1][1] != 0 ) //we don't want this reported at //the very beginning of the game { return 1; } if ( boardposition[3][1] == boardposition[2][2] == boardposition[1][3] && boardposition[3][1] != 0 ) //we don't want this reported at //the very beginning of the game { return 2; } return 0; } bool boardfull(int boardposition[][3]) //determines if the board is full, { //used to find a draw game if no winner int x, y; for ( x = 1; x <= 3; x++) { for ( y = 1; y <= 3; y++) { if (boardposition[x][y]==0) { return false; } } } return true; }