summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-02-17 21:39:00 +0000
committerJoursoir <chat@joursoir.net>2021-02-17 21:39:00 +0000
commitb89e003d7e1ed6997d2a5a9da45b5305b3bb9b05 (patch)
tree241372d8a2185c855b1f2c40cbd822ed265714f4
parent00fe0c43e7080a8d8af2566196a8540eee553ad2 (diff)
downloadlp-gomoku-b89e003d7e1ed6997d2a5a9da45b5305b3bb9b05.tar.gz
lp-gomoku-b89e003d7e1ed6997d2a5a9da45b5305b3bb9b05.tar.bz2
lp-gomoku-b89e003d7e1ed6997d2a5a9da45b5305b3bb9b05.zip
improve rows and cols scaning
-rw-r--r--GameField.cpp82
-rw-r--r--GameField.hpp6
2 files changed, 53 insertions, 35 deletions
diff --git a/GameField.cpp b/GameField.cpp
index 51c0347..acc742b 100644
--- a/GameField.cpp
+++ b/GameField.cpp
@@ -39,58 +39,76 @@ void GameField::Move(int y, int x)
who_move = -who_move; // change player
free_fields--;
- UpdateState();
+ UpdateState(y, x);
}
-void GameField::UpdateState()
+void GameField::UpdateState(int y, int x)
{
- int tmp;
-
if(!free_fields) {
state = G_DRAW;
return;
}
- tmp = ScanRows();
- if(tmp) {
- state = tmp;
+
+ if((state = ScanRowsAround(y, x)) != G_NONE)
return;
- }
- tmp = ScanCols();
- if(tmp) {
- state = tmp;
+ if((state = ScanColsAround(y, x)) != G_NONE)
return;
- }
- tmp = ScanDiags();
- if(tmp) {
- state = tmp;
+ if((state = ScanDiags()) != G_NONE)
return;
- }
}
-/* This version is a dummy */
-int GameField::ScanRows()
+int GameField::ScanRowsAround(int y, int x)
{
- int i;
- for(i = 0; i < 3; i++) {
- if(field[i][0] == field[i][1] && field[i][1] == field[i][2] &&
- field[i][1] != G_EMPTY)
- return field[i][1];
+ int i, n;
+ int player = field[y][x];
+ // ray left
+ for(i = x, n = 0; i >= 0; i--) {
+ if(player == field[y][i]) {
+ n++;
+ if(n == win_length)
+ return player;
+ }
+ else break;
}
- return 0;
+ // ray right
+ for(i = x; i < rows; i++) {
+ if(player == field[y][i]) {
+ n++;
+ if(n == win_length)
+ return player;
+ }
+ else break;
+ }
+
+ return G_NONE;
}
-/* This version is a dummy */
-int GameField::ScanCols()
+int GameField::ScanColsAround(int y, int x)
{
- int i;
- for(i = 0; i < 3; i++) {
- if(field[0][i] == field[1][i] && field[1][i] == field[2][i] &&
- field[1][i] != G_EMPTY)
- return field[1][i];
+ int i, n;
+ int player = field[y][x];
+ // ray up
+ for(i = y, n = 0; i >= 0; i--) {
+ if(player == field[i][x]) {
+ n++;
+ if(n == win_length)
+ return player;
+ }
+ else break;
}
- return 0;
+ // ray down
+ for(i = y; i < cols; i++) {
+ if(player == field[i][x]) {
+ n++;
+ if(n == win_length)
+ return player;
+ }
+ else break;
+ }
+
+ return G_NONE;
}
/* This version is a dummy */
diff --git a/GameField.hpp b/GameField.hpp
index d312644..9975caa 100644
--- a/GameField.hpp
+++ b/GameField.hpp
@@ -28,9 +28,9 @@ public:
void Move(int y, int x);
private:
- void UpdateState();
- int ScanRows();
- int ScanCols();
+ void UpdateState(int y, int x);
+ int ScanRowsAround(int y, int x);
+ int ScanColsAround(int y, int x);
int ScanDiags();
};