I have a project to create a game. This is my code since i am the way improvement. But now i face a problems is, when i run the code and play it, there have a bug and i don't know how to fix it, pls help me.
When i play it, the input "J" and "I" have no problems but the problems was found when the input "K" and "L".

Anyone can help me fix the bug, thx a lots !!

Here is the code:

package javaapplication1;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner Input = new Scanner (System.in);

        System.out.println("Sokoban"+"\n1.Start Game"+"\n2.Top Score"+"\n3.Setting"+"\n4.Help"+"\n5.Exit");

        boolean Terminated = true;

        while(Terminated == true)
        {
        System.out.print("Please enter the function you choose: ");
        int Project = Input.nextInt();

        if (Project == 1)
            StartGame();
        else if (Project == 2)
            TopScore();
        else if (Project == 3)
            Setting();
        else if (Project == 4)
            Help();
        else if (Project == 5)
            Terminated = Exit();
        }
    }

    public static void StartGame()
    {
        Scanner Input = new Scanner (System.in);
        System.out.print("Choose the level you want: ");
        int LevelWanted = Input.nextInt();

        char [][] Start = ChooseLevel(LevelWanted);
        PrintMaze(Start);
        GamePlay(Start, LevelWanted);
    }

    public static void TopScore()
    {

    }

    public static void Setting()
    {

    }

    public static void Help()
    {
        System.out.println("\nHow to play & Rules: "+
                            "\na.Sokoban can move up,down,left and right."+
                            "\nb.Every block should be pushed onto a target zone."+
                            "\nc.Block have not a specifically target zone, any blocks can place at any target zone."+
                            "\nd.At every level, the maze will be different, the amount of target zone n block will be different."+
                            "\ne.Once all the block was placed at target zone, the task is done."+
                            "\nf.Sokoban can't pass through walls."+
                            "\ng.If Sokoban wants to move towards a block it tries to push it."+
                            "\nh.Sokoban can’t to pull a block"+
                            "\ni.If a block has a wall or another block in pushing direction, Sokoban cannot move it."+
                            "\nj.At any time a square can only be occupied by one of a wall, box or man."+
                            "\nk.Step and time a player needed to accomplish his task is count, and the amount will be used to evaluate how many points the player obtain."+
                            "\nl.Sokoban able player to undo a step, restart, and pass a level."
                            );
    }

    public static boolean Exit()
    {
        return false;
    }

     public static void GamePlay(char [][] Game, int LevelWanted)
    {
        Scanner Input = new Scanner (System.in);
        char Instruction = 'A';

        while(Instruction != 'Q')
        {
           System.out.print("Enter a key: ");
           Instruction = Input.next().charAt(0);

           boolean Valid = true;
           boolean Won = false;

           if(Instruction == 'I')
           {
               Valid = Check(Game, Instruction);
               if(Valid == true)
                   Move(Game, Instruction);
               else
                   System.out.println("Movement is invalid.");
           }

           else if(Instruction == 'K')
           {
               Valid = Check(Game, Instruction);
               if(Valid == true)
                   Move(Game, Instruction);
               else
                   System.out.println("Movement is invalid.");
           }

           else if(Instruction == 'J')
           {
               Valid = Check(Game, Instruction);
               if(Valid == true)
                   Move(Game, Instruction);
               else
                   System.out.println("Movement is invalid.");
           }

           else if(Instruction == 'L')
           {
               Valid = Check(Game, Instruction);
               if(Valid == true)               
                   Move(Game, Instruction);
               else
                   System.out.println("Movement is invalid.");
           }
           else if (Instruction == 'U')
               Undo(Game);               
           else if (Instruction == 'R')
               Restart(Game);               
           else if (Instruction == 'H')
               Hints(Game, LevelWanted);
           else if (Instruction == 'Q')
               break;
           else
               System.out.println("The Instruction is invalid.");
        }
    }

    public static char [][] ChooseLevel (int LevelWanted)
    {
        final char Wall = '*';
        final char Block = '#';
        final char TargetZone = '@';
        final char Mover = '&';
        final char Floor = ' ';
        final char Free = ' ';

        char [][] Level1 = {
            {Wall,Wall,Wall,Wall,Wall,Wall,Wall,Wall},
            {Wall,Floor,Floor,Floor,Wall,Floor,Floor,Wall},
            {Wall,Floor,Floor,Floor,Block,Mover,TargetZone,Wall},
            {Wall,Floor,Floor,Floor,Wall,Floor,Floor,Wall},
            {Wall,Wall,Wall,Wall,Wall,Wall,Wall,Wall},
            {Free,Free,Free,Free,Free,Free,Free,Free},
            {Free,Free,Free,Free,Free,Free,Free,Free},
            {Free,Free,Free,Free,Free,Free,Free,Free}

        };

        char [][] Level2 = {
            {Free,Free,Wall,Wall,Wall,Wall,Free},
            {Wall,Wall,Wall,Floor,Floor,Wall,Free},
            {Wall,Floor,Block,Floor,TargetZone,Wall,Wall},
            {Wall,Floor,Block,Floor,Floor,Floor,Wall},
            {Wall,Mover,Wall,TargetZone,Floor,Floor,Wall},
            {Wall,Wall,Wall,Wall,Wall,Wall,Wall},
            {Free,Free,Free,Free,Free,Free,Free}

        };

        if (LevelWanted == 1)
            return Level1;
        else if (LevelWanted == 2)
            return Level2;
        else
            return null;

    }    

    public static void PrintMaze(char[][] Cell)
    {
        for(int row = 0; row < Cell.length; row++)
        {
            for(int column = 0; column< Cell.length; column++)
            {
                System.out.print(Cell[row][column] + " ");
            }   System.out.println();
        }
    }   

    public static void Move(char [][] Cell, char Key)
    {
        for(int row = 0; row < Cell.length; row++)
        {
            for(int column = 0; column< Cell.length; column++)
            {
                if (Cell[row][column] == '&' && Key == 'I')
                {
                    if (Cell[row - 1][column] == ' ')
                    {
                        Cell[row - 1][column] = '&';
                        Cell[row][column] = ' ';
                    }
                    else if (Cell[row - 1][column] == '#')
                    {
                        Cell[row - 1][column] = '&';
                        Cell[row - 2][column] = '#';
                        Cell[row][column] = ' ';
                    }
                }

                else if(Cell[row][column] == '&' && Key == 'K')
                {
                    if (Cell[row + 1][column] == ' ')
                    {
                        Cell[row + 1][column] = '&';
                        Cell[row][column] = ' ';
                    }
                    else if (Cell[row + 1][column] == '#')
                    {
                        Cell[row + 1][column] = '&';
                        Cell[row + 2][column] = '#';
                        Cell[row][column] = ' ';
                    }
                }

                else if(Cell[row][column] == '&' && Key == 'J')
                {
                   if (Cell[row][column - 1] == ' ')
                    {
                        Cell[row][column - 1] = '&';
                        Cell[row][column] = ' ';
                    }
                    else if (Cell[row][column - 1] == '#')
                    {
                        Cell[row][column - 1] = '&';
                        Cell[row][column - 2] = '#';
                        Cell[row][column] = ' ';
                    }
                }

                else if(Cell[row][column] == '&' && Key == 'L')
                {
                    if (Cell[row][column + 1] == ' ')
                    {
                        Cell[row][column + 1] = '&';
                        Cell[row][column] = ' ';
                    }
                    else if (Cell[row][column + 1] == '#')
                    {
                        Cell[row][column + 1] = '&';
                        Cell[row][column + 2] = '#';
                        Cell[row][column] = ' ';
                    }
                }
            }
        }
        PrintMaze(Cell);
    }

    public static boolean Check(char [][] Cell, char Key)
    {
        for(int row = 0; row < Cell.length; row++)
        {
            for(int column = 0; column< Cell.length; column++)
            {
                if (Cell[row][column] == '&' && Key == 'I')
                {
                    if (Cell[row - 1][column] == '*')
                        return false;
                    else if (Cell[row - 1][column] == '#' && Cell[row - 2][column] == '*')
                        return false;
                    else if (Cell[row - 1][column] == '#' && Cell[row - 2][column] == '#')
                        return false;
                    else
                        return true;
                }

                else if(Cell[row][column] == '&' && Key == 'K')
                {
                    if (Cell[row + 1][column] == '*')
                        return false;
                    else if (Cell[row + 1][column] == '#' && Cell[row + 2][column] == '*')
                        return false;
                    else if (Cell[row + 1][column] == '#' && Cell[row + 2][column] == '#')
                        return false;
                    else
                        return true;
                }

                else if(Cell[row][column] == '&' && Key == 'J')
                {
                    if (Cell[row][column - 1] == '*')
                        return false;
                    else if (Cell[row][column - 1] == '#' && Cell[row][column - 2] == '*')
                        return false;
                    else if (Cell[row][column - 1] == '#' && Cell[row][column - 2] == '#')
                        return false;
                    else
                        return true;
                }

                else if(Cell[row][column] == '&' && Key == 'L')
                {
                    if (Cell[row][column + 1] == '*')
                        return false;
                    else if (Cell[row][column + 1] == '#' && Cell[row][column + 2] == '*')
                        return false;
                    else if (Cell[row][column + 1] == '#' && Cell[row][column + 2] == '#')
                        return false;
                    else
                        return true;
                }
            }
        }
        return true;
    }

    public static void Undo(char [][] Game)
    {

    }

    public static void Restart(char [][] Game)
    {

    }

    public static void Hints(char [][] Game, int LevelHints)
    {
        if (LevelHints == 1)
            System.out.println("left,left,up,left,left,down,right,right,right,right");
        else if (LevelHints == 2)
            System.out.println("up,right,left,up,right,right,down");
        else
            System.out.println("Hints unavailable");
    }

}

What is the problem with those inputs?

And why repeat the exact same code in the I,J,K,L blocks when you can reduce that to a single if() statement?

What is the problem with those inputs?

And why repeat the exact same code in the I,J,K,L blocks when you can reduce that to a single if() statement?

i don't know how to explain exactly. you may try run the program and see the bug .

If you cannot explain, how do you know you have a bug? Surely you can describe the problem.

If you cannot explain, how do you know you have a bug? Surely you can describe the problem.

let me say like this, when i run the code and play the game in level 1.

Firstly i have to make the first move is move to the left, the program is run well.

Next, i have a move to up, the program still run well.

The problems come out when i have right move and down move. The mover will direct move to the final space but not the next space.

Same problems happen in right move and down move. when the mover wan push the box, the bug was came out. i don't understand why the bug just appear when right move and down move, but the up move and left move have no problems.

A suggestion so that everyone is working on the same problem.
Make a few changes to your code to remove the need for a user to enter data by having the answers already in a String in the program.
Move the Definition for Scanner Input to the class level and make it static. Remove the definition for Scanner in the StartGame method.
Add these two statements before the main() method at the class level

static String Data = "1\n1\nL\nL\nL\nL";    // pre answer all questions here

static Scanner Input = new Scanner (Data); //System.in); // get input from String

