steamengines

Lab 1 COSC 1337

Sep 2nd, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. // Christian Ballard
  2. // COSC 1337 - 001
  3. // August 30th 2015
  4. // A simple game of NIM
  5.  
  6. #include<iostream>
  7. #include<time.h>
  8. #include<Windows.h>
  9. using namespace std;
  10.  
  11. const int HUMAN = 1;
  12. const int COMPUTER = 0;
  13.  
  14. int getHumanMove(int);
  15. int getComputerMove(int);
  16. int playTheGame(int);
  17.  
  18. int main()
  19. {
  20.     int numMarbles = 0;
  21.     char currentPlayer;
  22.  
  23.     cout << "Rules of the game: " << endl << endl;
  24.     cout << "1. The game begins with 35 marbles in a jar." << endl;
  25.     cout << "2. Each player must take between 1 and 5 marbles" << endl;
  26.     cout << "   from the jar when it's their turn." << endl;
  27.     cout << "3. The player that takes the last marble wins the game." << endl << endl;
  28.    
  29.     cout << "Do you want to go first? (Y/N) ";
  30.  
  31.     do
  32.     {
  33.        
  34.         cin >> currentPlayer;
  35.         currentPlayer = toupper(currentPlayer);
  36.  
  37.         switch (currentPlayer)
  38.         {
  39.         case 'Y': playTheGame(HUMAN);
  40.             break;
  41.         case 'N': playTheGame(COMPUTER);
  42.             break;
  43.         default: cout << "Error: please enter Y or N." << endl;
  44.             break;
  45.         }
  46.  
  47.     } while (currentPlayer != 'Y' || currentPlayer != 'N');
  48.  
  49.  
  50.  
  51.     system("pause");
  52.     return 0;
  53. }
  54.  
  55. int getHumanMove(int numMarbles1)
  56. {
  57.     int humanMarbTaken = 0;
  58.  
  59.     do
  60.     {
  61.         cout << "How many marbles would you like to take? (1 - 5)" << endl;
  62.         cin >> humanMarbTaken;
  63.         if (humanMarbTaken < 0 || humanMarbTaken > 5)
  64.         {
  65.             cout << "Please try again with a number 1 - 5" << endl;
  66.         }
  67.     } while (humanMarbTaken < 1 || humanMarbTaken > 5);
  68.  
  69.  
  70.     return humanMarbTaken;
  71. }
  72.  
  73. int getComputerMove(int numMarbles2)
  74. {
  75.     int compMarbTaken = 0;
  76.     //int modMarb = numMarbles2 % 6;
  77.     srand((unsigned)time(NULL));
  78.     int min = 0, max = 5;
  79.  
  80.     if (numMarbles2 <= 5)
  81.     {
  82.         numMarbles2 -= numMarbles2;
  83.     }
  84.     else
  85.     {
  86.         compMarbTaken = rand() % 5 + 1;
  87.         numMarbles2 = numMarbles2 - compMarbTaken;
  88.     }
  89.    
  90.     cout << "The computer took " << compMarbTaken << " marbles from the jar."
  91.         << endl << endl;
  92.  
  93.     return compMarbTaken;
  94. }
  95.  
  96. int playTheGame(int player)
  97. {
  98.     int numMarbles = 35, currentMove = 0;
  99.  
  100.     while (numMarbles != 0)
  101.     {
  102.        
  103.         if (player == HUMAN && numMarbles != 0)
  104.         {
  105.             cout << "There are " << numMarbles << " marbles left in the jar." << endl;
  106.             numMarbles -= getHumanMove(numMarbles);
  107.             player = COMPUTER;
  108.             if (numMarbles == 0){
  109.                 cout << "Congrats! You won!" << endl;
  110.             }
  111.         }
  112.         else if (player == COMPUTER && numMarbles != 0)
  113.         {
  114.             cout << "There are " << numMarbles << " marbles left in the jar." << endl;
  115.             numMarbles -= getComputerMove(numMarbles);
  116.             player = HUMAN;
  117.             if (numMarbles == 0){
  118.                 cout << "Congrats! The computer won!" << endl;
  119.             }
  120.         }
  121.  
  122.     }
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment