# Odds or Evens

By: SirCxyrtyx on Jul 1st, 2012  |  syntax: C++  |  size: 2.93 KB  |  hits: 26  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. /*
2. Odds or Evens
3.
4. Nicholas Howes
5.
7. 1. If the user fails to follow instructions, the computer will rig the game to punish them
8.
9. */
10.
11. #include <cstdlib>
12. #include <iostream>
13. #include <ctime>
14.
15. using namespace std;
16.
17.
18.
19. //Is the player odd or even
20. char playerSide;
21.
22. //determines whether the computer should cheat
23. bool computerCheat = false;
24.
25. //should the game keep going
26. bool gameOver = false;
27.
28. //What round is it?
29. int roundNum = 1;
30.
31. int playerScore, compScore;
32.
33.
34. //The mechanics of the game
35. void round ()
36. {
37.         int playerNum;
38.
39.         int compNum;
40.
41.         int sum;
42.
43.         cout << endl << "Round " << roundNum << endl << "Please enter a number between 1 and 5:";
44.         cin >> playerNum;
45.
46.         if (playerNum <= 0 || playerNum >= 6)
47.         {
48.                 playerNum = rand()%5 + 1;
49.                 cout << "You have entered an invalid number. For this round, I'm going to assign you " << playerNum << ". \nIn the future, please choose either 1, 2, 3, 4, or 5. \n";
50.                 computerCheat = true;
51.         }
52.
53.         if (computerCheat)
54.         {
55.                 if (playerSide == 'o'&& playerNum%2) //player is odd and has entered an odd number
56.                 {
57.                         compNum = 3;
58.                 }
59.                 else if (playerSide == 'o') //player is odd and has entered an even number
60.                 {
61.                         compNum = 2;
62.                 }
63.                 else if (playerNum%2) // player is even and has entered and odd number
64.                 {
65.                         compNum = 4;
66.                 }
67.                 else //player is even and has entered an even number
68.                 {
69.                         compNum = 5;
70.                 }
71.         }
72.         else
73.         {
74.                 compNum = rand()%5 + 1;
75.                 cout << "My number is " << compNum << endl;
76.         }
77.
78.         sum = playerNum + compNum;
79.
80.         if (sum%2)
81.         {
82.                 cout << "The sum is " << sum << ", which is odd.  ";
83.
84.                 if(playerSide == 'o')
85.                 {
86.                         cout << "You win this round.";
87.                         ++playerScore;
88.                 }
89.                 else
90.                 {
91.                         cout << "I win this round.";
92.                         ++compScore;
93.                 }
94.         }
95.         else
96.         {
97.                 cout << "The sum is " << sum << ", which is even. ";
98.
99.                 if(playerSide == 'e')
100.                 {
101.                         cout << "You win this round.";
102.                         ++playerScore;
103.                 }
104.                 else
105.                 {
106.                         cout << "I win this round.";
107.                         ++compScore;
108.                 }
109.         }
110.
111.         cout << endl;
112.
113.         ++roundNum;
114. }
115.
116.
117. int main ()
118. {
119.
120.         //Initializes random number generator
121.         srand((unsigned)time(0));
122.
123.         cout << "Welcome to Odd or Even" << endl << "Would you like to be (O)dd or (E)ven? :";
124.         cin >> playerSide;
125.
126.         if (playerSide == 'O' || playerSide =='o')
127.         {
128.                 playerSide = 'o';
129.                 cout << "You have chosen Odd. \n";
130.         }
131.         else if (playerSide == 'E' || playerSide == 'e')
132.         {
133.                 playerSide = 'e';
134.                 cout << "You have chosen Even. \n";
135.         }
136.         else
137.         {
138.                 cout << "You have entered an invalid option. For this game, I'm going to assign you Odd.\n In the future, please choose either O or E. \n";
139.                 playerSide = 'o';
140.                 computerCheat = true;
141.         }
142.
143.
144.         while (gameOver == false)
145.         {
146.                 if (playerScore > 1)
147.                 {
148.                         cout << "\nCongratulations, you win!\n";
149.                         gameOver = true;
150.                 }
151.                 else if (compScore > 1)
152.                 {
153.                         cout << "\nI win! Better luck next time\n";
154.                         gameOver = true;
155.                 }
156.                 else
157.                 {
158.                         round ();
159.                 }
160.         }
161.
162.
163.         system("PAUSE");
164.
165. }