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");
}
}