From 42a258d42e0fb19f07d1a136f900ed20f182ca39 Mon Sep 17 00:00:00 2001 From: Muhammad Sameer Farooq Date: Sat, 17 Oct 2020 18:53:14 +0500 Subject: [PATCH] 23 Toothpicks Game in C++ --- Toothpick Game.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Toothpick Game.cpp diff --git a/Toothpick Game.cpp b/Toothpick Game.cpp new file mode 100644 index 0000000..16a61e5 --- /dev/null +++ b/Toothpick Game.cpp @@ -0,0 +1,71 @@ +#include +#include +using namespace std; + +int main() +{ + cout << "\t\tWelcome to the toothpick game!\n"; + int num = 0, computerPick = 0, humanPick = 0, toothpick = 23; + bool flag = true; + while (humanPick != 4) + { + while (humanPick != 4) + { + cout << "\nPlease pick up your toothpick(s), choose between 1 and 3 : "; + cin >> humanPick; + + if (humanPick > 3 || humanPick < 0 || !humanPick || humanPick>toothpick) + { + cout << "\nPlease enter a correct value!\n"; + cout << toothpick << " toothpick(s) remaining\n" << endl; + humanPick = 0; + continue; + } + + toothpick -= humanPick; + flag = false; + + if (toothpick <= 0) + break; + + if (toothpick > 4) + { + srand(time(0)); + computerPick = static_cast(rand() % 3 + 1); + } + else if (toothpick == 1) + computerPick = 1; + else if ((toothpick >= 2) || (toothpick <= 4)) + computerPick = toothpick - 1; + cout << "The computer took " << computerPick << " toothpick(s)" << endl; + + toothpick -= computerPick; + flag = true; + + if (toothpick <= 0) + break; + + cout << toothpick << " toothpick(s) remaining" << endl; + } + if (flag == true) + cout << "\n\tYou have Prevailed!"; + else if (flag == false) + cout << "\n\tComputer has Prevailed!"; + if (toothpick <= 0) + { + cout << "\n\nEnter any integer except 4 to Play agin\nEnter 4 to exit\n\n\tEnter your choice : "; + cin >> humanPick; + toothpick = 23; + + if (!cin) + break; + } + if (humanPick != 4) + { + continue; + } + } + cout << "\n\n\n"; + system("pause"); + return 0; +}