-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeader.hpp
168 lines (138 loc) · 3.26 KB
/
Header.hpp
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#pragma once
/*******************************************************************************************
* Programmer: Boaz Glassberg *
* Class: CptS 223 *
* Programming Assignment: PA1 *
* Date: 9/14/2022 *
* Description: This program plays a linux command matching game *
*******************************************************************************************/
//guard code
#define HEADER_H
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <time.h>
using namespace std;
using std::string;
using std::cout;
using std::cin;
using std::string;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::ostream;
using std::getline;
struct profile {
string name;
int points;
};
template <class T1,class T2>
struct node
{
T1 first;
T2 second;
node* next;
};
template <class T1,class T2>
class linked_list
{
private:
node<T1, T2>* head, * tail;
void destroyListHelper(node<T1, T2>* tmp)
{
if (tmp != nullptr)
{
destroyListHelper(tmp->next);
delete tmp;
}
}
void removeNodeHelper(node<T1, T2>* tmp) {
node<T1, T2>* ptr = head;
node<T1, T2>* pLast = head;
while (ptr != nullptr && ptr != tmp) {
pLast = ptr;
ptr = ptr->next;
}
pLast->next = ptr->next;
delete ptr;
return;
}
public:
linked_list()
{
head = NULL;
tail = NULL;
}
void add_node(T1 first, T2 second)
{
node<T1, T2>* tmp = new node<T1, T2>;
tmp->first = first;
tmp->second = second;
tmp->next = NULL;
if (head == NULL)
{
insertAtFront(tmp);
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
void insertAtFront(node<T1, T2>* tmp) {
if (tmp != nullptr) {
tmp->next = head;
head = tmp->next;
}
}
void removeNode(T1 first) {
node<T1, T2>* tmp = head;
while (tmp != nullptr) {
if (tmp->first != first) {
tmp = tmp->next;
}
else {
removeNodeHelper(tmp);
}
}
}
node<T1,T2>* getHead() {
return head;
}
node<T1, T2>* getTail() {
return tail;
}
~linked_list() {
this->destroyList();
}
void destroyList()
{
destroyListHelper(this->head);
}
//the command name is first, and the command description is second in this case
int buildList(fstream& infile, T1 first, T2 second) {
int size = 0;
while (!infile.eof()) {
getline(infile, first, ',');
getline(infile, second);
this->add_node(first, second);
size++;
}
return size;
}
};
void main_menu();
int printMenu();
void gameRules();
void playGame(linked_list<string, string> cList, fstream& infile);
void playGame(profile player);
void buildProfiles(profile pList[]);
profile loadGame();
void saveCommand(linked_list<string, string> cList);
void removeCommand(linked_list<string, string> cList);
void savePlayer(profile player);
void updatePlayer(profile player);
int playGameLoop(node<string, string>* head, profile pList[], profile player, int index);
ofstream& operator<< (ofstream& lhs, profile& rhs);