-2

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;
}
hyde
  • 60,639
  • 21
  • 115
  • 176
Deiota
  • 5
  • 2
  • 3
    What is your question? Which part of the desired changes is unclear to you? – Hulk Nov 18 '13 at 12:40
  • 1
    That's a bit much for us to look at. What precisely is your question: which part are you having trouble with? StackOverflow works best for precise, simple answer questions rather than general feedback and guidance. – Rup Nov 18 '13 at 12:40
  • the questions are above,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.Any help would be appreciated – Deiota Nov 18 '13 at 12:44
  • my main problem is that the board is changing each time a player is about to make a move and the board is empty, onlu with the moves already done, without the numbers on it, to players see the keys they have to press... – Deiota Nov 18 '13 at 12:48
  • 2
    It is a bit unclear if you have any clue of what you are doing, or if you merely have some copied code, which you are trying to change without understanding what it actually does and how it works... – hyde Nov 18 '13 at 12:58
  • thats what I want to avoid.. – Deiota Nov 18 '13 at 17:11
  • can you explain me a bit? – Deiota Nov 18 '13 at 22:02

2 Answers2

0

I'm not going to write code for you, but I'm slightly bored at the moment so I will structure it somewhat so you have an idea.

  1. You need to store the name per player, and some way to retrieve it for the winning playerwhen the game is finished.

  2. You need to keep track of scores, store between sessions which means file i/o.

I don't understand what you want with 3 or 4.

dutt
  • 7,909
  • 11
  • 52
  • 85
  • the forth is that the table is not static.on player plays a move and marks on a table the play and then a new table appears, iqual as the one before for the next player makes a move. i want a static table. – Deiota Nov 18 '13 at 13:18
0

Mi version en español y ademas lo mejoré un poquito

main.cpp

#include "Arbitro.h"
#include <iostream>
#include <stdlib.h>
#include <conio.h>

using namespace std;

int main()
{

system("color 09");

Arbitro Triqui;

Triqui.mostrarInstrucciones();
string continuar;

cout<<"\t\t\t\t     presione cualquier tecla para continuar";

continuar = (char)getch();
system("cls");

char deNuevo;
do
{
    Triqui.jugar();
    cout << endl << "\t\t\tQuieres jugar otra vez? (s/n): ";
    cin >> deNuevo;

    system ("cls");

} while (deNuevo != 'n');

cout << endl;
cout << "\t\t\t\t/////////////////////////////////////////////////////\n";
cout << "\t\t\t\t// Autor:                                          //\n";
cout << "\t\t\t\t//                                                 //\n";
cout << "\t\t\t\t// Camilo Andres Granda         Codigo: 201759710  //\n";
cout << "\t\t\t\t//                                                //\n";
cout << "\t\t\t\t/////////////////////////////////////////////////////\n";
cout << endl;
return 0;
 }

Arbitro.h

#ifndef ARBITRO_H
#define ARBITRO_H

#include "Tablero.h"
#include "Jugador.h"

class Arbitro
{
public:
Arbitro();
bool enJuego() const;
bool empate() const;
void mostrarInstrucciones() const;
void siguienteJugador();
void anunciarGanador() const;
void jugar();

private:
static const int numero_de_jugadores = 2;
static const int primero = 0;
static const int segundo = 1;

Tablero m_Tablero;
Jugador m_Jugadores[numero_de_jugadores];
int m_Actual;
};

#endif

Arbitro.cpp

#include "Arbitro.h"

#include <iostream>
using namespace std;

Arbitro::Arbitro():
m_Actual(primero)
{}

bool Arbitro::enJuego() const
{
 return ( !m_Tablero.estaLleno() 
&&!m_Tablero.ganador(m_Jugadores[primero].getPieza()) &&
    !m_Tablero.ganador(m_Jugadores[segundo].getPieza()) );
}

bool Arbitro::empate() const
{
return ( m_Tablero.estaLleno() 
&&!m_Tablero.ganador(m_Jugadores[primero].getPieza()) &&
       !m_Tablero.ganador(m_Jugadores[segundo].getPieza()) );
 }

