-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameOf23.cpp
87 lines (78 loc) · 2.01 KB
/
GameOf23.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//Fort Hays State University - CSCI 121
//Project 3 - "Simple Game of '23'"
//Author: Lindsey Jimenez
//Date: 09-20-2021
#include <iostream>
using namespace std;
int main()
{
int user_choice, comp_choice, toothpicks;
char choice;
do
{
toothpicks = 23;
cout << "Let's play a game of \"23\"!" << endl;
cout << endl;
while (toothpicks > 0)
{ //User choice portion
cout << "Enter the number of sticks you wish to pick: ";
cin >> user_choice;
cout << endl;
while (user_choice > 3 || user_choice < 1 || user_choice > toothpicks)
{
if (user_choice > 3 || user_choice < 1)
{
cout << "Wrong number of sticks. Please pick 1, 2, or 3 sticks: ";
cin >> user_choice;
cout << endl;
}
else
{
cout << "Not enough sticks in pile. Please try again.";
cin >> user_choice;
cout << endl;
}
}
toothpicks -= user_choice;
cout << "You picked " << user_choice << " stick. " << toothpicks << " left" << endl;
cout << endl;
if (toothpicks == 0)
{ //Computer wins output
cout << "You picked the last stick." << endl;
cout << endl;
cout << "Sorry, the computer beat you!" << endl;
cout << endl;
break;
}
//Computer choice portion
if (toothpicks > 4)
{
comp_choice = 4 - user_choice;
}
else if (toothpicks == 1)
{
comp_choice = toothpicks;
}
else
{
comp_choice = toothpicks - 1;
}
toothpicks -= comp_choice;
cout << "Computer picked " << comp_choice << " sticks. " << toothpicks << " left" << endl;
cout << endl;
if (toothpicks == 0)
{ //User wins output
cout << "Computer picked the last stick." << endl;
cout << endl;
cout << "You win!" << endl;
cout << endl;
break;
}
cout << "Your turn. ";
} //Determines loop repeat or termination
cout << "Do you want to play another game? <Y/N>: ";
cin >> choice;
cout << endl;
}while (choice == 'Y' || choice == 'y');
return 0;
}