aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar alecdwm 2015-01-28 20:06:01 +1000
committerGravatar alecdwm 2015-01-28 20:09:43 +1000
commit300b8a6734f52bd25bfe542d8e4405e122e81b51 (patch)
tree008c7ec1d9c26efadfa1a0009148f66c9f0b34b1 /src
parent12d1ab840ea5fa1ec3fdda380f95ae79bdc60548 (diff)
Moved src to src folder
Added short readme description
Diffstat (limited to 'src')
-rw-r--r--src/Gameboard.cpp101
-rw-r--r--src/Gameboard.h17
-rw-r--r--src/Player.cpp68
-rw-r--r--src/Player.h19
-rw-r--r--src/main.cpp43
5 files changed, 248 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;
+ }
+}
diff --git a/src/Gameboard.h b/src/Gameboard.h
new file mode 100644
index 0000000..3be9c0e
--- /dev/null
+++ b/src/Gameboard.h
@@ -0,0 +1,17 @@
+#pragma once
+#include <string>
+#include <iostream>
+
+using namespace std;
+
+class Gameboard {
+public:
+ Gameboard();
+ void printGameboard();
+ bool playMove(char symbol,int y,int x);
+ bool checkWin(int playerID,char symbol);
+private:
+ char _defaultSymbol = '#';
+ char _gameboard[3][3];
+ string _winner;
+};
diff --git a/src/Player.cpp b/src/Player.cpp
new file mode 100644
index 0000000..48fb76c
--- /dev/null
+++ b/src/Player.cpp
@@ -0,0 +1,68 @@
+#include "Player.h"
+
+Player::Player(int playerID) {
+ _playerID = playerID;
+
+ cout << "Enter Player " << playerID << "'s name:" << endl;
+ cout << " > ";
+ cin >> _name;
+ cout << endl;
+
+ cout << "Enter " << _name << "'s symbol:" << endl;
+ cout << " > ";
+ cin >> _symbol;
+ cout << endl;
+}
+
+int Player::getPlayerID() {
+ return _playerID;
+}
+
+string Player::getName() {
+ return _name;
+}
+
+char Player::getSymbol() {
+ return _symbol;
+}
+
+int Player::getMoveX() {
+ int moveX = -1;
+ while(moveX == -1) {
+ cout << "Enter letter coordinate:" << endl;
+ cout << " > ";
+ string testMoveX;
+ cin >> testMoveX;
+ if(testMoveX == "A" || testMoveX == "a") {
+ moveX = 1;
+ } else if(testMoveX == "B" || testMoveX == "b") {
+ moveX = 2;
+ }
+ else if(testMoveX == "C" || testMoveX == "c") {
+ moveX = 3;
+ }
+ cout << endl;
+ }
+ return moveX;
+}
+
+int Player::getMoveY() {
+ int moveY = -1;
+ while(moveY == -1) {
+ cout << "Enter number coordinate:" << endl;
+ cout << " > ";
+ string testMoveY;
+ cin >> testMoveY;
+ if(testMoveY == "1") {
+ moveY = 1;
+ }
+ else if(testMoveY == "2") {
+ moveY = 2;
+ }
+ else if(testMoveY == "3") {
+ moveY = 3;
+ }
+ cout << endl;
+ }
+ return moveY;
+}
diff --git a/src/Player.h b/src/Player.h
new file mode 100644
index 0000000..73f1f29
--- /dev/null
+++ b/src/Player.h
@@ -0,0 +1,19 @@
+#pragma once
+#include <string>
+#include <iostream>
+
+using namespace std;
+
+class Player {
+public:
+ Player(int playerID);
+ int getPlayerID();
+ string getName();
+ char getSymbol();
+ int getMoveX();
+ int getMoveY();
+private:
+ int _playerID;
+ string _name;
+ char _symbol;
+};
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..526516a
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,43 @@
+#include <iostream>
+
+#include "Player.h"
+#include "Gameboard.h"
+
+using namespace std;
+
+int main()
+{
+ // Initialize game
+ cout << "Welcome to TicTacToe!" << endl << endl;
+
+ // Initialize gameboard
+ Gameboard gameboard;
+
+ // Initialize players
+ Player player1(1);
+ Player player2(2);
+
+ // Do gameloop
+ int endGame = 0;
+ while(!endGame) {
+ gameboard.printGameboard();
+ cout << player1.getName() << "'s turn:" << endl;
+ gameboard.playMove(player1.getSymbol(),player1.getMoveY(),player1.getMoveX());
+ endGame = gameboard.checkWin(player1.getPlayerID(),player1.getSymbol());
+ if(endGame){break;}
+ gameboard.printGameboard();
+ cout << player2.getName() << "'s turn:" << endl;
+ gameboard.playMove(player2.getSymbol(),player2.getMoveY(),player2.getMoveX());
+ endGame = gameboard.checkWin(player2.getPlayerID(),player2.getSymbol());
+ }
+
+ // Print winning player
+ if(endGame == player1.getPlayerID()) {
+ cout << player1.getName() << " Wins!" << endl;
+ }
+ if(endGame == player2.getPlayerID()) {
+ cout << player2.getName() << " Wins!" << endl;
+ }
+
+ return 0;
+}