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

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 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…
    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
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Sliding puzzle Restart button(same exact game)

    Please:
    • Post your code in code tags so it's easy to read.
    • Use Java naming standards eg Class names should start with uppercase letters, variables and method names should start with lowercase letters etc.
    • Use sensible descriptive names - for example why is the applet called newbutton.
    • Show the relevant code, ie what's in the HandleRestartGame class.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Aug 2011
    Posts
    6

    Thumbs up Re: Sliding puzzle Restart button(same exact game)

    I'm lucky to find this instruction first before i turned the internet upside down! Thanks much, keang, for saving hours of searching!
    Last edited by Deonis; June 20th, 2012 at 03:34 AM.

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