Beginner exercise problem
Aug 7, 2016 at 5:49pm UTC
I wrote this code for tic tac toe (I am assuming its horrible but I am only beginning). The only problem i have is that if player 1(X) chooses 5 and then player 2(O) chooses 5 again, then the X turns into O... Also if player 1 picks a number he already chose it doesnt do anything and he looses the turn. Can you please give me a hint on how to solve this?
Here is the code i have
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
#include <iostream>
#include <cstdlib>
using namespace std;
void Board();
void ClearBoard();
int CheckGame();
char square[9] = {'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' };
int mark;
int x;
char PlayAgain;
int main()
{
do
{
system("Cls" );
ClearBoard();
Board();
do
{
cout<<"Player 1, choose a number. " ;
cin>>mark;
while (mark!=1 && mark!=2 && mark!=3 && mark!=4 && mark!=5 && mark!=6 && mark!=7 && mark!=8 && mark!=9)
{
cout<<"Wrong input. Try again." <<endl;
cin>>mark;
}
if (mark==0)
{
cout<<"" ;
}
else
{
square[--mark] = 'X' ;
system("CLS" );
Board();
CheckGame();
if (x==1)
{
cout<<"Player 1 won! Congratulations!" <<endl;
break ;
}
else if (x==0)
break ;
}
cout<<"Player 2, choose a number. " ;
cin>>mark;
while (mark!=1 && mark!=2 && mark!=3 && mark!=4 && mark!=5 && mark!=6 && mark!=7 && mark!=8 && mark!=9)
{
cout<<"Wrong input. Try again" <<endl;
cin>>mark;
}
if (mark==0)
{
cout<<"" ;
}
else
{
square[--mark] = 'O' ;
system("CLS" );
Board();
CheckGame();
if (x==1)
{
cout<<"Player 2 won! Congratulations!" <<endl;
break ;
}
}
}while (x==-1);
if (x==0)
{
system("CLS" );
Board();
cout<<"Draw!" <<endl;
}
cout<<"\nWould you like to play again?(y/n)" <<endl;
cin>>PlayAgain;
if (PlayAgain!='y' && PlayAgain!='n' )
{
cout<<"Wrong Input. Try again." <<endl;
cin>>PlayAgain;
}
}while (PlayAgain=='y' );
return 0;
}
int CheckGame()
{
if (square[0] == square[1] && square[1] == square[2])
x=1;
else if (square[3] == square[4] && square[4] == square[5])
x=1;
else if (square[6] == square[7] && square[7] == square[8])
x=1;
else if (square[0] == square[3] && square[3] == square[6])
x=1;
else if (square[3] == square[4] && square[4] == square[7])
x=1;
else if (square[2] == square[5] && square[5] == square[8])
x=1;
else if (square[0] == square[4] && square[4] == square[8])
x=1;
else if (square[2] == square[4] && square[4] == square[6])
x=1;
else if (square[0] != '1' && square[1] != '2' && square[2] != '3' && square[3] != '4' && square[4] != '5' && square[5] != '6'
&& square[6] != '7' && square[7] != '8' && square[8] != '9' )
x=0;
else
x=-1;
}
void Board()
{
system("cls" );
cout << "\n\n\t\t Tic Tac Toe\n\n" ;
cout << "\t Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;
cout << "\t\t | | " << endl;
cout << "\t\t " << square[0] << " | " << square[1] << " | " << square[2] << endl;
cout << "\t\t_____|_____|_____" << endl;
cout << "\t\t | | " << endl;
cout << "\t\t " << square[3] << " | " << square[4] << " | " << square[5] << endl;
cout << "\t\t_____|_____|_____" << endl;
cout << "\t\t | | " << endl;
cout << "\t\t " << square[6] << " | " << square[7] << " | " << square[8] << endl;
cout << "\t\t | | " << endl << endl;
}
void ClearBoard()
{
square[0] = '1' ;
square[1] = '2' ;
square[2] = '3' ;
square[3] = '4' ;
square[4] = '5' ;
square[5] = '6' ;
square[6] = '7' ;
square[7] = '8' ;
square[8] = '9' ;
}
Topic archived. No new replies allowed.