Need help with a game loop

Having problems solving a crash when trying to re-enter a game.

Page 1 of 1

1 Replies - 1405 Views - Last Post: 09 February 2010 - 04:14 AM Rate Topic: -----

#1 arjuna1356  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-November 09

Need help with a game loop

Posted 09 February 2010 - 03:43 AM

const int STATE_INTRO = 0;
const int STATE_PLAYERTURN = 1;
const int STATE_FLEETTURN = 2;
const int STATE_WIN = 3;
const int STATE_LOSE = 4;
Starship* pShip = 0;
GameTurn* opponent = 0;
GameTurn* player = 0;
Starship* opponentSelectedShip = 0;
Starship* playerSelectedShip = 0;

SolarisPrimus::SolarisPrimus()
{
	m_gameState = STATE_INTRO;

	//Note: Only to be used for checking
	//m_gameState = STATE_WIN;
	//m_gameState = STATE_LOSE;
}
//Initialising the game
void SolarisPrimus::Init()
{
	opponent = new GameTurn();
	player = new GameTurn();

	srand((unsigned)time(0));

	CreateAndDistributeShips(opponent);
	CreateAndDistributeShips(player);
}
//Running the Game
void SolarisPrimus::Run()
{
	while(Gameloop());
}
//GameLoop called by Run()
bool SolarisPrimus::Gameloop()
{
	// Switch handles the game states
	switch (m_gameState)
	{
	case STATE_INTRO:
		return StateIntro();
	case STATE_PLAYERTURN:
		return StatePlayerTurn();
	case STATE_FLEETTURN:
		return StateFleetTurn();
	case STATE_WIN:
		return StateWin();
	case STATE_LOSE:
		return StateLose();
	}
	return false;
}
//Shutting the game down
void SolarisPrimus::Shutdown()
{
	delete pShip;
	delete opponent;
	delete player;
	delete opponentSelectedShip;
	delete playerSelectedShip;

	pShip = 0;
	opponent = 0;
	player = 0;
	opponentSelectedShip = 0;
	playerSelectedShip = 0;
}
//Introduction Step of GameLoop()
bool SolarisPrimus::StateIntro()
{
	system("cls");

	cout << "--------------------------------------------------------------------------------";
	cout << "|                          Welcome to Solaris Primus                           |";
	cout << "--------------------------------------------------------------------------------" << endl;
	
	//Options available to the player
	cout << "1. New Game" << endl;
	cout << "2. Introduction / Instructions" << endl;
	cout << "3. Exit Game" << endl << endl;

	string optionselect;
	int optionselectNum;

	//Player selects an option
	do
	{
		cout << "Please choose an option (1 - 3): ";
		cin >> optionselect;
		stringstream(optionselect) >> optionselectNum;
	}while(optionselectNum < 1 || optionselectNum > 3);

	if(optionselectNum == 1)
	{
		int m_whoStartFirst = ((rand() % 2) + 1);
		if(m_whoStartFirst == 1)
		{
			m_gameState = STATE_PLAYERTURN;
		}
		else
		{
			m_gameState = STATE_FLEETTURN;
		}
	}
	else if(optionselectNum == 2)
	{
		Introduction();
	}
	else if(optionselectNum == 3)
	{
		Shutdown();
		return false;
	}
	return true;
}
//Player's Turn Function
bool SolarisPrimus::StatePlayerTurn()
{
	system("cls");

	cout << "--------------------------------------------------------------------------------";
	cout << "|                                   Your turn                                  |";
	cout << "--------------------------------------------------------------------------------" << endl;

	//Showing the player the ships the computer has
	cout << "\nComputer's Ships:" << endl << endl;
	opponent->printAll();

	//Computer selects a ship
	opponentSelectedShip = ComputerSelectShip();
	cout << endl;
	cout << "--------------------------------------------------------------------------------";

	//Player selects a ship
	playerSelectedShip = PlayerSelectShip();
	cout << endl;

	//Player's ship attack the enemy ship
	Fight(playerSelectedShip, opponentSelectedShip);

	//Checking to see if the opponent ship is destroyed. If it is, transfer the ship over to the player's side
	bool opponentShipDie = opponent->CheckShipDie(player, opponent);
	if(opponentShipDie == true)
	{
		cout << opponentSelectedShip->getShipName() << " is destroyed! It is added to your fleet!" << endl << endl;
	}

	//Checking to see if the game should end
	if(opponent->getFleetSize() == 0)
	{
		cout << "You have destroyed the enemy fleet!" << endl << endl;;
		cout << "Press enter to continue...";
		string b;
		getline(cin, b);
		m_gameState = STATE_WIN;
		return true;
	}

	string r;
	getline(cin, r);

	//Step to be carried out if the enemy ship is not destroyed
	if(opponentShipDie == false)
	{
		cout << "--------------------------------------------------------------------------------";
		cout << "Computer's turn to attack!" << endl << endl;

		Fight(opponentSelectedShip, playerSelectedShip);

		bool playerShipDie = player->CheckShipDie(opponent, player);
		if(playerShipDie == true)
		{
			cout << playerSelectedShip->getShipName() << " is destroyed! It is added to your opponent's fleet!" << endl << endl;
		}
	}

	if(player->getFleetSize() == 0)
		{
			cout << "Your fleet has been destroyed!" << endl << endl;
			cout << "Press enter to continue...";
			string h;
			getline(cin, h);
			m_gameState = STATE_LOSE;
			return true;
		}

	//Pause the game here so player can see progress
	cout << "Press enter to continue...";
	string m;
	getline(cin, m);

	//Going to computer's turn
	m_gameState = STATE_FLEETTURN;
	return true;
}
//Computer's Turn Function
bool SolarisPrimus::StateFleetTurn()
{
	system("cls");

	cout << "--------------------------------------------------------------------------------";
	cout << "|                                Computer's turn                               |";
	cout << "--------------------------------------------------------------------------------" << endl;

	//Showing the player the ships the computer has
	cout << "\nComputer's Ships:" << endl << endl;
	opponent->printAll();
	cout << "--------------------------------------------------------------------------------";

	//Player selects a ship first
	playerSelectedShip = PlayerSelectShip();
	cout << endl;

	//Computer selects a ship
	opponentSelectedShip = ComputerSelectShip();
	cout << endl;

	//Conflict resolvation between the two ships
	Fight(opponentSelectedShip, playerSelectedShip);

	//Checking to see if the player's ship is destroyed. If it is, transfer the ship to the computer's side
	bool playerShipDie = player->CheckShipDie(opponent, player);
	if(playerShipDie == true)
	{
		cout << playerSelectedShip->getShipName() << " is destroyed! It is added to your opponent's fleet!" << endl << endl;
	}

	//Checking to see if the game should end
	if(player->getFleetSize() == 0)
	{
		cout << "Your fleet has been destroyed!" << endl << endl;
		cout << "Press enter to continue...";
		string q;
		getline(cin, q);
		m_gameState = STATE_LOSE;
		return true;
	}

	string q;
	getline(cin, q);

	//Step to be carried out if the player's ship is not destroyed
	if(playerShipDie == false)
	{
		cout << "--------------------------------------------------------------------------------";
		cout << "Your turn to attack!" << endl << endl;

		Fight(playerSelectedShip, opponentSelectedShip);

		bool opponentShipDie = opponent->CheckShipDie(player, opponent);
		if(opponentShipDie == true)
		{
			cout << opponentSelectedShip->getShipName() << " is destroyed! It is added to your fleet!" << endl << endl;
		}
	}

	if(opponent->getFleetSize() == 0)
		{
			cout << "You have destroyed the enemy fleet!" << endl << endl;
			cout << "Press enter to continue...";
			string p;
			getline(cin, p);
			m_gameState = STATE_WIN;
			return true;
		}

	//Pause the game so the player can see progress
	cout << "Press enter to continue...";
	string t;
	getline(cin, t);

	//Going to player's turn
	m_gameState = STATE_PLAYERTURN;
	return true;
}
//If the player wins
bool SolarisPrimus::StateWin()
{
	system("cls");

	cout << "--------------------------------------------------------------------------------";
	cout << "|                         Congratulations! You Won!!!                          |";
	cout << "--------------------------------------------------------------------------------" << endl;

	cout << "What would you like to do now?" << endl << endl;

	cout << "1. Start a new game" << endl;
	cout << "2. Go back to the main menu" << endl;
	cout << "3. Exit Game" << endl << endl;

	string optionselect;
	int optionselectNum;

	do
	{
		cout << "Please choose an option (1 - 3): ";
		cin >> optionselect;
		stringstream(optionselect) >> optionselectNum;
	}while(optionselectNum < 1 || optionselectNum > 3);

	if(optionselectNum == 1)
	{
		Shutdown();
		Init();

		int m_whoStartFirst = ((rand() % 2) + 1);
		if(m_whoStartFirst == 1)
		{
			m_gameState = STATE_PLAYERTURN;
		}
		else
		{
			m_gameState = STATE_FLEETTURN;
		}

	}
	else if(optionselectNum == 2)
	{
		Shutdown();
		Init();
		m_gameState = STATE_INTRO;
	}
	else if(optionselectNum == 3)
	{
		Shutdown();
		return false;
	}
	return false;
}
//If the player loses
bool SolarisPrimus::StateLose()
{
	system("cls");

	cout << "--------------------------------------------------------------------------------";
	cout << "|                        Sorry, You Lost... Try Again!                          |";
	cout << "--------------------------------------------------------------------------------" << endl;

	cout << "What would you like to do now?" << endl << endl;

	cout << "1. Start a new game" << endl;
	cout << "2. Go back to the main menu" << endl;
	cout << "3. Exit Game" << endl << endl;

	string optionselect;
	int optionselectNum;

	do
	{
		cout << "Please choose an option (1 - 3): ";
		cin >> optionselect;
		stringstream(optionselect) >> optionselectNum;
	}while(optionselectNum < 1 || optionselectNum > 3);

	if(optionselectNum == 1)
	{
		Shutdown();
		Init();

		int m_whoStartFirst = ((rand() % 2) + 1);
		if(m_whoStartFirst == 1)
		{
			m_gameState = STATE_PLAYERTURN;
		}
		else
		{
			m_gameState = STATE_FLEETTURN;
		}
	}
	else if(optionselectNum == 2)
	{
		Shutdown();
		Init();
		m_gameState = STATE_INTRO;
	}
	else if(optionselectNum == 3)
	{
		Shutdown();
		return false;
	}
	return false;
}
//Function to create and distribute random ships
void SolarisPrimus::CreateAndDistributeShips(GameTurn* gameTurn)
{
	//Randomly creating five ships to be placed into fleets
	for(int i = 0; i < 5; i++)
	{
		int createShip = rand() % 3;
		switch(createShip)
		{
		case 0:
			pShip = new Starfighter("Starfighter", 150, 150, 30, 15, 210);
			break;
		case 1:
			pShip = new Frigate("Frigate", 300, 300, 45, 60, 35);
			break;
		case 2:
			pShip = new Capital("Capital Ship", 700, 700, 7, 90, 140);
			break;
		}
		gameTurn->addShip(pShip);
	}
}
//Function to print out the introduction of the game
void SolarisPrimus::Introduction()
{
	system("cls");

	cout << "--------------------------------------------------------------------------------";
	cout << "|                          Welcome to Solaris Primus                           |";
	cout << "--------------------------------------------------------------------------------" << endl;

	cout << "Solaris Primus is a simple turn-based space combat game. Given five random ships";
	cout << "of three different classes, players will have to use their fleet to their best  ";
	cout << "ability to defeat a computer opponent, who also has a fleet of five ships." << endl << endl;

	cout << "1. Classes" << endl;
	cout << "2. Instructions" << endl;
	cout << "3. Back to Main Menu" << endl << endl;

	string optionselect;
	int optionselectNum;

	do
	{
		cout << "Please choose an option (1 - 3): ";
		cin >> optionselect;
		stringstream(optionselect) >> optionselectNum;
	}while(optionselectNum < 1 || optionselectNum > 3);

	if(optionselectNum == 1)
	{
		Classes();
	}
	else if(optionselectNum == 2)
	{
		Instructions();
	}
	else if(optionselectNum == 3)
	{
		StateIntro();
	}
}
//Function to print out the ship classes in the game
void SolarisPrimus::Classes()
{
	system("cls");

	cout << "--------------------------------------------------------------------------------";
	cout << "|                          Welcome to Solaris Primus                           |";
	cout << "--------------------------------------------------------------------------------" << endl;

	cout << "The three classes that can make up a fleet are Starfighters, Frigates, and      ";
	cout << "Capital Ships." << endl << endl;

	cout << "Starfighters, being small and manoeuvrable, are capable of bringing down the    ";
	cout << "slow and immense Capital Ships with their torpedos, but can be quickly decimated";
	cout << "by the defenses of Frigates." << endl << endl;

	cout << "Frigates, which are armed with rapid-fire turbo-laser batteries, can lay waste  ";
	cout << "to any Starfighters which approach, but are easy targets for the laser cannons  ";
	cout << "which the Capital Ships are equipped with." << endl << endl;

	cout << "Capital Ships, equipped with huge laser cannons, can quickly slice through a    ";
	cout << "Frigate's armoured hull. However, being slow to aim, they are virtually useless ";
	cout << "against a Starfighter, which can easily dodge around the laser beams." << endl << endl;

	cout << "1. Instructions" << endl;
	cout << "2. Introduction" << endl;
	cout << "3. Back to Main Menu" << endl << endl;

	string optionselect;
	int optionselectNum;

	do
	{
		cout << "Please choose an option (1 - 3): ";
		cin >> optionselect;
		stringstream(optionselect) >> optionselectNum;
	}while(optionselectNum < 1 || optionselectNum > 3);

	if(optionselectNum == 1)
	{
		Instructions();
	}
	else if(optionselectNum == 2)
	{
		Introduction();
	}
	else if(optionselectNum == 3)
	{
		StateIntro();
	}
}
//Function to print out the instructions of the game
void SolarisPrimus::Instructions()
{
	system("cls");

	cout << "--------------------------------------------------------------------------------";
	cout << "|                          Welcome to Solaris Primus                           |";
	cout << "--------------------------------------------------------------------------------" << endl;

	cout << "In a new game, you always start first. During your turn, you will have the      ";
	cout << "over the computer, because the computer will have to randomly choose a ship to  ";
	cout << "send out for that turn. After the computer has selected a ship, it will be your ";
	cout << "turn to choose one of your ships to attack the enemy ship." << endl << endl;

	cout << "If your ship attacks the enemy ship, but doesn't destroy it, the enemy ship     ";
	cout << "will be able to retaliate, attacking your ship in kind. If your attack destroys ";
	cout << "the enemy ship, however, the computer will lose that ship, and it will be added ";
	cout << "into your fleet, with its health restored." << endl << endl;

	cout << "Should the enemy ship destroy your ship during its retaliation, though, the same";
	cout << "will happen to your selected ship, with it being added to the computer's fleet." << endl << endl;

	cout << "During the computer's turn, you will have to randomly select a ship to send out,";
	cout << "and once you have made your choice, the computer will also send one of its ships";
	cout << "out to attack yours. The same rules to the battle, as explained above, apply." << endl << endl;

	cout << "In order to win and end the game, you will have to destroy all of the computer's";
	cout << "ships. However, if the computer destroys all of your ships, you will lose the   ";
	cout << "game." << endl << endl;

	cout << "1. Start Game" << endl;
	cout << "2. Classes" << endl;
	cout << "3. Introduction" << endl;
	cout << "4. Back to Main Menu" << endl << endl;

	string optionselect;
	int optionselectNum;

	do
	{
		cout << "Please choose an option (1 - 4): ";
		cin >> optionselect;
		stringstream(optionselect) >> optionselectNum;
	}while(optionselectNum < 1 || optionselectNum > 4);

	if(optionselectNum == 1)
	{
		int m_whoStartFirst = ((rand() % 2) + 1);
		if(m_whoStartFirst == 1)
		{
			m_gameState = STATE_PLAYERTURN;
		}
		else
		{
			m_gameState = STATE_FLEETTURN;
		}
	}
	else if(optionselectNum == 2)
	{
		Classes();
	}
	else if(optionselectNum == 3)
	{
		Introduction();
	}
	else if(optionselectNum == 4)
	{
		StateIntro();
	}
}
//Function for when the player has to select a ship
Starship* SolarisPrimus::PlayerSelectShip()
{
	//Showing the player his available ships
	cout << "Your Ships:" << endl << endl;

	player->printAll();

	cout << "--------------------------------------------------------------------------------";

	int playerShipNum;
	string playerShip;

	//Asking the player to select a ship
	do
	{
		cout << "Please select a ship (1 - " << player->getFleetSize() <<  "): ";
		cin >> playerShip;
		stringstream(playerShip) >> playerShipNum;
	}while(playerShipNum < 1 || playerShipNum > player->getFleetSize());

	playerSelectedShip = player->getShip(playerShipNum - 1);

	return playerSelectedShip;
}
//Function for when the computer has to select a ship
Starship* SolarisPrimus::ComputerSelectShip()
{	
	//Randomly selecting a ship
	int selectShip = rand() % opponent->getFleetSize();

	Starship* opponentShip = opponent->getShip(selectShip);

	cout << "--------------------------------------------------------------------------------";
	cout << "Opponent selected: ";
	opponentShip->print();

	return opponentShip;
}
//Function for conflict resolvation
bool SolarisPrimus::Fight(Starship* attacker, Starship* defender)
{
	//Attacking ship attacks defending ship
	attacker->attack(defender);

	//Showing the player the damage dealth by the attacking ship
	cout << attacker->getShipName() << " attacks " << defender->getShipName() << ", dealing " << attacker->getDamageDealt(defender) << " damage." << endl << endl;

	//Shows the player the remaining hit pointsof the defending ship
	if(defender->getHitPoints() > 0)
	{
		cout << defender->getShipName() << " has " << defender->getHitPoints() << "/" << defender->getMaxHitPoints() << " hit points remaining." << endl << endl << endl;
	}
	return true;
}


