General c++ gaming question.

This is a discussion on General c++ gaming question. within the Game Programming forums, part of the General Programming Boards category; I just have a general question. If I want to write a program that plays a game (computer vs player) ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Post General c++ gaming question.

    I just have a general question. If I want to write a program that plays a game (computer vs player) how would I do that? What kind of loop would I use?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    It depends on the game and what kind of game play you have in mind.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    I just would like to know how to take turns.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Yet again, it would depend on your game.
    Code:
    while ( bRunGame == true )
    {
       //Update Game Logic
       //Render Graphics or Text
       //Do whatever else you need to do
    }
    That would be about as simple as you can get.

    Each frame ( or each loop through the while block ) would be a "Turn", and you could add conditional checks to see who's turn it is.
    Last edited by Raigne; 03-31-2011 at 06:00 PM.

  5. #5
    Registered User nathandelane's Avatar
    Join Date
    Dec 2010
    Posts
    9

    Taking Turns 101

    In a game, players generally take consecutive turns. Once the last player in a set of players has played, then the first player takes another turn, and each other player consecutively takes their turn again. This is done until the game ends. Raigns pseudo-code is a perfect example of this. A simple coded example of this might look like
    Code:
    #include <iostream>
    
    void takeTurn(int player)
    {
        std::cout << "Player " << player << " takes a turn" << std::endl;
    }
    
    int main()
    {
        bool gameIsOver = false;
        int numPlayers = 2;
        int numTurns = 4;
        int currentPlayer = 0;
        int currentTurn = 0;
    
        while (!gameIsOver);
        {
            takeTurn(currentPlayer);
    
            if (currentTurn >= numTurns)
            {
                gameIsOver = true;
            }
    
            currentPlayer++;
    
            if (currentPlayer >= numPlayers)
            {
                currentPlayer = 0;
                currentTurn++;
            }
        }
    
        return 0;
    }

  6. #6
    Registered User nathandelane's Avatar
    Join Date
    Dec 2010
    Posts
    9

    Wink

    This is relatively poorly designed but it shows a computer player taking a turn:
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <cstdlib>
    
    /**
     * Class Player
     * This would normally be found in a Player.hpp header
     * file.
     */
    class Player
    {
    	private:
    		std::string _name;
    		bool _isComputer;
    	public:
    		static const std::string possibleAnswers[26];
    
    		Player(std::string name, bool isComputer);
    		~Player();
    
    		std::string GetName();
    		bool IsComputer();
    };
    
    const std::string Player::possibleAnswers[] = { "a", "b", "c", 
    	"d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 
    	"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", 
    	"x", "y", "z" };
    
    /**
     * Class Player implementation.
     * This would normally be found in a Player.cpp file
     * and would #include Player.hpp from above.
     */
    Player::Player(std::string name, bool isComputer = false)
    {
    	_name = name;
    	_isComputer = isComputer;
    }
    
    Player::~Player()
    {
    }
    
    std::string Player::GetName()
    {
    	return _name;
    }
    
    bool Player::IsComputer()
    {
    	return _isComputer;
    }
    
    /**
     * TurnResult enum could be definded externally.
     */
    enum TurnResult
    {
    	Passed,
    	Executed,
    	Quitting
    };
    
    /**
     * Computer needs to take a turn.
     */
    std::string ComputerTakeTurn()
    {
    	std::string response;
    	int character = rand() % 26;
    
    	response = Player::possibleAnswers[character];
    
    	return response;
    }
    
    /**
     * Enters turn for given Player.
     */
    TurnResult TakeTurn(Player * player)
    {
    	std::string userInput;
    	TurnResult result;
    	result = Passed;
    
    	std::cout << "It is " << player->GetName() << "'s turn." << std::endl;
    	std::cout << player->GetName() << ", please enter input (q quits): ";
    
    	if (player->IsComputer())
    	{
    		userInput = ComputerTakeTurn();
    
    		std::cout << std::endl << "Computer [" << player->GetName() << "] entered: " << userInput << std::endl;
    	}
    	else
    	{
    		std::cin >> userInput;
    
    		std::cout << "You entered: " << userInput << std::endl;
    	}
    
    	if (userInput.compare("q") == 0)
    	{
    		result = Quitting;
    	}
    
    	return result;
    }
    
    int main()
    {
    	int result = 0;
    	std::vector<Player *> players;
    	bool gameContinues = true;
    
    	players.push_back(new Player("black", true));
    	players.push_back(new Player("white"));
    
    	int numberOfPlayers = players.size();
    	int currentPlayerIndex = 0;
    	
    	while (gameContinues)
    	{
    		if (currentPlayerIndex >= numberOfPlayers)
    		{
    			currentPlayerIndex = 0;
    		}
    
    		Player * currentPlayer = players[currentPlayerIndex];
    
    		TurnResult turnResult = TakeTurn(currentPlayer);
    
    		if (turnResult == Quitting)
    		{
    			gameContinues = false;
    		}
    
    		currentPlayerIndex++;
    	}
    
    	return result;
    }

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    In a game, players generally take consecutive turns
    What about Real-Time games, instead of turn-based games?

  8. #8
    Registered User nathandelane's Avatar
    Join Date
    Dec 2010
    Posts
    9

    Smile

    Quote Originally Posted by Raigne View Post
    What about Real-Time games, instead of turn-based games?
    In real-time games, for example a platformer, real-time-strategy, or shooter, input is polled continuously rather than being a blocking input. Usually in these types of games there is a single thread for the game loop, and threads are spawned for events. There really don't need to be multiple threads, that is just one method. Events can be handled synchronously, which is probably more common. For example if there are five sprites on-screen and you, the game will poll for your input, while it continues to animate the sprites. The sprites themselves usually take care of the animation, the game loop simply asks the sprite to animate its next frame and move according to its AI. Things start to get pretty complex almost immediately when there are multiple things happening on-screen at the same time, but since this is mostly standard, algorithms have been created, and design patterns exist in order to ensure simplicity in design. Also game/graphics/windowing APIs usually have a system that is incorporated for you, since window-based development (Windows, GNOME, KDE, Aqua) is generally event-driven anyway.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I must have misunderstood your point. I was merely meaning that not all games use synchronized input. Although your point on threads I find odd. In my game I use 3 threads, General Logic, Rendering, and AI. Syncronizing with simple high frequency timers. Even though monitoring this method I have found that my timers rarely ever start. Most modern games today are multi-threaded. Crysis as an example would probably not be plausible on a single core processor. Even Quad-core, with dual core video-cards are pushed to their limits with this particular game.

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Actually most games still appear to be single threaded evidenced by how they max out one of my cores but leave the others alone. There are a few that are optimized for dual core but very few, if any, that are optimized for quad-core CPUs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 10-26-2010, 01:12 PM
  2. general question about how struct works.
    By nullifyed in forum C Programming
    Replies: 14
    Last Post: 06-21-2010, 06:03 AM
  3. A general question aout programming?
    By bradt93 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-26-2008, 11:00 AM
  4. general question regarding a function parameter
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-13-2002, 07:32 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21