aboutsummaryrefslogtreecommitdiffstats
path: root/src/Gameboard.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Gameboard.cpp')
-rw-r--r--src/Gameboard.cpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/Gameboard.cpp b/src/Gameboard.cpp
new file mode 100644
index 0000000..387e68d
--- /dev/null
+++ b/src/Gameboard.cpp
@@ -0,0 +1,101 @@
+#include "Gameboard.h"
+
+Gameboard::Gameboard() {
+ for(int y=0;y<3;y++) {
+ for(int x=0;x<3;x++) {
+ _gameboard[y][x] = _defaultSymbol;
+ }
+ }
+}
+
+void Gameboard::printGameboard() {
+ cout << "Game State:" << endl;
+ cout << " ABC" << endl;
+ for(int y=0;y<3;y++) {
+ cout << y+1;
+ for(int x=0;x<3;x++) {
+ cout << _gameboard[y][x];
+ }
+ cout << endl;
+ }
+}
+
+bool Gameboard::playMove(char symbol,int y,int x) {
+ if(_gameboard[y-1][x-1] == _defaultSymbol) {
+ _gameboard[y-1][x-1] = symbol;
+ return true;
+ }
+ else {
+ return false;
+ }
+}
+
+bool Gameboard::checkWin(int playerID,char symbol) {
+ string horizontalTop,horizontalMid,horizontalBottom,
+ verticalLeft,verticalMid,verticalRight,
+ diagonalTopLeft,diagonalTopRight,symbolRow = "";
+
+ symbolRow += symbol;
+ symbolRow += symbol;
+ symbolRow += symbol;
+
+ horizontalTop += _gameboard[0][0];
+ horizontalTop += _gameboard[0][1];
+ horizontalTop += _gameboard[0][2];
+
+ horizontalMid += _gameboard[1][0];
+ horizontalMid += _gameboard[1][1];
+ horizontalMid += _gameboard[1][2];
+
+ horizontalBottom += _gameboard[2][0];
+ horizontalBottom += _gameboard[2][1];
+ horizontalBottom += _gameboard[2][2];
+
+ verticalLeft += _gameboard[0][0];
+ verticalLeft += _gameboard[1][0];
+ verticalLeft += _gameboard[2][0];
+
+ verticalMid += _gameboard[0][1];
+ verticalMid += _gameboard[1][1];
+ verticalMid += _gameboard[2][1];
+
+ verticalRight += _gameboard[0][2];
+ verticalRight += _gameboard[1][2];
+ verticalRight += _gameboard[2][2];
+
+ diagonalTopLeft += _gameboard[0][0];
+ diagonalTopLeft += _gameboard[1][1];
+ diagonalTopLeft += _gameboard[2][2];
+
+ diagonalTopRight += _gameboard[0][2];
+ diagonalTopRight += _gameboard[1][1];
+ diagonalTopRight += _gameboard[2][0];
+
+ if(horizontalTop == symbolRow) {
+ return playerID;
+ }
+ else if(horizontalMid == symbolRow) {
+ return playerID;
+ }
+ else if(horizontalBottom == symbolRow) {
+ return playerID;
+ }
+ else if(verticalLeft == symbolRow) {
+ return playerID;
+ }
+ else if(verticalMid == symbolRow) {
+ return playerID;
+ }
+ else if(verticalRight == symbolRow) {
+ return playerID;
+ }
+ else if(diagonalTopLeft == symbolRow) {
+ return playerID;
+ }
+ else if(diagonalTopRight == symbolRow) {
+ return playerID;
+ }
+ else {
+ return 0;
+ }
+}