By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,474 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,474 IT Pros & Developers. It's quick & easy.

Tic Tac Toe - Every time it comes two the second player it tells me space already use

P: 1
Every time it comes two the second player it tells me space already used. What am I doing wrong?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. //<strong class = "highlight" > using </strong> namespace std;
  7.  
  8. class TicTacToe
  9. {
  10. public:
  11.     TicTacToe();    // constructor
  12.         int Pick_Player();   // member function
  13.         int Pick_Row();    // member functions
  14.         int Pick_Column();  // member functions
  15.         int Check_Board();  // member function
  16.         void Choice_by_Player(int);  // member functions
  17.         void Choice_of_Row(int);   // member functions
  18.         void Choice_of_Column(int);  // member functions
  19.         void Tic_Tac_Toe_Board();  // member functions
  20.         bool Check_Move(int,int); // member functions
  21.  
  22. private:
  23.         int row;
  24.         int column;
  25.         int player;
  26.         int board[3][3];
  27.         char display_board[3][3];
  28. };
  29.  
  30. TicTacToe::TicTacToe()//:row(0):column(0):player(1):board(0)(0):display_board(' ')(' ')
  31. {
  32.     row = 0;
  33.     column = 0;
  34.     player = 1;
  35.     int i = 0;
  36.     int j = 0;
  37.     for ( i = 0; i < 3; i++)
  38.     {
  39.         for ( j = 0; j < 3; j++)
  40.         {
  41.             board[i][j] = 0;
  42.             display_board[i][j] = ' ';
  43.         }
  44.     }    
  45. }
  46.  
  47. int TicTacToe::Pick_Player()
  48. {
  49.     return player;
  50. }
  51.  
  52. int TicTacToe::Pick_Row()
  53. {
  54.     return row;
  55. }
  56.  
  57. int TicTacToe::Pick_Column()
  58. {
  59.     return column;
  60. }
  61.  
  62. void TicTacToe::Choice_by_Player(int a)
  63. {
  64.     player = a;
  65. }
  66.  
  67. void TicTacToe::Choice_of_Row(int b)
  68. {
  69.     row = b;
  70. }
  71.  
  72. void TicTacToe::Choice_of_Column(int c)
  73. {
  74.     column = c;
  75. }
  76.  
  77. bool TicTacToe::Check_Move(int row, int column)
  78. {
  79.     row = 0;
  80.     column = 0;
  81.  
  82.     if ( row != 0  && row != 1 && row != 2 ) 
  83.     {
  84.         cout << " Invalid choice!!";
  85.         cout << endl;
  86.         return 0;
  87.     }
  88.     else if ( column != 0 && column != 1 && column != 2 )
  89.     {
  90.         cout << " Invalid choice!! " << endl;
  91.         return 0;
  92.     }
  93.     else if ( board[row][column] == 1 || board[row][column] == 2)
  94.     {
  95.         cout << " Space already used. Try Again. " << endl;
  96.         return 0;
  97.     }
  98.     else 
  99.     {
  100.         board[row][column] = player;
  101.         return 1;
  102.     }
  103. }   // end of Check_Move
  104.  
  105. int TicTacToe::Check_Board()
  106. {
  107.     int i = 0;
  108.     int j = 0;
  109.     int sum = 0;
  110.     int test = 0;
  111.     int count = 0;
  112.  
  113.     for (i = 0; i < 3; i++)
  114.     {
  115.         sum = 0;
  116.         for ( j = 0; j < 3; j++)
  117.         {
  118.             if (board[i][j] == 0)
  119.             {
  120.                 count++;
  121.             }    
  122.             sum += (board[i][j] * board[i][j]);
  123.         }
  124.  
  125.          if ( sum == 3 || sum == 12)
  126.          {
  127.             test = sum;
  128.             break;
  129.         }
  130.         sum = 0;
  131.     }    // end of for loop    
  132.  
  133.     for ( j = 0; j < 3; j++)
  134.     {
  135.         sum = 0;
  136.         for ( i = 0; i < 3; i++)
  137.         {
  138.             sum += (board[i][j] * board[i][j]);
  139.         }
  140.  
  141.         if ( sum == 3 || sum == 12)
  142.         {
  143.             test = sum;
  144.             break;
  145.         }
  146.  
  147.         sum = 0;
  148.     }   // end of for loop
  149.  
  150.     if ( test != 3 || test != 12)
  151.     {
  152.  
  153.             sum = (board[0][0] * board[0][0])+ (board[1][1] * board[1][1]) + (board[2][2] * board[2][2]);
  154.  
  155.         if ( sum == 3 || sum == 12)
  156.         {
  157.             test  = sum;
  158.  
  159.         }
  160.     }  // end of if condition
  161.  
  162.     if (test != 3 || test != 12)
  163.     {
  164.  
  165.             sum = (board[2][0] * board[2][0])+ (board[1][1] * board[1][1]) + (board[0][2] * board[0][2]);
  166.  
  167.             if ( sum == 3 || sum == 12 )
  168.             {
  169.                 test = sum;
  170.             }
  171.     }  // end of if condition
  172.  
  173. //    }
  174.  
  175.     if ( test == 3)
  176.     {
  177.         test = 1;
  178.     }
  179.  
  180.     else if ( test == 12)
  181.     {
  182.         test = 2;
  183.     }
  184.  
  185.     else if ( count == 0)
  186.     {
  187.          test = 3;
  188.     }
  189.     else
  190.     {
  191.         test = 0;
  192.     }
  193.     return test;
  194.  
  195. } // end of Check_Board
  196.  
  197. void TicTacToe::Tic_Tac_Toe_Board()
  198. {
  199.     for ( int row = 0; row < 3; row ++)
  200.     {
  201.  
  202.         for ( int column = 0; column < 3; column++)
  203.         {
  204.  
  205.             if ( board[row][column] == 0)
  206.             {
  207.                 display_board[row][column] = ' ';
  208.             }
  209.             if ( board[row][column] == 1)
  210.             {
  211.                 display_board[row][column] = 'X';
  212.             }
  213.             if ( board[row][column] == 2)
  214.             {
  215.                 display_board[row][column] = 'O';
  216.             }
  217.         }  // end of inner for loop
  218.     }  // end of outer for loop
  219.  
  220.     cout << "         Let's Play Tic-Tac-Toe!            " << endl;
  221.     cout << "    Current Player: X        Current Player: O    " << endl;
  222.     cout << endl;
  223.     cout << "        |        |        "  << endl;
  224.     cout << "         |        |        "  << endl;
  225.     cout <<                    display_board[0][0] << "         |   "  << display_board[0][1]  <<  "         | "  << display_board[0][2] << "   "  << endl;
  226.     cout << "        |        |        " << endl;
  227.     cout << "-----------------------------------------------" << endl;
  228.     cout << "        |        |        " << endl;
  229.     cout << "        |        |        " << endl;
  230.     cout <<                      display_board[1][0] << "         |   "  << display_board[1][1]  <<  "         | "  << display_board[1][2] << "   "  << endl;
  231.     cout << "        |        |        " << endl;
  232.         cout << "-----------------------------------------------" << endl;
  233.     cout << "         |        |        " << endl;
  234.     cout <<                             display_board[2][0] << "         |   "  << display_board[2][1]  <<  "         | "  << display_board[2][2] << "   "  << endl;
  235.      cout << "        |        |        " << endl;
  236.     cout << "        |        |        " << endl;
  237.  
  238. }   // end of Tic_Tac_Toe_Board
  239.  
  240.  
  241.  
  242.  
  243. int main()
  244. {
  245.  
  246.  
  247.     TicTacToe game;
  248.     bool test;
  249.     bool more = true;
  250.     int row = 0;
  251.     int column= 0;
  252.     int player;
  253.     int check = 0;
  254.  
  255.  
  256.     TicTacToe();
  257.  
  258.     while ( more )
  259.     {
  260.         game.Tic_Tac_Toe_Board();
  261.         player = game.Pick_Player();
  262.  
  263.  
  264.         cout << " Current Player " << player;
  265.         cout << endl;
  266.         cout << " Enter Row Index ( 0,1,2):  " ;
  267.         cout << endl;
  268.         cin >> row;
  269.         cout << " Enter Column Index (0,1,2): " << endl;
  270.         cin >> column;
  271.  
  272.         game.Choice_of_Row(row);
  273.         game.Choice_of_Column(column);
  274.  
  275.         test = game.Check_Move( game.Pick_Row(), game.Pick_Column());
  276.  
  277.         if ( test == 1)
  278.         {
  279.  
  280.             check = game.Check_Board();
  281.         }
  282.  
  283.  
  284.         else
  285.         {
  286.             while ( test == 0 ) 
  287.             {
  288.  
  289.                 cout << " Current Player " << game.Pick_Player() <<"  Invalid Choice" << endl;
  290.  
  291.                 cout << " Enter Row Index ( 0,1,2): " ;
  292.  
  293.                 cin >> row;
  294.                 cout << endl;
  295.                 cout << " Enter Column Index ( 0,1,2): " ;
  296.  
  297.                 cin >> column;
  298.                 cout << endl;
  299.                 game.Choice_of_Row(row);
  300.                 game.Choice_of_Column(column);
  301.  
  302.                 test = game.Check_Move(game.Pick_Row(),game.Pick_Column());
  303.             } // end of while loop
  304.             check = game.Check_Board();
  305.         }
  306.  
  307.         if ( check == 1 || check == 2)
  308.         {    
  309.             break;
  310.         }
  311.  
  312.         else if ( check == 3 ) 
  313.         {
  314.             game.Tic_Tac_Toe_Board();
  315.             cout << " The game is tied. " << endl;
  316.  
  317.         }
  318.  
  319.         if ( player == 1)
  320.         {
  321.             player = 2;
  322.         }
  323.         else
  324.         {
  325.             player = 1;
  326.         }
  327.         game.Choice_by_Player(player);
  328.  
  329.     } // end of outer while loop
  330.  
  331.     game.Tic_Tac_Toe_Board();
  332.  
  333.     cout << " Player " << check << " wins. " << endl;
  334.  
  335.     return 0;
  336. } // end of main function
May 18 '10 #1
Share this Question
Share on Google+
1 Reply


100+
P: 687
You always place the check in column 0, row 0.
May 19 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.