-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic tac toe.cpp
80 lines (70 loc) · 2.17 KB
/
tic tac toe.cpp
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
#include <iostream>
using namespace std;
char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
void displayBoard() {
cout<<"\t\tTIC TAC TOE GAME"<<endl;
cout<<"Payer1[X]"<<endl;
cout<<"Player2[O]"<<endl;
cout<<"\t\t | | \n";
cout<<"\t\t "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<" \n";
cout<<"\t\t_____|_____|_____\n";
cout<<"\t\t | | \n";
cout<<"\t\t "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<" \n";
cout<<"\t\t_____|_____|_____\n";
cout<<"\t\t | | \n";
cout<<"\t\t "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<" \n";
cout<<"\t\t | | \n";
}
int main() {
int player, choice;
cout<<"Enter number of player 1 or 2 :"<<endl;
cin>>player;
char mark;
bool gameover = false;
do {
displayBoard();
if(player%2){
player=1;
}
else{
player=2;
}
cout << "Player " << player << ", enter a number: ";
cin >> choice;
if(player == 1){
mark ='X';
}
else{
mark ='0';
}
if (choice == 1 && board[0][0] == '1')
board[0][0] = mark;
else if (choice == 2 && board[0][1] == '2')
board[0][1] = mark;
else if (choice == 3 && board[0][2] == '3')
board[0][2] = mark;
else if (choice == 4 && board[1][0] == '4')
board[1][0] = mark;
else if (choice == 5 && board[1][1] == '5')
board[1][1] = mark;
else if (choice == 6 && board[1][2] == '6')
board[1][2] = mark;
else if (choice == 7 && board[2][0] == '7')
board[2][0] = mark;
else if (choice == 8 && board[2][1] == '8')
board[2][1] = mark;
else if (choice == 9 && board[2][2] == '9')
board[2][2] = mark;
else {
cout << "Invalid move!";
player--;
}
// Switch player
if(player == 1)
player=2;
else
player=1;
gameover = false;
} while (!gameover);
return 0;
}