Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Arrays;
- public class HelloWorld
- {
- public static void main(String args[])
- {
- Mancala m = new Mancala(true);
- }
- }
- class Mancala
- {
- private int[] board;
- private boolean avalanche;
- private boolean free = true;
- Scanner scan;
- public Mancala(Boolean avalanche)
- {
- this.avalanche = avalanche;
- scan = new Scanner(System.in);
- board = new int[14];
- for(int i = 0; i < 14; i++) // 0 = opponent's mancala, moving counterclockwise
- {
- board[i] = scan.nextInt();
- }
- while(!isEmpty(board) && free)
- {
- int bestMove = bestMove(board, 0)[0];
- System.out.println(bestMove);
- if(board[bestMove] == 0)
- break;
- moveBoard(bestMove);
- print(board);
- }
- }
- public boolean isEmpty(int[] b)
- {
- for(int i = 1; i < 7; i++)
- {
- if(b[i] != 0)
- return false;
- }
- System.out.println("Empty board");
- return true;
- }
- public void moveBoard(int slot)
- {
- print(board);
- int stones = board[slot];
- if(stones == 0)
- System.out.println("Something's wrong, Jim.");
- board[slot] = 0;
- while(stones > 0)
- {
- slot = slot % 13 + 1; // next hole
- board[slot]++; // add one to the hole
- stones--; // subtract one from stones in hand
- }
- if(board[slot] != 1 && slot != 7)
- moveBoard(slot);
- else if(slot == 7)
- free = true;
- else
- free = false;
- }
- public int[] bestMove(int[] s, int currentScore) // call move() 6 times, returning the slot of the best option
- {
- boolean match = true;
- int[] state = new int[14]; // duplicate the array to avoid aliasing
- for(int i = 0; i < 14; i++)
- {
- if(s[i] != board[i])
- match = false;
- state[i] = s[i];
- }
- boolean empty = true; // if your board is empty, return best possible value
- for(int i = 1; i < 7; i++)
- {
- if(state[i] != 0)
- empty = false;
- }
- if(empty)
- return new int[]{-1, currentScore};
- int best = -1;
- int bestScore = -1;
- for(int i = 1; i < 7; i++) // range constricted for testing
- {
- int thisScore = move(i, state, currentScore);
- if(match)
- System.out.println("Slot: " + i + " Score: " + thisScore);
- if(thisScore > bestScore)
- {
- best = i;
- bestScore = thisScore;
- }
- }
- return new int[]{best, bestScore};
- }
- public int move(int position, int[] s, int score) // return the number of stones that this move scores
- {
- int[] state = new int[14]; // duplicate the array to avoid aliasing
- for(int i = 0; i < 14; i++)
- {
- state[i] = s[i];
- }
- if(state[position] == 0)
- {
- return score;
- }
- int stones = state[position];
- state[position] = 0; // empty the current hole
- while(stones > 0)
- {
- position = position % 13 + 1;
- state[position]++; // add one to the hole
- stones--; // subtract one from stones in hand
- if(position == 7) // award a point if caching a stone
- score++;
- }
- if(position == 7) // check if free move
- {
- int[] bestMove = bestMove(state, score);
- score = bestMove[1];
- return score;
- }
- if(state[position] == 1) // if last stone was placed in empty hole, end recursion
- return score;
- return move(position, state, score); // otherwise, continue the avalanche
- }
- public void print(int[] b)
- {
- for(int i = 0; i < 14; i++)
- {
- System.out.print(b[i] + " ");
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement