-
Notifications
You must be signed in to change notification settings - Fork 0
/
NimGame_driver.cpp
119 lines (95 loc) · 2.89 KB
/
NimGame_driver.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Christian Jeune, ID: 40279265
// Pawan Kumar Gupta, ID: 40254781
// COEN 243-Nim To Win project
// Due Date: December 3, 2023
// main file to initiate the Nim Game
#include "NimGame.h"
#include <iostream>
#include <cstdlib>
using namespace std;
// Function to print the rules of the Nim Game
void printGameRules() {
cout << "Welcome to the Nim Game!\n";
cout << "Rules:\n";
cout << "1. This game includes two players.\n";
cout << "2. Each player can pick up from one pile.\n";
cout << "3. Each player can only take one or two tokens.\n";
cout << "4. The winner is the player who makes the last move.\n";
cout << "5. Enjoy the game!\n\n";
}
// Function to prompt the user to choose an option (User vs. User, PC vs. User, or Exit)
void chooseOption() {
int choice;
cout << "Choose an option:\n";
cout << "1. User vs. User\n";
cout << "2. PC vs. User\n";
cout << "3. Exit\n\n";
// Get input for the user's choice
cout << "Enter your choice: ";
while (!(cin >> choice)) {
cout << "\nInvalid choice. Please try again.\n\n";
cin.clear();
cin.ignore(10000, '\n');
cout << "Enter your choice: ";
}
switch (choice) {
case 1: {
string player1, player2;
int numTokens;
// Get input for User vs. User game
cout << "\nEnter Player 1's name: ";
cin >> player1;
cout << "\nEnter Player 2's name: ";
cin >> player2;
cout << "\nEnter the total number of tokens(greater than or equal to 5): ";
while (!(cin >> numTokens) || numTokens < 5) {
cout << "\nInvalid choice. Please try again.\n";
cin.clear();
cin.ignore(10000, '\n');
cout << "\nEnter the total number of tokens(greater than or equal to 5): ";
}
// Initialize NimGame object for User vs. User
NimGame nimGame(player1, player2, 5, numTokens);
cout << "\nGame Start" << endl;
// Start User vs. User game
nimGame.userVsUser(0);
break;
}
case 2: {
string playerName;
int numHeaps;
// Get input for PC vs. User game
cout << "\nEnter your name: ";
cin >> playerName;
cout << "\nEnter the number of heaps: ";
while (!(cin >> numHeaps) || numHeaps < 1) {
cout << "\nInvalid choice. Please try again.\n";
cin.clear();
cin.ignore(10000, '\n');
cout << "\nEnter the number of heaps: ";
}
// Initialize NimGame object for PC vs. User
NimGame nimGame("Computer", playerName, numHeaps);
cout << "\nGame Start" << endl;
// Start PC vs. User game
nimGame.userVsComputer(0);
break;
}
case 3:
cout << "\nGoodbye!\n";
return;
default:
cout << "\nInvalid choice. Please try again.\n\n";
break;
}
// Recursively prompt the user for another option
chooseOption();
}
// Main function to initiate the Nim Game
int main() {
// Display the rules of the game
printGameRules();
// Prompt the user to choose an option and start the game
chooseOption();
return 0;
}