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
|
#include <iostream>
using namespace std;
int main()
{
char board[3][3] = {{'.','.','.'},{'.','.','.'},{'.','.','.'}};
int playerOneMove = 0;
int playerTwoMove = 0;
int turn = 1;
int checkWin = 0;
cout << "Welcome to Tic Tac Toe!" << endl << endl;
//Board
for(int row = 0; row < 3; row++){
for(int column = 0; column < 3; column++){
cout << " " << board[row][column] << " ";
}
cout << endl << endl;
}
while(checkWin == 0){
cout << "Press: 1,2,3,4,5,6,7,8, or 9 to make your move" << endl << endl << endl;
cin >> playerOneMove;
//Player's Move
while(turn <= 2){
turn++;
if(turn == 1){
switch(playerOneMove){
case 1: board[0][0] = 'X'; break;
case 2: board[0][1] = 'X'; break;
case 3: board[0][2] = 'X'; break;
case 4: board[1][0] = 'X'; break;
case 5: board[1][1] = 'X'; break;
case 6: board[1][2] = 'X'; break;
case 7: board[2][0] = 'X'; break;
case 8: board[2][1] = 'X'; break;
case 9: board[2][2] = 'X'; break;
}
}
else if(turn == 2){
switch(playerTwoMove){
case 1: board[0][0] = 'X'; break;
case 2: board[0][1] = 'X'; break;
case 3: board[0][2] = 'X'; break;
case 4: board[1][0] = 'X'; break;
case 5: board[1][1] = 'X'; break;
case 6: board[1][2] = 'X'; break;
case 7: board[2][0] = 'X'; break;
case 8: board[2][1] = 'X'; break;
case 9: board[2][2] = 'X'; break;
}
}
else if(turn > 2){
turn = 1;
}
}
//Check Win
if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2]){
checkWin = 1;
}
}
}
|