I've been using this code for my TicTacToe game but I want to make some changes: first I want to introduce the name of the players, and when one of them wins, it appears on the message. second, I want to introduce a ranking table of records, winning games and tied ones third I want the table to be static, doesn't change in every move. forth I want the table to have always the numbers in the square that each of them represent, and when someone chooses that position, it replaces the number for the pieace, X or O.
Now I'll post the code above of every file:
Player.h
#ifndef PLAYER_H
#define PLAYER_H
class Board;
class Player
{
public:
Player();
char GetPiece() const;
void MakeMove(Board& aBoard) const;
private:
static const int NUM_PIECES = 2;
static const char PIECES[NUM_PIECES];
static int current;
char m_Piece;
};
#endif
Player.cpp
#include "player.h"
#include "board.h"
#include <iostream>
using namespace std;
const char Player::PIECES[NUM_PIECES] = {'X', 'O'};
int Player::current = 0;
Player::Player()
{
m_Piece = PIECES[current];
current = (current + 1) % NUM_PIECES;
}
char Player::GetPiece() const
{
return m_Piece;
}
void Player::MakeMove(Board& aBoard) const
{
int move;
do
{
cout << "Player " << GetPiece();
cout << ", where would you like to move? (0-8): ";
cin >> move;
} while (!aBoard.IsLegalMove(move));
aBoard.ReceiveMove(GetPiece(), move);
}
Board.h
#ifndef BOARD_H
#define BOARD_H
class Board
{
public:
Board();
bool IsFull() const;
bool IsLegalMove(int move) const;
bool IsWinner(char piece) const;
void Display() const;
void Reset();
void ReceiveMove(char piece, int move);
static const int NUM_SQUARES = 9;
static const char EMPTY = ' ';
private:
static const int NUM_COMBOS = 8;
static const int NUM_IN_COMBO = 3;
static const int WINNING_COMBOS[NUM_COMBOS]
[NUM_IN_COMBO];
char m_Squares[NUM_SQUARES];
};
#endif
Board.cpp
#include "board.h"
#include <iostream>
using namespace std;
const int Board::WINNING_COMBOS[NUM_COMBOS]
[NUM_IN_COMBO] = { {0, 1, 2},{3, 4, 5},{6, 7, 8},{0, 3, 6},{1, 4, 7},{2, 5, 8},{0, 4, 8}, {2, 4, 6} };
Board::Board()
{
Reset();
}
bool Board::IsFull() const
{
bool full = true;
int i = 0;
while (full && i < NUM_SQUARES)
{
if (m_Squares[i] == EMPTY)
{
full = false;
}
++i;
}
return full;
}
bool Board::IsLegalMove(int move) const
{
return (move >= 0 && move < NUM_SQUARES && m_Squares[move] == EMPTY);
}
bool Board::IsWinner(char piece) const
{
bool winner = false;
int i = 0;
while (!winner && i < NUM_COMBOS)
{
int piecesInCombo = 0;
for (int j = 0; j < NUM_IN_COMBO; ++j)
{
if (m_Squares[WINNING_COMBOS[i][j]] == piece)
{
++piecesInCombo;
}
}
if (piecesInCombo == NUM_IN_COMBO)
{
winner = true;
}
++i;
}
return winner;
}
void Board::Display() const
{
cout << endl << "\t" << m_Squares[0] << " | " << m_Squares[1];
cout << " | " << m_Squares[2];
cout << endl << "\t" << "---------";
cout << endl << "\t" << m_Squares[3] << " | " << m_Squares[4];
cout << " | " << m_Squares[5];
cout << endl << "\t" << "---------";
cout << endl << "\t" << m_Squares[6] << " | " << m_Squares[7];
cout << " | " << m_Squares[8];
cout << endl << endl;
}
void Board::Reset()
{
for (int i=0; i<NUM_SQUARES; ++i)
{
m_Squares[i] = EMPTY;
}
}
void Board::ReceiveMove(char piece, int move)
{
m_Squares[move] = piece;
}
Game.h
#ifndef GAME_H
#define GAME_H
#include "board.h"
#include "player.h"
class Game
{
public:
Game();
bool IsPlaying() const;
bool IsTie() const;
void DisplayInstructions() const;
void NextPlayer();
void AnnounceWinner() const;
void Play();
private:
static const int NUM_PLAYERS = 2;
static const int FIRST = 0;
static const int SECOND = 1;
Board m_Board;
Player m_Players[NUM_PLAYERS];
int m_Current;
};
#endif
Game.cpp
#include "game.h"
#include <iostream>
using namespace std;
Game::Game():
m_Current(FIRST)
{}
bool Game::IsPlaying() const
{
return ( !m_Board.IsFull() &&!m_Board.IsWinner(m_Players[FIRST].GetPiece()) &&
!m_Board.IsWinner(m_Players[SECOND].GetPiece()) );
}
bool Game::IsTie() const
{
return ( m_Board.IsFull() &&!m_Board.IsWinner(m_Players[FIRST].GetPiece()) &&
!m_Board.IsWinner(m_Players[SECOND].GetPiece()) );
}
void Game::DisplayInstructions() const
{
cout << "\tWelcome to the ultimate intellectual
showdown: Tic-Tac-Toe.";
cout << endl << endl;
cout << "Make your move by entering a number,
0 - 8. The number" << endl;
cout << "corresponds with board position, as
illustrated:" << endl << endl;
cout << endl << "\t" << "0 | 1 | 2";
cout << endl << "\t" << "---------";
cout << endl << "\t" << "3 | 4 | 5";
cout << endl << "\t" << "---------";
cout << endl << "\t" << "6 | 7 | 8";
cout << endl << endl << "Prepare yourself. The
battle is about to begin.";
cout << endl << endl;
}
void Game::NextPlayer()
{
m_Current = (m_Current + 1) % NUM_PLAYERS;
}
void Game::AnnounceWinner() const
{
cout << "The raging battle has come to a fi nal end.";
cout << endl;
if (IsTie())
{
cout << "Sadly, no player emerged victorious.";
cout << endl;
}
else
{
cout << "The winner of this climatic ";
cout << "confrontation is Player ";
if (m_Board.IsWinner(m_Players[FIRST].
GetPiece()))
{
cout << m_Players[FIRST].GetPiece() << "!";
cout << endl;
}
else
{
cout << m_Players[SECOND].GetPiece() << "!";
cout << endl;
}
}
}
void Game::Play()
{
m_Current = FIRST;
m_Board.Reset();
while (IsPlaying())
{
m_Board.Display();
m_Players[m_Current].MakeMove(m_Board);
NextPlayer();
}
m_Board.Display();
AnnounceWinner();
}
main.cpp
#include "game.h"
#include <iostream>
using namespace std;
int main()
{
Game ticTacToe;
ticTacToe.DisplayInstructions();
char again;
do
{
ticTacToe.Play();
cout << endl << "Play again? (y/n): ";
cin >> again;
} while (again != 'n');
return 0;
}