-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multi.cpp
83 lines (81 loc) · 1.61 KB
/
Multi.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
#include "NIM.h"
#define playerA 1
#define playerB 2
bool winnerIs(int whoseTurns)
{
if (whoseTurns == playerB)
{
return 1;
}
else
{
return 2;
}
}
int multiPlayer()
{
printf_s("Who take a last token lose this game\n\n");
int plr2Win = 0, plr1Win = 0, token = 0, ppt = 0, ppt2 = 0, pTT = -1, nextTok = -1, onTurn = -1, save = 0;
char plr1Name = 'A';
char plr2Name = 'B';
FILE* s = nullptr;
printf_s("Would you like to save the game? (1/0): ");
scanf_s("%d", &save);
printf_s("number of tokens: ");
scanf_s("%d", &token);
onTurn = move();
if (save == 1)
{
fopen_s(&s, "save.txt", "w");
}
while (token > 0)
{
if (onTurn == 1)
{
printf_s("Player A: How many token would you take?: ");
pTT = -1;
scanf_s("%d", &pTT);
nextTok = playerTurn(token, pTT);
token = nextTok;
++ppt;
if (save == 1)
{
fprintf_s(s, "player A takes: %d tokens remaining: %d\n", pTT, token);
}
onTurn = 2;
}
else
{
printf_s("Player B: How many token would you take?: ");
pTT = -1; //player take token
scanf_s("%d", &pTT);
nextTok = playerTurn(token, pTT);
token = nextTok;
++ppt2;
if (save == 1)
{
fprintf_s(s, "player B takes: %d tokens remaining: %d\n", pTT, token);
}
onTurn = 1;
}
}
if (save == 1)
{
fclose(s);
}
if (winnerIs(1))
{
printf_s("The winner is: %c\n", plr1Name);
++plr1Win;
}
else
{
printf_s("The winner is: %c\n", plr2Name);
++plr2Win;
}
FILE* f = nullptr;
fopen_s(&f, "Multi.txt", "a");
fprintf_s(f, "%c: %d (%d) %c: %d (%d)\n", plr1Name, plr1Win, ppt, plr2Name, plr2Win, ppt2, "Multi.txt", "a");
system("pause");
return 0;
}