-
Notifications
You must be signed in to change notification settings - Fork 0
/
allFunctions.cpp
160 lines (154 loc) · 3.7 KB
/
allFunctions.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
/*
Here all the user defined functions of chainReaction.cpp are written.
Functions are:
containerDisplay
winCheck
react
read
play
Note : 'i' refers to player id(except containerDisplay)
*/
void containerDisplay(){
for(int i = 0; i < size; i++){
std::cout << "\n\t" ;
for(int j = 0; j < size; j++){
if (Box[i][j]%4 == 0){
std::cout << " 0" ;
continue;
}
std::cout << " " << "\033[1;"<< 31 + Box[i][j]/4 << "m" << Box[i][j]%4 << "\033[0m";
}
}
}
int winCheck(){
winnerID = -1;
int a[9] = {};
for (int j = 0; j < size; j++) {
for (int k = 0; k < size; k++) {
if(Box[j][k]%4 > 0 && round > 2){
a[Box[j][k]/4] = 1;
}
}
}
int c = 0;
for (int j = 0; j < playerCount; j++) {
if(a[j] == 1){
c++;
}
}
if(c == 1){
for (int j = 0; j < playerCount; j++) {
if(a[j] == 1){
winnerID = j;
break;
}
}
}
return winnerID;
}
void react(int x, int y, int i) {
if(winnerID > -1){
return;
}
if(winCheck() > -1){
return;
}
int f = 0;
if(x == 0){
if (y == 0){
if(Box[0][0] % 4 > 0){
f = 1;
react(1,0,i);
react(0,1,i);
Box[0][0] = 0;
}
}else if(y+1 == size){
if(Box[0][y] % 4 > 0){
f = 1;
react(1,y,i);
react(0,y-1,i);
Box[0][y] = 0;
}
}else{
if(Box[0][y] % 4 > 1){
f = 1;
react(0,y-1,i);
react(0,y+1,i);
react(1,y,i);
Box[0][y] = 0;
}
}
}else if(x+1 == size){
if (y == 0){
if(Box[x][0] % 4 > 0){
f = 1;
react(x-1,0,i);
react(x,1,i);
Box[x][0] = 0;
}
}else if(y+1 == size){
if(Box[x][y] % 4 > 0){
f = 1;
react(x-1,y,i);
react(x,y-1,i);
Box[x][y] = 0;
}
}else{
if(Box[x][y] % 4 > 1){
f = 1;
react(x,y-1,i);
react(x,y+1,i);
react(x-1,y,i);
Box[x][y] = 0;
}
}
}else if(y == 0){
if(Box[x][y] % 4 > 1){
f = 1;
react(x-1,y,i);
react(x+1,y,i);
react(x,y+1,i);
Box[x][y] = 0;
}
}else if(y+1 == size){
if(Box[x][y] % 4 > 1){
f = 1;
react(x,y-1,i);
react(x+1,y,i);
react(x-1,y,i);
Box[x][y] = 0;
}
}else if(Box[x][y] % 4 > 2){
f = 1;
react(x,y-1,i);
react(x,y+1,i);
react(x-1,y,i);
react(x+1,y,i);
Box[x][y] = 0;
}
if (f == 0){
Box[x][y] = 4 * i + ((Box[x][y] + 1) % 4);
}
}
void read() {
int x, y; //co-ordinates of value entry.
for (int i = 0; i < playerCount; i++) {
std::cout << "\n\nPlayer" << i << "'s turn : ";
std::cin >> x;
std::cin >> y;
if(Box[x][y]/4 == i || Box[x][y] == 0){
react(x,y,i);
} else {
std::cout << "\n\tInvalid Entry\n" << '\n';
i--;
}
containerDisplay();
}
}
void play() {
if(winCheck() > -1){
return;
}
std::cout << "\n\n\t Round" << round++;
read();
}