package me.dvorkin;
import java.util.Random;
public class NimModel {
private int[] piles;
private boolean move;
public NimModel() {
move = true;
Random random = new Random();
piles = new int[5];
for (int i = 0; i < piles.length; i++) {
piles[i] = random.nextInt(10) + 1;
}
}
public int[] getField() {
return piles.clone();
}
/**
* @return true if first player is to move
*/
public boolean whoMoves() {
return move;
}
public boolean isOver() {
for (int i : piles) {
if (i != 0) {
return false;
}
}
return true;
}
public boolean update(int index, int amount) {
if (index < 0 || index >= piles.length) {
return false;
}
if (amount <= 0 || amount > piles[index]) {
return false;
}
piles[index] -= amount;
move ^= true;
return true;
}
}
Comments