Advertisement
ZornTaov

TicTacToe

Apr 7th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //initialize a globally used grid with *'s
  5. char grid[3][3] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
  6.  
  7. //acts as a bool, only 'x' or 'o' is used
  8. char turn = 'o';
  9.  
  10. void displayBoard()
  11. {
  12.     //display ALL of the board! _o/
  13.     cout << "\n  1  2  3"
  14.         <<"\n1 " << grid[0][0] << " | " << grid[0][1] << " | " << grid[0][2] << endl
  15.         << " ---------" << endl
  16.         << "2 " << grid[1][0] << " | " << grid[1][1] << " | " << grid[1][2] << endl
  17.         << " ---------" << endl
  18.         << "3 " << grid[2][0] << " | " << grid[2][1] << " | " << grid[2][2] << endl;
  19. }
  20. bool checkWin(){
  21.    
  22.     if( grid[0][0] == turn && grid[0][1] == turn &&  grid[0][2] == turn){
  23.         return true;//row 1
  24.     } else if( grid[1][0] == turn &&  grid[1][1] == turn  && grid[1][2] == turn){
  25.         return true;//row 2
  26.     } else if( grid[2][0] == turn &&  grid[2][1] == turn  && grid[2][2] == turn){
  27.         return true;//row 3
  28.     } else if( grid[0][0] == turn &&  grid[1][0] == turn  && grid[2][0] == turn){
  29.         return true;//col 1
  30.     } else if( grid[0][1] == turn &&  grid[1][1] == turn  && grid[2][1] == turn){
  31.         return true;//col 2
  32.     } else if( grid[2][0] == turn &&  grid[2][1] == turn  && grid[2][2] == turn){
  33.         return true;//col 3
  34.     } else if( grid[0][0] == turn &&  grid[1][1] == turn  && grid[2][2] == turn){
  35.         return true;//diagonal 1
  36.     } else if( grid[0][2] == turn &&  grid[1][1] == turn  && grid[2][0] == turn){
  37.         return true;//diagonal 1
  38.     }
  39.     return false;
  40. }
  41. //check if the input is between upper and lower
  42. bool inBounds(int i, int lower, int upper)
  43. {
  44.     if(i>=lower&&i<=upper) return true;
  45.     else return false;
  46. }
  47.  
  48. void move()
  49. {
  50.     int x,y;
  51.     cout << "\nEnter your move Player "<<turn<<": ";
  52.     cout << "\nEnter Row: ";
  53.     cin >> x;
  54.     cout << "\nEnter Col: ";
  55.     cin >> y;
  56.     //check if it's a * and if the x and y are between 1 and 3
  57.     if(grid[x-1][y-1] == '*' && inBounds(x, 1, 3) && inBounds(y, 1, 3)){
  58.         grid[x-1][y-1] = turn;
  59.     }
  60.     else
  61.         move();//recursion if invalid move
  62.     displayBoard();//refresh the board
  63.  
  64. }
  65. int main()
  66. {
  67.     displayBoard();
  68.     //has someone won yet? if not keep playing
  69.     while(!checkWin())
  70.     {  
  71.         if(turn == 'x') turn = 'o';
  72.         else turn = 'x';
  73.         move();
  74.         //swap players
  75.     }
  76.     //all done! call the winner!
  77.     cout << turn << " Wins!!!!";
  78.  
  79.     cin >> turn;//dummy input
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement