aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2015-08-16 12:52:36 +1000
committerGravatar alecdwm 2015-08-16 12:52:36 +1000
commit94fd8c074fa9a52f45d67f30fa3996b28b64070a (patch)
tree3564c787cd832a608f2226395c1e1d1e2baf2c68
parent0f5571b2f7f6f99796b35e8e2ce838cba4edc93d (diff)
playerTurn logic into separate function && fix invalid move detection
-rw-r--r--src/main.cpp30
1 files changed, 22 insertions, 8 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 49765b3..7ba60a1 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -5,6 +5,24 @@
using namespace std;
+/*
+ * Logic for a player to make one complete turn
+ * @param gameboard [Gameboard] the gameboard this turn takes place on
+ * @param player [Player] the player who is making this turn
+ * @return [Boolean] this turn has ended the game
+ */
+int playerTurn(Gameboard& gameboard,Player& player) {
+ gameboard.printGameboard();
+ cout << player.getName() << "'s turn:" << endl;
+ bool playerMoved = gameboard.playMove(player.getSymbol(),player.getMoveY(),player.getMoveX());
+ while(playerMoved != true) {
+ cout << player.getName() << " made an invalid move! Try again." << endl;
+ playerMoved = gameboard.playMove(player.getSymbol(),player.getMoveY(),player.getMoveX());
+ }
+ int endGame = gameboard.checkWin(player.getPlayerID(),player.getSymbol());
+ return endGame;
+}
+
int main()
{
// Initialize game
@@ -20,15 +38,11 @@ int main()
// 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());
+ endGame = playerTurn(gameboard,player1);
+
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());
+
+ endGame = playerTurn(gameboard,player2);
}
// Print final gameboard