【发布时间】:2013-11-28 17:33:14
【问题描述】:
我正在做一个用 C++ 设计游戏的项目,当我尝试从玩家那里获得动作时,我的程序不断崩溃。该程序允许用户为这两个玩家中的每一个选择是计算机玩家还是人类玩家。如果是人类玩家,它会收集玩家的名字。
程序启动时,我的主类创建一个游戏对象,运行 selectPlayers() 函数,然后运行 play() 函数。程序正在加载,向我询问每个玩家的人或计算机,收集人名并显示棋盘(显示在 play() 函数中,然后崩溃并弹出一个带有消息的窗口
程序停止工作,windows 正在寻找解决方案
在下面的代码中添加了注释以显示问题所在。如果我在该行上方放置一个 cout 它会打印,但在该行之后没有任何内容打印...如果我在 HumanPlayer 类的 makeMove 方法的第一行放置一个 cout,它不会被打印,所以程序在进入方法之前崩溃。
这是我的游戏类的标题:
#include "Board.h"
#include "Player.h"
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
class Game
{
Board b;
int turn;
bool winner;
Player* player1;
Player* player2;
public:
Game();
~Game();
void selectPlayers();
Player* nextPlayer() const;
void play();
void announceWinner();
};
#endif // GAME_H_INCLUDED
还有课程本身:
#include "Game.h"
#include "HumanPlayer.h"
#include "RandomPlayer.h"
#include <iostream>
Game::Game()
{
b.reset();
turn = 1;
winner = false;
}
Game::~Game()
{
}
void Game::selectPlayers()
{
int x = 0;
std::string type;
std::string name;
std::cout << "Enter type for Player 1 (Human/Computer): ";
std::cin >> type;
while(x == 0)
{
if(type.compare("Human") == 0)
{
x = 1;
std::cout << "Enter name for Player 1: ";
std::cin >> name;
HumanPlayer p(name, LIGHT);
HumanPlayer * player1 = &p;
}
else if(type.compare("Computer") == 0)
{
x = 1;
RandomPlayer p(1, LIGHT);
RandomPlayer * player1 = &p;
}
else
{
std::cout << "Please enter Human or Computer for Player 1: ";
std::cin >> type;
}
}
std::cout << "Enter type for Player 2 (Human/Computer): ";
std::cin >> type;
x = 0;
while(x == 0)
{
if(type.compare("Human") == 0)
{
x = 1;
std::cout << "Enter name for Player 2: ";
std::cin >> name;
HumanPlayer p(name, DARK);
HumanPlayer * player2 = &p;
}
else if(type.compare("Computer") == 0)
{
x = 1;
RandomPlayer p(2, DARK);
RandomPlayer * player2 = &p;
}
else
{
std::cout << "Please enter Human or Computer for Player 2: ";
std::cin >> type;
}
}
}
Player* Game::nextPlayer() const
{
}
void Game::play()
{
while(winner == false)
{
b.display();
if(turn%2 == 1)
{
player1->makeMove(b); //PROGRAM CRASHES HERE
++turn;
}
else
{
player2->makeMove(b);
++turn;
}
}
}
void Game::announceWinner()
{
}
任何帮助都会很棒,谢谢大家。
【问题讨论】: