Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.64 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 main()
  14. {
  15.  
  16.     int pennies = 21; // Stores how many pennies the game starts at. Increase/decrease number for desired starting pennies.
  17.     int penniesRemaining = pennies;
  18.     int numberOfGamesToPlay = 5; // Stores how many games are to be played against the computer. Increase/decrease for desired length.
  19.     int numberOfGamesPlayed = 0; // Stores how many games have currently been played. Literal is initialised as 0 as no games have been played.
  20.     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.
  21.     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.
  22.     int penniesToDisplay = 0; // A variable that helps check how many 'O's to display to the user in correlation to remaining 'pennies'.
  23.     int computersMove = 0;
  24.     int playersMove = 0;
  25.     bool computerTakesFirstTurn = true; // Decides whether computer takes the first turn. 'true' to go first 'false' to go second.
  26.     bool computersTurn;
  27.     bool gameInProgress;
  28.  
  29.     srand(time(0));
  30.  
  31.     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;
  32.     system("pause");
  33.     while (numberOfGamesPlayed < numberOfGamesToPlay)
  34.     {
  35.         cout << "\nLet's start! Game " << numberOfGamesPlayed +1 << " out of " << numberOfGamesToPlay << endl;
  36.         if (computerTakesFirstTurn)
  37.         {
  38.             cout << "\nComputer goes first!" << endl;
  39.             computersTurn = true;
  40.         }
  41.         else
  42.         {
  43.             cout << "Player to go first" << endl;
  44.             computersTurn = false;
  45.         }
  46.         gameInProgress = true;
  47.  
  48.         while (gameInProgress)
  49.         {
  50.             if (computersTurn)
  51.             {
  52.                 if (penniesRemaining == 1)
  53.                 {
  54.                     cout << "The computer takes away the final coin.\n" << endl;
  55.                     cout << "Congratulations player, you win game " << numberOfGamesPlayed +1 << endl;
  56.                     numberOfGamesPlayed++;
  57.                     gamesPlayerHasWon++;
  58.                     cout << "The scores\n" << endl;
  59.                     cout << "Player has won: " << gamesPlayerHasWon << " games." << endl;
  60.                     cout << "Computer has won: " << gamesComputerHasWon << " games." << endl;
  61.                     penniesRemaining = pennies;
  62.                     gameInProgress = false;
  63.                     system("pause");
  64.                 }
  65.                 else if ((penniesRemaining < 7) && (penniesRemaining >1))
  66.                 {
  67.                     computersMove = penniesRemaining - 1;
  68.                     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;
  69.                     penniesRemaining = penniesRemaining - computersMove;
  70.                     cout << "Pennies remaining: (1) O" << endl;
  71.                     computersTurn = false;
  72.                 }
  73.                 else
  74.                 {
  75.                     computersMove = (rand()%5+1);
  76.                     cout << "The computer decides to take away " << computersMove << " pennies." << endl;
  77.                     penniesRemaining = penniesRemaining - computersMove;
  78.                     cout << "Pennies remaining: (" << penniesRemaining << ") ";
  79.                     while (penniesToDisplay < penniesRemaining)
  80.                     {
  81.                         cout << "O";
  82.                         penniesToDisplay++;
  83.                     }
  84.                     cout << "\n" << endl;
  85.                     penniesToDisplay = 0;
  86.                     computersTurn = false;
  87.                 }
  88.             }
  89.             else
  90.             {
  91.                 cout << "Your move, player." << endl;
  92.                 if (penniesRemaining == 1)
  93.                 {
  94.                     while ( playersMove != 1)
  95.                     {
  96.                         cout << "Oh dear, player. You're left with the last penny to take. Take away the final coin: ";
  97.                         cin >> playersMove;
  98.                     }
  99.                     cout << "Unfortunately player, you lose game " << numberOfGamesPlayed +1 << endl;
  100.                     numberOfGamesPlayed++;
  101.                     gamesComputerHasWon++;
  102.                     cout << "The scores\n" << endl;
  103.                     cout << "Player has won: " << gamesPlayerHasWon << " games." << endl;
  104.                     cout << "Computer has won: " << gamesComputerHasWon << " games." << endl;
  105.                     penniesRemaining = pennies;
  106.                     gameInProgress = false;
  107.                     computersTurn = true;
  108.                     system("pause");
  109.                 }
  110.                 while ((playersMove > 5) || (playersMove < 1))
  111.                 {
  112.                     cout << "How many pennies will you remove from the pile?: ";
  113.                     cin >> playersMove;
  114.                 }
  115.                 penniesRemaining = penniesRemaining - playersMove;
  116.                 if (penniesRemaining <=0)
  117.                 {
  118.                     cout << "Oh dear, you've taken the last coin! You lose!" << endl;
  119.                     numberOfGamesPlayed++;
  120.                     gamesComputerHasWon++;
  121.                     cout << "The scores\n" << endl;
  122.                     cout << "Player has won: " << gamesPlayerHasWon << " games." << endl;
  123.                     cout << "Computer has won: " << gamesComputerHasWon << " games." << endl;
  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