Newer
Older
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
#include <ncurses.h>
#include <iostream>
#include <csignal>
#include <cstdlib>
#include <time.h>
#include "board.h"
#include "networking.h"
#include "util.h"
connection *c;
// Stop the terminal from freaking out when the program is exited
// Even if other stuff isn't cleaned up, the terminal will
// still be useable
void cleanup_screen(int signum) {
destroy_screen();
delete c;
exit(signum);
}
int main(int argc, char *argv[]) {
srand(time(NULL));
init_screen();
signal(SIGINT, cleanup_screen);
signal(SIGTERM, cleanup_screen);
signal(SIGSEGV, cleanup_screen);
mvprintw(0, 0, "Client or server?");
mvprintw(1, 0, "1) Server");
mvprintw(2, 0, "2) Client");
int key = getch();
while (!(key == '1' || key == '2')) key = getch();
if (key == '1') {
c = new server_connection;
} else {
c = new client_connection;
}
board player_board(PLAYER);
std::string opponent_board_desc = c->exchange_message(player_board.get_description());
board opponent_board(OPPONENT, opponent_board_desc);
clear();
player_board.draw();
opponent_board.draw();
while(!player_board.has_lost() && !opponent_board.has_lost()) {
std::string move = opponent_board.get_move();
std::string opponent_move = c->exchange_message(move);
player_board.make_move(opponent_move);
opponent_board.make_move(move);
player_board.draw();
opponent_board.draw();
usleep(1000000);
}
getch();
destroy_screen();
delete c;
}