-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
150 lines (126 loc) · 2.81 KB
/
Game.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 "Game.h"
Game::Game(char symbolM,char symbolP){
this->machineSymbol=symbolM;
this->playerSymbol=symbolP;
//Memory allocation for array
initArr();
}
void Game::initArr(){
//Initialize the ticArray
this->ticArray=new char*[SIZE];
if(this->ticArray==NULL){
cout<<"Memory allocation failed."<<endl;
exit(1);
}
//Initialize the columns
for(int i=0;i<SIZE;i++){
this->ticArray[i]=new char[SIZE];
if(this->ticArray[i] == NULL) {
cout < "Memory allocation error!\n";
exit(1);
}
}
//Initialize with 0
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
this->ticArray[i][j]='\0';
}
}
}
void Game::freeMemory(){
delete this->ticArray;
}
void Game::printArr(){
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
if(this->ticArray[i][j]=='\0'){
cout<<"| ";
}
else{
cout <<"|"<<this->ticArray[i][j];
}
}
cout<<endl;
}
}
int Game::someOneWins(){
int howMuchEmpty=0;
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
//We create if we see one symbol at every point
if(this->ticArray[i][j]==this->playerSymbol || this->ticArray[i][j]==this->machineSymbol){
howMuchEmpty++;
}
}
}
if(howMuchEmpty==SIZE*SIZE){
return 3;
}
int playerWins=0;
int machineWins=0;
//We check Horizontal
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
//We create if we see one symbol at every point
if(this->ticArray[i][j]==this->playerSymbol){
playerWins++;
}
else if(this->ticArray[i][j]==this->machineSymbol){
machineWins++;
}
}
//Player Wins return 1
if(playerWins==SIZE){
cout<<"Horizontal Win: "<<playerWins<<endl;
return 1;
}
else if(machineWins==SIZE){
return 2;
}
playerWins=0;
machineWins=0;
}
//Initialize the variables
playerWins=0;
machineWins=0;
//We check Down
for(int j=0;j<SIZE;j++){
for(int i=0;i<SIZE;i++){
//We create if we see one symbol at every point
if(this->ticArray[i][j]==this->playerSymbol){
playerWins++;
}
else if(this->ticArray[i][j]==this->machineSymbol){
machineWins++;
}
}
//Player Wins return 1
if(playerWins==SIZE){
cout<<"Down Win"<<endl;
return 1;
}
else if(machineWins==SIZE){
return 2;
}
playerWins=0;
machineWins=0;
}
return 0;
}
void Game::setElement(int row,int col){
this->ticArray[row][col]=this->playerSymbol;
}
int Game::isFilled(int row,int col){
if(this->ticArray[row][col]==playerSymbol || this->ticArray[row][col]==machineSymbol){
return 1;
}
return 0;
}
void Game::resetArray(){
//Initialize with 0
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
this->ticArray[i][j]='\0';
}
}
}