-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
130 lines (119 loc) · 2.99 KB
/
game.cpp
File metadata and controls
130 lines (119 loc) · 2.99 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
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "game.h"
#include <iostream>
#include "minmax.h"
using namespace std;
Game::Game(int size, int win) : size(size),capacity(0), win(win),board(size,vector<Element>(size,EMPTY)) {}//constructor
int Game::isWin() {
for(int i=0;i<size;i++){
for(int j=0;j<=size-win;j++){
int check=0;
for(int k=0;k<win;k++){
if(board[i][j+k]!=EMPTY && board[i][j]==board[i][j+k]){
check++;
}
}
if(check==win){
return 1;
}
}
}
for(int i=0;i<=size-win;i++){
for(int j=0;j<size;j++){
int check=0;
for(int k=0;k<win;k++){
if(board[i+k][j]!=EMPTY && board[i][j]==board[i+k][j]){
check++;
}
}
if(check==win){
return 1;
}
}
}
for(int i=0;i<=size-win;i++){
for(int j=0;j<=size-win;j++){
int check=0;
for(int k=0;k<win;k++){
if(board[i+k][j+k]!=EMPTY && board[i][j]==board[i+k][j+k]){
check++;
}
}
if(check==win){
return 1;
}
}
}
for(int i=win-1;i<size;i++){
for(int j=0;j<=size-win;j++){
int check=0;
for(int k=0;k<win;k++){
if(board[i-k][j+k]!=EMPTY && board[i][j]==board[i-k][j+k]){
check++;
}
}
if(check==win){
return 1;
}
}
}
if(capacity==size*size){
return 2;
}
return 0;
}
void Game::printBoard(bool s) {
char c_s;
char h_s;
if(s==0){//setting signs
h_s='O';
c_s='X';
}
if(s==1){
h_s='X';
c_s='O';
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
switch (board[i][j]) {
case EMPTY:
cout << " ";
break;
case COMPUTER:
cout<<" ";
cout << c_s;
cout<<" ";
break;
case HUMAN:
cout<<" ";
cout << h_s;
cout<<" ";
break;
}
if (j < size - 1) {
cout << " | ";
}
}
cout <<endl;
if (i < size - 1) {
for (int j = 0; j < size; j++) {
cout << " - ";
if (j < size - 1) {
cout << " + ";
}
}
cout <<endl;
}
}
}
void Game::humanMove(int x, int y) {
if (board[x][y] == EMPTY) {
board[x][y] = HUMAN;
capacity++;
}
}
void Game::computerMove(pair<int,int> move) {
if (board[move.first][move.second] == EMPTY) {
board[move.first][move.second] = COMPUTER;
capacity++;
}
}