Then change what I have as the values for Data to be the values that show the problem.

Look at your nested loops in the move method. You are having problems with your +1 methods, right and down. Your -1 methods, left and up, work fine. Think about what goes on with those +1 operations as you loop through in a positive direction from 0 to end.

Look at your nested loops in the move method. You are having problems with your +1 methods, right and down. Your -1 methods, left and up, work fine. Think about what goes on with those +1 operations as you loop through in a positive direction from 0 to end.

I think is that's problems. But i don't know how to solve it, can you give me some suggestion ??

give me some suggestion

Try debugging the code by printing out variables as they change and to show execution flow.
If you understand your code, you should know what the values should be and can look at what is printed out to see if the code is doing what you want it to do.

Consider whether you need to go any further in your array loops once you have found and moved your player symbol. It seems to me that you're done at that point.

Now, i almost done the program. I just left 1 bug, that is i don't know how to fix the coordinate of my target zone.

In my Move method, even i try to save the coordinate in the program, but i fail to do that. I really don't know where is my mistake and i have no idea to solve this problems.

Anyone can help ??

Your help will appreciate so much.

Here is my code:

package javaapplication1;
import java.util.Scanner;

public class Main
{
        public static final char Wall = '*';
        public static final char Block = '#';
        public static final char TargetZone = '@';
        public static final char Mover = '&';
        public static final char Floor = ' ';
        public static final char Free = ' ';

