Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.45 KB | None | 0 0
  1. //*****Penny Game.
  2. //*****By Luke Hill
  3. //*****22/10/2010
  4.  
  5.  
  6.  
  7.  
  8. #include <iostream>
  9. #include <time.h>
  10.  
  11. using namespace std;
  12.  
  13. void displayScores(int playerScores,int computerScores)
  14. {
  15.     cout << "The scores\n" << endl;
  16.     cout << "Player has won: " << playerScores << " games." << endl;
  17.     cout << "Computer has won: " << computerScores << " games." << endl;
  18. }
  19.  
  20. void main()
  21. {
  22.  
  23.     int pennies = 21; // Stores how many pennies the game starts at. Increase/decrease number for desired starting pennies.
  24.     int penniesRemaining = pennies;
  25.     int numberOfGamesToPlay = 5; // Stores how many games are to be played against the computer. Increase/decrease for desired length.
  26.     int numberOfGamesPlayed = 0; // Stores how many games have currently been played. Literal is initialised as 0 as no games have been played.
  27.     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.
  28.     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.
  29.     int penniesToDisplay = 0; // A variable that helps check how many 'O's to display to the user in correlation to remaining 'pennies'.
  30.     int computersMove = 0;
  31.     int playersMove = 0;
  32.     bool computerTakesFirstTurn = true; // Decides whether computer takes the first turn. 'true' to go first 'false' to go second.
  33.     bool computersTurn;
  34.     bool gameInProgress;
  35.  
  36.     srand(time(0));
  37.  
  38.     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;
  39.     system("pause");
  40.     while (numberOfGamesPlayed < numberOfGamesToPlay)
  41.     {
  42.         cout << "\nLet's start! Game " << numberOfGamesPlayed +1 << " out of " << numberOfGamesToPlay << endl;
  43.         if (computerTakesFirstTurn)
  44.         {
  45.             cout << "\nComputer goes first!" << endl;
  46.             computersTurn = true;
  47.         }
  48.         else
  49.         {
  50.             cout << "Player to go first" << endl;
  51.             computersTurn = false;
  52.         }
  53.         gameInProgress = true;
  54.  
  55.         while (gameInProgress)
  56.         {
  57.             if (computersTurn)
  58.             {
  59.                 if (penniesRemaining == 1)
  60.                 {
  61.                     cout << "The computer takes away the final coin.\n" << endl;
  62.                     cout << "Congratulations player, you win game " << numberOfGamesPlayed +1 << endl;
  63.                     numberOfGamesPlayed++;
  64.                     gamesPlayerHasWon++;
  65.                     displayScores(gamesPlayerHasWon,gamesComputerHasWon);
  66.                     penniesRemaining = pennies;
  67.                     gameInProgress = false;
  68.                     system("pause");
  69.                 }
  70.                 else if ((penniesRemaining < 7) && (penniesRemaining >1))
  71.                 {
  72.                     computersMove = penniesRemaining - 1;
  73.                     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;
  74.                     penniesRemaining = penniesRemaining - computersMove;
  75.                     cout << "Pennies remaining: (1) O" << endl;
  76.                     computersTurn = false;
  77.                 }
  78.                 else
  79.                 {
  80.                     computersMove = (rand()%5+1);
  81.                     cout << "The computer decides to take away " << computersMove << " pennies." << endl;
  82.                     penniesRemaining = penniesRemaining - computersMove;
  83.                     cout << "Pennies remaining: (" << penniesRemaining << ")\n ";
  84.                     while (penniesToDisplay < penniesRemaining)
  85.                     {
  86.                         cout << "O";
  87.                         penniesToDisplay++;
  88.                     }
  89.                     penniesToDisplay = 0;
  90.                     computersTurn = false;
  91.                 }
  92.             }
  93.             else
  94.             {
  95.                 cout << "Your move, player." << endl;
  96.                 if (penniesRemaining == 1)
  97.                 {
  98.                     while ( playersMove != 1)
  99.                     {
  100.                         cout << "Oh dear, player. You're left with the last penny to take. Take away the final coin: ";
  101.                         cin >> playersMove;
  102.                     }
  103.                     cout << "Unfortunately player, you lose game " << numberOfGamesPlayed +1 << endl;
  104.                     numberOfGamesPlayed++;
  105.                     gamesComputerHasWon++;
  106.                     displayScores(gamesPlayerHasWon,gamesComputerHasWon);
  107.                     penniesRemaining = pennies;
  108.                     gameInProgress = false;
  109.                     computersTurn = true;
  110.                     system("pause");
  111.                 }
  112.                 while ((playersMove > 5) || (playersMove < 1))
  113.                 {
  114.                     cout << "How many pennies will you remove from the pile?: ";
  115.                     cin >> playersMove;
  116.                 }
  117.                 penniesRemaining = penniesRemaining - playersMove;
  118.                 if (penniesRemaining <=0)
  119.                 {
  120.                     cout << "Oh dear, you've taken the last coin! You lose!" << endl;
  121.                     numberOfGamesPlayed++;
  122.                     gamesComputerHasWon++;
  123.                     displayScores(gamesPlayerHasWon,gamesComputerHasWon);
  124.                     penniesRemaining = pennies;
  125.                     gameInProgress = false;
  126.                     computersTurn = true;
  127.                     system("pause");
  128.                 }
  129.                 else
  130.                 {
  131.                     cout << "Pennies remaining: (" << penniesRemaining << ") ";
  132.                     while (penniesToDisplay < penniesRemaining)
  133.                     {
  134.                         cout << "O";
  135.                         penniesToDisplay++;
  136.                     }
  137.                     cout << "\n" << endl;
  138.                     penniesToDisplay = 0;
  139.                     playersMove = 0;
  140.                     computersTurn = true;
  141.                     system("pause");
  142.                 }
  143.             }
  144.         }
  145.     }
  146.     cout << "All the games have been played. Let's check the final scores!" << endl;
  147.     if (gamesComputerHasWon > gamesPlayerHasWon)
  148.     {
  149.         cout << "The computer has won " << gamesComputerHasWon << " whereas the player has only won " << gamesPlayerHasWon << ". It looks like the computer wins!" << endl;
  150.     }
  151.     else
  152.     {
  153.         cout << "The player has won " << gamesPlayerHasWon << " whereas the computer has only won " << gamesComputerHasWon << ". It looks like the player wins!" << endl;
  154.     }
  155.     cout << "Thanks for playing. Please play again soon!" << endl;
  156.     system("pause");
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement