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

Thread: minesweeper game creation problem

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Location
    kolkata, India
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question minesweeper game creation problem

    okay so the last problem was solved. thanks to freakychris for that(although i did manage to figure it out before he replied, but still. so now i got a new problem. and if this one comes through, then finally my game will be complete and you guys can play it if you want. mind u its supposed to work exactly like windows minesweeper. only its not gui.

    import java.io.*;
    class minesweeper
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     
        char Mine[][]=new char[20][20];
        char Field[][]=new char[20][20];
     
        boolean cond=true;
     
        int x,y,r1,r2;
        char cho;
     
        void Miner()
        {
            for(int i=0;i<20;i++)
            {
                for(int j=0;j<20;j++)
                {
                    Field[i][j]='#';
                    double rand=Math.random()*100;
                    if(rand>r1 && rand<r2)
                        Mine[i][j]='@';
                    else
                        Mine[i][j]='#';
                    }
                }
            }
     
         void ValFiller()
         {
             for(int i=0;i<20;i++)
             {
                 for(int j=0;j<20;j++)
                 {
                     if(Mine[i][j]!='@')
                        Mine[i][j]=Char(Seeker(i,j));
                    }
                }
            }
     
         char Char(int g)
         {
             switch(g)
             {
                 case 1:
                 return '1';
                 case 2:
                 return '2';
                 case 3:
                 return '3';
                 case 4:
                 return '4';
                 case 5:
                 return '5';
                 case 6:
                 return '6';
                 case 7:
                 return '7';
                 case 8:
                 return '8';
                }
                return '0';
            }
     
         int Seeker(int i,int j)
         {
             int MineVal=0;
             for(int k=i-1;k<=i+1;k++)
             {
                 for(int l=j-1;l<=j+1;l++)
                 {
                     if(k>=0 && l>=0&& k<20 && l<20)
                     {
                        if(Mine[k][l]=='@')
                            MineVal++;
                        }
                    }
                }
                return MineVal;
            }
     
          void Exhibit()
          {
              System.out.println();
              for(int i=0;i<20;i++)
              {
                  for(int j=0;j<20;j++)
                  {
                      System.out.print(Field[i][j]+" ");
                    }
                    System.out.println();
                }
            }
     
          void ExhibitM()
          {
              System.out.println();
              for(int i=0;i<20;i++)
              {
                  for(int j=0;j<20;j++)
                  {
                      System.out.print(Mine[i][j]+" ");
                    }
                    System.out.println();
                }
            }  
     
          void Coords()throws IOException
          {
              System.out.println("Enter Co-ordinates:");
              System.out.print("X:");
     
            x=Integer.parseInt(br.readLine());
              System.out.print("Y:");
     
            y=Integer.parseInt(br.readLine());
            }
     
          void Choice(char c)throws IOException  
          {
              System.out.println("What would you like to do?");
              System.out.println("R.Reveal the Cell.");
              System.out.println("F.Flag the Cell as Mine.");
              System.out.print("Choice:");
             cho=(char)br.read();
     
            }
     
          void Action()
          {
              if(cho=='F')
                Field[x][y]='F';
              else if(cho=='R')
                {
                    if(Mine[x][y]=='@')
                       {
                        Detonate(); cond=false;
                        }
                    else
                        {
                            Field[x][y]=Mine[x][y];
                            Reveal();
                    }
                }
            }
     
          boolean Adja(int i,int j)
          {
     
             if(i+1>=0 && i+1<20)
             {
                if(Field[i+1][j]!='#')
                    return true;
                }
             if(i-1>=0 && i-1<20)
             {
                if(Field[i-1][j]!='#')
                    return true;
                }   
             if(j+1>=0 && j+1<20)
             {
                if(Field[i][j+1]!='#')
                    return true;
                }
             if(j-1>=0 && j-1<20)
             {
                if(Field[i][j-1]!='#')
                    return true;
                }   
                return false;
            }
     
          void Reveal()
          {
              for(int i=0;i<20;i++)
              {
                  for(int j=0;j<20;j++)
                  {
                      if(Mine[i][j]!='@' && Adja(i,j))
                        Field[i][j]=Mine[i][j];
                    }
                }
     
                for(int i=19;i>=0;i--)
              {
                  for(int j=19;j>=0;j--)
                  {
                      if(Mine[i][j]!='@' && Adja(i,j))
                        Field[i][j]=Mine[i][j];
                    }
                }
            }          
     
          void DiffLvl()throws IOException
          {
              System.out.println("E.Easy");
              System.out.println("M.Medium");
              System.out.println("H.Hard");
              System.out.print("Enter your choice:");
              char ch=(char)(br.read());
              if(ch=='E') r1=50; r2=60;
              if(ch=='M') r1=35; r2=70;
              if(ch=='H') r1=30; r2=70;
            }
     
          void Play()throws IOException
          {
              System.out.println("Welcome to ShaunSweeper");
              System.out.println("Choose a difficulty level:");
              DiffLvl();
              Miner();
              ValFiller();
     
              while(cond)
              {
                  Exhibit();
                  System.out.println();
                 Coords();
                 System.out.println();
                 Choice();
                 Action(); 
     
                }
            }
     
          void Activate()throws IOException
          {
              Play();
            }
     
          void Detonate()
          {
              for(int i=0;i<20;i++)
              {
                  for(int j=0;j<20;j++)
                  {
                      if(Mine[i][j]=='@')
                        Field[i][j]='@';
                    }
                }
                Exhibit();
                cond=false;
            }
    }

    theres no use my babbling too much. the program works flawlessly...almost. there seems to be something working with the entry method. try it tout and see the error i get. and please see if u can solve it. i've used blue j for this and hope u can use it too.
    Last edited by kaylors; June 24th, 2009 at 07:58 AM. Reason: wasn't getting any responses and previous problem solved anyway...


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: minesweeper game creation problem

    Hello kaylors. I added:

        public static void main(String[] args){
     
            minesweeper m = new minesweeper();
            m.Miner();
            m.Exhibit();
            m.ValFiller();
     
        }

    To get your code to run in Eclipse. It seems to run OK for me.. I'm not really sure what the problem is?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: minesweeper game creation problem

    I think the phrase oops, springs to mind.

     for(int l=j-1;j<j+1;j++)

    See what you have done wrong
    ...
    I'll give you a clue, J will always be less than J+1 xD

    Regards,
    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  4. The Following User Says Thank You to Freaky Chris For This Useful Post:

    kaylors (June 24th, 2009)

  5. #4
    Junior Member
    Join Date
    Jun 2009
    Location
    kolkata, India
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: minesweeper game creation problem

    lol, i see i see, yes that is what i have done. and now i have corected it. thanks anyway guys.

  6. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: minesweeper game creation problem

    Quote Originally Posted by Freaky Chris View Post
    I think the phrase oops, springs to mind.

     for(int l=j-1;j<j+1;j++)
    See what you have done wrong
    ...
    I'll give you a clue, J will always be less than J+1 xD

    Regards,
    Chris
    Ah good work Chris. I overlooked that
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: minesweeper game creation problem

    p.s can you please mark this as solved

    Thanks,
    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

Similar Threads

  1. Programmer for a Java based game project
    By Takkun in forum Project Collaboration
    Replies: 4
    Last Post: June 14th, 2010, 06:47 PM
  2. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 10:41 PM
  3. Replies: 2
    Last Post: July 8th, 2009, 07:35 AM
  4. WAR file creation in Eclipse JEE
    By katty in forum Java IDEs
    Replies: 5
    Last Post: May 21st, 2009, 10:45 AM
  5. Replies: 1
    Last Post: March 28th, 2009, 08:21 AM

Tags for this Thread