Escolar Documentos
Profissional Documentos
Cultura Documentos
Read
Contribute
Search
Search or jump to �
FORUM CATEGORIES
laptop Hardware and Software
code Programming
live_tv Digital Media
local_cafe Community Center
ACTIVITIES
Donate with PayPal
forumLatest Posts
Member Top List
DaniWeb Ads
Topics Feed
Forum API Docs
DaniWeb Connect API
Terms of Service
Privacy Policy
Community Rules
About Us
Contact Us
� 2019 DaniWeb� LLC
Wumpus World
Home
Programming Forum
Software Development Forum
Discussion / Question
Member Avatar
coxxie
7 Years Ago
Need some help. My game locks up when you go to load file and it just locks up.
Please help.
/*
WumpusGame header file
Contains the WumpusGame Class. This is the main workhorse
of the program, and contains all of the global data,
including the master world map. WumpusGame also handles
the full user interface.
*/
#ifndef WUMPUS_H
#define WUMPUS_H
#include <iostream> //normal stream i/o (cout)
//#include <conio.h> //getch() and other dos console i/o
//not available under UNIX
#include <cstdio> //other IO routines
#include <fstream> //file i/o
#include <cstdlib> //atoi()
#include <cstring> //string
using namespace std;
#include "agent.h"
class WumpusGame
{
public:
void shell(); //outer control structure
void init(); //loads option file
void mainmenu();
void optionsmenu(); //select options
void agentmenu(); //select an agent
void filemenu(); //choose a new map file
void saveops(); //save current options to file
void playgame(int human); //play the game
void loadmap(); //loads the current map file into the game grid
void display(int); //GUI function (hahaha)
void makemap(); //inits the map to blank
void printline( int j, char sidebar [70]); //print one line of the map w/
sidebar
void printline( int j, char sidebar [70], int number); //overloaded to get
around itoa
void displayPercepts(); //throws down current percepts
int getinput(int &); //player input control
int autopilot(int & code); //agent input control
int gank(); //grabs the arrow or gold if there
int fire(); //fires off an arrow if we have one
int trajectory(); //actually moves the arrow around
int moveAgentForward(); //walk the agent forward if possible
void informAgent(); //tells the agent wazzup
void highscore (int current_score); //locks in a new high score
void savescores(); //writes the high scores back out
void loadscores(); //loads the high score list
void Help(); //displays the help screen
void closeout(); //exits the game
private:
char map [22][22]; //holds the game world (loaded from file)
Agent MrMan; //our adverturous agent
char map_file[9]; //current map file
int agent_x,
agent_y;
int agent_dir; //stores the agent's direction
// 1=N,2=E,3=S,4=W
//option variables - loaded from another file
int op_walls,
op_pits,
op_gold,
op_wumpus,
op_arrow_stay,
op_arrow_num,
op_move_limit;
int op_agent; //which agent we're going to use
int debug; //debug mode on or off
//holds the high score - yes this is kinda dumb, but why else have points?
int score_val;
char score_name[20];
int moves; //total # of moves taken
};
//#include "wumpus.cpp"
#endif
/*
WumpusGame Implementation file
See wumpus.h for information on WumpusGame.
*/
#include "wumpus.h"
void WumpusGame::shell()
{
init();
mainmenu();
closeout();
return;
}
void WumpusGame::init()
{
ifstream opsfile ("wumpus.dat", ios::in);
opsfile >> op_walls;
opsfile >> op_pits;
opsfile >> op_gold;
opsfile >> op_wumpus;
opsfile >> op_arrow_stay;
opsfile >> op_arrow_num;
opsfile >> op_move_limit;
opsfile >> op_agent;
opsfile.close();
strcpy_s(map_file,"default"); //set default map_file name
makemap(); //load in blank map (with walls)
agent_x=1; agent_y = 1; //set agent start point
agent_dir = 3; //agent starts in upper left, pointing South
//these are the defaults, retained for reference
/* op_walls = 1;
op_pits = 1;
op_gold = 1;
op_wumpus = 1;
op_arrow_stay = 1;
op_arrow_num = 5;
op_move_limit = 0;*/
debug = 0;
srand(13); //seed the random # gen with my lucky number...
loadscores(); //load the high score
}
void WumpusGame::makemap()
{
//loads in a totally blank map
int i,j;
for (i = 0; i< 22; i++)
{
for (j=0; j<22; j++)
{
map[i][j] = '.';
}
}
//walls down sides
i=0;
for (j=0; j<22; j++)
map[i][j] = '#';
i=21;
for (j=0; j<22; j++)
map[i][j] = '#';
//walls on top and bottom
j=0;
for (i=0; i<22; i++)
map[i][j] = '#';
j=21;
for (i=0; i<22; i++)
map[i][j] = '#';
return;
}
void WumpusGame::mainmenu()
{
int done = 0;
char choice;
while (!done)
{
//clear the screen here if we could
cout << "Welcome to: "<<endl;
cout << "Wumpus World!"<<endl;
cout << "Written by: James Cox, 2012"<<endl;
cout << endl;
cout<< "Current High Score:"<<endl;
cout << score_name << " = " << score_val<<endl;
cout << endl;
cout << "Choose an Option:"<<endl;
cout << "1 - Play Hunt the Wumpus" <<endl;
cout << "2 - Watch Current Agent play Hunt the Wumpus"<<endl;
cout << "3 - Options Menu"<<endl;
cout << "4 - Help"<<endl;
cout << "5 - Quit"<<endl;
cout << ">";
cin >> choice;
switch (choice)
{
case '1':
playgame(1);
break;
case '2':
playgame(0);
break;
case '3':
optionsmenu();
break;
case '4':
Help();
break;
case '5':
done = 1;
break;
default:
break;
}
}
//cout << "Thanks for playing!!"<< endl;
return;
}
void WumpusGame::optionsmenu()
{
//clear the screen here too
char choice;
int done = 0;
int new_num;
while (!done)
{
//print menu screen
cout << "Options Menu"<<endl;
case '43':
if (op_gold == 1)
op_gold = 0;
else
op_gold = 1;
break;
case '4':
if (op_wumpus == 1)
op_wumpus = 0;
else
op_wumpus = 1;
break;
case '5':
if (op_arrow_stay == 1)
op_arrow_stay = 0;
else
op_arrow_stay = 1;
break;
case '6':
cout << "Enter new number of arrows: ";
cin >> new_num;
op_arrow_num = new_num;
break;
case '7':
cout << "Enter new move limit (0 for none): ";
cin >> new_num;
op_move_limit = new_num;
break;
case '8':
saveops();
break;
case '9':
done = 1;
break;
case 'a':
case 'A':
agentmenu();
break;
case 'm':
case 'M':
filemenu();
break;
default:
break;
}//end switch
} //end while loop
return;
}
void WumpusGame::agentmenu()
{
int choice;
cout << "Choose an Agent!" << endl;
cout << "1> Dummy" <<endl;
cout << "2> Cautious" <<endl;
cout << "3> Explorer" <<endl;
cout << endl<< " >";
cin >> choice;
switch (choice) //provides some error checking
{
case 1:
op_agent = 1;
break;
case 2:
op_agent = 2;
break;
case 3:
op_agent = 3;
break;
default:
break;
}
}
void WumpusGame::filemenu()
{
cout << endl;
cout << "Enter a new file name:"<<endl;
char newfile[30];
cin >> newfile;
char name[30];
strcpy_s(name,newfile);
strcat_s(name,".map");
ifstream infile ("wumpus.dat", ios::in );
if (!infile) //check for input file
{
cout << "Fatal Error: - Failed to open input file: "<< name << endl;
return; //exit if file does not exist.
}
strcpy_s(map_file,newfile);
return;
}
void WumpusGame::saveops()
{
ofstream opsfile ("wumpus.dat", ios::out );
opsfile << op_walls <<endl;
opsfile << op_pits <<endl;
opsfile << op_gold <<endl;
opsfile << op_wumpus <<endl;
opsfile << op_arrow_stay <<endl;
opsfile << op_arrow_num <<endl;
opsfile << op_move_limit <<endl;
opsfile << op_agent << endl;
opsfile.close();
cout << "Options Saved!"<<endl;
return;
}
void WumpusGame::playgame(int human)
{
//load map file
cout << "Loading Map File"<<endl;
loadmap();
//reset location and direction
agent_x = 1;
agent_y = 1;
agent_dir = 3; //south, remember?
moves = 0;
int done=0;
int code=0;
//if manual, spawn manual interface
if (human)
{
return;
}
void WumpusGame::loadmap()
{
//cout << "Loading game:"<<endl;
char * name;
name = new char[12];
strcpy(name,map_file);
strcat(name,".map");
ifstream infile ("wumpus.dat", ios::in );
if (!infile) //check for input file
{
cout << "Fatal Error: - Failed to open input file: "<< name << endl;
//getch();
return;//exit(-1); //exit if file does not exist.
}
int i,j;
char temp;
infile >> temp;
i=1; j=1;
while (temp != '~')
{
switch (temp)
{
case '.':
map[i][j]='.';
break;
case '#':
if (op_walls == 1)
map[i][j]='#';
else
map[i][j]='.';
break;
case 'p':
if (op_pits == 1)
map[i][j]='p';
else
map[i][j]='.';
break;
case 'g':
if (op_gold == 1)
map[i][j]='g';
else
map[i][j]='.';
break;
case 'w':
if (op_wumpus == 1)
map[i][j]='w';
else
map[i][j]='.';
break;
case 'a':
if (op_arrow_stay == 1)
map[i][j]='a';
else
map[i][j]='.';
break;
case ';':
i=0;
j++;
default:
break;
}//end switch
i++;
infile >> temp;
} //end while
infile.close();
cout << "Game Loaded!"<<endl;
//getch();
return;
}
void WumpusGame::display(int code)
{
char current_message[70];
//int plist[8];
//load the current player message based on the status code
switch (code)
{
case 1:
strcpy_s(current_message," Your Path is Blocked.");
break;
case 2:
strcpy_s(current_message," You cannot climb out here.");
break;
case 3:
strcpy_s(current_message," You climb out of the maze and escape.");
break;
case 10:
strcpy_s(current_message," There is nothing to grab here.");
break;
case 11:
strcpy_s(current_message," You grab the arrow!");
break;
case 12:
strcpy_s(current_message," You grab the gold! (ka-ching!)");
break;
case 20:
strcpy_s(current_message," You fire into the darkness.");
break;
case 21:
strcpy_s(current_message," You fire into the darkness - and hear a
SCREAM!.");
break;
case 22:
strcpy_s(current_message," You have no more arrows.");
break;
case 13:
strcpy_s(current_message, " Hey... I had some kinda error...");
break;
case 130:
strcpy_s(current_message, " What - you have something better to do?");
break;
case 131:
strcpy_s(current_message, " You have been eaten by the Wumpus!
(crunch)");
break;
case 132:
strcpy_s(current_message, " You have fallen down a bottomless pit!
(Ahhhh!)");
break;
case 136:
strcpy_s(current_message, " You have run out of moves!");
break;
default:
strcpy_s(current_message," ");
break;
}
char agent_message1[50];
char agent_message2[50];
char arrow_stat[15];
char score_stat[15];
char turn_stat[15];
// char temp[10];
if ( (MrMan.iamhuman() == 0) ) //if the robot is playing
{
strcpy_s(agent_message1, " Agent ");
strcat_s(agent_message1, MrMan.myName() );
strcat_s(agent_message1, " is playing.");
strcpy_s(agent_message2, " Press a Key to Advance");
}
else
{
strcpy_s(agent_message1, " ");
strcpy_s(agent_message2, " ");
}
strcpy_s(arrow_stat," Arrows Left: ");
//itoa(MrMan.myArrows(),temp,10);
//strcat(arrow_stat, temp);
strcpy_s(score_stat," Score: ");
//itoa(MrMan.myScore(),temp,10);
//strcat(score_stat, temp);
strcpy_s(turn_stat," Moves: ");
//itoa(moves,temp,10);
//strcat(turn_stat, temp);
//note to self - itoa is not avaialble under UNIX (argh)
//line 0:
//cout << "######################" << " Gabe's Amazing Wumpus Simulator" <<
endl;
printline(0," Wumpus World");
//note: the space needs to be the first character, since the string starts
immediately after the map
printline(1, agent_message1);
printline(2, agent_message2);
printline(3, " ");
printline(4, " Commands:");
printline(5, " ");
printline(6, " 8 - Advance");
printline(7, " 4 - Turn Left");
printline(8, " 6 - Turn Right");
printline(9, " C - Climb out of Maze");
printline(10, " G - Grab (gold or arrow)");
printline(11, " S - Shoot Arrow");
printline(12, " ");
printline(13, " Q or 5 - Quit");
printline(14, " H - Help");
printline(15, " ");
printline(16, turn_stat, moves);
printline(17, arrow_stat, MrMan.myArrows() );
printline(18, score_stat, MrMan.myScore() );
printline(19, " Status:");
printline(20, current_message);
//cout << "######################" << endl;
printline(21, " ");
{
int i;
char agent_map[22][22];
MrMan.getmap(agent_map); //yes, this really wastes memory space
for (i=0; i<22; i++)
{
if ( (i==agent_x) && (j==agent_y) )
{
switch (agent_dir)
{
case 1:
cout << '^';
break;
case 2:
cout << '>';
break;
case 3:
cout << 'v';
break;
case 4:
cout << '<';
break;
default:
cout << "X";
break;
}
}
else if (debug == 1)
cout << map[i][j];
else
cout << agent_map[i][j];
}
cout << sidebar;
cout << number;
cout << endl;
return;
}
void WumpusGame::displayPercepts()
{
int plist[8];
MrMan.getpercepts(plist);
cout << '\t';
if (plist[0] == 1)
cout << "BUMP, ";
else
cout << "bump, ";
if (plist[1] == 1)
cout << "BREEZE, ";
else
cout << "breeze, ";
if (plist[3] == 1)
cout << "GLITTER, ";
else
cout << "glitter, ";
if (plist[4] == 1)
cout << "SMELL, ";
else
cout << "smell, ";
if (plist[5] == 1)
cout << "ARROW, ";
else
cout << "arrow, ";
if (plist[6] == 1)
cout << "SCREAM, ";
else
cout << "scream, ";
if (plist[7] == 1)
cout << "HOME";
else
cout << "home";
cout <<endl;
//cout << '\t' << "bump, breeze, glitter, smell, arrow, scream, home" <<
endl;
}
int WumpusGame::getinput(int & code)
{
int err;
char first;
char in;
moves ++; //increment the move counter
if (op_move_limit > 0)
if (moves > op_move_limit)
{
code = 136;
return 1;
}
cin >> first;
//glitter
if (map[agent_x][agent_y] == 'g')
status[3] = 1;
//smell
if ( (N == 'w') || (E == 'w') || (S == 'w') || (W == 'w') )
status[4] =1;
//arrow
if (map[agent_x][agent_y] == 'a')
status[5] = 1;
MrMan.wazzup(status,agent_x,agent_y,agent_dir);
return;
}
void WumpusGame::highscore(int current_score)
{
//this would be a keen place to clrscr()
if (current_score < score_val)
return;
if (MrMan.iamhuman() == 1)
{
score_val = current_score;
cout << "You've gotten the new high score!"<<endl;
cout << "What's your name, adventurer? "<<endl;
cin >> score_name;
}
else
{
score_val = current_score;
cout << "Agent " << MrMan.myName() << " has the new high score!"<<endl;
strcpy_s(score_name,MrMan.myName() );
}
return;
}
void WumpusGame::savescores()
{
ofstream score_file ("wumpus.dat", ios::out );
score_file << score_name << endl;
score_file << score_val << endl;
score_file.close();
return;
}
void WumpusGame::loadscores()
{
ifstream score_file ("wumpus.dat", ios::in );
score_file >> score_name;
score_file >> score_val;
score_file.close();
return;
}
void WumpusGame::Help()
{
cout <<endl;
cout << "Wumpus World Help Screen"<<endl;
cout<< endl << "Well, the readme file is probably a better bet, actually."<<endl;
cout <<"Map Key:"<<endl;
cout << "^ > v < = Your Agent (facing the direction of the point)"<<endl;
cout << " = unknown square"<<endl;
cout << ".= visited and empty square"<<endl;
cout << ",= unvisited square, thought to be empty"<<endl;
cout << endl;
cout << "#= Wall"<<endl;
cout << "p= Confirmed pit"<<endl;
cout << "@= Unvisited, thought to be pit"<<endl;
cout << "b= Confirmed bat"<<endl;
cout << "*= Unvisited, thought to be bat"<<endl;
cout << "w= Confirmed Wumpus"<<endl;
cout << "!= Unvisited, thought to be Wumpus"<<endl;
cout << "a= Arrow"<<endl;
cout << "g= Gold"<<endl;
cout << endl;
cout << "Press a key to return"<<endl;
char temp;
cin >> temp;
return;
}
void WumpusGame::closeout()
{
savescores();
cout << "Thanks for playing!" <<endl;
//exit(0);
}
/*
Agent header file
The Agent Class handles everything the agent knows about the
world, whether the agent is the computer or a human player.
Agent also handles infering the location of hazards (in
updatemap() ) and the AI logic routines (in Action() )
*/
#ifndef AGENT_H
#define AGENT_H
#include <cstdlib>
using namespace std;
class Agent
{
private:
int human;
int agent_code;
int my_map[22][22];
int me_x,
me_y; //where the agent thinks it is
int me_dir; //the direction the agent thinks it's facing
//current percepts
/*int bump,
breeze,
squeak,
glitter,
smell,
arrow,
scream,
home;*/
int percepts[8];
int quiver; //current quiver count
int score; //oh yeah - it's points time
int shot; //remembers if we fired last turn
//attack conditions:
//smell it and sides clear (ie, facing wumpus and know it)
if ( (percepts[4] == 1) )
if ( (blocked(blockedSide('r')) != 1) &&
(blocked(blockedSide('l')) != 1) )
my_move = 's';
default:
my_move = 'q';
break;
}
return my_move;
}
int Agent::blocked(char temp)
//-1 - already visited '.'
//0 - empty and open ','
//1 - the wumpus
//2 - walls
//3 - pits
//4 - bats
{
//char temp;
//temp = nextSquare();
switch (temp)
{
case '#':
return 2;
break;
case 'b':
case '*':
return 4;
break;
case '@':
return 3;
break;
case '!':
return 1;
break;
case '.':
return -1;
break;
default:
return 0;
break;
}
}
char Agent::nextSquare()
{
char temp;
switch (me_dir)
{
case 1:
temp = my_map[me_x] [me_y-1];
break;
case 2:
temp = my_map[me_x+1] [me_y];
break;
case 3:
temp = my_map[me_x] [me_y+1];
break;
case 4:
temp = my_map[me_x-1] [me_y];
break;
}
return temp;
}
char Agent::blockedSide(char side) //side can be r or l - boy I'm sick of int
codes...
{
char right, left, N, S, E, W;
N = my_map[me_x] [me_y-1]; //get surrounding squares
E = my_map[me_x+1] [me_y];
S = my_map[me_x] [me_y+1];
W = my_map[me_x-1] [me_y];
switch (me_dir) //which direction we're facing
{
case 1: //N
right = E;
left = W;
break;
case 2: //E
right = S;
left = N;
break;
case 3://S
right = W;
left = E;
break;
case 4://W
right = N;
left = S;
break;
}
if (side == 'r')
return right;
else
return left;
}
/*
This is the main shell file of the progam.
All code and work in this program was designed and developed by
Gabriel Helman. Really. The AIMA code was fairly useless, so I
eneded up designing it all from scratch.
The C++ libraries included are listed at the top of wumpus.h,
along with why they were included.
*/
#include "wumpus.h"
void main()
{
WumpusGame OhSoFun;
OhSoFun.shell();
return;
}
Thanks in advance.
c++
3 Contributors
forum5 Replies
794 Views
2 Days Discussion Span
comment Latest Post 7 Years Ago by coxxie
Member Avatar
coxxie7 Years Ago
sorry i know its a lot of code. Put doing this for a class project and this is 50%
of my total grade.
Member Avatar
subith86 177 Years Ago
Dude, you are giving us a 500 line code and asking us to find the bug. Why can't
you debug a bit on your own and post only that code portion which is the reason for
your "lock up"?
Member Avatar
coxxie7 Years Ago
its not a compiler error. It is in the wumpus.cpp file. I don't think the file is
being created thus it can't open the saved options. Thats just a guess though.
Member Avatar
DJSAN10 287 Years Ago
Paste what output you are getting before problem occurs
Or what you could do is write a print statement in every function at the beginning
so you can atleast know which function is messed up
Related Topics
Member Avatar C/C++ Cross-compiling problems -- Using wrong compiler flags?
Member Avatar fatal error C1075: end of file found before the left brace '{' at
'c:\documents and s
Member Avatar Cannot find symbol
Member Avatar help with c program to count # of words in a string
Member Avatar Simple Hello World in Visual C++ Express
Member Avatar illegal start of expression
Member Avatar User Input: Strings and Numbers [C]
Member Avatar Cannot Compile Win32 Hello World
Member Avatar A few questions about C++
Member Avatar Need Help with this please anybody help me
Not what you need?
add_comment Reply to this Topic