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?
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?
It depends on the game and what kind of game play you have in mind.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I just would like to know how to take turns.
Yet again, it would depend on your game.
That would be about as simple as you can get.Code:while ( bRunGame == true ) { //Update Game Logic //Render Graphics or Text //Do whatever else you need to do }
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.
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; }
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; }
What about Real-Time games, instead of turn-based games?In a game, players generally take consecutive turns
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.
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.
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.