Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*****Penny Game.
- //*****By Luke Hill
- //*****22/10/2010
- #include <iostream>
- #include <time.h>
- using namespace std;
- void displayScores(int playerScores,int computerScores)
- {
- cout << "The scores\n" << endl;
- cout << "Player has won: " << playerScores << " games." << endl;
- cout << "Computer has won: " << computerScores << " games." << endl;
- }
- void main()
- {
- int pennies = 21; // Stores how many pennies the game starts at. Increase/decrease number for desired starting pennies.
- int penniesRemaining = pennies;
- int numberOfGamesToPlay = 5; // Stores how many games are to be played against the computer. Increase/decrease for desired length.
- int numberOfGamesPlayed = 0; // Stores how many games have currently been played. Literal is initialised as 0 as no games have been played.
- int gamesComputerHasWon = 0; // Stores the amount of times the computer has beaten the player. Literal is initialised as 0 as no games have been played.
- int gamesPlayerHasWon = 0; // Stores the amount of times the player has beaten the computer. Literal is initialised as 0 as no games have been played.
- int penniesToDisplay = 0; // A variable that helps check how many 'O's to display to the user in correlation to remaining 'pennies'.
- int computersMove = 0;
- int playersMove = 0;
- bool computerTakesFirstTurn = true; // Decides whether computer takes the first turn. 'true' to go first 'false' to go second.
- bool computersTurn;
- bool gameInProgress;
- srand(time(0));
- cout << "Welcome to the Penny game. The rules are simple. There are " << pennies << " pennies in a pile. Taking turns you and the computer must take away between 1-5 pennies. The personwho takes the last penny loses.\n" << endl;
- system("pause");
- while (numberOfGamesPlayed < numberOfGamesToPlay)
- {
- cout << "\nLet's start! Game " << numberOfGamesPlayed +1 << " out of " << numberOfGamesToPlay << endl;
- if (computerTakesFirstTurn)
- {
- cout << "\nComputer goes first!" << endl;
- computersTurn = true;
- }
- else
- {
- cout << "Player to go first" << endl;
- computersTurn = false;
- }
- gameInProgress = true;
- while (gameInProgress)
- {
- if (computersTurn)
- {
- if (penniesRemaining == 1)
- {
- cout << "The computer takes away the final coin.\n" << endl;
- cout << "Congratulations player, you win game " << numberOfGamesPlayed +1 << endl;
- numberOfGamesPlayed++;
- gamesPlayerHasWon++;
- displayScores(gamesPlayerHasWon,gamesComputerHasWon);
- penniesRemaining = pennies;
- gameInProgress = false;
- system("pause");
- }
- else if ((penniesRemaining < 7) && (penniesRemaining >1))
- {
- computersMove = penniesRemaining - 1;
- cout << "Oh dear, it looks like you're about to lose! The computer takes away " << computersMove << " pennies. Leaving you to take the very last coin." << endl;
- penniesRemaining = penniesRemaining - computersMove;
- cout << "Pennies remaining: (1) O" << endl;
- computersTurn = false;
- }
- else
- {
- computersMove = (rand()%5+1);
- cout << "The computer decides to take away " << computersMove << " pennies." << endl;
- penniesRemaining = penniesRemaining - computersMove;
- cout << "Pennies remaining: (" << penniesRemaining << ")\n ";
- while (penniesToDisplay < penniesRemaining)
- {
- cout << "O";
- penniesToDisplay++;
- }
- penniesToDisplay = 0;
- computersTurn = false;
- }
- }
- else
- {
- cout << "Your move, player." << endl;
- if (penniesRemaining == 1)
- {
- while ( playersMove != 1)
- {
- cout << "Oh dear, player. You're left with the last penny to take. Take away the final coin: ";
- cin >> playersMove;
- }
- cout << "Unfortunately player, you lose game " << numberOfGamesPlayed +1 << endl;
- numberOfGamesPlayed++;
- gamesComputerHasWon++;
- displayScores(gamesPlayerHasWon,gamesComputerHasWon);
- penniesRemaining = pennies;
- gameInProgress = false;
- computersTurn = true;
- system("pause");
- }
- while ((playersMove > 5) || (playersMove < 1))
- {
- cout << "How many pennies will you remove from the pile?: ";
- cin >> playersMove;
- }
- penniesRemaining = penniesRemaining - playersMove;
- if (penniesRemaining <=0)
- {
- cout << "Oh dear, you've taken the last coin! You lose!" << endl;
- numberOfGamesPlayed++;
- gamesComputerHasWon++;
- displayScores(gamesPlayerHasWon,gamesComputerHasWon);
- penniesRemaining = pennies;
- gameInProgress = false;
- computersTurn = true;
- system("pause");
- }
- else
- {
- cout << "Pennies remaining: (" << penniesRemaining << ") ";
- while (penniesToDisplay < penniesRemaining)
- {
- cout << "O";
- penniesToDisplay++;
- }
- cout << "\n" << endl;
- penniesToDisplay = 0;
- playersMove = 0;
- computersTurn = true;
- system("pause");
- }
- }
- }
- }
- cout << "All the games have been played. Let's check the final scores!" << endl;
- if (gamesComputerHasWon > gamesPlayerHasWon)
- {
- cout << "The computer has won " << gamesComputerHasWon << " whereas the player has only won " << gamesPlayerHasWon << ". It looks like the computer wins!" << endl;
- }
- else
- {
- cout << "The player has won " << gamesPlayerHasWon << " whereas the computer has only won " << gamesComputerHasWon << ". It looks like the player wins!" << endl;
- }
- cout << "Thanks for playing. Please play again soon!" << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement