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

Thread: Sliding puzzle Restart button(same exact game)

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sliding puzzle Restart button(same exact game)

    I am writing a sliding game program for my java class and i have it pretty much down. i have a new game button that works perfectly, and a score box that works the way i want it... i had to put in a restart button(restarts the same exact game). i have the button in the game but all it does is restart the score and doesnt mix it up.. so if i hit a button to move it, then hit the restart button, all it does is reset the score but keeps the same blocks in the place i put them.. i need help with that part of the code as i have searched everywhere, the internet and my java programming book(which is pretty much useless for almost anything i do in the class)... i have the Handler programs in there too so im all out of ideas... heres what i have...

    import java.applet.*;
    import java.awt.*;
     
    public class newbutton extends Applet
    {
    Button b[] = new Button[16];
    HandleGameButton me;
    HandleNewGame you;
    HandleRestartGame we;
    rcolor c = new rcolor(16);
    Font f1, f2;
    int blankPosition = 15;
    Panel panel1,panel2;
    int score = 0;
    Label scoreLabel, setLabel;
    Button newGameButton;
    Button RestartGame;
    String buttonText[];
     
     
     
     
    public void init()
    {
     
    buttonText = new String[16];
    panel1 = new Panel();
    panel2 = new Panel();
    newGameButton = new Button("New Game");
    newGameButton.setBackground(Color.blue);
    newGameButton.setForeground(Color.white)…
    you = new HandleNewGame(this);
    newGameButton.addActionListener(you);
    panel2.add(newGameButton);
     
    RestartGame = new Button("Restart Game");
    RestartGame.setBackground(Color.orange);
    RestartGame.setForeground(Color.black);
    we = new HandleRestartGame(this);
    RestartGame.addActionListener(we);
    panel2.add(RestartGame);
     
     
    f1 = new Font("Courier",Font.PLAIN, 70);
    setFont(f1);
    f2 = new Font("Courier",Font.PLAIN, 20);
    panel2.setFont(f2);
    c.fill_colors();
    setLayout(new GridLayout(1,2,0,0) );
    add(panel1);
    add(panel2);
    panel1.setLayout(new GridLayout(4,4,0,0) );
    panel2.setLayout(new GridLayout(4,1,0,0) );
     
    for (int index = 0; index < b.length; index++)
    {
    b[index] = new Button(String.valueOf(index+1));
    b[index].setBackground(Color.red);
    b[index].setForeground(Color.black);
    me = new HandleGameButton(index,this);
    b[index].addActionListener(me);
    panel1.add(b[index]);
    }
    b[15].setBackground(Color.black);
     
    panel2.setBackground(Color.green);
     
    scoreLabel = new Label("Your Score is " + score);
    scoreLabel.setBackground(Color.yellow);
    panel2.add(scoreLabel);
     
     
     
    mix();
    saveGame();
     
    score = 0;
    scoreLabel.setText("Your Score is " + score);
    }
     
    public void saveGame()
     
    {
     
     
     
    for( int index = 0; index <= 15; index++)
    {
     
     
    buttonText[index] = b[index].getLabel();
     
    }
     
     
    }
     
     
     
     
     
     
     
    public void pressButton(int buttonNumber)
    {
    if(nextToBlank( buttonNumber))
    {
    b[blankPosition].setBackground(Color.red&#8230;
    b[buttonNumber].setBackground(Color.blac&#8230;
    b[blankPosition].setLabel(b[buttonNumber&#8230; );
    blankPosition = buttonNumber;
    score++;
    scoreLabel.setText("Your Score is " + score);
    }
    }
    public boolean nextToBlank( int buttonNumber)
    {
    boolean answer = false;
    if(buttonNumber-4 == blankPosition) answer = true;
    else if(buttonNumber+4 == blankPosition) answer = true;
    else if(buttonNumber+1 == blankPosition && buttonNumber/4 == blankPosition/4 ) answer = true; 
    else if(buttonNumber-1 == blankPosition && buttonNumber/4 == blankPosition/4 ) answer = true; 
    return(answer); 
    }
     
    public int randInt( int limit )
    {
    return ( (int) (limit*Math.random() ) );
    }
     
    public void mix()
    {
    for(int index=1; index<=8000; index++)
    {
    int scrambleUp = randInt(16);
     
    pressButton(scrambleUp);
     
    }
    }
    }
    Last edited by copeg; April 19th, 2011 at 09:40 PM.


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Sliding puzzle Restart button(same exact game)

    Cross-posted at CodeGuru forums.

Similar Threads

  1. Java sliding block puzzle issues
    By Vesper in forum What's Wrong With My Code?
    Replies: 15
    Last Post: April 11th, 2011, 08:30 AM
  2. Search Word puzzle game
    By lew1s in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 9th, 2011, 05:23 AM
  3. JavaScript pic puzzle
    By fr334a11 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 25th, 2011, 09:29 AM
  4. Help to write Kakuro puzzle solver
    By divadola in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 12:10 PM
  5. [SOLVED] Problem in implementation of Fifteen Puzzle with 2D Arrays
    By bruint in forum Collections and Generics
    Replies: 8
    Last Post: May 3rd, 2009, 11:37 PM