- ย้าย check catch ไปใส่เป็น method ใน class board
- แก้ไขชื่อตัวแปรที่ไม่สื่อความหมาย
- ทำให้หนูเดินได้
=======class Board=========
ย้ายเงื่อนไขการตรวจสอบการชนกันมาใส่ใน class board
public class Board {
int row;
int col;
private char board[][];
char player = 'C';
int score = 0;
public Board (int row,int col) {
this.row = row;
this.col = col;
this.board= new char[this.row][this.col];
}
public char getBoard(int row,int col) {
return this.board[row][col];
}
public void setBoard(char C_or_R,int row,int col) {
if(player == C_or_R) this.board= new char[this.row][this.col];
this.board[row][col] = C_or_R ;
}
public boolean check(int ratrow, int ratcol, int catrow , int catcol) {
if(catrow == ratrow && catcol == ratcol) {
score++;
return true;
}else return false;
}
}
========class Display============
เรียกคะแนนจาก class board มาแสดง
public class Display {
private Board board;
public Display(Board board) {
this.board = board;
}
public void display() {
// create and show board
for (int i = 0; i < this.board.row; i++) {
for (int j = 0; j < this.board.col; j++) {
System.out.print(this.board.getBoard(i,j)+"|");
}
System.out.println();
}
System.out.println("Score : "+this.board.score);
}
}
===========class Cat และ class Input ไม่มีการเปลี่ยนแปลง================
========class Rat==========
import java.util.*;
public class Rat {
private Board board;
private Cat cat;
private boolean run_random;
int ratrow = 2;
int ratcol = 2;
int ratran=1;
int ratbf = 0;
public Rat(Board board , Cat cat){
this.board = board;
this.cat = cat;
}
public void spawn() {
this.board.setBoard('R' , ratrow, ratcol);
}
public void randomRat(){
run_random = true;
Random ran_r = new Random();
Random ran_c = new Random();
if(run_random == true && this.board.check(ratrow, ratcol, this.cat.row, this.cat.col) == false)this.board.setBoard('R',ratrow,ratcol);
while(run_random) {
//เปลี่ยนวิธีหาหนู ไม่วน loop แล้ว
if(this.board.getBoard(ratrow, ratcol) == 'R') {
run_random = false;
}
else if ((this.board.row+1)*(this.board.col+1) == this.board.row*this.board.col && run_random == true) {
ratrow = ran_r.nextInt(this.board.row);
ratcol = ran_c.nextInt(this.board.col);
this.board.setBoard('R',ratrow,ratcol);
}
}
}
//เงื่อนไขให้หนูเดินทีละช่อง
public void ratrun() {
Random ranmove = new Random();
ratran = ranmove.nextInt(4);
if(ratran == 0 && ratrow >0) {
ratbf = ratrow;
ratrow -= 1;
this.board.setBoard(' ',ratbf,ratcol);
this.board.setBoard('R',ratrow,ratcol);
System.out.println("up");
}
else if(ratran == 1 && ratrow < this.board.row-1) {
ratbf = ratrow;
ratrow += 1;
this.board.setBoard(' ',ratbf,ratcol);
this.board.setBoard('R',ratrow,ratcol);
System.out.println("down");
}
else if(ratran == 2 && ratcol > 0) {
ratbf = ratcol;
ratcol -= 1;
this.board.setBoard(' ',ratrow,ratbf);
this.board.setBoard('R',ratrow,ratcol);
System.out.println("left");
}
else if(ratran == 3 && ratcol < this.board.col-1) {
ratbf = ratcol;
ratcol += 1;
this.board.setBoard(' ',ratrow,ratbf);
this.board.setBoard('R',ratrow,ratcol);
System.out.println("right");
}
else ratrun();
}
}
======================================================
ไม่มีความคิดเห็น:
แสดงความคิดเห็น