Sliding puzzle Restart button(same exact game)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Sliding puzzle Restart button(same exact game)

  1. #1
    Join Date
    Apr 2011
    Posts
    1

    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 doesn't put it back in place. 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 I'm all out of ideas. Here's what I have.

    Code:
    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…
    b[buttonNumber].setBackground(Color.blac…
    b[blankPosition].setLabel(b[buttonNumber… );
    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);
    
    }
    }
    }

  2. #2
    Join Date
    Feb 2011
    Posts
    27

    Re: Sliding puzzle Restart button(same exact game)

    Maybe as part of your mix() method you should saved the ordering of the puzzle in an array and when you want to restart the game, you can rearrange the board to reflect the state of the board after it was mixed up by your mix() method.

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Sliding puzzle Restart button(same exact game)

    Cross-posted to Java Programming Forums and OTN Discussion Forums.

    For that reason, I'm out.

    Please don't cross-post without providing links to all cross-posted forums, otherwise people will waste their time on what may already be answered and you will end up on forum blacklists & ignorelists.

    Experience is a poor teacher: it gives its tests before it teaches its lessons...
    Anon.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Sliding puzzle Restart button(same exact game)

    It's also a duplicate of this post
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center