-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
61 lines (51 loc) · 1.56 KB
/
game.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
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include "Board.cpp"
#include "View.cpp"
#include "Random.cpp"
#include "Human.cpp"
#include "Minimax.cpp"
Player* getClass(char* name, View* view){
if (strcmp(name, "Random") == 0){
return new Random();
}else if (strcmp(name, "Minimax") == 0){
return new Minimax();
}else if (strcmp(name, "Human") == 0){
return new Human(view);
}
return NULL;
}
int main(int argc, char **argv){
Board* board = new Board(20, 20, 5);
View* view = new View();
Player* firstPlayer = getClass(argv[1], view);
Player* secondPlayer = getClass(argv[2], view);
view->init();
view->render(board);
int gameResult = -1;
int activePlayerNum = 1;
Player* players[3] = {NULL, firstPlayer, secondPlayer};
while((gameResult = board->determineWinner()) == 0){
Player* activePlayer = players[activePlayerNum];
int selectedRow = 0, selectedColumn = 0;
do{
view->render(board);
activePlayer->getNextStep(board, activePlayerNum, &selectedRow, &selectedColumn);
}while(!board->fill(selectedRow, selectedColumn, activePlayerNum));
activePlayerNum = 3 - activePlayerNum;
view->render(board);
usleep(50000);
}
view->render(board);
getch();
view->end();
if (gameResult == 1){
printf("Player 1 (%s) wins for this game\n", argv[1]);
}else if (gameResult == 2){
printf("Player 2 (%s) wins for this game\n", argv[2]);
}else{
printf("Draw\n");
}
return 0;
}