带有coard.h board.cpp播放器.h player.cpp游戏.h game.cpp和main.cpp的c+

c++ tic tac toe with coard.h board.cpp player.h player.cpp game.h game.cpp and main.cpp I want to make some changes

本文关键字:cpp main game player coard board 播放器 带有 游戏      更新时间:2023-10-16

我一直在TicTacToe游戏中使用此代码,但我想做一些更改:首先,我想介绍一下球员的名字,当其中一人获胜时,就会出现在消息上。其次,我想介绍一个记录、获胜和平局的排名表第三,我希望桌子是静态的,不会每次移动都改变。第四,我希望表格中始终有每个数字所代表的正方形中的数字,当有人选择该位置时,它会替换拼接、X或O的数字。

现在我将在上面发布每个文件的代码:

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;
}

我不打算为您编写代码,但我现在有点无聊,所以我会对它进行一些结构化,以便您有一个想法。

  1. 你需要存储每个玩家的名字,并在游戏结束后以某种方式为获胜玩家检索。

  2. 你需要跟踪分数,在会话之间存储,这意味着文件i/o。

我不明白你用3或4想要什么。

Mi版本

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<<"tttt     presione cualquier tecla para continuar";
continuar = (char)getch();
system("cls");
char deNuevo;
do
{
    Triqui.jugar();
    cout << endl << "tttQuieres jugar otra vez? (s/n): ";
    cin >> deNuevo;
    system ("cls");
} while (deNuevo != 'n');
cout << endl;
cout << "tttt/////////////////////////////////////////////////////n";
cout << "tttt// Autor:                                          //n";
cout << "tttt//                                                 //n";
cout << "tttt// Camilo Andres Granda         Codigo: 201759710  //n";
cout << "tttt//                                                //n";
cout << "tttt/////////////////////////////////////////////////////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 << "ttt   
///////////////////////////////////////////////////////n";
cout << "ttt   //                     Bienvenidos                   
//n";
cout << "ttt   
///////////////////////////////////////////////////////n";
cout << endl << endl;
cout << "tttHaga su movimiento ingresando un numero, [1 - 9].  El numero" 
<< endl;
cout << "tttcorresponde con la posicion de la tabla, como se ilustra:" << 
endl << endl;
cout << endl << "tttttt" << "-------------";
cout << endl << "tttttt" << "| 1 | 2 | 3 |";
cout << endl << "tttttt" << "-------------";
cout << endl << "tttttt" << "| 4 | 5 | 6 |";
cout << endl << "tttttt" << "-------------";
cout << endl << "tttttt" << "| 7 | 8 | 9 |";
cout << endl << "tttttt" << "-------------";
cout << endl << endl << "ttttPreparate. 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 << "tttEmpate, ningun jugador gano.";
    cout << endl;
}
else
{
    cout << "tttEl 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 << "tttJugador " << getPieza() << ", Donde te gustaria hacer el movimiento? [1 - 9]: ";
    cin >> linea;
    if (unTablero.esNumerico(linea)) {
     move = atoi(linea.c_str());
  } else {
        cout << "ntttttError!, ingrese valor validonn";
     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 << "tttttt" << "-------------";
  cout << endl << "tttttt" << "| " << m_Cuadrados[1] << " | " << m_Cuadrados[2]; cout << " | " << m_Cuadrados[3]; cout << " | ";
  cout << endl << "tttttt" << "-------------";
  cout << endl << "tttttt" << "| " << m_Cuadrados[4] << " | " << m_Cuadrados[5]; cout << " | " << m_Cuadrados[6]; cout << " | ";
  cout << endl << "tttttt" << "-------------";
  cout << endl << "tttttt" << "| " << m_Cuadrados[7] << " | " << m_Cuadrados[8]; cout << " | " << m_Cuadrados[9]; cout << " | ";
  cout << endl << "tttttt" << "-------------";
  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;
 }