Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Need help placing mines and another point on a field!!

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help placing mines and another point on a field!!

    i need to fill up 1/10th of the board with mines but i am only able to successfully put 1! also my goal is not showing up on the game even though i put it as an up arrow.

    package Homework4;
     
    import java.util.Random;
    import java.util.Scanner;
     
     
    public class HW4 {
     
    	//Creates a new type to be used to create the board
    	//We know that the board can only take on the value of Empty, Player, Walked_Path, and Goal
    	//Thus it is better to use an enum and define these types instead of using an integer or a string
    	//which would take up a lot of space in memory
    	enum Spaces {Empty, Player, Walked_Path, Goal, mine};
    	public static final int BOARD_SIZE = 10;
    	//
    	public static final double mine = (BOARD_SIZE*BOARD_SIZE)/10;
     
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		//Keep track of the number of moves made by the player
    		int numberOfMoves = 0;
    		//The player's location
    		int pX = 0;
    		int pY = 0;
    		//The target location
    		Random r = new Random();
    		int tX = r.nextInt(BOARD_SIZE);
    		int tY = r.nextInt(BOARD_SIZE);
    		//User input
    		Scanner keyboard = new Scanner(System.in);
    		//Set up the board
    		Spaces[][] board = new Spaces[BOARD_SIZE][BOARD_SIZE];
    		//Initialize the board
    		for(int y=0;y<board.length;y++)
    		{
    			for(int x=0;x<board[y].length;x++)
    			{
    				board[x][y] = Spaces.Empty;
    			}
    		}
    		//Put the user on the board
    		board[pX][pY] = Spaces.Player;
    		//Puts the goal on the board
    		board[tX][tY] = Spaces.Goal;
    		//puts the mines on the board
    		board[tX][tY] = Spaces.mine;
    		//Prompt the user
    		System.out.println("Welcome to catch me where you have to catch the enemy");
    		//Game over condition
    		boolean gameOver = false;
     
    		while(gameOver == false)
    		{
    			//Draw the board
    			for(int y=0;y<board.length;y++)
    			{
    				for(int x=0;x<board[y].length;x++)
    				{
    					switch(board[x][y])
    					{
    					case Empty:
    						System.out.print("_");
    						break;
    					case Player:
    						System.out.print("X");
    						break;
    					case Walked_Path:
    						System.out.print("#");
    						break;
    					case Goal:
    						System.out.print("^");
    						break;
    					case mine:
    						System.out.print("M");
    					break;
    					}
    				}
    				System.out.println(" ");
    			}
    			//Calculates the distance.  This doesn't use square root in order to save processing cycles
     
    			//The player moves
    			System.out.println("Enter either -1,0,1 to move in the x or 9 to quit");
    			//Movement in the X direction
    			int dX = keyboard.nextInt();
    			//Or quit
    			if(dX == 9)
    			{
    				System.out.println("Game over");
    				break;
    			}
     
    			System.out.println("Enter either -1,0,1 to move in the y");
    			//Movement in the y direction
    			int dY = keyboard.nextInt();
     
    			//Checks to see if the movement is valid
    			if(dX <-1 || dX>1)
    			{
    				System.out.println("Invalid input X");
    				dX = 0;
    			}
    			if(dY <-1 || dY>1)
    			{
    				System.out.println("Invalid input Y");
    				dY = 0;
    			}
     
    			//Sets the player position to a walked path
    			board[pX][pY] = Spaces.Walked_Path;
    			//Moves the player
    			pX+=dX;
    			pY+=dY;
     
    			//Makes sure everything is still in bounds
    			if(pX < 0)
    			{
    				pX = 0;
    			}
    			else if(pX>BOARD_SIZE-1)
    			{
    				pX = BOARD_SIZE-1;
    			}
    			if(pY < 0)
    			{
    				pY = 0;
    			}
    			else if(pY> BOARD_SIZE-1)
    			{
    				pY = BOARD_SIZE-1;
    			}
     
     
    			//Winning condition
    			if(board[pX][pY]==Spaces.mine)
    			{
    				System.out.println("You are dead!");
    				break;
    			}
    			if(board[pX][pY]==Spaces.Goal)
    			{
    				System.out.println("You win! The secret location was at "+tX+" "+tY);
    				System.out.println("It took "+numberOfMoves+" moves");
    				gameOver = true;
    			}
    			//Sets the player in the new position
    			board[pX][pY] = Spaces.Player;
    			numberOfMoves++;
    		}
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Need help placing mines and another point on a field!!

    Since the mines occupy 1/10 of the board, I would expect the appropriate number of mines to be determined randomly and added to the game board when the board is first created. I don't see where you've determined more than one mine.

    Give your variables better names: I'm not sure what pX/pY, tX/tY, etc. are supposed to be. NAME them.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help placing mines and another point on a field!!

    sorry i know my variables are a bit confusing but would i just have to randomize the number of mines? would i have to create a public static final int of 10? since the board is 10 by 10

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Need help placing mines and another point on a field!!

    Assuming the program is not required to be scalable, set the variable to a constant, whatever you wish. But if there's any chance the size of the game board will change, then calculate the number of mines based on the game board's size. Then when you build the game board, assign that number of locations randomly to mines. Choosing those locations in a grid without repeating will be an interesting exercise. Any ideas?

  5. #5
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help placing mines and another point on a field!!

    hmm im thinking....how would i assign the number of locations randomly to mines? thats what im having trouble with haha

  6. #6
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Need help placing mines and another point on a field!!

    Random (Java Platform SE 6)

    Produce random numbers, then assign the variables returned as you see fit.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Need help placing mines and another point on a field!!

    Quote Originally Posted by HappyCamper View Post
    hmm im thinking....how would i assign the number of locations randomly to mines? thats what im having trouble with haha
    For this specific game, there are a hundred locations, so you could randomly generate 10 unique numbers from 0 - 99 and then determine how to assign those numbers to locations on the game board. If you think about the for() loops used to iterate a 2D array, it might occur to you that there's a way to calculate a number in the range 0 - 99 from each game board's location using the row/column values for each.

Similar Threads

  1. [SOLVED] placing comma between strings in a line
    By shenaz in forum Java Theory & Questions
    Replies: 3
    Last Post: March 27th, 2013, 05:33 AM
  2. [SOLVED] Problem with placing text with JPanels
    By Artem.N in forum AWT / Java Swing
    Replies: 5
    Last Post: April 17th, 2012, 05:38 AM
  3. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM
  4. Placing files in jre bin...
    By sulaiman2043 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 29th, 2010, 02:55 AM
  5. [SOLVED] placing packages of the same directory into an array
    By javanub:( in forum Java Theory & Questions
    Replies: 16
    Last Post: May 18th, 2010, 07:49 AM