/
player.cpp
181 lines (162 loc) · 3.92 KB
/
player.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
/***************************************************************************************************
** Author: Kurt Kaiser
** Website: kurtkaiser.us
** Info: Designed a one-player, text-based game, the player can move through
** spaces to get items and accomplish goals.
****************************************************************************************************/
#include <iostream>
#include "player.hpp"
#include <string>
#include <array>
#include <stdlib.h>
#include <time.h>
#include <cstdlib>
//#include <limits.h>
using std::string;
using std::cout;
using std::endl;
// Constructor for the Player class
Player::Player() {
name = "player";
health = 40;
// index 0 is food count
itemCount = 6;
packLimit = 10;
// Starting item pack
pack = new string [itemCount]{"meat", "meat", "meat", "meat", "meat", "meat"};
weapon = "fists";
weaponPower = 1;
weaponHealth = 100;
hasPickaxe = false;
hasOil = false;
hasHide = false;
}
// Declaring public methods
// Setters to define private variables
void Player::setName(string nameIn) {
name = nameIn;
}
// Add items to the players pack to collect goods and food
void Player::addToPack(string itemIn) {
if (itemIn == "sword" && weapon == "sword") {
cout << "Too much weight. You can't carry two swords." << endl;
}
else {
// check if winning had been picked up
if (itemIn == "sword") {
weapon = "sword";
weaponPower = 2;
}
else if (itemIn == "pickaxe") {
hasPickaxe = true;
}
else if (itemIn == "oil") {
hasOil = true;
}
else if (itemIn == "hide") {
hasHide = true;
}
// keep item count up to date and resize array
if (itemCount < packLimit) {
string *tempPack = new string[itemCount + 1];
for (int i = 0; i < itemCount; i++) {
tempPack[i] = pack[i];
}
itemCount++;
delete[] pack;
pack = tempPack;
pack[itemCount - 1] = itemIn;
}
else {
cout << "-- Your pack is full. --" << endl;
printPack();
}
}
}
// output the contents of the pack
void Player::printPack() {
cout << "---- Pack ----" << endl;
for (int i = 0; i < itemCount; i++) {
cout <<"| "<< i + 1 << ": " << pack[i] << endl;
}
cout << "------------" << endl;
}
// Remove items from the pack and resize the array
void Player::removeFromPack(string itemOut) {
string deleteItem = itemOut;
int index = 0;
bool found = false;
if (itemOut == "sword") {
weapon = "fists";
weaponPower = 1;
}
if (itemCount == 1){
if (pack[0] == deleteItem) {
pack[0] = "";
itemCount = 0;
}
} else if (itemCount > 0) {
string *tempPack = new string[itemCount - 1];
for (int i = 0; i < itemCount; i++) {
if (pack[i] == deleteItem) {
deleteItem = "false item here";
found = true;
}
else {
tempPack[index] = pack[i];
index++;
}
}
// if found delete pack and set it to temp array
if (found) {
itemCount--;
delete[] pack;
pack = tempPack;
printPack();
}
// Output if nothing in pack
else {
cout << "The item is not in your pack." << endl;
}
}
else {
cout << "You have nothing in your pack." << endl;
}
}
// Setters and Getters
void Player::setHealth(int healthIn) {
health = healthIn;
}
int Player::getHealth() {
return health;
}
int Player::getPackLimit() {
return packLimit;
}
int Player::getItemCount() {
return itemCount;
}
string Player::getWeapon() {
return weapon;
}
bool Player::getHasPickaxe() {
return hasPickaxe;
}
bool Player::getHasOil() {
return hasOil;
}
bool Player::getHasHide() {
return hasHide;
}
int Player::getWeaponPower() {
return weaponPower;
}
int Player::getWeaponHealth() {
return weaponHealth;
}
void Player::setWeaponPower(int weaponPowerIn) {
weaponPower = weaponPowerIn;
}
void Player::setWeaponHealth(int weaponHealthIn) {
weaponHealth = weaponHealthIn;
}