WUMPUS
GAME SIMULATION THAT USES HEURISTIC ANALYSIS
DESIGNED & CODED BY FRED FINKELSTEIN, 2019
WRITTEN IN "STANDARD C"
GAME SIMULATION THAT USES HEURISTIC ANALYSIS
DESIGNED & CODED BY FRED FINKELSTEIN, 2019
WRITTEN IN "STANDARD C"
/*
PURPOSE: All Explorer Functions
1. To simplify all the coding in Wumpus and just let Wumpus make all calls
2. Explorer enters the map always at 1,1 upper left corner
3. Needs to have the ability to move U,D,L,R
4. Needs to decide if he stepped into a dangerous square
4.1 'w' is for Wind, indicates he is adjacent to a pit
4.2 'g' is for glitter means he is adjacent to GOLD
4.3 's' is for stink means he is adjacent to the WUMPUS i.e. 'U'
4.4 '.' is for an ordinary square, no danger
5. Explorer needs to keep track of where he has been by assigning 'b' to his map
6. Explorer logic (courage or caution):
6.1 If explorer enters a 'w' he should back-out
6.1.1 The code over-writes 'g' with 'w'
6.2 If explorer enters a 'g' he should advance
6.3 If explorer enters a 's' he should back-out
6.3.1 The code over-writes 'g' with 's'
6.4 Since 'g' is 'blinded' by 's' and 'w', explorer should back-out
6.5 Once, back-out, explorer needs to choose from the other 2 options,
either U,D,L,R, which ever he has not chosen prior. If all 3 choices are
either 's' or 'w' then he should back-out again, and try the other 2 choices.
6.6 Repeat as needed, test to see if this logic is promising
6.7 NOTE: Advancing once you entered 's' or 'w' has HIGH RISK of DEATH.
Though the risk might be adjacent, its too RISKY to take a chance,
especially since explorer cannot see into adjacent squares until he
enters them. If explorer enters 'U' or 'P' he will DIE.
6.8 Do NOT change 'w' or 's' to 'b' on explorer map, only change
'.' to 'b' on explorer map. These 'b' will be used to ESCAPE
once he has the Gold. 'g' & 'G' must be recorded on explorer_map
6.9 Explorer can see all the 'b' on explorer map and must use this to
make quick escape to the exit point; explorer_map[1][1]; Use
current_loc[i][j] - explorer_map[1][1] to find shortest distance to ESCAPE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "cave_build.h"
#include "explorer.h"
int main(void)
{
int response, exited, cave_size; // flag up on all calls
printf ("Welcome to WUMPUS A.I. \n\n", response);
// ---------- call to cave builder-----------
response = cave_build(); // call to build cave and passes back the cave size (n)
if (response > 0)
{
printf ("Cave_Build SUCCESS %i: \n", response); // cave was built
}
else
{
printf ("Cave_Build FAILURE %i: \n", response);
};// end if
//----------call to explorer -----------
cave_size = response;
exited = actions(cave_size); // call to build explorer and passes cave size
if (exited > 0)// explorer found gold and exited cave
{
printf ("Explorer SUCCESS %i: \n", response); // we have gold
}
else
{
printf ("Explorer FAILURE %i: \n", response);// was killed by a pit or the wumpus
// or the program is un-responsive
};// end if
printf("This is the END of the program, Press 1 and ENTER to quit : \n");
scanf("%i", response);
return 0;
}//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdlib.h>
#include <ctime.h>
char cave_array[100][100]; // 961 squares means 31x31, though the memory is 100x100
int pit_locations[100]; // 100 maximum pits
int random_locator[10]; // stores the locations for the wumpus and the gold
int s,t,e,f,i,j,k, temp, counter; // just a couple counter for the for loops & sort
int * ptr_data;
// create memory allocation calloc() here
int cave_build(void)
{
int n; // cave dimension
int m; // just a value
int randlower;
int randupper;
int pits;
int gold_loc[1][1]; // number where the wumpus lives
int wumpus_loc[1][1]; // number where the gold is
int cave_size; // MAX squares in the guest cave
/*printf ("Caves in this WUMPUS simulation are always Square\n");
printf ("The maximum number of squares within the cave is 1,000\n");
printf ("Approximately 31 x 31 vertically & horizontally\n");
printf ("A good size is 144 squares ~12 x 12, having 4 quadrants\n");
printf ("As in Algebra, quadrants are numbered counter-clockwise\n");
printf ("NOTE: The simulation will put the WUMPUS atleast 3\n");
printf (" squares away from the entrance [LOC: 1,1]\n\n"); */
do { // loop to check cave size within boundary
printf ("Enter the cave size between 12 and 31 : ");
scanf ("%i", &n); // cave size input from guest
}while (n < 12 || n > 31); // boundary checking
cave_size = n * n; // cave MAX squares < 961 = 31 x 31
printf ("Cave width will be : %i\n", n);
printf ("Cave height will be : %i\n", n);// cave size looks good
printf ("Cave squares are : %i\n", cave_size);// cave dimensions
pits = cave_size * 8/100;// arbitrary number of pits
printf ("Cave Pits will be : %i \n", pits);// number of pits
// --- start of populating the pits
randlower = cave_size *1/10;
randupper = cave_size * 95 / 100;
printf("Rand Lower : %i\n", randlower);
printf("Rand Upper : %i\n", randupper);
srand(time(0));
// computes all the pits
for (i=1; i<=pits; i++)
{//
pit_locations[i] = (rand() % (randupper-randlower+1))+randlower;
//printf("Pit Locations : %i \n", pit_locations[i]);
}// end for pit creator
printf("\n");
// sort the pits array
for (i=1; i<=pits; i++)
{
for (j=1; j<=pits-1; j++)
{
if(pit_locations[j] == pit_locations[j+1])
{
pit_locations[j+1] = pit_locations[j+1]+1;
}// this code tries to eliminate duplicates in the random selection
if(pit_locations[j] > pit_locations[j+1])
{
temp = pit_locations[j];
pit_locations[j] = pit_locations[j+1];
pit_locations[j+1] = temp;
}//simply sorts the elements bubble sort style
}// end of j sort
}// end of i sort
for (i=1; i<=pits; i++)
{
//printf("%i ",pit_locations[i]);
}//
printf("\n");
// ---- end of populating the pits
printf("Wumpus Cave Being Designed \n\n");
counter = 1; // counts to find the location of the pits
k = 1; // counter for pits_locations[x];
// first build of the cave from empty array
for (i=1; i<=n; i++) // this the row counter (top down)
{
for (j=1; j<=n; j++) // used n twice because all caves are squares
{
if (i==1 && j==1)// this condition is only for the corner [1][1];
{//
cave_array[i][j] = 'E';
}// end if
else // these conditions are for all the rest of the cave_array[x][y];
{//
cave_array[i][j] = '.'; // first just paint '.' in the cave square
//printf("%i ", counter);
//printf("%i \n", pit_locations[k]);
//printf(" paint P \n");
if (counter == pit_locations[k]) // if the counter is the same
// as the location of the pit, then
// put a P for pit in the cave_array at i,j;
{//
cave_array[i][j] = 'P';// later on update the square with a pit
//printf("%i,%i,%i ",counter,pit_locations[k], k);
k++; // index to pointer to the next place in pit_locations[x];
//printf("\n");
}// end if
}// end else
counter++;// counts through the whole cave_size
//printf("%i",counter);
}// end of for j for loop
// prints built cave with pits only
}// end for - this will display the initial cave on the screen as a starting point
// ------------------------------------
printf("Press 1 and ENTER : ");
scanf("%i",&m);
//------------------------------------
printf ("\n");
printf("--- PITS SHOWN ONLY ---");
printf("\n");
// print whole cave with pits, wind and explorer
for (i=1; i<=n; i++)
{
for (j=1; j<=n; j++)
{
printf("%c",cave_array[i][j]);
}// end for j
printf("\n");
}// end for i
printf(" \n");
// add in the wind
counter=1; // reset counter for this loop
for (i=1; i<=n; i++)
{
for (j=1; j<=n; j++)
{
if (cave_array[i][j]=='P' && cave_array[i][j-1] == '.') // to the left
{
cave_array[i][j-1]='w';
//printf("[");
//printf ("%i",i);
//printf("]");
//printf("[");
//printf ("%i",j);
//printf("L \n");
}//
if (cave_array[i][j]=='P' && cave_array[i][j+1] == '.')// to the right
{
cave_array[i][j+1]='w';
//printf("[");
//printf ("%i",i);
//printf("]");
//printf("[");
//printf ("%i",j);
//printf("R \n");
}//
if (cave_array[i][j]=='P' && cave_array[i-1][j] == '.')// above
{
cave_array[i-1][j]='w';
//printf("[");
//printf ("%i",i);
//printf("]");
//printf("[");
//printf ("%i",j);
//printf("U \n");
}//
if (cave_array[i][j]=='P' && cave_array[i+1][j] == '.')// below
{
cave_array[i+1][j]='w';
//printf("[");
//printf ("%i",i);
//printf("]");
//printf("[");
//printf ("%i",j);
//printf("D \n");
}//
counter++;
//printf("%i ", counter);
}// end for j
//printf("\n");
}// end for i
//-------------------------------------WUMPUS-----------
randlower = cave_size / 2;
randupper = cave_size * 3 / 4;
printf("Wumpus Rand Lower : %i\n", randlower);
printf("Wumpus Rand Upper : %i\n", randupper);
srand(time(0));
// computes Wumpus_Loc
m = (rand() % (randupper-randlower+1))+randlower; // gets a random number for the wumpus
printf("Wumpus will be initially located at position : %i \n",m);
e = m / n;
f = m % n;
while ( cave_array[e][f] != '.') // while Wumpus is available as '.'
{
m = (rand() % (randupper-randlower+1))+randlower; // gets a random number for the wumpus
printf("Wumpus will be moved to position : %i \n", m);
e = m / n;
f = m % n;
}// end while Wumpus
printf("Wumpus will be in ROW : %i\n", e);
printf("Wumpus will be in COL : %i\n", f);
m = n*e + f;
printf("Wumpus will be positioned a location %i : \n", m);
wumpus_loc[e][f] = 'U';
s = e;
t = f;
//----------------------------------------GOLD-----------
printf("\n\n");
randlower = cave_size * 3 / 4;
randupper = cave_size;
printf("Gold Rand Lower : %i\n", randlower);
printf("Gold Rand Upper : %i\n", randupper);
srand(time(0));
// computes Gold_Loc
m = (rand() % (randupper-randlower+1))+randlower; // gets a random number for the gold
printf("Gold will be initially located at position : %i \n", m);
e = m / n;
f = m % n;
while ( cave_array[e][f] != '.')// while Gold is available as '.'
{
m = (rand() % (randupper-randlower+1))+randlower; // gets a random number for the wumpus
printf("Gold will be moved to position : %i \n", m);
e = m / n;
f = m % n;
}// end while Gold
printf("Gold will be in ROW : %i\n", e);
printf("Gold will be in COL : %i\n", f);
m = n*e + f;
printf("GOLD will be positioned a location %i : \n", m);
printf("\n");
gold_loc[e][f] = 'G';
// ------------------------------------
printf("Press 1 and ENTER : ");
scanf("%i",&m);
//------------------------------------
printf ("\n");
printf("--- PITS and WIND SHOWN ONLY ---");
printf("\n");
// print whole cave with pits, wind and explorer
for (i=1; i<=n; i++)
{
for (j=1; j<=n; j++)
{
printf("%c",cave_array[i][j]);
}// end for j
printf("%c\n");
}// end for i
//printf("%i counter : \n ", counter);
printf("\n");
// ------- this section adds the stick to the WUMPUS ---------
cave_array[s][t] = 'U'; // transfer the WUMPUS data to cave_array[x][y];
if (cave_array[s][t-1] == '.')
{
cave_array[s][t-1]='s'; // means stink left
}// end stink left
if (cave_array[s][t+1] == '.')
{
cave_array[s][t+1]='s'; // means stink right
}// end stink right
if (cave_array[s-1][t] == '.')
{
cave_array[s-1][t]='s'; // means stink upper
}// end stink upper
if (cave_array[s+1][t] == '.')
{
cave_array[s+1][t]='s'; // means stink lower
}// end stink lower
//---------- this section adds the glitter to the GOLD ----------
cave_array[e][f] = 'G'; // transfer the GOLD data to cave_array[x][y];
if (cave_array[e][f-1] == '.')
{
cave_array[e][f-1]='g'; // means glitter left
}// end glitter left
if (cave_array[e][f+1] == '.')
{
cave_array[e][f+1]='g'; // means glitter right
}// end glitter right
if (cave_array[e-1][f] == '.')
{
cave_array[e-1][f]='g'; // means glitter upper
}// end glitter upper
if (cave_array[e+1][f] == '.')
{
cave_array[e+1][f]='g'; // means glitter lower
}// end glitter lower
// ------------------------------------
printf("Press 1 and ENTER : ");
scanf("%i",&m);
//------------------------------------
printf ("\n");
printf("--- ALL ELEMENTS SHOWN ---");
printf("\n");
// print whole cave with pits, wind and explorer
for (i=1; i<=n; i++)
{
for (j=1; j<=n; j++)
{
printf("%c",cave_array[i][j]);
}// end for j
printf("%c\n");
}// end for i
//printf("%i counter : \n ", counter);
printf("\n");
return n; // n > 0 means all is good
}//
#include <stdio.h>
#include <stdlib.h>
int i,j,k,m,s,t;
// create a struct of the explorer and populate explorer_map[][] with cave_array[][];
typedef struct Explorers // the explorer struct
{
int map_size;
int explorer_map[map_size][map_size]; // pass the cave size
// from cave builder so that
// explorer can build his map
int current_loc[s][t]; // need to know my coordinates in the map
int my_location; // need to know my location in the map
}; // end of Explorers struct
int actions(int n)
{
struct Explorers explorer;
explorer.map_size = n;
for (i=1; i<=n; i++) // initiate populating the explorers map, with cave_array[][];
{
for (j=1; j<=n; j++)
{
explorer.explorer_map [i][j] = cave_size[i][j];
}// end j for loop
}// end i for loop
return 1; // 0 means all is good
}// end of actions function
CONFIDENTIALITY / COPYRIGHT NOTICE: This site and my information and any attachments contains the PRIVILEGED AND CONFIDENTIAL INFORMATION of Fred Finkelstein & Irondesigner DBA / LLC mutually, Inc., its affiliated corporations or legal entities, and is intended only for the use of the individual(s) named above. If you are not the intended recipient of this e-mail or invited to this site, or the employee or agent responsible for delivering this to the intended recipient, you are hereby notified that any unlawful interception, dissemination, disclosure, printing or copying of this e-mail or site or any attachments is strictly prohibited under the Electronics Communication Privacy Act (ECPA), 18 USCA 2510, 18 USCA 2511, and any applicable laws. If you have received this e-mail or viewing this site in error, please delete or destroy it & close down, including all attachments or copies, and immediately notify us by e-mail at mechanicalengrg@gmail.com