Jump to content
  • Advertisement
Sign in to follow this  
kerilynne

Implementing Game states in a console blackjack game.

This topic is 3811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there everyone, I recently got my Blackjack game running and I've noticed something about my loop that made it pretty difficult to follow. It looks like I'm performing fairly similar operations in each case in my game loop, shown here:
void Card_Game::play_game()
{
	using namespace std;
	int gameloop = 1;
	int result = 0;
	int controller = 0;
	
	while (gameloop != 0)
	{
		IO::instance().clear_screen();
		switch(controller)
		{
		case 0: // main menu
			std::cout << "Welcome to the Casino!\n";
			std::cout << "What would you like to do?\n";
			std::cout << "1. Play Blackjack\n";
			std::cout << "2. Quit\n";
			result = IO::instance().get_input();
			controller = main_menu(result);
			if (controller == 4)
			{
				gameloop = 0; // end game
			}
			break;
		case 1:	// blackjack function
			cout << players[0].show_hand();
			std::cout << players[1].show_hand();
			std::cout << "The current jackpot is " << pot->get_pot() << " dollars!\n";
			std::cout << "\nWhat would you like to do?\n";
			std::cout << "1. Hit me\n";
			std::cout << "2. Stay\n";
			std::cout << "3. Fold\n";
			result = IO::instance().get_input();
			controller = blackjack(result);
			break;
		case 2: // deal again?
			for (unsigned int loop = 0; loop < players[0].hand->card_list.size(); loop++)
			{
				players[0].hand->card_list[loop].flip_card(true);
			}
			std::cout << players[0].show_hand();
			std::cout << players[1].show_hand();
			if (player_wins == true)
			{
				std::cout << "You win!!\n"
						  << "You've won " 
					      << calculate_winnings()
						  << " dollars!\n\n";
				players[1].cash += calculate_winnings();
			}
			else
			{
				std::cout << "You Lose!!\n"
					      << "You've lost "
						  << pot->get_pot()
						  << " dollars!\n\n";
			}
			std::cout << "Would you like to deal again?\n";
			std::cout << "1. Yes\n";
			std::cout << "2. No\n";
			result = IO::instance().get_input();
			controller = deal_again(result);
			break;
		case 3: // place bets
			std::cout << players[0].show_hand();
			std::cout << players[1].show_hand();
			std::cout << "How much would you like to bet?  You have "
				      << players[1].cash
					  << " dollars.\n";
			controller = place_bets(IO::instance().get_input());
			if (controller == 4)
			{
				controller = 3;
			}
			break;
		}
	}
}
It kinda looks like it works with gamestates already but I guess I just have a few questions about how this setup (shown below) works.
///////////GAME SCREEN///////////
#include "Game_Engine.h"

class Screen
{
public:

	virtual void init() = 0;
	virtual void cleanup() = 0;

	virtual void pause() = 0;
	virtual void resume() = 0;

	virtual void handle_events(Game_Engine * game) = 0;
	virtual void update(Game_Engine * game) = 0;
	virtual void draw(Game_Engine* game) = 0;

	void ChangeState(Game_Engine * game, Screen * screen) 
	{
		game->change_screen(screen);
	}

  protected: Screen() { }
};

///////////////////////////GAME ENGINE/////////////////

#include "Screen.h"
#include <vector>

class Game_Engine
{
public:
  void init();
  void cleanup();

  void change_screen(Screen * screen);
  void push_screen(Screen * screen);
  void pop_screen();

  void handle_events();
  void update();

  bool running() { return m_running; }
  void quit() { m_running = false; }

private:
  // the stack of screens
	std::vector<Screen*> screens;

  bool m_running;
};
I decided to rename the state class as a screen, since I'm working with a console app I don't really change up the input ever, nor the output, really all I will be changing is update and handle_events... Question #1: The Handle_Events function, what exactly is that function name implying? Is it referring to a prompt in one of my cases in my original code? Is it referring to specific input and the result of that input? What exactly would an "event" be? Question #2: What exactly are we updating here? Output information? Like I need to output the dealer and the player's hands when I'm in the blackjack screen right? Question #3: What is a state? What isn't a state? As you see in my switch that I have currently I have a case for the main menu, a case for the end of game sequence, a case for the blackjack game itself where you actually get to draw cards and choose to stay and whatnot. Honestly none of them are really states, they are screens, which is why I changed the naming convention, but I figure it all works the same really.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!