Loops and multi-threading with a text-based RPG

Okay, I have been coding a simple text-based RPG, and I figured out how to multi-thread the combat loop (see my previous posts if this is unclear).

The only problem i have now is this:

I have the game in a while loop for while the input from the player is not "quit", it will keep running. in the while loop, i have it check for whether or not the player is dead. if the player is dead, it restarts the game (so to speak).

The problem I have is that when I multi-thread the combat, and the player dies, the primary thread is still waiting for input from the player before it loops back around and realizes the player is dead.

Ill link the code below if you want to see it.

The question i have is this:

How can i make the game do the restart when the player dies from combat, without waiting for input?

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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// NEW_MUD_PROJECT.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include"database.h"
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<algorithm>
#include<iterator>
#include<locale>
#include<sstream>
#include "windows.h"
#include "process.h"

using namespace MUD;

void initCombat(void* mob);

void test_game()
{
	Room* startingRoom=new Room("The starting Room","This is the starting room\nThere are five exits: North, South, East, West,and Northeast\n",false);
	Room* northRoom=new Room("North Room","This is the room north of the starting room\nThere are two exits: South and East\n",false);
	Room* southRoom=new Room("South Room","This is the room south of the starting room\nThere are two exits: North and South\n",false);
	Room* eastRoom=new Room("East Room","This is the room east of the starting room\nThere are two exits: North and West\n",false);
	Room* westRoom=new Room("West Room","This is the room west of the starting room\nThere is one exit: East\n",false);
	Room* northeastRoom=new Room("Northeast Room","This is the room to the northeast of the starting room\nThere are three exits: South, West, and Southwest\n",false);
	Room* trapRoom=new Room("Trap Room","This room is a trap\nThere is one exit: North\n",true);

	trapRoom->setTrapDesc("\nYou hear a faint click as you enter the room.\n\nA solid rock door slams shut behind you, leaving you trapped.\n\nSuddenly spikes spring from the floor and ceiling.\n\nYou feel them piercing your body everywhere, and then...black.\n\n");

	startingRoom->link("n",northRoom);
	startingRoom->link("s",southRoom);
	startingRoom->link("e",eastRoom);
	startingRoom->link("w",westRoom);
	startingRoom->link("ne",northeastRoom);
	northRoom->link("s",startingRoom);
	northRoom->link("e",northeastRoom);
	southRoom->link("n",startingRoom);
	southRoom->link("s",trapRoom);
	eastRoom->link("n",northeastRoom);
	eastRoom->link("w",startingRoom);
	westRoom->link("e",startingRoom);
	northeastRoom->link("s",eastRoom);
	northeastRoom->link("w",northRoom);
	northeastRoom->link("sw",startingRoom);
	trapRoom->link("n",southRoom);

	Item* testSword=new Item("Test Sword",Item::WEAPON,5,10,3,10,50);
	Item* testShield=new Item("Test Shield",Item::SHIELD,3,5,2,12,150);
	Item* testHead=new Item("Test Head",Item::HEAD,4,5,0,15,200);
	Item* testChest=new Item("Test Chest",Item::CHEST,1,3,0,20,250);
	Item* testHerb=new Item("Test Herb",Item::HEALTH,0,0,0,0,1000);
	Item* testSuperShield=new Item("super shield",Item::SHIELD,0,0,0,1000,1000);


	Mob* testPanther=new Mob("panther",Mob::ANIMAL,50,25,30,0,1.5);
	Mob* testMan=new Mob("ruffian",Mob::HUMANOID,25,30,35,0,1);
	Mob* testDragon=new Mob("baby dragon",Mob::MONSTER,75,75,80,0,1.25);

	startingRoom->setItem(testHead);
	startingRoom->setItem(testChest);
	startingRoom->setItem(testHerb);
	northRoom->setItem(testSword);
	northRoom->setItem(testShield);
	eastRoom->setItem(testSuperShield);

	westRoom->setMob(testPanther);
	westRoom->setMob(testMan);
	westRoom->setMob(testDragon);
	
	Dungeon* dungeon=new Dungeon(2,5);

	dungeon->addRoom(startingRoom);
	dungeon->addRoom(northRoom);
	dungeon->addRoom(southRoom);
	dungeon->addRoom(eastRoom);
	dungeon->addRoom(westRoom);
	
	//Create a filter
	Filter filter;
	//Create a dispatch object with a command string and a value string
	Dispatch dispatch;
	string command;
	string value;

	//Simulate a new game
	//Create a new player and add to the dungeon
	string input;
	cout<<"		       .__                               \n";
	cout<<"	__  _  __ ____ |  |   ____  ____   _____   ____  \n";
	cout<<"	| |/ |/ // __ ||  | _/ ___|/  _ | /     |_/ __ | \n";
	cout<<"	 |     /|  ___/|  |_|  |__(  <_> )  Y Y  |  ___/ \n";
	cout<<"	  |/|_/  |___  >____/|___  >____/|__|_|  /|___  >\n";
	cout<<endl;
	cout<<"Name: ";
	getline(cin,input,'\n');
	
	Player* one=new Player(input,startingRoom);
	cout<<dungeon->addPlayer(one)<<endl;

	cout<<startingRoom->getDesc();
	cout<<startingRoom->getItemList();
	cout<<">";
	getline(cin,input,'\n');

	//Start the game loop
	while(input!="quit")
	{
		//Pass the users message through the filter
		if(filter.validateMessage(input))
		{
			//Split the users message into the command and the value
			command=dispatch.extractCommand(input);
			value=dispatch.extractValue(input);

			//Match the command and forward the value
			if(command=="n" || command=="north")
			{
				cout<<one->move("n");
			}
			if(command=="s" || command=="south")
			{
				cout<<one->move("s");
			}
			if(command=="e" || command=="east")
			{
				cout<<one->move("e");
			}
			if(command=="w" || command=="west")
			{
				cout<<one->move("w");
			}
			if(command=="ne" || command=="northeast")
			{
				cout<<one->move("ne");
			}
			if(command=="nw" || command=="northwest")
			{
				cout<<one->move("nw");
			}
			if(command=="se" || command=="southeast")
			{
				cout<<one->move("se");
			}
			if(command=="sw" || command=="southwest")
			{
				cout<<one->move("sw");
			}
			if(command=="show")
			{
				if(value=="inventory")
				{
					cout<<"---------\n";
					cout<<"INVENTORY\n";
					cout<<"---------\n";
					cout<<one->getInv();
				}
				else if(value=="stats")
				{
					cout<<"---------\n";
					cout<<"STATS\n";
					cout<<"---------\n";
					cout<<one->showStats();
				}
				else if(value=="equip")
				{
					cout<<"---------\n";
					cout<<"EQUIP\n";
					cout<<"---------\n";
					cout<<one->showEquip();
				}
				else if(value=="score")
				{
					cout<<"---------\n";
					cout<<"INVENTORY\n";
					cout<<"---------\n";
					cout<<one->getInv();
					cout<<"---------\n";
					cout<<"EQUIP\n";
					cout<<"---------\n";
					cout<<one->showEquip();
					cout<<"---------\n";
					cout<<"STATS\n";
					cout<<"---------\n";
					cout<<one->showStats();
				}
				else
				{
					cout<<"The 'show' command needs to have a valid argument\n";
				}
			}
			if(command=="take")
			{
				cout<<one->take(value);
			}
			if(command=="equip")
			{
				cout<<one->equip(value);
			}
			if(command=="unequip")
			{
				cout<<one->unequip(value);
			}
			if(command=="drop")
			{
				cout<<one->drop(value);
			}
			if(command=="l" || command=="look")
			{
				cout<<one->look();
			}
			if(command=="k" || command=="kill")
			{
				if(one->mLocation->getMob(value)!=NULL)
				{
					Combatants* combat=new Combatants(one,one->mLocation->getMob(value));
					HANDLE hThread1=(HANDLE)_beginthread(Combatants::initCombatPlayer,0,combat);
					HANDLE hThread2=(HANDLE)_beginthread(Combatants::initCombatMob,0,combat);
				}
				else
				{
					cout<<"There is not a "+value+" here\n";
				}
			}
		}
		else
		{
			cout<<"What?\n";
		}

		if(one->death())
		{
			cout<<one->getName()+" was slain!\n\n";
			cout<<"Name: ";
			getline(cin,input,'\n');

			one->setName(input);
			one->resetStats();
			one->mLocation=startingRoom;

			cout<<startingRoom->getDesc();
			cout<<startingRoom->getItemList();
		}
		//Get the next command
		cout<<">";
		getline(cin,input,'\n');
	}

	dungeon->removePlayer(one->getName());
}

int main()
{
	test_game();
	return 0;
}




Last edited on
the code compiles without errors, and runs great.

I have tried nesting "while()" loops, but however I do it, it doesn't work as intended.
The only thing i can think of is multi-threading a check-death function, but then i don't know how to make the "check-death" thread tell the primary thread to restart the game.

Any suggestions would be appreciated, thanks in advance!
bumpity bump
Topic archived. No new replies allowed.