-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbigGame.cpp
More file actions
115 lines (93 loc) · 2.94 KB
/
bigGame.cpp
File metadata and controls
115 lines (93 loc) · 2.94 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
#include "include/bigGame.h"
#include <time.h>
#include <cstdio>
#include <cstdlib>
BigGame::BigGame() {
// o primeiro jogador devera jogar em um game definido aleatoriamente
srand(time(NULL));
curGame = std::make_pair(rand()%3, rand()%3);
}
int BigGame::checkWin() {
//determina se ganhou em alguma linha
for (int i = 0; i < 3; i++) {
if (tab[i][0].checkWin() && tab[i][0].checkWin() == tab[i][1].checkWin() && tab[i][0].checkWin() == tab[i][2].checkWin())
return tab[i][0].checkWin();
}
//determina se ganhou em alguma coluna
for (int j = 0; j < 3; j++) {
if (tab[0][j].checkWin() && tab[0][j].checkWin() == tab[1][j].checkWin() && tab[0][j].checkWin() == tab[2][j].checkWin())
return tab[0][j].checkWin();
}
//determina se ganhou na diagonal principal
if (tab[0][0].checkWin() && tab[0][0].checkWin() == tab[1][1].checkWin() && tab[0][0].checkWin() == tab[2][2].checkWin())
return tab[0][0].checkWin();
//determina se ganhou na diagonal secundaria
if (tab[0][2].checkWin() && tab[0][2].checkWin() == tab[1][1].checkWin() && tab[0][2].checkWin() == tab[2][0].checkWin())
return tab[0][2].checkWin();
// nenhum jogador ganhou ainda
return 0;
}
bool BigGame::checkFull() {
if (checkWin()) return true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// ainda se pode jogar nesse game por ha posicoes vazias e ninguem o ganhou
if (!tab[i][j].checkWin() && !tab[i][j].checkFull()) return false;
}
}
// nao ha mais games para serem jogados
return true;
}
bool BigGame::validPos(std::pair<int, int> g, std::pair<int, int> p) {
// uma posicao eh valida caso esteja vazia e pertenca ao game correto
return (g == curGame && tab[g.first][g.second].validPos(p.first, p.second));
}
std::pair<int, int> BigGame::setGame(int i, int j) {
// caso o game atual esteja indisponivel passamos ao da direita
while (tab[i][j].checkWin() || tab[i][j].checkFull()) {
j++;
if (j == 3) {
i++;
j = 0;
}
if (i == 3) {
i = 0;
j = 0;
}
}
return std::make_pair(i, j);
}
bool BigGame::markPos(std::pair<int, int> g, std::pair<int, int> p, int player) {
// retorna que deu erro caso a posicao que o jogador quer marcar seja invalida
if (!validPos(g, p)) return false;
// marca a posicao jogada
tab[g.first][g.second].mat[p.first][p.second] = player;
// determina qual o proximo game a ser jogado
if (!checkFull()) curGame = setGame(p.first, p.second);
return true;
}
bool BigGame::checkTie() {
// se ninguem tiver ganhado mas nao ha mais onde jogar deu velha
return (checkFull() && !checkWin());
}
std::string BigGame::completeGame() {
std::string res;
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
res += tab[i][j].toString();
}
}
return res;
}
std::string BigGame::toString() {
std::string res;
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
res += '0' + tab[i][j].checkWin();
}
}
return res;
}
int BigGame::getCurPos() {
return curGame.first * 3 + curGame.second;
}