-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgameController.cpp
More file actions
88 lines (68 loc) · 1.41 KB
/
Copy pathgameController.cpp
File metadata and controls
88 lines (68 loc) · 1.41 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
#include <cassert>
#include <sstream>
#include <iostream>
#include "include/gameController.h"
enum{
NADA = '0',
PERDEU,
GANHOU,
VELHA
};
GameController::GameController(){
b = new BigGame();
turn = 1;
}
int GameController::userInput(std::string msg, int player){
if(turn != player) return -1; //Não é sua vez
if (b->checkFull()) {
return 1;
}
assert(msg.size() == 4);
std::pair<int, int> g, p;
std::stringstream ss(msg);
char x, y;
ss >> x;
ss >> y;
g = std::make_pair((int)(x-'0'), (int)(y-'0'));
ss >> x;
ss >> y;
p = std::make_pair((int)(x-'0'), (int)(y-'0'));
if(!b->markPos(g, p, player)) return 1; //Posicao invalida
turn = 3 - turn;
return 0; //Sem error
}
std::string GameController::getBoardPackage(){
return 'T' + b->completeGame();
}
std::string GameController::getBigGamePackage(){
return 'B' + b->toString();
}
std::string GameController::getControlPackage(int toPlayer){
char pos;
char state;
if(toPlayer == turn){
pos = '0' + b->getCurPos();
}
else{
pos = '9';
}
if(b->checkTie()) state = VELHA;
else{
if(b->checkWin() == 0) state = NADA;
else if(b->checkWin() == 1){
if(toPlayer == 1) state = GANHOU;
else state = PERDEU;
}
else if(b->checkWin() == 2){
if(toPlayer == 1) state = PERDEU;
else state = GANHOU;
}
}
std::ostringstream ss;
ss << "C";
ss << pos << state;
return ss.str();
}
bool GameController::isTurn(int p) {
return turn == p;
}