473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Hunt the Wumpus

3
i'm trying to write the hunt the wumpus game with simple conditions: (check-marked the ones i already accomplished)
[x]the pits will be placed randomly
[x] the wumpus is placed in the maze randomly (not sure if i did that one correctly)
[x] the player starts on the lower left of the maze row 3 col 0
-the player will only have 1 chance to shoot the Wumpus
[x]if the player is close to the Wumpus then there would be a prompt saying there is stench around
[x]if the player is close to a pit then she/he will be prompted that there is a breeze.
-if the player falls in a pit or gets eaten by the wumpus the game will be over and the map of the maze will be shown. I used 0 for empty cells and p for pits and w for wumpus in the array.

There is some problem with the program i've written so far but i don't know how to fix it. also, i don't know how to check if the wumpus will be in the direction the player wants to shoot. i'd appreciate any suggestions

Expand|Select|Wrap|Line Numbers
  1.  -java
  2.  
  3. import java.util.*;
  4. public class playWumpus 
  5. {
  6.     //initMaze METHOD
  7.     //put the pits and wumpus into the world
  8.     public static void initMaze(char [][] maze)
  9.     {
  10.         Random r = new Random();
  11.  
  12.         for (int row = 0; row <4; row ++)
  13.         {
  14.             maze[row][col] = 'O';    <<<<<<there is a problem here
  15.         }
  16.         int pitRow, pitCol;
  17.         for (int pitNum = 0; pitNum <4; pitNum++)
  18.         {
  19.             pitRow = r.nextInt(4); //randomly choose row
  20.             pitCol = r.nextInt(6); //randomly choose column
  21.             if (pitRow == 3 && pitCol == 0)//start location
  22.             {    
  23.                 pitCol=5;
  24.             }
  25.         }
  26.         maze [pitRow][pitCol] = 'p'; //places the pits in the maze randomly
  27.         maze [r.nextInt(4)][r.nextInt(6)] = 'w'; //places the wumpus randomly in the maze
  28.     }// end of Maze method
  29.  
  30.  
  31.     //CHECKMOVE method
  32.     //possible outcomes: run into the wall , fall into the pit, eaten by wumpus,
  33.     //feel breeze, smell stench, nothing. 
  34.     public static int checkMove(char [] [] maze, int newRow, int newCol)
  35.     {
  36.         //checks if they run into a wall
  37.         if (newRow<0 || newRow>3 || newCol<0 || newCol>5)
  38.         {
  39.             System.out.println("You hit a wall");
  40.             return 0;
  41.             // it will return to the main method and places 0 in the state variable
  42.         }
  43.         else if (maze[newRow][newCol] == 'p')  
  44.         //this checks the maze if there is a P in that location
  45.         {
  46.             System.out.println("You fall into a pit");
  47.             return 2;
  48.             // it will return to the main method and places 2 in the state variable
  49.         }
  50.         else if (maze[newRow][newCol] == 'w')//checks for the wumpus
  51.         {
  52.             System.out.println("You've eaten by the Wumpus!!");
  53.         }
  54.         //checks for the breeze (right,left,down,up)   //<<is the following if-statement correct?>>>
  55.         if (
  56.             (newCol>0 && maze[newRow][newCol-1] == 'p') || 
  57.             (newCol<5 && maze[newRow][newCol+1] =='p') ||
  58.             (newRow>0 && maze[newCol][newRow-1] == 'p') ||
  59.             (newRow<3 && maze[newCol][newRow+1] =='p')
  60.             )
  61.         {
  62.             System.out.println("You feel a breeze");
  63.         }
  64.         return 1;
  65.  
  66.     }//end of the maze method
  67.  
  68.     public static void main(String [ ] args)
  69.     {
  70.         char [ ] [ ] maze = new char [4][6]; //the actual map of the game
  71.         int playerRow=3, playerCol=0;  // player location aka lat/long
  72.         Scanner in= new Scanner(System.in);   //<<there is something wrong with my scanner>>>
  73.         int move, state;
  74.         // state of the game
  75.         // state   0= illegal move, 1= legal move, 3= end game
  76.         initMaze (maze); // calling the initMaze method
  77.         do
  78.         {
  79.             System.out.println("What would you like to do? 1=up, 2=down, 3=right 4=left, 5=shoot");
  80.             move = in.nextInt(System.in);   //<<for some reason eclipse is telling me that the type is incorrect>>
  81.             if (move ==1) // move up in the world
  82.             {
  83.                 state = checkMove(maze, playerRow-1,playerCol); //these are coordinates
  84.                 if ( state >0 ) // legal move
  85.                 {
  86.                     playerRow = playerRow-1;
  87.                 }
  88.             }
  89.             if (move ==2) // move down in the world
  90.             {
  91.                 state = checkMove(maze, playerRow+1,playerCol); 
  92.                 if ( state >0 ) // legal move
  93.                 {
  94.                     playerRow = playerRow+1;
  95.                 }
  96.             }
  97.             if (move ==3) // move right in the world
  98.             {
  99.                 state = checkMove(maze, playerRow,playerCol+1); 
  100.                 if ( state >0 ) // legal move
  101.                 {
  102.                     playerCol = playerCol+1;
  103.                 }
  104.             }
  105.             if (move ==4) // move left in the world
  106.             {
  107.                 state = checkMove(maze, playerRow,playerCol-1); 
  108.                 if ( state >0 ) // legal move
  109.                 {
  110.                     playerRow = playerCol-1;
  111.                 }
  112.             }
  113.             if (move == 5) // shoot the wumpus in the world
  114.             { 
  115.                 System.out.println("Which direction would you like to shoot? 1=up, 2=down, 3=right, 4=left");
  116.                 int shootDir = in.nextInt(System.in);
  117.  
  118.                 // check if the wumpus gets killed
  119.                 if (  shootDir == 1 )
  120.                 {
  121.                     System.out.println("You shot the Wumpus!");
  122.                 }
  123.  
  124.             }
  125.         }while (state!=2);
  126.     }//end of main
  127.  
  128. }//end of class
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
Nov 28 '07 #1
1 6593
yuki
3
i already fixed this part:

