summaryrefslogtreecommitdiffstats
path: root/GameField.cpp
blob: 703d677b67d6da826f312bc2961282ceed63e30b (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/***
	This file is part of Let's play gomoku!
	Copyright (C) 2021 Aleksandr D. Goncharov (Joursoir) <chat@joursoir.net>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
***/

#include "GameField.hpp"

GameField::GameField(int a_rows, int a_cols, int a_lwin)
	: rows(a_rows), cols(a_cols), win_length(a_lwin), state(G_NONE),
	who_move(G_XPLAYER)
{
	int r, c;
	field = new int*[rows];
	for(r = 0; r < rows; r++) {
		field[r] = new int[cols];
		for(c = 0; c < cols; c++) {
			field[r][c] = G_EMPTY;
		}
	}
	free_fields = rows * cols;
}

GameField::GameField(const GameField& a)
{
	int r, c;
	rows = a.rows;
	cols = a.cols;
	free_fields = a.free_fields;
	win_length = a.win_length;
	state = a.state;
	who_move = a.who_move;

	field = new int*[rows];
	for(r = 0; r < rows; r++) {
		field[r] = new int[cols];
		for(c = 0; c < cols; c++) {
			field[r][c] = a.field[r][c];
		}
	}
}

GameField::~GameField()
{
	if(field) {
		int r;
		for(r = 0; r < rows; r++) {
			delete[] field[r];
		}
		delete[] field;
	}
}

bool GameField::CanMove(int y, int x)
{
	if(field[y][x] != G_EMPTY)
		return false;
	return true;
}

void GameField::Move(int y, int x)
{
	field[y][x] = who_move;
	who_move = -who_move; // change player
	free_fields--;

	UpdateState(y, x);
}

void GameField::UndoMove(int y, int x)
{
	who_move = field[y][x];
	field[y][x] = G_EMPTY;
	free_fields++;

	UpdateState(y, x);
}

void GameField::UpdateState(int y, int x) 
{
	if((state = ScanRowsAround(y, x)) != G_NONE)
		return;
	if((state = ScanColsAround(y, x)) != G_NONE)
		return;
	if((state = ScanDiagsAround(y, x)) != G_NONE)
		return;

	if(!free_fields) {
		state = G_DRAW;
		return;
	}
}

int GameField::ScanRowsAround(int y, int x)
{
	int i, n;
	int player = field[y][x];
	// ray to left
	for(i = x, n = 0; i >= 0; i--) {
		if(player == field[y][i]) {
			n++;
			if(n == win_length)
				return player;
		}
		else break;
	}

	// ray to right
	for(i = x, n -= 1; i < cols; i++) {
		if(player == field[y][i]) {
			n++;
			if(n == win_length)
				return player;
		}
		else break;
	}

	return G_NONE;
}

int GameField::ScanColsAround(int y, int x)
{
	int i, n;
	int player = field[y][x];
	// ray to up
	for(i = y, n = 0; i >= 0; i--) {
		if(player == field[i][x]) {
			n++;
			if(n == win_length)
				return player;
		}
		else break;
	}

	// ray to down
	for(i = y, n -= 1; i < rows; i++) {
		if(player == field[i][x]) {
			n++;
			if(n == win_length)
				return player;
		}
		else break;
	}

	return G_NONE;
}

int GameField::ScanDiagsAround(int y, int x)
{
	int i, j, n;
	int player = field[y][x];
	// ray to left up corner
	for(i = x, j = y, n = 0; i >= 0 && j >= 0; i--, j--) {
		if(player == field[j][i]) {
			n++;
			if(n == win_length)
				return player;
		}
		else break;
	}

	// ray to right down corner
	for(i = x, j = y, n -= 1; i < cols && j < rows; i++, j++) {
		if(player == field[j][i]) {
			n++;
			if(n == win_length)
				return player;
		}
		else break;
	}
	
	/* change the diagonal */

	// ray to right up corner
	for(i = x, j = y, n = 0; i < cols && j >= 0; i++, j--) {
		if(player == field[j][i]) {
			n++;
			if(n == win_length)
				return player;
		}
		else break;
	}

	// ray to left down cornet
	for(i = x, j = y, n -= 1; i >= 0 && j < rows; i--, j++) {
		if(player == field[j][i]) {
			n++;
			if(n == win_length)
				return player;
		}
		else break;
	}

	return G_NONE;
}