void Arbitro::mostrarInstrucciones() const
{
cout << "\t\t\t   
///////////////////////////////////////////////////////\n";
cout << "\t\t\t   //                     Bienvenidos                   
//\n";
cout << "\t\t\t   
///////////////////////////////////////////////////////\n";
cout << endl << endl;
cout << "\t\t\tHaga su movimiento ingresando un numero, [1 - 9].  El numero" 
<< endl;
cout << "\t\t\tcorresponde con la posicion de la tabla, como se ilustra:" << 
endl << endl;
cout << endl << "\t\t\t\t\t\t" << "-------------";
cout << endl << "\t\t\t\t\t\t" << "| 1 | 2 | 3 |";
cout << endl << "\t\t\t\t\t\t" << "-------------";
cout << endl << "\t\t\t\t\t\t" << "| 4 | 5 | 6 |";
cout << endl << "\t\t\t\t\t\t" << "-------------";
cout << endl << "\t\t\t\t\t\t" << "| 7 | 8 | 9 |";
cout << endl << "\t\t\t\t\t\t" << "-------------";
cout << endl << endl << "\t\t\t\tPreparate. La partida esta a punto de 
comenzar.";
cout << endl << endl;
}

void Arbitro::siguienteJugador()
 {
 m_Actual = (m_Actual + 1) % numero_de_jugadores;
}

void Arbitro::anunciarGanador() const
{
if (empate())
{
    cout << "\t\t\tEmpate, ningun jugador gano.";
    cout << endl;
}

else
{
    cout << "\t\t\tEl ganador de esta partida es el jugador ";
    if (m_Tablero.ganador(m_Jugadores[primero].
    getPieza()))
    {
        cout << m_Jugadores[primero].getPieza() << "!";
        cout << endl;
    }
    else
    {
        cout << m_Jugadores[segundo].getPieza() << "!";
        cout << endl;
    }
  }
  }

void Arbitro::jugar()
 {
m_Actual = primero;
m_Tablero.reiniciarTablero();

while (enJuego())
{
    m_Tablero.mostrarTablero();
    m_Jugadores[m_Actual].hacerMovimiento(m_Tablero);
     siguienteJugador();
   }
m_Tablero.mostrarTablero();
 anunciarGanador();
 }

Jugador.h

#ifndef JUGADOR_H
#define JUGADOR_H

#include <iostream>
#include <string> // string, stoi
#include <cctype> // isdigit
#include <cstdlib> // atoi
class Tablero;

class Jugador
{
public:
Jugador();
char getPieza() const;
void hacerMovimiento(Tablero& unTablero) const;

private:
static const int numero_de_fichas = 2;
static const char fichas[numero_de_fichas];
static int actual;
char m_Ficha;
};

#endif

Jugador.cpp

#include "Jugador.h"
 #include "Tablero.h"

 #include <iostream>
using namespace std;

 const char Jugador::fichas[numero_de_fichas] = {'N', 'B'};
int Jugador::actual = 0;

Jugador::Jugador()
  {
m_Ficha = fichas[actual];
actual = (actual + 1) % numero_de_fichas;
}

char Jugador::getPieza() const
{
 return m_Ficha;
}

   void Jugador::hacerMovimiento(Tablero& unTablero) const
   {
   string linea;
   int move;

  do
   {
    cout << "\t\t\tJugador " << getPieza() << ", Donde te gustaria hacer el movimiento? [1 - 9]: ";
    cin >> linea;
    if (unTablero.esNumerico(linea)) {
     move = atoi(linea.c_str());
  } else {
        cout << "\n\t\t\t\t\tError!, ingrese valor valido\n\n";
     move=0;
  }
} while (!unTablero.movimientoValido(move));

unTablero.recibirMovimiento(getPieza(), move);
}

Tablero.h

