Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.ArrayList;
- import java.util.Set;
- public class Goban {
- protected HashMap<Pair<Integer, Integer>, StoneGroup> groups;
- protected HashMap<Pair<Integer, Integer>, Color> grid;
- static public int size = 19;
- public Goban() {
- groups = new HashMap<Pair<Integer, Integer>, StoneGroup>();
- grid = new HashMap<Pair<Integer, Integer>, Color>();
- for (int x=0; x < Goban.size; x++) {
- for (int y=0; y < Goban.size; y++) {
- Pair<Integer, Integer> position = Pair.of(x, y);
- grid.put(position, Color.VOID);
- }
- }
- }
- public boolean addStone(Color color, Pair<Integer, Integer> position) {
- if (isVoid(position)) {
- StoneGroup newGroup = new StoneGroup(color, position);
- ArrayList<Pair<Integer, Integer>> neighbours =
- getNeighbourhood(position);
- if (!neighbours.isEmpty()) {
- /* Si la pierre est encerclée, il faut déterminer si cette dernière
- * va capturer d'autres pierres.
- */
- if (neighbours.size() == 4) {
- boolean isKiller = false;
- HashMap<StoneGroup, Integer> livesGroup =
- new HashMap<StoneGroup, Integer>();
- for (Pair<Integer, Integer> s : neighbours) {
- StoneGroup group = groups.get(s);
- if (!group.getColor().equals(color)) {
- if (!livesGroup.containsKey(group)) {
- livesGroup.put(group, group.lives());
- }
- livesGroup.put(group, livesGroup.get(group)-1);
- if (livesGroup.get(group) == 0) {
- isKiller = true;
- }
- }
- }
- /* Si la pierre ne capture rien, le coup est refusé.
- */
- if (!isKiller) {
- return false;
- }
- }
- for (Pair<Integer, Integer> s : neighbours) {
- StoneGroup group = groups.get(s);
- if (group.getColor().equals(color)) {
- groups.put(s, group.fusion(newGroup));
- } else {
- if (!group.isAlive()) {
- deleteGroup(group);
- }
- }
- group.downDof(s);
- newGroup.downDof(position);
- grid.put(position, color);
- }
- }
- groups.put(position, newGroup);
- }
- return false;
- }
- private void deleteGroup(StoneGroup group) {
- Set<Pair<Integer, Integer>> stones = group.getStones();
- for (Pair<Integer, Integer> stone : stones) {
- ArrayList<Pair<Integer, Integer>> neighbours =
- getNeighbourhood(stone);
- /* Il faut mettre à jour la liberté des pierres ennemies adjacentes
- * au groupe qui va disparaître.
- */
- for (Pair<Integer, Integer> s : neighbours) {
- if (!grid.get(s).equals(group.getColor())) {
- groups.get(s).upDof(s);
- }
- }
- groups.remove(stone);
- }
- }
- public boolean isVoid(Pair<Integer, Integer> position) {
- return groups.get(position).equals(Color.VOID);
- }
- public ArrayList<Pair<Integer, Integer>> getNeighbourhood(
- Pair<Integer, Integer> position) {
- ArrayList<Pair<Integer, Integer>> neighbours;
- neighbours = new ArrayList<Pair<Integer, Integer>>();
- int offsets[][] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
- for (int[] offset : offsets){
- Pair<Integer, Integer> neighbour = Pair.of(
- position.first+offset[0], position.second+offset[1]
- );
- if (!isVoid(neighbour)) {
- neighbours.add(neighbour);
- }
- }
- return neighbours;
- }
- public void display() {
- for (int x = 0)
- }
- }
Add Comment
Please, Sign In to add comment