我试图为我的班级制作一个Mancala游戏,并且我在这里分散种子的基本代码。 K是我从
中取出种子的董事会职位所通过的这是初始代码
public class Mancala {
private static final int BOARD_SIZE = 14;
private static final int START_SEEDS=4;
private int[] board;
public Mancala(){
board = new int[BOARD_SIZE];
for(int i = 0; i < board.length; i++)
board[i]=START_SEEDS;
board[0]=0;
board[BOARD_SIZE/2]=0;
}
public boolean makeMove(int k){
int seeds = board[k];
while(seeds>0){
for(int i = k; i <= board.length; i++){
seeds--;
board[i]++;
}
}
board[k] = seeds;
我在[i] ++上不断出现错误?有什么想法吗?
答案 0 :(得分:1)
for(int i = k; i <= board.length; i++){
seeds--;
board[i]++;
}
如果board.length == 14
循环将运行
board[12]
board[13]
board[14] out of bounds here as board goes from 0-13
修正版
for(int i = k; i < board.length; i++){
seeds--;
board[i]++;
}