0
나는 moveGhost가하는 방법을 이해하지 못합니까? 0-7 사이의 임의의 숫자를 선택하는 이유는 무엇입니까 팩맨을 쫓는 것과 관련이 있습니까? 아, 귀신의 GUI 이미지를 만들려면 어떻게해야합니까? 이 코드를 발견하고 어떻게 작동하는지 이해하려고합니다.팩맨 유령 클래스,이 방법은 무엇을합니까?
public class GhostRed {
private int size;
private int row, col;
//constructor
public GhostRed(int r, int c, int s) {
row = r;
col = c;
size = s;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public void setRow(int r) {
row = r;
}
public void setCol(int c) {
col = c;
}
private boolean checkMove(int r, int c) {
boolean check=true;
if (r<0 || r > size-1 || c < 0 || c > size-1) {
check= false;
}
return check;
}
public boolean moveGhost() {
boolean move=true;
Random r = new Random();
int newMove = r.nextInt(7); //taking a random number btw 0-7
if (newMove == 0) {
if(checkMove(row-1, col)==false) {
move=false;
}
else{
row = row-1;
}
}
if (newMove == 1) {
if(checkMove(row+1, col)==false) {
move= false;
}
else{
row = row+1;
}
}
if (newMove == 2) {
if(checkMove(row, col-1)==false) {
move= false;
}else{
col = col-1;
}
}
if (newMove == 3) {
if(checkMove(row, col+1)==false) {
move= false;
}
else{
col = col+1;
}
}
if (newMove == 4) {
if(checkMove(row-1, col-1)==false) {
move= false;
}
else{
row = row-1;
col = col-1;
}
}
if (newMove == 5) {
if(checkMove(row-1, col+1)==false) {
move= false;
}
else{
row = row-1;
col = col+1;
}
}
if (newMove == 6) {
if(checkMove(row+1, col-1)==false) {
move= false;
}
else{
row = row+1;
col = col-1;
}
}
if (newMove == 7) {
if(checkMove(row+1, col+1)==false) {
move= false;
}else{
row = row+1;
col = col+1;
}
}
return move;
}
}