-
Notifications
You must be signed in to change notification settings - Fork 3
/
NimGame.c
141 lines (107 loc) · 3.24 KB
/
NimGame.c
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
#include<stdio.h>
#include<string.h>
void main(){
//Game Starts Here!!
printf("Welcome to NimGame\n\n");
//Intialising useful Variables
int C=0,Sticks,u,TBlanks,Blanks,Counter,i,x,p,j,NoBlank,Start,End;
//Initialising and Declaring useful Variables
char Input[5],Array[15]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
//This Piece of code(Z) Displays the Moves
Z:
//Declaring useful Variables
Sticks=3,Blanks=0,Counter=0,NoBlank=00;
//Start of Pattern
printf("\t + + + + + + + + +\n");
while(Sticks<=7){
printf("\t + ");
for(i=1;i<=Sticks;i++){
if(Array[Counter]==1)
{
printf("| ");
}else{
printf(" ");
}
++Counter;
}
for(j=1;j<=(7-Sticks);j++){
printf(" ");
}
printf("+");
printf("\n");
Sticks+=2;
}
printf("\t + + + + + + + + +\n");
//End of Pattern
//=========+=======+=======+=======+=======+=========+=============+===============++==============+============+=========+
printf("%d",C);
//Asking Apt. User for Input
if((C%2)!=0){
printf("Player 1 Enter The Range to be Deleted <Start-End><Start=End, for Single Deletion> : ");
}else{
printf("Player 2 Enter The Range to be Deleted <Start-End><Start=End, for Single Deletion> : ");
}
//Input from User
scanf("%s",&Input);
printf("\n");
//Extracting Start of Range
Start=(((Input[0]-48)*10)+(Input[1]-48));
//Extracting End of Range
End=(((Input[3]-48)*10)+(Input[4]-48));
//printf("%d%d",Start,End);
//Loop to count No. Of Blanks b/w User's Start and End Position(s)
for(x=Start;x<=End;x++)
{
if(Array[(x-1)]==0){Blanks++;}
}//printf("%d",Blanks);
//Validating the Move
if(Blanks==0){
NoBlank=11;
}
else{
NoBlank=00;
}
//Counting No. Of Blanks in Nimgame
for(u=0;u<15;u++)
{
if(Array[u]==0){TBlanks++;}
}
//Checks for Valid Move i.e without Blanks
if(NoBlank==11){
//Checks for Valid Move i.e Same Row is Valid else not
if(((Start<=3)&&(End>=4))||((Start<=8)&&(End>=9)))
{
printf("Invalid Move!! Choose From Single Row...\n\n");
goto Z;
}else{
if(TBlanks<15){
if((C%2)!=0){
printf("\n\tPlayer 1's Move:%s\n",Input);
//Makes Blanks in ARRAY as per Valid Input
for(p=Start;p<=End;p++){
Array[(p-1)]=0;
}
}else{
printf("\n\tPlayer 2's Move:%s\n",Input);
//Makes Blanks in ARRAY as per Valid Input
for(p=Start;p<=End;p++){
Array[(p-1)]=0;
}
}
//Counter of Chance
++C;
goto Z;
}else{
if((C%2)!=0){
printf("\n\tPlayer 2 Won!! :)\n");
}else{
printf("\n\tPlayer 1 Won!! :)\n");
}
}
}
}else{
printf("Blanks Cannot be Deleted\n\n");
goto Z;
}
//End Of Game
}