Hi everyone,
I have a few issues with my new program, which is making the tic tac toe game from Jumping Into C++. There is another thread about this (we are obviously using the same book, at the same time!). This is causing slight issues in me not wanting to view the other thread in case it gives away the answers. So, I am working at this and my problems follow the code:
What works:Code:// Tic tac toe
// Created by Sam on 21/04/2013.
#include <iostream>
//Top right (TR), Centre left (CL), Bottom Middle (BM) etc...
void printGameBoard(char TL, char TM, char TR, char CL, char CM, char CR, char BL, char BM, char BR);
void facilitateMove(int squareChoice);
//initialize values for starting board
char TL('1');
char TM('2');
char TR('3');
char CL('4');
char CM('5');
char CR('6');
char BL('7');
char BM('8');
char BR('9');
//variable to hold the 'X' for player 1, and 'O' for player 2
char playerMarker;
int main()
{
enum SquareStates {SS_O, SS_X, SS_HAS_NUMBER}; //3 states - has a 'O', has a 'X', has a number '1', '2' etc...
SquareStates currentValue; //current value of the square on the board, SS_O, SS_X or SS_HAS_NUMBER
//the board squares already have a value 1, 2, 3, 4... so how to use SS_HAS_NUMBER?
//variable to switch whos turn it is (starts with player 1 == true)
int playerTurn = 1;
//after game is finished, a choice for another go
bool anotherGo = 1;
//saves the number a user has inputted to compare with enums (??) for allowed/illegal move
char squareChoice;
do
{
//set markers for each player
if (playerTurn == 1)
{
playerMarker = 'X';
}
else
{
playerMarker = 'O';
}
//prints game board with updated variables depending on player move
printGameBoard(TL, TM, TR, CL, CM, CR, BL, BM, BR);
std::cout << "Player " << playerTurn << ", your go: ";
std::cin >> squareChoice;
//CHECK MOVE IS LEGAL
/* I planned to use like this, presuming player 1 is using an 'X':
if(player1 == 1 && currentValue == SS_X)
{
illegal move
}
if(player2 == 1 && currentValue == SS_O)
{
illegal move
}
*/
//facilitate move
facilitateMove(squareChoice);
//switches between player 1 and player 2 turn after each move
if (playerTurn == 1)
{
playerTurn = 2;
}
else
{
playerTurn = 1;
}
} while (anotherGo == 1);
}
void printGameBoard(char TL, char TM, char TR, char CL, char CM, char CR, char BL, char BM, char BR)
{
std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
std::cout << "|" << TL << "|" << TM << "|" << TR << "|" << std::endl;
std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
std::cout << "|" << CL << "|" << CM << "|" << CR << "|" << std::endl;
std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
std::cout << "|" << BL << "|" << BM << "|" << BR << "|" << std::endl;
std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
}
void facilitateMove(int squareChoice)
{
switch(squareChoice)
{
case '1':
TL = playerMarker;
break;
case '2':
TM = playerMarker;
break;
case '3':
TR = playerMarker;
break;
case '4':
CL = playerMarker;
break;
case '5':
CM = playerMarker;
break;
case '6':
CR = playerMarker;
break;
case '7':
BL = playerMarker;
break;
case '8':
BM = playerMarker;
break;
case '9':
BR = playerMarker;
break;
}
}
1) Switching players
2) Using correct marker for each player (X, O etc..)
Problems:
1) I am using global variables, is there a way around this other than dumping it all in main, or is it suitable for this program?
2) How do I get my enums involved? It hasn't yet clicked with me on how to actually use them. There are 3 states to each square on the board, a number, a 'X' or a 'O'.
I have a few ideas which I have commented in but the lightbulb is most certainly out right now! Once I understand how to use the enums, I think it will be a lot easier getting the logic for:
a) when a move is illegal
c) when someone has won
Just a bit stuck. Any help is very appreciated, thanks.