-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnimsmart.c
More file actions
executable file
·114 lines (100 loc) · 3.17 KB
/
nimsmart.c
File metadata and controls
executable file
·114 lines (100 loc) · 3.17 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int rrange(int, int);
int main()
{
srand(time(NULL));
int pile = rrange(10, 100);
int turn = rrange(0, 1);
int firstTurn = 1;
printf("Welcome to the game of Nim (version B).\n");
printf("In this game, two players alternatley take marbles from a pile.\n");
printf("In each move, a player chooses how many marbles to take.\n");
printf("The player must take at least one but at most half of the marbles.\n");
printf("Then the other player takes a turn.\n");
printf("The player who takes the last marble wins.\n");
printf("====================================================\n");
printf("There are %i marbles on the pile.\n", pile);
do
{
if(turn == 0) turn = 1;
else turn = 0;
if(turn == 0)
{
if(firstTurn == 1) printf("You have the first turn.\n");
else printf("It is your turn.\n");
do
{
printf("Please enter how many marbles you would like to remove from the pile: ");
int howMany = 0;
if(scanf("%i", &howMany) != EOF)
{
if(pile == 1)
{
if(howMany == 1)
{
pile = 0;
printf("There are now %i marbles on the pile.\n", pile);
break;
}
else
{
printf("Please enter a valid number.\n");
printf("You must take at least one but at most half of the marbles.\n");
}
}
else if((howMany >= 1) && (howMany <= (pile / 2)))
{
pile -= howMany;
printf("There are now %i marbles on the pile.\n", pile);
break;
}
else
{
printf("Please enter a valid number.\n");
printf("You must take at least one but at most half of the marbles.\n");
}
}
else
{
printf("Please enter a number.\n");
}
}
while(1);
}
else
{
if(firstTurn == 1) printf("The computer has the first turn.\n");
else printf("It is the computers turn.\n");
if(pile == 63 || pile == 31 || pile == 15 || pile == 7 || pile == 3) pile = pile - rrange(1, pile / 2);
else if(pile > 63) pile = 63;
else if(pile > 31) pile = 31;
else if(pile > 15) pile = 15;
else if(pile > 7) pile = 7;
else if(pile > 3) pile = 3;
else if(pile == 2) pile = 1;
else if(pile == 1) pile = 0;
printf("There are now %i marbles on the pile.\n", pile);
}
firstTurn = 0;
}
while(pile != 0);
if(turn == 0)
{
printf("The computer has won.\n");
}
else
{
printf("You have won.\n");
}
}
int rrange(int a, int b)
{
int temp = rand();
while(temp < a || temp > b)
{
temp = rand();
}
return temp;
}