    public static void main(String[] args)
    {
        Scanner Input = new Scanner (System.in);
        int Project;

        System.out.println("Sokoban"+"\n1.Start Game"+"\n2.Top Score"+"\n3.Setting"+"\n4.Help"+"\n5.Exit");

        do
        {
            System.out.print("Please enter the function you choose: ");
            Project = Input.nextInt();

            if (Project == 1)
                StartGame();
            else if (Project == 2)
                TopScore();
            else if (Project == 3)
                Setting();
            else if (Project == 4)
                Help();
            else if (Project == 5)
            {
                System.out.println("Thanks for play.");
                break;
            }
            else
                System.out.println("Instruction invalid.");
        }while(Project != 5);
    }

    public static void StartGame()
    {
        Scanner Input = new Scanner (System.in);
        System.out.print("Choose the level you want: ");
        int LevelWanted = Input.nextInt();

        char [][] Start = ChooseLevel(LevelWanted);
        PrintMaze(Start);
        GamePlay(Start, LevelWanted);
    }

    public static void TopScore()
    {

    }

    public static void Setting()
    {

    }

    public static void Help()
    {
        System.out.println(
        "\n\tHow to play & Rules"+
    "\n1.   Sokoban can move up,down,left and right."+
    "\n2.   Sokoban can't pass through walls."+
        "\n3.   Every block should be pushed onto a target zone."+
        "\n4.   Once all the block was placed at target zone, the task is done."+
        "\n5.   Block have not a specifically target zone, any blocks can place at any target zone."+
    "\n6.   If Sokoban wants to move towards a block, it tries to push it."+
    "\n7.   Sokoban can’t to pull a block"+
    "\n8.   If a block has a wall or another block in pushing direction, Sokoban cannot move it."+
    "\n9.   At any time a square can only be occupied by one of a wall, box or man."+
    "\n10.  Step and time a player needed to accomplish his task is count, and the amount will be used to evaluate how many points the player obtain."+
        "\n11.  At every level, the maze will be different, the amount of target zone n block will be different."+
    "\n12.  Sokoban able player to undo a step, restart, and pass a level."
        );
    }

     public static void GamePlay(char [][] Game, int LevelWanted)
    {
        Scanner Input = new Scanner (System.in);
        char Instruction = 'A';
        int Step = 0;

        while(Instruction != 'Q')
        {
           System.out.print("Enter a key: ");
           Instruction = Input.next().charAt(0);

           boolean Valid = true;
           boolean Won ;

           if(Instruction == 'I')
           {
               Valid = Check(Game, Instruction);
               if(Valid == true)
               {
                   Move(Game, Instruction);
                   Step++;
               }
               else
                   System.out.println("Movement is invalid.");

               Won = CheckStatus(Game);
               if(Won == true)
               {
                    System.out.println("You won!");
                    break;
               }
           }

           else if(Instruction == 'K')
           {
               Valid = Check(Game, Instruction);
               if(Valid == true)
               {
                   Move(Game, Instruction);
                   Step++;
               }
               else
                   System.out.println("Movement is invalid.");

               Won = CheckStatus(Game);
               if(Won == true)
               {
                    System.out.println("You won!");
                    break;
               }
           }

           else if(Instruction == 'J')
           {
               Valid = Check(Game, Instruction);
               if(Valid == true)
               {
                   Move(Game, Instruction);
                   Step++;
               }
               else
                   System.out.println("Movement is invalid.");

               Won = CheckStatus(Game);
               if(Won == true)
               {
                    System.out.println("You won!");
                    break;
               }
           }

           else if(Instruction == 'L')
           {
               Valid = Check(Game, Instruction);
               if(Valid == true)
               {
                   Move(Game, Instruction);
                   Step++;
               }
               else
                   System.out.println("Movement is invalid.");

               Won = CheckStatus(Game);
               if(Won == true)
               {
                    System.out.println("You won!");
                    break;
               }
           }
           else if (Instruction == 'U')
               Undo(Game);
           else if (Instruction == 'R')
               Restart(Game);
           else if (Instruction == 'H')
               Hints(Game, LevelWanted);
           else if (Instruction == 'Q')
               break;
           else
               System.out.println("The Instruction is invalid.");
           System.out.println("Steps: "+Step);
        }
    }

