-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchips_game.cpp
231 lines (215 loc) · 4.33 KB
/
chips_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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include<iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<iomanip>
#define ll long long
using namespace std;
class chipsGame
{
ll r,c;
vector< vector<char> > dp;
public :
chipsGame()
{
r=10;
c=10;
dp.resize(r);
for(ll i=0;i<r;i++)
{
dp[i].resize(c);
}
precomputeBoard();
}
// precomputing all the possibility and storing in matrix
void precomputeBoard()
{
for(ll i=0;i<r;i++)
{
if(i%2==1)
{
dp[i][0]='w';
dp[0][i]='w';
}
else
{
dp[i][0]='l';
dp[0][i]='l';
}
}
for(ll i=1;i<c;i++)
{
for(ll j=1;j<r;j++)
{
if(dp[j][i-1]=='l'||dp[j-1][i]=='l'||dp[j-1][i-1]=='l')
{
dp[j][i]='w';
}
else
dp[j][i]='l';
}
}
}
// printing the board;
void printBoard()
{
cout<<endl;
for(ll i=0;i<r;i++)
{
for(ll j=0;j<c;j++)
{
cout<<dp[i][j]<<" ";
}
cout<<endl;
}
}
pair<ll,ll> computerLogic(ll r,ll c)
{
pair<ll,ll> result;
if((c-1)>=0&&dp[r][c-1]=='l')
{
cout<<"Computer has chosen one from right"<<endl;
result=make_pair(r,c-1);
}
else
if((r-1)>=0&&dp[r-1][c]=='l')
{
cout<<"Computer has chosen one from left"<<endl;
result=make_pair(r-1,c);
}
else
if((r-1)>=0&&(c-1)>=0&&dp[r-1][c-1]=='l')
{
cout<<"Computer has chosen from both"<<endl;
result=make_pair(r-1,c-1);
}
return result;
}
void chipsGraphic(ll left,ll right)
{
cout<<"Quantity of chips in containers "<<endl;
cout<<"Left : ";
for(ll i=0;i<left;i++)
{
cout<<setw(2)<<"0 ";
}
cout<<" Right : ";
for(ll i=0;i<right;i++)
{
cout<<setw(2)<<"0 ";
}
cout<<"\n \n";
}
void gameutil(ll row,ll col,string c)
{
ll nrow,ncol;
string choice;
pair<ll,ll> temp;
if(row==0&&col==0)
{
//base case
if(c=="player")
{
cout<<"Computer wins the game"<<endl;
}
else
{
cout<<"Player wins the game"<<endl;
}
return;
}
cout<<"Current Status of chips in piles"<<endl;
cout<<"Chips left in left pile : "<<row<<" chips left in right pile : "<<col<<endl;
chipsGraphic(row,col);
cout<<"\n \n \n";
if(c=="computer")
{
temp=computerLogic(row,col);
nrow=temp.first;
ncol=temp.second;
gameutil(nrow,ncol,"player");
}
if(c=="player")
{
cout<<" You have three options : "<<endl;
cout<<" 1. To Take one from left pile enter 'left' "<<endl;
cout<<" 2. To Take one from right pile enter 'right' "<<endl;
cout<<" 3. To Take one from both pile enter 'both' "<<endl;
//screening
while(1)
{
cout<<" Enter your choice here : "<<endl;
cout<<"\t";
cin>>choice;
cout<<"\n\n";
if(choice=="left")
{
if(row-1>=0)
{
break;
}
else
{
cout<<"You cant choose this option :( Choose again"<<endl;
}
}
else
if(choice=="right")
{
if(col-1>=0)
{
break;
}
else
{
cout<<"You cant choose this option :( choose again"<<endl;
}
}
else
if(choice=="both")
{
if((row-1)>=0&&(col-1)>=0)
{
break;
}
else
{
cout<<"You cant choose this option :( Choose again"<<endl;
}
}
else
cout<<"Invalid input Enter again"<<endl;
}
if(choice=="left")
{
gameutil(row-1,col,"computer");
}
if(choice=="right")
{
gameutil(row,col-1,"computer");
}
if(choice=="both")
{
gameutil(row-1,col-1,"computer");
}
}
}
void game()
{
cout<<"Welcome to the chipsGame!!"<<endl;
cout<<"Short description about game "<<endl;
cout<<"(1) There will be two container each having 9 chips"<<endl;
cout<<"(2) The game will be played between Grandmaster computer and you"<<endl;
cout<<"(3) In each turn you can choose 1 chips from left or one from right or one from both"<<endl;
cout<<"(4) Contestant who takes last chips from container wins the game"<<endl;
cout<<"(5) There is no turn back, prepare and play!!"<<endl;
cout<<"(6) Computer starts the game with its 1st turn"<<endl;
cout<<"\n \n \n";
gameutil(r-1,c-1,"computer");
}
};
int main()
{
chipsGame g;
g.game();
return 0;
}