-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.cpp
136 lines (133 loc) · 2.49 KB
/
tictactoe.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
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
#include <iostream>
#include <conio.h>
using namespace std;
class TicTacToe{
private:
char board[3][3];
char mark;
int turn;
int won;
int input;
public:
TicTacToe():board{'1','2','3','4','5','6','7','8','9'}{
turn = 1;
mark = 'O';
won = 0;
input = 0;
}
int GetResult(){
return won;
}
void Play(){
for(int i=0; i<9; i++){
system("cls");
PrintBoard();
Input();
int validInput = AddMark();
if( !validInput ){
i--;
continue;
}
Result(i);
if(won != 0) break;
turn = !turn;
}
}
void Result(int i){
if(Check()){
system("cls");
PrintBoard();
if(turn){
cout<<endl<<"*** Player 1 - You Won ***";
won = 1;
return;
}
else {
cout<<endl<<"*** Player 2 - You Won ***";
won = 2;
return;
}
}
if( i==8 ){
system("cls");
PrintBoard();
cout<<endl<<"*** Match Draw ***";
won = 3;
return;
}
won = 0;
}
void Input(){
cin>>input;
while(input<0 || input >9){
cout<<"Invalid input. Please re-enter: ";
cin>>input;
}
if(turn) mark = 'O';
else mark = 'X';
}
void PrintBoard(){
cout<<" "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<" "<<endl;
cout<<"-----------"<<endl;
cout<<" "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<" "<<endl;
cout<<"-----------"<<endl;
cout<<" "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<" "<<endl;
if(turn) cout<<"O - Player 1: ";
else cout<<"X - Player 2: ";
}
int AddMark(){
for(int i=0, k=1; i<3; i++){
for(int j=0; j<3; j++, k++){
if(k == input){
if(board[i][j]!='O' && board[i][j]!='X'){
board[i][j] = mark;
return 1;
}
else return 0;
}
}
}
}
int Check(){
//rows
if( board[0][0]==mark && board[0][1]==mark &&board[0][2]==mark )
return 1;
if( board[1][0]==mark && board[1][1]==mark &&board[1][2]==mark )
return 1;
if( board[2][0]==mark && board[2][1]==mark &&board[2][2]==mark )
return 1;
//columns
if( board[0][0]==mark && board[1][0]==mark &&board[2][0]==mark )
return 1;
if( board[0][1]==mark && board[1][1]==mark &&board[2][1]==mark )
return 1;
if( board[0][2]==mark && board[1][2]==mark &&board[2][2]==mark )
return 1;
//diagonals
if( board[0][0]==mark && board[1][1]==mark &&board[2][2]==mark )
return 1;
if( board[0][2]==mark && board[1][1]==mark &&board[2][0]==mark )
return 1;
}
void Reset(){
board[0][0] = '1'; board[0][1] = '2'; board[0][2] = '3';
board[1][0] = '4'; board[1][1] = '5'; board[1][2] = '6';
board[2][0] = '7'; board[2][1] = '8'; board[2][2] = '9';
turn = 1;
mark = 'O';
won = 0;
input = 0;
}
};
int main(){
TicTacToe t;
char replay;
do{
t.Play();
t.Reset();
cout<<endl<<endl<<"Do you want to play again (Y/N): ";
replay = getche();
}while(replay=='y'||replay=='Y');
getch();
return 0;
}