-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
150 lines (140 loc) · 3.27 KB
/
Source.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include<iostream>
using namespace std;
string board[4];
int isWin();
void drawBoard();
void takeTurns();
void startNewGame();
bool validPlace(int n);
int main()
{
bool continuePlaying = 1;
while (continuePlaying)
{
startNewGame();
takeTurns();
cout << "Do you want play agin !?\nEnter y or Y to yes or Press any other button to exit\n";
string str;
cin >> str;
if (str != "y" && str != "Y")
{
continuePlaying = 0;
cout << "Good bye (:\n";
}
}
return 0;
}
void startNewGame()
{
cout << "--------STARTIG NEW GAME-------\n";
for (int i = 1; i <= 3; i++)
{
string s = "-";
for (int j = 1; j <= 3; j++)
{
s += char((i - 1) * 3 + j + '0');
}
board[i] = s;
}
}
void takeTurns()
{
int place;
bool turn = 0;
while (isWin() == 0)
{
cout << "Player ";
if (!turn) cout << " X ";
else cout << " O ";
// Cheek if place valed
cin >> place;
while (validPlace(place) == 0)
{
cout << "Invaled Place pleace select another one !!\n";
cin >> place;
}
// convert the number to XY
int x = 0, y = 0;
if (place == 9)
{
x = 3, y = 3;
}
else
{
x = (1 + (place / 3));
if (place % 3 == 0)
y = 3;
else
y = place % 3;
}
(!turn) ? board[x][y] = 'X' : board[x][y] = 'O';
turn = !(turn);
drawBoard();
}
if (isWin() == 1)
{
if (turn) cout << "Player X WON the game!!\n";
else cout << "Player O WON the game!!\n";
}
else cout << "The game ended in a draw\n";
}
bool validPlace(int n)
{
int x = 0, y = 0;
if (n > 9 || n < 1) return 0;
else if (n == 9)
{
x = 3;
y = 3;
}
else
{
x = (1 + (n / 3));
if (n % 3 == 0) y = 3;
else y = n % 3;
}
if (board[x][y] >= '1' && board[x][y] <= '9') return 1;
return 0;
}
int isWin()
{
if (board[1][1] == board[1][2] && board[1][1] == board[1][3] ||
board[2][1] == board[2][2] && board[2][1] == board[2][3] ||
board[3][1] == board[3][2] && board[3][1] == board[3][3])
return 1;
if (board[1][1] == board[2][1] && board[1][1] == board[3][1] ||
board[1][2] == board[2][2] && board[1][2] == board[3][2] ||
board[1][3] == board[3][2] && board[1][3] == board[3][3])
return 1;
if (board[1][1] == board[2][2] && board[1][1] == board[3][3] ||
board[1][3] == board[2][2] && board[1][3] == board[3][1])
return 1;
int cnt = 0;
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
if (!(board[i][j] >= '1' && board[i][j] <= '9')) cnt++;
}
}
if (cnt == 9) return -1;
return 0;
}
void drawBoard()
{
cout << "-------------------------------\n";
cout << "|---------|---------|---------|\n";
cout << "| | | |\n";
cout << "| " << board[1][1] << " | " << board[1][2] << " | " << board[1][3] << " |\n";
cout << "| | | |\n";
cout << "|---------|---------|---------|\n";
cout << "| | | |\n";
cout << "| " << board[2][1] << " | " << board[2][2] << " | " << board[2][3] << " |\n";
cout << "| | | |\n";
cout << "|---------|---------|---------|\n";
cout << "| | | |\n";
cout << "| " << board[3][1] << " | " << board[3][2] << " | " << board[3][3] << " |\n";
cout << "| | | |\n";
cout << "|---------|---------|---------|\n";
cout << "-------------------------------\n";
}