-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
183 lines (154 loc) · 5.45 KB
/
main.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
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
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <chrono>
#include <thread>
using namespace std;
//array size dimmension
const int row =4;
const int column = 9;
//prototypes
void Start();
void coutArr(char [][column]); //! C++ requires that we specify the number of columns in the function definition
void ClearTable(char [][column]);
void WinnerDetect(char [][column]);
int CheckXOFirst(char);
void GameOver();
int main() {
//gameLogic game;
//game.Start();
Start();
return 0;
}
void Start() {
//Fill up the array by empty cells
char arr[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
arr[i][j] = 0;
}
}
ClearTable(arr);//print empty gamefield
//this loop is cycling the game before all fields don't filled up
for (int i = 1; i < 9; i++) { //!start
coutArr(arr);
}
}
void coutArr(char arr[][column]){
//block of local variables
int rowIn,columnIn,checkIn;
char in=' ';// empty space for checking sequence
cout<<"Enter pozition(row column): ";
cin>>rowIn>>columnIn;
if(arr[rowIn][columnIn] ==0) { //make a cell constant
cout << '\n' << "Now enter X or O(lowercase...I'll check!): ";
cin >> in;
//Check input values.
checkIn = CheckXOFirst(in);
switch (checkIn) {
case 0: //compliance sequence
system("cls");
cout << in << " moved before,it's not your time!" << endl;
this_thread::sleep_for(chrono::seconds(2));//w8 2 sec
system("cls");
break;
case 1: // regular game time
system("cls");
cout << endl;
arr[rowIn][columnIn] = in;
ClearTable(arr);
WinnerDetect(arr);
break;
case 2: //Secret case...
system("cls");
cout << "\n \n \n \n \n I told you...";
this_thread::sleep_for(chrono::seconds(2));//w8 2 sec
GameOver();
break;
default: // Wrong input
cout << endl;
cout << endl;
cout << "Wrong input.Try again.";
this_thread::sleep_for(chrono::seconds(2));//w8 2 sec
system("cls");
break;
}
}else cout<<"this cell already exist"<<endl;
}
void WinnerDetect(char arr[][column]){ //Detect the winner
//check rows
if(arr [2][2]!=0 && arr [1][1]!=0 && arr[3][3]) {
if (arr[1][1] == arr[1][2] && arr[1][2] == arr[1][3]) {
system("cls");
cout << arr[1][1] << " is Winner!";
exit(0);///drop the program
} else if (arr[2][1] == arr[2][2] && arr[2][2] == arr[2][3]) {
system("cls");
cout << arr[2][1] << " is Winner!";
exit(0);///drop the program
} else if (arr[3][1] == arr[3][2] && arr[3][2] == arr[3][3]) {
system("cls");
cout << arr[3][1] << " is Winner!";
exit(0);///drop the program
}
//check columns
else if (arr[1][1] == arr[2][1] && arr[2][1] == arr[3][1]) {
system("cls");
cout << arr[1][1] << " is Winner!";
exit(0);///drop the program
} else if (arr[1][2] == arr[2][2] && arr[2][2] == arr[3][2]) {
system("cls");
cout << arr[2][2] << " is Winner!";
exit(0);///drop the program
} else if (arr[1][3] == arr[2][3] && arr[2][3] == arr[3][3]) {
system("cls");
cout << arr[2][3] << " is Winner!";
exit(0);///drop the program
}
//check diagonal
else if (arr[1][1] == arr[2][2] && arr[2][2] == arr[3][3]) {
system("cls");
cout << arr[2][2] << " is Winner!";
exit(0);///drop the program
}
}
}
//This function check input value
int CheckXOFirst(char a){
static char sequence=' ';//store previous player
cout<<'_'<<sequence<<'_';
if(a=='x' || a=='o'){
//sequence check
if(sequence==a){ // if x,moving again gonna be error
return 0;
}else
sequence=a;
cout<<'_'<<sequence<<'_';
return 1;
}else if(a=='X'||a=='O'){
return 2;
}else{
return 3;
}
}
void GameOver(){
system("cls");
cout<<" .d8888b. .d88888b. "<<endl;
cout<<"d88P Y88b d88P Y88b "<<endl;
cout<<"888 888 888 888 "<<endl;
cout<<"888 8888b. 88888b.d88b. .d88b. 888 888888 888 .d88b. 888d888"<<endl;
cout<<"888 88888 88b888 888 88bd8P Y8b 888 888888 888d8P Y8b888P\""<<endl;
cout<<"888 888.d888888888 888 88888888888 888 888Y88 88P88888888888 "<<endl;
cout<<"Y88b d88P888 888888 888 888Y8b. Y88b. .d88P Y8bd8P Y8b. 888"<<endl;
cout<<" Y8888P88 Y888888888 888 888 Y8888 Y88888P Y88P Y8888 888"<<endl;
exit(0);///drop the program
}
void ClearTable(char arr[][column]){
cout<<endl;
cout<<arr[1][1]<<" | "<<arr[1][2]<<" | "<<arr[1][3]<<endl;
cout<<"- | - | -"<<endl;
cout<<arr[2][1]<<" | "<<arr[2][2]<<" | "<<arr[2][3]<<endl;
cout<<"- | - | -"<<endl;
cout<<arr[3][1]<<" | "<<arr[3][2]<<" | "<<arr[3][3]<<endl;
cout<<endl;
}