我是一名学生,从事滑道和梯子游戏。我必须制作有 100 个空间的板,在板上放 10 个随机滑槽和梯子,我还必须打印 10*10 板。到目前为止,一切正常,直到打印板部分。当我使用打印方法时,我的电路板会打印所有需要打印的东西,但排列得不好。关于如何排列所有打印输出的任何提示?
import java.util.Random;
public class ChutesAndLadders {
String[] board;
Random ran = new Random();
public void setBoard(String[] b) {
board = b;
for(int i=0;i<board.length;i++){
board[i]=" ";
}
}
public void makeChutes(int x){
for(int i=0;i<x;i++){
int temp = ran.nextInt(board.length);
if (board[temp].equals(" "))
board[temp]="C"+x;
else
i--;
}
}
public void makeLadders(int y){
for(int i=0;i<y;i++){
int temp = ran.nextInt(board.length);
if (board[temp].equals(" "))
board[temp]="L"+y;
else
i--;
}
}
public void printBoard(){
int counter = 0;
for(int i=0;i<board.length;i++){
counter++;
System.out.print("|"+board[i]);
if(counter==10){
System.out.print("|"+"\n");
counter=0;
}
}
}
public static void main(String[] args) {
ChutesAndLadders cl = new ChutesAndLadders();
cl.setBoard(new String[100]);
cl.makeChutes(10);
cl.makeLadders(10);
cl.printBoard();
}
}