summaryrefslogtreecommitdiffstats
path: root/GameField.cpp
blob: 75485b4f50cd18f0d4f97699dcc0f9a9dc47a28c (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
#include "GameField.hpp"

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

GameField::~GameField()
{
	if(field) {
		int c;
		for(c = 0; c < cols; c++) {
			delete[] field[c];
		}
		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::UpdateState(int y, int x) 
{
	if(!free_fields) {
		state = G_DRAW;
		return;
	}

	if((state = ScanRowsAround(y, x)) != G_NONE)
		return;
	if((state = ScanColsAround(y, x)) != G_NONE)
		return;
	if((state = ScanDiagsAround(y, x)) != G_NONE)
		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 < rows; 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 < cols; 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 < rows && j < cols; 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 < rows && 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 < cols; i--, j++) {
		if(player == field[j][i]) {
			n++;
			if(n == win_length)
				return player;
		}
		else break;
	}

	return G_NONE;
}