    public static char [][] ChooseLevel (int LevelWanted)
    {


        char [][] Level1 = {
            {Wall,Wall,Wall,Wall,Wall,Wall,Wall,Wall},
            {Wall,Floor,Floor,Floor,Wall,Floor,Floor,Wall},
            {Wall,Floor,Floor,Floor,Block,Mover,TargetZone,Wall},
            {Wall,Floor,Floor,Floor,Wall,Floor,Floor,Wall},
            {Wall,Wall,Wall,Wall,Wall,Wall,Wall,Wall},
            {Free,Free,Free,Free,Free,Free,Free,Free},
            {Free,Free,Free,Free,Free,Free,Free,Free},
            {Free,Free,Free,Free,Free,Free,Free,Free}

        };

        char [][] Level2 = {
            {Free,Free,Wall,Wall,Wall,Wall,Free},
            {Wall,Wall,Wall,Floor,Floor,Wall,Free},
            {Wall,Floor,Block,Floor,TargetZone,Wall,Wall},
            {Wall,Floor,Block,Floor,Floor,Floor,Wall},
            {Wall,Mover,Wall,TargetZone,Floor,Floor,Wall},
            {Wall,Wall,Wall,Wall,Wall,Wall,Wall},
            {Free,Free,Free,Free,Free,Free,Free}

        };

        if (LevelWanted == 1)
            return Level1;
        else if (LevelWanted == 2)
            return Level2;
        else
            return null;

    }

    public static boolean CheckStatus(char [][] Game)
    {
        for(int row = 0; row < Game.length; row++)
        {
            for(int column = 0; column< Game.length; column++)
            {
                if(Game[row][column] == '@')
                    return false;
            }
        }
        return true;
    }

    public static void PrintMaze(char[][] Cell)
    {
        for(int row = 0; row < Cell.length; row++)
        {
            for(int column = 0; column< Cell.length; column++)
            {
                System.out.print(Cell[row][column] + " ");
            }   System.out.println();
        }
    }

   public static void Move(char [][] Cell, char Key)
    {
       int X = 0;
       int Y = 0;

        for(int row = 0; row < Cell.length; row++)
        {
            for(int column = 0; column< Cell.length; column++)
            {
                if (Cell[row][column] == '&' && Key == 'I')
                {
                    if (Cell[row - 1][column] == ' ')
                    {
                        Cell[row - 1][column] = '&';
                                Cell[row][column] = ' ';
                    }
                    else if (Cell[row - 1][column] == '#')
                    {
                        Cell[row - 1][column] = '&';
                        Cell[row - 2][column] = '#';
                        //row=row-1;
                        Cell[row][column] = ' ';
                    }
                    else if(Cell[row - 1][column] == '@')
                    {
                        Cell[row - 1][column] = '&';
                        Cell[row][column] = ' ';
                        X = row-1;
                        Y = column;
                    }
                    row--;
                }
                else if(Cell[row][column] == '&' && Key == 'K')
                {
                    if (Cell[row + 1][column] == ' ')
                    {
                        Cell[row + 1][column] = '&';

                        Cell[row][column] = ' ';
                    }
                    else if (Cell[row + 1][column] == '#')
                    {
                        Cell[row + 1][column] = '&';
                        Cell[row + 2][column] = '#';
                        //row=row+1;
                        Cell[row][column] = ' ';
                    }
                    else if(Cell[row + 1][column] == '@')
                    {
                        Cell[row + 1][column] = '&';
                        Cell[row][column] = ' ';
                        X = row+1;
                        Y = column;
                    }
                    row++;
                }
                else if(Cell[row][column] == '&' && Key == 'J')
                {
                   if (Cell[row][column - 1] == ' ')
                    {
                        Cell[row][column - 1] = '&';
                        Cell[row][column] = ' ';

                    }
                    else if (Cell[row][column - 1] == '#')
                    {
                        Cell[row][column - 1] = '&';
                        Cell[row][column - 2] = '#';
                        //column=column-1;


                        Cell[row][column] = ' ';
                    }
                    else if(Cell[row][column - 1] == '@')
                    {
                        Cell[row][column - 1] = '&';
                        Cell[row][column] = ' ';

                    }
                   column--;
                }
                else if(Cell[row][column] == '&' && Key == 'L')
                {
                    if (Cell[row][column + 1] == ' ')
                    {
                        Cell[row][column + 1] = '&';
                        Cell[row][column] = ' ';
                        column=column+1;
                    }
                    else if (Cell[row][column + 1] == '#')
                    {
                        Cell[row][column + 1] = '&';
                        Cell[row][column + 2] = '#';

                        Cell[row][column] = ' ';
                    }
                    else if (Cell[row][column + 1] == '@')
                    {
                        Cell[row][column + 1] = '&';
                        Cell[row][column] = ' ';
                        column=column+1;
                    }
                    column++;
                }
                if(Cell[X][Y] == ' ')
                    Cell[X][Y] = '@';
            }
        }
        PrintMaze(Cell);
    }

