-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.cpp
112 lines (94 loc) · 2.38 KB
/
utils.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
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
#include "utils.h"
#include <iostream>
#include <sstream>
#include "boardc4.h"
#include "boardc5.h"
#include "boardothello.h"
#include "boardblocks.h"
#include "boardmatches.h"
#include "boardawale.h"
Game parse_game(const char *arg) {
std::string string(arg);
if (string=="connect4") return BOARDC4;
else if (string=="connect5") return BOARDC5;
else if (string=="othello") return OTHELLO;
else if (string=="blocks") return BLOCKS;
else if (string=="matches") return MATCHES;
else if (string=="awale") return AWALE;
else return OTHELLO;
}
float parse_float(const char *str,float defvalue) {
std::stringstream ss(str);
float v = 0;
ss >> v;
if (ss.fail()) return defvalue;
return v;
}
Board *choose_game(Game game) {
std::cout<<"let's play ";
switch (game) {
case BOARDC4:
std::cout<<"connect 4"<<std::endl;
return new BoardC4();
case BOARDC5:
std::cout<<"connect 5"<<std::endl;
return new BoardC5();
case OTHELLO:
std::cout<<"othello"<<std::endl;
return new BoardOthello();
case BLOCKS:
std::cout<<"blocks"<<std::endl;
return new BoardBlocks(20,14,true);
case MATCHES:
std::cout<<"matches"<<std::endl;
return new BoardMatches();
case AWALE:
std::cout<<"awale"<<std::endl;
return new BoardAwale();
}
}
Player *play_game(Player *player_a,Player *player_b,Board *board,int max_move) {
assert(board);
assert(player_a);
assert(player_b);
player_a->print();
std::cout<<" VS ";
player_b->print();
std::cout<<std::endl;
//FIXME check if there is a player one and a player two
Player *player_current=player_a;
Player *winner= nullptr;
Move *last_move= nullptr;
int k=0;
while (k<max_move or not max_move) {
board->print();
//get the move
Move *move=player_current->get_move(board,last_move);
if (not move) break;
delete last_move;
last_move=move;
//actually play the move
board->play_move(*move);
//check for win
Token winner_token=board->check_for_win();
if (winner_token!=NOT_PLAYED) {
if (winner_token==player_a->get_player()) winner=player_a;
else winner=player_b;
break;
}
//switch player
if (player_current==player_a) player_current=player_b;
else player_current=player_a;
k++;
}
delete last_move;
board->print();
if (winner) {
std::cout<<"winner: ";
winner->print();
} else {
std::cout<<"draw";
}
std::cout<<std::endl;
return winner;
}