In the STATE_WIN and STATE_LOSE part, I'm trying to get the game to delete it's previous instance, and re-initialise a new game instance, but it keeps crashing. I'm fairly certain that the problem is with the Shutdown() function, but I can't figure out the problem. Any help with my problem would be great. Thank you.

I'm also having another minor problem. in the StateIntro() function, when I go in to any introduction or instruction page, exit back to the main menu, then choose 3 to try to exit the game, it seems to refresh itself, but when I choose 3 after it refreshes it self, it seems to work. Can anyone tell me where the problem is?

This post has been edited by arjuna1356: 09 February 2010 - 03:49 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with a game loop

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: Need help with a game loop

Posted 09 February 2010 - 04:14 AM

your code seems to be in a complete mess - why do you have global pointers? I strongly suspect that you're leaking memory too, and perhaps suffering from 'dangling' pointers.

You need to get rid of some of those pointers, and rethink your use of global variables. Its impossible to really see whats going on in your program because its all over the place.

Your debugger might be able to show you exactly what the source of your crash is - i suspect you're dereferencing a dangling or null pointer, but since you haven't shown what the rest of your program looks like, its impossible to tell. The best solution will probably be to write your program without new - I can't see any good reason for using it.

This post has been edited by Bench: 09 February 2010 - 04:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1