    public static boolean Check(char [][] Cell, char Key)
    {
        for(int row = 0; row < Cell.length; row++)
        {
            for(int column = 0; column< Cell.length; column++)
            {
                if (Cell[row][column] == '&' && Key == 'I')
                {
                    if (Cell[row - 1][column] == '*')
                        return false;
                    else if (Cell[row - 1][column] == '#' && Cell[row - 2][column] == '*')
                        return false;
                    else if (Cell[row - 1][column] == '#' && Cell[row - 2][column] == '#')
                        return false;
                    else
                        return true;
                }

                else if(Cell[row][column] == '&' && Key == 'K')
                {
                    if (Cell[row + 1][column] == '*')
                        return false;
                    else if (Cell[row + 1][column] == '#' && Cell[row + 2][column] == '*')
                        return false;
                    else if (Cell[row + 1][column] == '#' && Cell[row + 2][column] == '#')
                        return false;
                    else
                        return true;
                }

                else if(Cell[row][column] == '&' && Key == 'J')
                {
                    if (Cell[row][column - 1] == '*')
                        return false;
                    else if (Cell[row][column - 1] == '#' && Cell[row][column - 2] == '*')
                        return false;
                    else if (Cell[row][column - 1] == '#' && Cell[row][column - 2] == '#')
                        return false;
                    else
                        return true;
                }

                else if(Cell[row][column] == '&' && Key == 'L')
                {
                    if (Cell[row][column + 1] == '*')
                        return false;
                    else if (Cell[row][column + 1] == '#' && Cell[row][column + 2] == '*')
                        return false;
                    else if (Cell[row][column + 1] == '#' && Cell[row][column + 2] == '#')
                        return false;
                    else
                        return true;
                }
            }
        }
        return true;
    }

    public static void Undo(char [][] Game)
    {

    }

    public static void Restart(char [][] Game)
    {

    }

    public static void Hints(char [][] Game, int LevelHints)
    {
        if (LevelHints == 1)
            System.out.println("left,left,up,left,left,down,right,right,right,right");
        else if (LevelHints == 2)
            System.out.println("up,right,left,up,right,right,down");
        else
            System.out.println("Hints unavailable");
    }

}

i have no idea to solve this problems.

You need to debug your code. One way is to add print outs of the variables to show how they change and to show execution flow. Finding bugs is not magic. It takes time looking at how the values of variable change and how the code handles those changes.

Also look at my previous post about how to add a String to your program to simulate user input to the code so that it demonstrates the path to the error. That will allow everyone to execute your code and have it do the same thing and show the problem.

Please edit your code and include it in code tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.