Repository navigation
Expand file tree
/
Copy pathNIMgame.cpp
More file actions
140 lines (128 loc) · 3.26 KB
/
Copy pathNIMgame.cpp
File metadata and controls
140 lines (128 loc) · 3.26 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "NIMgame.h"
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <ctime>
using namespace std;
enum STATUS { OUT_OF_BOUNDS = -1, SUCCESS, EMPTY, TOO_MANY_ITEMS_TAKEN, ZERO_ITEMS_TAKEN };
enum GAMEMODE { PVP = 1, PVC, CVC };
Game::Game() {
srand(time(NULL));
init(gamemode());
generate();
start();
}
int Game::take(int bunchNum, int items) {
if (bunchNum < 0 || bunchNum >= numberOfBunches) return OUT_OF_BOUNDS;
if (bunches[bunchNum] < 1) return EMPTY;
if (bunches[bunchNum] < items) return TOO_MANY_ITEMS_TAKEN;
if (items < 1) return ZERO_ITEMS_TAKEN;
else {
bunches[bunchNum] -= items;
if (bunches[bunchNum] == 0)
notEmpty_iterator--;
return SUCCESS;
}
}
void Game::show() {
system("cls");
cout << "There's next items in bunches:\n";
for (int i = 0; i < numberOfBunches; i++) {
if (bunches[i] > 0)
printf("Bunch #%d: %d items\n", i, bunches[i]);
}
cout << endl;
}
void Game::generate() {
cout << "Insert count of bunches: ";
cin >> numberOfBunches;
bunches = new int[numberOfBunches];
for (int i = 0; i < numberOfBunches; i++)
bunches[i] = rand() % (max - min + 1) + min;
notEmpty_iterator = numberOfBunches;
}
int Game::gamemode() {
system("cls");
cout << "[[[THE NIM GAME]]] ---> Igromania: 8/10 | StopGame: 9/10 | PC Gamer: 8/10 \n\n";
cout << "Choose gamemode:\n1) Player vs Player\n2) Player vs Computer\n3) Computer vs Computer\n";
while (1)
switch (_getch() - '0') {
case PVP: return PVP;
case PVC: return PVC;
case CVC: return CVC;
default: break;
}
}
void Game::init(int gm) {
switch (gm) {
case PVP:
Players[0] = new Human("Player 1");
Players[1] = new Human("Player 2");
return;
case PVC:
Players[0] = new Human("Player");
Players[1] = new Computer("Computer");
return;
case CVC:
Players[0] = new Computer("Computer 1");
Players[1] = new Computer("Computer 2");
return;
}
}
void Game::start() {
Player* currentPlayer = Players[0];
pair pair;
while (notEmpty_iterator) {
currentPlayer = Players[step % 2];
show();
//cout << currentPlayer->name << ", insert number of bunch and count of items: ";
pair = currentPlayer->move(bunches, numberOfBunches);
if (take(pair.bunch, pair.items) == SUCCESS) {
step++;
}
else {
cout << "Wrong move, try again";
Sleep(2000);
}
}
cout << currentPlayer->name << " wins!\n";
}
Game::Human::Human(const char* name) {
this->name = name;
}
Game::pair Game::Human::move(int* array, int size) {
int bunch, items;
cout << name << ", insert number of bunch and count of items: ";
cin >> bunch >> items;
return { bunch, items };
}
Game::Computer::Computer(const char* name) {
this->name = name;
}
Game::pair Game::Computer::move(int* array, int size) {
cout << name << ", insert number of bunch and count of items: ";
for (int i = 0; i < size; i++) {
if (array[i] > 0) {
for (int j = array[i]; j > 0; j--) {
int xorSum = array[0];
for (int k = 1; k < size; k++)
xorSum = xorSum ^ (k != i ? array[k] : array[k] - j);
if (!xorSum) {
Sleep(1000);
cout << i << " " << j << endl;
Sleep(2000);
return { i,j };
}
}
}
}
for (int i = 0; i < size; i++) {
if (array[i] > 0) {
int j = rand() % array[i] + 1;
Sleep(1000);
cout << i << " " << j << endl;
Sleep(2000);
return { i,j };
}
}
}