#ifndef TABLERO_H
#define TABLERO_H
#include <iostream>
#include <string> // string, stoi
using namespace std;

class Tablero
{
public:
Tablero();
bool esNumerico(string linea);
bool estaLleno() const;
bool movimientoValido(int move) const;
bool ganador(char ficha) const;
void mostrarTablero() const;
void reiniciarTablero();
void recibirMovimiento(char pieza, int mover);
static const int numero_de_cuadros = 10;
static const char vacio = ' ';

private:
static const int numero_de_combos = 8;
static const int numero_en_combo = 3;
string linea;
static const int combos_ganadores[numero_de_combos] [numero_en_combo];
char m_Cuadrados[numero_de_cuadros];
};
#endif

Tablero.cpp

  #include "Tablero.h"
   #include <iostream>
   using namespace std;

  const int Tablero::combos_ganadores[numero_de_combos]
  [numero_en_combo] = { {1, 2, 3},{4, 5, 6},{7, 8, 9},{1, 4, 7},{2, 5, 8},{3, 6, 9},{1, 5, 9}, {3, 5, 7} };

     Tablero::Tablero()
     {
     reiniciarTablero();
     }
     bool Tablero::estaLleno() const
     {
     bool lleno = true;
     int i = 1;
    while (lleno && i < numero_de_cuadros)
     {
      if (m_Cuadrados[i] == vacio)
      {
        lleno = false;
      }
    ++i;
    }
    return lleno;
    }

   bool Tablero::movimientoValido(int mover) const
   {
  return (mover >= 1 && mover < numero_de_cuadros && m_Cuadrados[mover] == 
  vacio);
  }

  bool Tablero::ganador(char ficha) const
   {
  bool ganador = false;
  int i = 0;
  while (!ganador && i < numero_de_combos)
  {
    int fichasEnCombo = 0;
    for (int j = 0; j < numero_en_combo; ++j)
    {
        if (m_Cuadrados[combos_ganadores[i][j]] == ficha)
        {
            ++fichasEnCombo;
        }
    }
        if (fichasEnCombo == numero_en_combo)
    {
        ganador = true;
    }
    ++i;
  }

  return ganador;
 }

  void Tablero::mostrarTablero() const
  {
  cout << endl << "\t\t\t\t\t\t" << "-------------";
  cout << endl << "\t\t\t\t\t\t" << "| " << m_Cuadrados[1] << " | " << m_Cuadrados[2]; cout << " | " << m_Cuadrados[3]; cout << " | ";
  cout << endl << "\t\t\t\t\t\t" << "-------------";
  cout << endl << "\t\t\t\t\t\t" << "| " << m_Cuadrados[4] << " | " << m_Cuadrados[5]; cout << " | " << m_Cuadrados[6]; cout << " | ";
  cout << endl << "\t\t\t\t\t\t" << "-------------";
  cout << endl << "\t\t\t\t\t\t" << "| " << m_Cuadrados[7] << " | " << m_Cuadrados[8]; cout << " | " << m_Cuadrados[9]; cout << " | ";
  cout << endl << "\t\t\t\t\t\t" << "-------------";
  cout << endl << endl;
}

  void Tablero::reiniciarTablero()
  {
  for (int i=0; i<numero_de_cuadros; ++i)
  {
    m_Cuadrados[i] = vacio;
  }
  }

 void Tablero::recibirMovimiento(char pieza, int mover)
 {
  m_Cuadrados[mover] = pieza;
  }

  bool Tablero::esNumerico(string linea)
  {
   bool b = true;
  int longitud = linea.size();

  if (longitud == 0) { // Cuando el usuario pulsa ENTER
   b = false;
  } else if (longitud == 1 && !isdigit(linea[0])) {
  b = false;
  } else {
  int i;
  if (linea[0] == '+' || linea[0] == '-')
     i = 1;
  else
     i = 0;

  while (i < longitud) {
     if (!isdigit(linea[i])) {
        b = false;
        break;
     }
     i++;
   }
  }

   return b;
 }