for (int row = 0; row <4; row ++)
{
for (int col = 0; col <6; col ++)
{
maze[row][col] = '0';
}
}



and this part:

move = in.nextInt(System.in);

to this:

move = in.nextInt();


i just need to find out how to check where the wumpus is and if that is the direction the player is shooting.
Nov 29 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Andrew | last post by:
I need a java english dictionary api, and it needs the following features, in order of importance: To verify given words (duh). To select a random word based on length. To rearrange a...
7
by: jimspace | last post by:
Can someone look at each of the classes below and tell me where the subtle bugs are? Thanks. Handle/Body idiom class A { private: int i; public:
10
by: connyledin | last post by:
Im trying to create a version of the game Wumpus. Mine is called Belzebub. But im STUCK! And its due tuesday 2 maj. Im panicing! Can some one help me?? here is the file:...
1
by: Bill | last post by:
Hi all, I've got my self a bug that I am out of ideas on how to fix... A section of my program launches a thread to handle a FIFO work buffer, I make the fifo as a single link list, the head...
2
by: spenn | last post by:
I'm VERY new to VB and have an assignment that seems to jump from simple to complex in one leap. The assignment goes like this: access multiple files, search for a specific string, extract the...
3
by: Mandla Mzimba | last post by:
I want to create a game where by you dispaly a moving bee and you use a mouse cursor to hit the moving image and the program must display the number of appearences, misses, hits. Thanks
10
by: sugumar.chat | last post by:
hi..everybody. currently one of our client COVANSYS-CSC Company is looking for C++ team & tech lead ...by the way job location is in chennai..if interested with this opening send your updated...
4
by: Noah Roberts | last post by:
Is there anything that will hunt down c-style casts and replace them with the appropriate new-style cast?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.