tic tac toe
How do I would reset the board if I did play again? also, how do I keep tracks of eins and losses?
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
#include<iostream>
using namespace std;
char board[3][3] = { '1','2', '3', '4', '5', '6', '7', '8', '9' };
char player = 'X';
int n;
int exitP(int x) {
cout << "\nWould you like to play again?? [1 -> no :: 2 -> yes]: ";
cin >> x;
switch (x) {
case 1: cout << "Goodbye!";
exit(EXIT_SUCCESS);
break;
case 2: return x;
break;
default: cout << "Please only enter numbers 1-2 only. ";
exitP(x);
break;
}
}
void Draw()
{
system("cls");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
}
void TogglePlayer()
{
if (player == 'X')
player = 'O';
else
player = 'X';
}
char Win()
{
//first player
if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
return 'X';
if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
return 'X';
if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
return 'X';
if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
return 'X';
if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
return 'X';
if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
return 'X';
if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
return 'X';
if (board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X')
return 'X';
//second player
if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
return 'O';
if (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
return 'O';
if (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
return 'O';
if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
return 'O';
if (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')
return 'O';
if (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')
return 'O';
if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
return 'O';
if (board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O')
return 'O';
return '/';
}
void Input()
{
int a;
cout << "It's " << player << " turn. " << "Press the number of the field: ";
cin >> a;
if (a == 1)
{
if (board[0][0] == '1')
board[0][0] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 2)
{
if (board[0][1] == '2')
board[0][1] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 3)
{
if (board[0][2] == '3')
board[0][2] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 4)
{
if (board[1][0] == '4')
board[1][0] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 5)
{
if (board[1][1] == '5')
board[1][1] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 6)
{
if (board[1][2] == '6')
board[1][2] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 7)
{
if (board[2][0] == '7')
board[2][0] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 8)
{
if (board[2][1] == '8')
board[2][1] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 9)
{
if (board[2][2] == '9')
board[2][2] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
}
int main()
{
n = 0;
Draw();
while (1)
{
int exitProgram = 1;
n++;
Input();
Draw();
if (Win() == 'X')
{
cout << "X wins!" << endl;
exitP(exitProgram);
}
else if (Win() == 'O')
{
cout << "O wins!" << endl;
exitP(exitProgram);
}
else if (Win() == '/' && n == 9)
{
cout << "It's a draw!" << endl;
exitP(exitProgram);
}
TogglePlayer();
}
system("pause");
return 0;
}
|
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
|
#include<iostream>
using namespace std;
char board[3][3] = { '1','2', '3', '4', '5', '6', '7', '8', '9' };
char player = 'X';
int n;
int totals = 0; // played games
int xWins = 0; // games won by player 'X'
int oWins = 0; // games won by player 'O'
// resets the board
void resetBoard()
{
for(int i = 0; i < 9; ++i)
board[0][i] = i+1;
}
void exitP(int x)
{
cout << "Total played games: " << totals << endl
<< "Player X won "<< xWins << " games." << endl
<< "Player O won "<< oWins << " games." << endl;
while (true)
{
cout << "\nWould you like to play again?? [1 -> no :: 2 -> yes]: ";
cin >> x;
switch (x)
{
case 1:
cout << "Goodbye!";
exit(EXIT_SUCCESS);
break;
case 2:
resetBoard();
break;
default:
cout << "Please only enter numbers 1-2 only. ";
// exitP(x); // don't do this
break;
}
}
}
void Draw()
{
system("cls");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
}
void TogglePlayer()
{
player = (player == 'X') ? 'O' : 'X';
}
bool Win( char player)
{
if ((board[0][0] == player && board[0][1] == player && board[0][2] == player)
|| (board[1][0] == player && board[1][1] == player && board[1][2] == player)
|| (board[2][0] == player && board[2][1] == player && board[2][2] == player)
|| (board[0][0] == player && board[1][0] == player && board[2][0] == player)
|| (board[0][1] == player && board[1][1] == player && board[2][1] == player)
|| (board[0][2] == player && board[1][2] == player && board[2][2] == player)
|| (board[0][0] == player && board[1][1] == player && board[2][2] == player)
|| (board[2][0] == player && board[1][1] == player && board[0][2] == player)
) return true;
else return false;
}
// Returns true if game at end and no player wins
bool isDraw()
{
if (Win('X') || Win('O')) return false;
for (int i = 0; i < 3; ++i)
for ( int j = 0; j < 3; ++j)
if ( board[i][j] != 'X' && board[i][j] != 'O')
return false;
return true;
}
void Input()
{
while (true)
{
int a;
cout << "It's " << player << " turn. " << "Press the number of the field: ";
cin >> a;
if ( a < 1 || a > 9)
{
cout << "The Numbers must be between 1 an 9. Try again!\n";
continue;
}
if ( board[0][a-1] == 'X' || board[0][a-1] == 'O')
{
cout << "Field " << a << " is already in use. Try again!\n";
continue;
}
board[0][a-1] = player;
break;
}
}
int main()
{
Draw();
while (1)
{
int exitProgram = 1;
totals++;
Input();
Draw();
if (Win('X'))
{
cout << "X wins!" << endl;
xWins++;
exitP(exitProgram);
}
else if (Win('O'))
{
cout << "O wins!" << endl;
oWins++;
exitP(exitProgram);
}
else if (isDraw())
{
cout << "It's a draw!" << endl;
exitP(exitProgram);
}
TogglePlayer();
}
system("pause");
return 0;
}
|
wrong answer
Hello imthekingbabyz,
nuderobmonkey has some very good code, so before I waste my time why do you not say what is wrong or what you can not use or what you can use.
Just saying "wrong answer" with out an explanation is not enough.
Andy
because the board does not reset and the games played is actually the tries of the players.
Last edited on
Awww, that's a shame isn't it.
Having to think for yourself.
It's a "more than you deserve" push in the right direction.
Now pick up the ball, study what you've been given and take it to the finish line.
I just wanna know how to reset the board that's really it
Hello imthekingbabyz,
nuderobmonkey has given you an answer with his "resetBoard" function. And it does work.
If yo can not figure out what to do from his code then go find a tutor.
I will give you a hint his for loop stores a number not a character.
Andy
Sorry 'imthekingbabyz' , I was in rush so I hadn't enough time for checking logical errors, here the correct function:
1 2 3 4 5
|
void resetBoard()
{
for(int i = 0; i < 9; ++i)
board[0][i] = i + '1';
}
|
Last edited on
Topic archived. No new replies allowed.