-
Notifications
You must be signed in to change notification settings - Fork 0
/
NIM The Game Made By Ruchir Mehta.cpp
222 lines (220 loc) · 4.89 KB
/
NIM The Game Made By Ruchir Mehta.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
#include<bits/stdc++.h>
using namespace std;
bool gameOver(int a[],int n)
{for(int i=0;i<n;i++)
{if(a[i]>0)
return false;
}
return true;
}
void playchoice1(string name)
{system("CLS");
int n;
cout<<"Enter the number of piles : ";
cin>>n;
int a[n];
cout<<"Enter the number of coins in those piles respectively"<<endl;
for(int i=0;i<n;i++)
cin>>a[i];
//bool b=false;
while(!gameOver(a,n))
{int myxor=a[0];
for(int i=1;i<n;i++)
{myxor=myxor^a[i];
}
if(myxor!=0)
{for(int i=0;i<n;i++)
{if((a[i]^myxor)<a[i])
{cout<<"Computer :"<<endl;
cout<<"Pile no. :"<<i+1<<endl;
cout<<"Coins Removed :"<<a[i]-(a[i]^myxor)<<endl;
a[i]=a[i]^myxor;
break;
}
}
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
else
{int max_index=0;
for(int i=0;i<n;i++)
{if(a[i]>a[max_index])
max_index=i;
}
cout<<"Computer :"<<endl;
cout<<"Pile no. :"<<max_index+1<<endl;
cout<<"Coins Removed :"<<1<<endl;
a[max_index]-=1;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
if(gameOver(a,n))
{cout<<"Computer Won!!!"<<endl;
break;}
//
int temp1,temp2;
cout<<name<<" :"<<endl;
cout<<"Pile no. :";
cin>>temp1;
cout<<"Coins Removed :";
cin>>temp2;
a[temp1-1]-=temp2;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
if(gameOver(a,n))
{cout<<name<<" Won!!!";
break;}
}
}
//
//
//
//
void playchoice2(string name)
{system("CLS");
int n=5+rand()%5,temp1,temp2;
int a[n];
for(int i=0;i<n;i++)
{a[i]=1+rand()%20;
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"Human :"<<endl;
cout<<"Pile no. :";
cin>>temp1;
cout<<"Coins Removed :";
cin>>temp2;
a[temp1-1]-=temp2;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
while(!gameOver(a,n))
{int myxor=a[0];
for(int i=1;i<n;i++)
{myxor=myxor^a[i];
}
if(myxor!=0)
{for(int i=0;i<n;i++)
{if((a[i]^myxor)<a[i])
{cout<<"Computer :"<<endl;
cout<<"Pile no. :"<<i+1<<endl;
cout<<"Coins Removed :"<<a[i]-(a[i]^myxor)<<endl;
a[i]=a[i]^myxor;
break;}
}
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
else
{int max_index=0;
for(int i=0;i<n;i++)
{if(a[i]>a[max_index])
max_index=i;
}
cout<<"Computer :"<<endl;
cout<<"Pile no. :"<<max_index+1<<endl;
cout<<"Coins Removed :"<<1<<endl;
a[max_index]-=1;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
if(gameOver(a,n))
{cout<<"Computer Won!!!";
break;}
//
int temp1,temp2;
cout<<name<<" :"<<endl;
cout<<"Pile no. :";
cin>>temp1;
cout<<"Coins Removed :";
cin>>temp2;
a[temp1-1]-=temp2;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
if(gameOver(a,n))
{cout<<name<<" Won!!!";
break;}
}
}
//
//
//
//
void playchoice3(string name1,string name2)
{system("CLS");
int n,temp1,temp2;
cout<<name1<<", Enter the number of piles : ";
cin>>n;
int a[n];
cout<<name1<<", Enter the number of coins in those piles respectively"<<endl;
for(int i=0;i<n;i++)
cin>>a[i];
bool b=false;
while(!gameOver(a,n))
{if(!b)
cout<<name2<<" :"<<endl;
else
cout<<name1<<" :"<<endl;
cout<<"Pile no. :";
cin>>temp1;
cout<<"Coins Removed :";
cin>>temp2;
a[temp1-1]-=temp2;
b=!b;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
if(b)
cout<<name2<<" Won!!!!";
else
cout<<name1<<" Won!!!!";
}
main()
{string name1,name2;
int choice;
cout<<" ..............Welcome To The Great Game Of NIM............."<<endl;
cout<<" -Made By Ruchir Mehta"<<endl<<endl;
cout<<" Which Type of Game do you want to play?"<<endl;
cout<<" 1. Comuter vs Human (Computer plays first)"<<endl;
cout<<" 2. Human vs Computer (Human plays first)"<<endl;
cout<<" 3. Human vs Human "<<endl<<endl;
cout<<" Please Enter your Choice : ";
cin>>choice;
switch(choice)
{
case 1:
system("CLS");
cout<<" Kindly Enter Your Name : ";
cin>>name1;
cout<<endl;
cout<<" ..........Hello "<<name1<<" ..........."<<endl;
playchoice1(name1);
break;
case 2:
system("CLS");
cout<<" Kindly Enter Your Name : ";
cin>>name1;
cout<<endl;
cout<<" ..........Hello "<<name1<<" ..........."<<endl;
playchoice2(name1);
break;
case 3:
system("CLS");
cout<<" Kindly Enter Player 1 Name : ";
cin>>name1;
cout<<endl;
cout<<" Kindly Enter Player 2 Name : ";
cin>>name2;
playchoice3(name1,name2);
break;
default:
cout<<endl<<"Please Enter a Correct Choice.... :( ....."<<endl;
}
}