-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
executable file
·62 lines (52 loc) · 1.21 KB
/
Copy pathmain.c
File metadata and controls
executable file
·62 lines (52 loc) · 1.21 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
// hw2.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
#include "sp_nim.h"
#include "main_aux.h"
#define SIZE 32
int isEmpty(int arr[], int len);
void user_move(int heaps[], int numOfHeaps);
void comp_move(int heaps[], int numOfHeaps);
void printHeaps(int arr[], int len);
void printNames(int arr[], int len, int cnt);
int main()
{
int heaps[32] = { 0 };
int numOfHeaps = 0;
int i = 0;
int cnt = 0;
printf("Enter the number of heaps:\n");
scanf("%d", &numOfHeaps);
if (numOfHeaps < 1 || numOfHeaps>32)
{
printf("Error: the number of heaps must be between 1 and 32.\n");
exit(0);
}
printf("Enter the heap sizes:\n");
while (i < numOfHeaps && scanf("%d", &heaps[i]) == 1)
{
if (heaps[i] <= 0)
{
printf("Error: the size of heap %d should be positive.\n", i + 1);
exit(0);
}
i++;
}
while (1) {
printNames(heaps, numOfHeaps, ++cnt);
comp_move(heaps, numOfHeaps);
if (isEmpty(heaps, numOfHeaps)) {
printf("Computer wins!\n");
exit(0);
}
printNames(heaps, numOfHeaps, ++cnt);
printHeaps(heaps, numOfHeaps);
user_move(heaps, numOfHeaps);
if (isEmpty(heaps, numOfHeaps)) {
printf("You win!\n");
exit(0);
}
}
return 0;
}