-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnimgame.cpp
137 lines (135 loc) · 2.33 KB
/
nimgame.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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<stdbool.h>
#define COMPUTER 1
#define HUMAN 2
int heap(void);
int objects(void);
int firstplayer(void);
void hmturn(void);
void cpturn(void);
void termination(void);
void printa(void);
int heapnum,object;
int a[10];
bool end=true;
int main(void)
{
int i,firstp,turn;
turn = 1;
srand((unsigned int)time(NULL));
printf("Create %d heaps of size",heapnum = heap());
for (i = 1; i <= heapnum; i++)
{
printf(" %d", object=objects());
a[i-1] = object;// store heaps in an array
}
printf("\n");
if ((firstp = firstplayer()) == COMPUTER)
{
printf("Player computer goes first\n");
turn = 0;
}
else
printf("Player human goes first\n");
while (turn <= 1000)
{
if ((turn % 2) == 0)//even turn,computer
{
cpturn();
printa();
}
else //odd turn,human
{
hmturn();
printa();
}
termination();
if (end == false)
break;
turn++;
}
if ((turn % 2) == 0)
printf("Player computer has won\n");
else
printf("Player human has won\n");
return 0;
}
int heap(void)
{
int i,j;
j = 0;
i = rand() % 9;
if (i <= 2)
j = 3;
else if (i >= 6)
j = 5;
else j = 7;
return j;
}
int objects(void)
{
int i, j;
i = rand() % 3;
if (i == 0)
j = 9;
else if (i == 1)
j = 11;
else j = 13;
return j;
}
int firstplayer(void)
{
int i, j;
i = rand() % 2;
if (i == 0)
j = COMPUTER;
else j = HUMAN;
return j;
}
void cpturn(void)
{
int i, j, m;
i = rand() % heapnum+1;//computer chooses heap
while(a[i-1]==0)
i = rand() % heapnum + 1;//valid heap
m = rand() % a[i-1];
j = m + 1;
a[i-1]-=j;
printf("Player computer takes %d objects from heap %d\n",j,i);
}
void printa(void)
{
int i;
for (i = 0; i < heapnum; i++)
printf("%d ", a[i]);
printf("\n");
}
void hmturn(void)
{
int i, j;
printf("Player human enter the number of objects(Y) to take from what heap (X)- in order: Y X\n");
while (scanf_s("%d %d", &i, &j) != 2||(a[j-1]<i))
{
while (getchar() != '\n')
continue;
printf("Player human that is an invalid move,try again\n");
printf("Player human enter the number of objects(Y) to take from what heap (X)- in order: Y X\n");
}
a[j-1] -= i;
}
void termination(void)//check if game terminates
{
int i;
for (i = 0; i < heapnum; i++)
{
if (a[i] != 0)
{
end = true;
break;
}
else
end = false;
}
}