summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-02-17 17:45:28 +0000
committerJoursoir <chat@joursoir.net>2021-02-17 17:45:28 +0000
commit87d7320f84926c4d10734c773ecf3cf4eb47ddc4 (patch)
treeb840169cebb266e1ad7c99f3d3a850e832422517
downloadlp-gomoku-87d7320f84926c4d10734c773ecf3cf4eb47ddc4.tar.gz
lp-gomoku-87d7320f84926c4d10734c773ecf3cf4eb47ddc4.tar.bz2
lp-gomoku-87d7320f84926c4d10734c773ecf3cf4eb47ddc4.zip
init project: create game field
-rw-r--r--GameField.cpp81
-rw-r--r--GameField.hpp37
2 files changed, 118 insertions, 0 deletions
diff --git a/GameField.cpp b/GameField.cpp
new file mode 100644
index 0000000..381275e
--- /dev/null
+++ b/GameField.cpp
@@ -0,0 +1,81 @@
+#include "GameField.hpp"
+
+bool GameField::CanMove(int x, int y)
+{
+ if(field[y][x] != G_EMPTY)
+ return false;
+ return true;
+}
+
+void GameField::Move(int x, int y)
+{
+ field[y][x] = who_move;
+ who_move = -who_move; // change player
+ free--;
+
+ UpdateState();
+}
+
+void GameField::UpdateState()
+{
+ int tmp;
+
+ if(!free) {
+ state = G_DRAW;
+ return;
+ }
+ tmp = ScanRows();
+ if(tmp) {
+ state = tmp;
+ return;
+ }
+ tmp = ScanCols();
+ if(tmp) {
+ state = tmp;
+ return;
+ }
+ tmp = ScanDiags();
+ if(tmp) {
+ state = tmp;
+ return;
+ }
+}
+
+/* This version is a dummy */
+int GameField::ScanRows()
+{
+ 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];
+ }
+
+ return 0;
+}
+
+/* This version is a dummy */
+int GameField::ScanCols()
+{
+ 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];
+ }
+
+ return 0;
+}
+
+/* This version is a dummy */
+int GameField::ScanDiags()
+{
+ if(field[0][0] == field[1][1] && field[1][1] == field[2][2] &&
+ field[1][1] != G_EMPTY)
+ return field[1][1];
+ else if(field[0][2] == field[1][1] && field[1][1] == field[2][0] &&
+ field[1][1] != G_EMPTY)
+ return field[1][1];
+
+ return 0;
+}
diff --git a/GameField.hpp b/GameField.hpp
new file mode 100644
index 0000000..1d2af31
--- /dev/null
+++ b/GameField.hpp
@@ -0,0 +1,37 @@
+#ifndef LPG_GAMEFIELD_H
+#define LPG_GAMEFIELD_H
+
+enum states {
+ G_EMPTY = 0,
+ G_NONE,
+ G_DRAW,
+ G_XPLAYER = 10,
+ G_OPLAYER = -10
+};
+
+class GameField {
+ // optional:
+ int field[3][3];
+ int free;
+ int win_length;
+
+ // non-optional:
+ int state;
+ int who_move;
+public:
+ GameField() : field{0}, free(9), win_length(3), state(G_NONE),
+ who_move(G_XPLAYER) { }
+ ~GameField() { }
+ int GetState() { return state; }
+
+ bool CanMove(int x, int y);
+ void Move(int x, int y);
+
+private:
+ void UpdateState();
+ int ScanRows();
+ int ScanCols();
+ int ScanDiags();
+};
+
+#endif /* LPG_GAMEFIELD_H */