-2

I'm trying to make a Mancala game for my class and for the base code of dispersing the seeds here is what I have. K is what is passed for the position of the board that I am taking the seeds out of

Here is the initial code

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 keep getting an out of bounds error on board[i]++ ? Any idea?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
James P
  • 15
  • 2
  • We need more code showing how board is being declared/modified. Have you checked to see if `board[i]` exists? – Eric G Jan 21 '16 at 18:47
  • 1
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – JFPicard Jan 21 '16 at 18:48
  • @EricG I edited my post – James P Jan 21 '16 at 18:52
  • I think its `i <= board.length` should just be `i < board.length` – Eric G Jan 21 '16 at 18:53
  • @EricG That gets rid of the outofbounds error, however, now if I call the method at a position of 4 for k, board[4] ends up being -6 at the end of the code. Also, should I make it int i = k + 1 because I do not want to add seeds to the initial space? But if I do i = k + 1 I get another out of bounds. – James P Jan 21 '16 at 18:57
  • @JamesP I dont know what a `Mancala game` is, so i cant help anymore without an explanation of what youre trying to do with the `makeMove` function. – Eric G Jan 21 '16 at 19:01
  • @EricG Here is the assignment. https://docs.google.com/viewer?a=v&pid=sites&srcid=ZGVmYXVsdGRvbWFpbnx0c2NvbXB1dGVyc2NpZW5jZXxneDo3Y2UzYzQ3NjdmYTM4Mzgw – James P Jan 21 '16 at 19:02
  • There is a lot of work left to be done on the assignment, you're going to have to try to work it out. – Eric G Jan 21 '16 at 19:07
  • 3
    Please do not deface your post. – NathanOliver Jan 21 '16 at 19:10

1 Answers1

1
for(int i = k; i <= board.length; i++){
    seeds--;
    board[i]++;
}

If board.length == 14

the loop will be running

board[12]
board[13]
board[14] out of bounds here as board goes from 0-13

Fixed version

for(int i = k; i < board.length; i++){
    seeds--;
    board[i]++;
}
Eric G
  • 928
  • 1
  • 9
  • 29
  • When I do this at the end of my code board[k] is -6. The while loop is not stopping the code when it gets to 0 for some reason. Also, how can I get it to not add seeds to board[k]? If I do int i = k then it will mess up the code because it's supposed to give seeds to the rest of the spaces. – James P Jan 21 '16 at 19:00