Ok, I first wrote my tictactoe game as one class, and got it to work. I need to split the code up into two classes. Since splitting them up I can't get class tictactoe to read my makeMove or checkWin functions in my board class.
#ifndef Board_HPP
#define Board_HPP
class Board
{
private:
//This stores the board state
char board[3][3];
public:
Board();
// Prints the board
staticvoid print();
//Places a marker. If it returns false, it couldnt place!
bool makeMove(int x, int y, char currentPlayer);
//Returns true if the current player won!
bool checkWin(char currentPlayer);
//Gets input from the user
int getXCoord();
int getYCoord();
};
#endif
#include "TicTacToe.hpp"
#include <iostream>
usingnamespace std;
void TicTacToe::play()
{
char player1 = 'X';
char player2 = 'O';
char currentPlayer = player1;
bool isDone = false;
int x, y;
int turn = 0;
//Inner game loop
while (isDone == false) {
//Print out the board each time we loop
Board::print();
//Get the coordinates from Board getXCoord and
//GetYCoord for where the user wants to go
Board::getXCoord() = x;
Board::getYCoord() = y;
//Try to place a marker
if (Board::makeMove(x, y, currentPlayer) == false) {
//If we failed to place a marker, tell him he failed!
cout << "That spot is occupied!\n";
}
else {
//Otherwise, we successfully did this turn!
turn++;
//See if the player won!
if (Board::checkWin(currentPlayer) == true) {
//He won!
cout << "The game is over! Player " << currentPlayer << " has won!\n";
isDone = true;
}
elseif (turn == 9) {
//Tie game!
cout << "Its a tie game!\n";
isDone = true;
}
// Switch players
if (currentPlayer == player1) {
currentPlayer = player2;
}
else {
currentPlayer = player1;
}
}
}
}
int main()
{
char input;
bool isDone = false;
//here is our game object
TicTacToe game;
//Outer Game Loop
while (isDone == false) {
//this plays a game of tic tac toe!
game.play();
// We have to see if they want to play again!
cout << "Would you like to play again? (Y/N): ";
cin >> input;
if (input == 'N' || input == 'n') {
isDone = true;
}
}
return 0;
}