Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Tic-Tac-Toe program
- //
- // <<<AMAR FAROOQI>>>
- //
- // U. of Illinois, Chicago
- // CS109, Fall 2012: HW11-PE2
- #include <iostream>
- #include <string>
- using namespace std;
- //
- // draw:
- //
- // Outputs the board on the console like this:
- //
- // X | O |
- // O | X | O
- // | | X
- //
- // In other words, board values of 1 are drawn as X, values of 0 are drawn
- // as O, and values of -1 are drawn empty.
- //
- void draw(int board[3][3])
- {
- int r, c;
- for (r=0; r<3; r++)
- {
- for (c=0; c<3; c++)
- {
- if (c==0)
- cout<< " ";
- if (board[r][c]==1)
- cout<<"X";
- else if (board [r][c]==0)
- cout<<"O";
- else if (board [r][c]==-1)
- cout<<" ";
- if (c==0 || c==1)
- cout<< " | ";
- else if (c==2)
- cout<<endl;
- }
- }
- }//draw
- //
- // winner:
- //
- // Returns:
- // 0: if player "O" has won
- // 1: if player "X" has won
- // -1: if neither player has won
- //
- int winner(int board[3][3])
- {
- //Across for X
- if ( board[0][0] == 1 && board[0][1] == 1 && board[0][2] == 1)
- return 1;
- else if ( board[1][0] == 1 && board[1][1] == 1 && board[1][2] == 1 )
- return 1;
- else if (board[2][0] == 1 && board[2][1] == 1 && board[2][2] == 1 )
- return 1;
- // Across for 0
- else if ( board[0][0] == 0 && board[0][1] == 0 && board[0][2] == 0)
- return 0;
- else if ( board[1][0] == 0 && board[1][1] == 0 && board[1][2] == 0 )
- return 0;
- else if (board[2][0] == 0 && board[2][1] == 0 && board[2][2] == 0 )
- return 0;
- //UP Down for X
- else if (board [0][0] ==1 && board [1][0]==1 && board[2][0]==1)
- return 1;
- else if (board [0][1] ==1 && board [1][1]==1 && board[2][1]==1)
- return 1;
- else if (board [0][2] ==1 && board [1][2]==1 && board[2][2]==1)
- return 1;
- //up down for 0
- else if (board [0][0] ==0 && board [1][0]==0 && board[2][0]==0)
- return 0;
- else if (board [0][1] ==0 && board [1][1]==0 && board[2][1]==0)
- return 0;
- else if (board [0][2] ==0 && board [1][2]==0 && board[2][2]==0)
- return 0;
- //diagnol for x
- else if (board [0][0] ==1 && board [1][1]==1 && board[2][2]==1)
- return 1;
- else if (board [0][2] ==1 && board [1][1]==1 && board[2][0]==1)
- return 1;
- else if (board [0][0] ==0 && board [1][1]==0 && board[2][2]==0)
- return 0;
- else if (board [0][2] == 0 && board [1][1]==0 && board[2][0]==0)
- return 0;
- //
- // TODO! There are 8 ways for player O to win, and 8 ways for player X to win.
- //
- return -1;
- }//winner
- //
- // gameover:
- //
- // Returns true if the game is over, false if not
- //
- bool gameover(int board[3][3])
- {
- int r, c;
- //
- // the game is over if someone has won:
- //
- if (winner(board) == 0 || winner(board) == 1)
- {
- return true;
- }
- //
- // the game is also over if it's a draw --- i.e. neither player
- // has won, and there are no more moves available on the board:
- //
- bool moveOpen; // move open flag
- moveOpen = false; // assume there is no move open:
- for (r = 0; r < 3; r++) // now search board for an open move...
- {
- for (c = 0; c < 3; c++)
- {
- if (board[r][c] == -1) // found one!
- {
- moveOpen = true; // set flag denoting an open move:
- }
- }//for
- }//for
- // is the game over? Not if there is an open move...
- if (moveOpen)
- return false; // open move ==> not over
- else
- return true; // NO open moves ==> gave over
- }
- //
- // main:
- //
- // Plays a game of Tic-Tac-Toe, using a 3x3 integer array. The human is player X, and
- // represented by a 1 in the array. The computer is player O, and represented by a 0
- // in the array. Empty locations are denoted by -1. Human goes first.
- //
- int main()
- {
- //
- // NOTE: this is an initial configuration of the board for TESTING purposes
- // only; change values to test your draw and winner functions. Once those
- // functions are working, you can use this approach to initialize the board
- // to empty.
- int r,c;
- cout << "**TESTING: draw function: " << endl;
- int board [3][3]={-1, -1, -1,
- -1, -1, -1,
- -1, -1, -1};
- while (!gameover(board))
- {
- draw(board);
- cin >> r >> c;
- r=r-1;
- c=c-1;
- board[r][c]=1;
- for (r=0; r<3; r++)
- {
- for (c=0; c<3; c++)
- {
- if (board[r][c] == -1)
- break;
- }
- board[r][c] = 0;
- break;
- }
- }
- // test your winner function:
- cout << endl;
- cout << "** TESTING: winner function returns: " << winner(board) << endl;
- cout << endl;
- //
- // TODO!
- //
- //
- // done!
- //
- //system("Pause"); // keep console open when running inside Dev-C++ or Visual Studio:
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement