-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
159 lines (128 loc) · 4.89 KB
/
main.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
/*
* File: Main.cpp
* Author: RAJIB AHMED - raae10
* Author: MOHAMMAD ABDUR RAZZAK - morc10
*
* Created on November 15, 2011, 10:52 AM
*/
#include <iostream>
#include "Parser.h"
#include "Player.h"
#include "Element.h"
#include "Node.h"
#include "Socket.h"
#include <cstdlib>
#include <cctype>
#include <signal.h>
using namespace std;
#define HOST "localhost"
#define PORT 3333
int exit_with_error(string msg){
cout << msg << endl;
return EXIT_FAILURE;
}
int main() {
string
advent_350 ="\n\n\nwelcome to ...\n";
advent_350 +=" _ __ _ _ ___ _ _ ___ _ _ ___ ___ _____ _\n";
advent_350+=" / \\| \\| | | __| \\| |_ _| | | o \\ __| |_ / _// \\ \n";
advent_350+="| o | o ) V | _|| \\\\ || || U | / _| __)\\_ \\ 0 |\n";
advent_350+="|_n_|__/ \\_/|___|_|\\_||_||___|_|\\\\___| \\__/__/\\_/\n";
advent_350+="\n Rajib (raae10) -n- Razzak (morc10)\n";
cout << advent_350 <<endl;
Parser game("advent.dat");
string client_said="";
Socket server;
int client_count = 0;
if(!server.create()) return exit_with_error("Error creating ");
if(!server.bind(PORT)) return exit_with_error("Error binding ");
if(!server.listen()) return exit_with_error("Error listenting");
while(true){
Socket client;
client.create();
if(server.accept(client)){
int pid = fork();
if( pid > 0 ){
Player player("Rajib Ahmed", 0 , 1);
map<string, int>::iterator it;
string words[3];
bool show_item= true;
client.send(advent_350 + "\n");
for(;;){//game loop
//this is a socket message
client.send(game.locations[player.get_current_location()]->get_clue() + "\n");
player.set_location(game.locations[player.get_current_location()]);
/*
* check for items in current player location
*/
int numer_of_items = player.get_location()->items.size();
int items_carried = player.carrying.size();
if(numer_of_items > 0){
for (int i = 0; i < numer_of_items; i++) {
for(int j=0; j< items_carried ; j++){
if(player.carrying[j]->id == player.get_location()->items[i]->id){
show_item=false; break;
}
}
if(show_item){
client.send("=> " + player.get_location()->items[i]->messages[0] + "\n" ) ;
}
show_item = true;
}
}
/*
* User input handling & formatting
*/
client.send("~> ") ;//REPL sign :)
//getline(cin,player.verb);
client.recv(player.verb);
player.format_answer();
//player.carrying.push_back(game.elements[110]);
//cout << player.carrying[0]->description << endl;
game.start_idx=0;
for(int i=0; i<3 ;i++){
words[i] = game.tokenizer(player.verb," ");
}
!words[0].empty() ? player.verb = words[0] : "";
!words[1].empty() ? player.noun = words[1] : "";
if(player.verb == "QUIT"){
client.close();
break;
}
/*
* player wants to take Elements
*/
if(!player.verb.empty() && !player.noun.empty()){
it = game.vocabulary.find(player.verb);
if(it!=game.vocabulary.end()){
if(game.is_action(player.verb) && game.is_object(player.noun)){
//cout << "action verb and object sequence" << endl;
int element_id = game.vocabulary.find(player.noun)->second;
player.carry_item(element_id);
}
}
/*
* player want to only move
*/
}else if(!player.verb.empty()){
it = game.vocabulary.find(player.verb);
if(it!=game.vocabulary.end()){
if(game.is_motion(player.verb)){
player.moves = game.vocabulary.find(player.verb)->second;
//cout << player.moves << endl;
player.go_to_next_location();
if(player.message_id>0){
client.send("*=> "+ game.abbr_messages[player.message_id] + "\n");
player.message_id=0;
}
}
}else{
cout << "dont understand that" << endl;
}
}
}//game loop
}//endif fork
}//client accept
}//socket while
return EXIT_SUCCESS;
}