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 5 of 5

Thread: Minesweeper in eclipse

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    2
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Minesweeper in eclipse

    Hi guys,

    I have just started learning java in Eclipse and was wondering about this:

    I have tried to make a minesweeper application (just threw myself into the deep end there) and have created a class that fills a [field_size][field_size][2] multidimensional array with:

    1st. 0's and 1's for no bombs and bombs, respectively,
    2nd. the number of mines surrounding each coördinate (1 to 8, 9 if its a mine itself) in that first part.

    All in one class file. I have never worked with projects that involved multiple class files yet. Now the thing is, I want to know how I can make this class that I have into a method that can provide another class with this randomly filled matrix. I suppose that if I am going to write the rest of the minesweeper app in the same class, and I have to run it again and again after clicking a box (no idea how to do that yet btw) it would create a new minefield every time, which would pretty much ruin the whole idea of the game.

    Code so far, which I am quite proud of:
    package part1;
     
    public class msw {
     
    	public static void main(String[] args) {
     
    		int field_size=10;
    		int numberofmines=10;
    		int[][][]field=new int[field_size][field_size][3];
    		//fill field with 0's
    		for(int i=0;i<field_size;i++)
    		{
    			for(int j=0;j<field_size;j++)
    			{
    				field[i][j][1]=0;
    			}
    		}
    		// pick mines
    		for (int i=0;i<numberofmines;i++)
    		{
    			int rx =(int)(Math.random()*field_size);
    			int ry =(int)(Math.random()*field_size);
    			if (field[rx][ry][1]==1)
    				i=i-1;
    			else
    			field[rx][ry][1]=1;
    		}
    		// print field[x][x][1] (for debugging purposes)
    		for (int i=0;i<field_size;i++)
    		{
    			for(int j=0;j<field_size;j++)
    			{
    				System.out.print(field[i][j][1]+" ");
    			}
    			System.out.println();
    		}
    		// fill field[x][x][2] with amount of mines surrounding a coordinate in field[x][x][1]
    		for(int i=0;i<field_size;i++)
    		{
    			for(int j=0;j<field_size;j++)
    			{
    				for(int k=-1;k<2;k++)
    				{
    					for(int l=-1;l<2;l++)
    					{
    						if (field[i][j][1]==1)
    							field[i][j][2]=9;
    						else
    						{
    							if (i+k<0)
    								k=k+1;
    							if (j+l<0)
    								l=l+1;
    							if (i+k>field_size)
    								k=2;
    							if (j+l>field_size)
    								l=2;
    							if (i+k<field_size&&j+l<field_size&&k<2&&l<2)
    							{	
    								if (field[i+k][j+l][1]==1)
    									field[i][j][2]=field[i][j][2]+1;
    							}			
    						}
    					}
    				}
    			}
    		}
    		// printing 2nd part of matrix
    		System.out.println();
    		for (int i=0;i<field_size;i++)
    		{
    			for(int j=0;j<field_size;j++)
    			{
    				System.out.print(field[i][j][2]+" ");
    			}
    			System.out.println();
    		}
    	}
    }

    Any help is most welcome.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Minesweeper in eclipse

    I have never worked with projects that involved multiple class files yet
    I noticed you have everything in your main. Have you worked with projects with multiple methods before?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    2
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Minesweeper in eclipse

    I don't know what a main is, and I have only vague ideas about what a method is. Would like to know what they are

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Minesweeper in eclipse

    vague ideas about what a method is.
    Take a look at the tutorial: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Minesweeper in eclipse

    And since you don't know what main() is, I suggest you start with the discussion that follows the HelloWorld tutorial.

Similar Threads

  1. Minesweeper Game Need Help!
    By Punkymedic in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 13th, 2013, 02:47 PM
  2. [SOLVED] Minesweeper Recursion Method Help
    By beansnbacon in forum What's Wrong With My Code?
    Replies: 12
    Last Post: April 4th, 2013, 07:15 AM
  3. GUI minesweeper help
    By naitomea4664 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 23rd, 2012, 08:33 PM
  4. Minesweeper java problem
    By bluez_exe in forum Paid Java Projects
    Replies: 1
    Last Post: March 3rd, 2010, 08:55 PM
  5. [SOLVED] minesweeper game creation problem
    By kaylors in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 27th, 2009, 04:06 PM

Tags for this Thread