-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate_game.c
67 lines (59 loc) · 1.6 KB
/
evaluate_game.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
#include <stdio.h>
#include "game.h"
int greatest_common_divisor(int a, int b) // NWD - algorym Euklidesa
{
while (a != b) {
if (a > b)
a -= b;
else
b -= a;
}
return a; // lub b - obie zmienne przechowują wynik NWD(a,b)
}
void make_relatively_first(int *a, int *b) {
int gcd = greatest_common_divisor(*a, *b);
*a /= gcd;
*b /= gcd;
}
int play_game(int a, int b) {
int counter = 0;
int decision = 1;
int winner;
while (decision != 0) {
decision = play(a, b);
++counter;
switch (decision) {
case -1:
// printf("invalid input values provided");
return -1;
case 0:
// printf("the game is over\n");
// (counter % 2 == 0) ? printf("player one wins\n") :
// printf("player two wins\n");
winner = (counter % 2 == 0) ? 0 : 1;
return winner;
case 1:
--a;
make_relatively_first(&a, &b);
// printf("a = %d", a);
// printf(", b = %d\n", b);
break;
case 2:
--b;
make_relatively_first(&a, &b);
// printf("a = %d", a);
// printf(", b = %d\n", b);
break;
default:
break;
}
}
return -2;
}
int main() {
int a, b;
if (!scanf("%d", &a)) printf("invalid a provided\n");
if (!scanf("%d", &b)) printf("invalid b provided\n");
printf("%d", play_game(a, b));
return 0;
}