Guest User

Untitled

a guest
Oct 17th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.ArrayList;
  3. import java.util.Set;
  4.  
  5. public class Goban {
  6.     protected HashMap<Pair<Integer, Integer>, StoneGroup> groups;
  7.     protected HashMap<Pair<Integer, Integer>, Color> grid;
  8.     static public int size = 19;
  9.    
  10.     public Goban() {
  11.         groups = new HashMap<Pair<Integer, Integer>, StoneGroup>();
  12.         grid = new HashMap<Pair<Integer, Integer>, Color>();
  13.        
  14.         for (int x=0; x < Goban.size; x++) {
  15.             for (int y=0; y < Goban.size; y++) {
  16.                 Pair<Integer, Integer> position = Pair.of(x, y);
  17.                 grid.put(position, Color.VOID);
  18.             }
  19.         }
  20.     }
  21.    
  22.     public boolean addStone(Color color, Pair<Integer, Integer> position) {
  23.         if (isVoid(position)) {
  24.             StoneGroup newGroup = new StoneGroup(color, position);
  25.             ArrayList<Pair<Integer, Integer>> neighbours =
  26.                 getNeighbourhood(position);
  27.            
  28.             if (!neighbours.isEmpty()) {
  29.                 /* Si la pierre est encerclée, il faut déterminer si cette dernière
  30.                  * va capturer d'autres pierres.
  31.                  */
  32.                 if (neighbours.size() == 4) {
  33.                     boolean isKiller = false;
  34.                     HashMap<StoneGroup, Integer> livesGroup =
  35.                         new HashMap<StoneGroup, Integer>();
  36.                    
  37.                     for (Pair<Integer, Integer> s : neighbours) {
  38.                         StoneGroup group = groups.get(s);
  39.                        
  40.                         if (!group.getColor().equals(color)) {
  41.                             if (!livesGroup.containsKey(group)) {
  42.                                 livesGroup.put(group, group.lives());
  43.                             }
  44.                             livesGroup.put(group, livesGroup.get(group)-1);
  45.                             if (livesGroup.get(group) == 0) {
  46.                                 isKiller = true;
  47.                             }
  48.                         }
  49.                     }
  50.                    
  51.                     /* Si la pierre ne capture rien, le coup est refusé.
  52.                      */
  53.                     if (!isKiller) {
  54.                         return false;
  55.                     }
  56.                 }
  57.                
  58.                 for (Pair<Integer, Integer> s : neighbours) {
  59.                     StoneGroup group = groups.get(s);
  60.                     if (group.getColor().equals(color)) {
  61.                         groups.put(s, group.fusion(newGroup));
  62.                     } else {
  63.                         if (!group.isAlive()) {
  64.                             deleteGroup(group);
  65.                         }
  66.                     }
  67.                    
  68.                     group.downDof(s);
  69.                     newGroup.downDof(position);
  70.                     grid.put(position, color);
  71.                 }
  72.             }
  73.            
  74.             groups.put(position, newGroup);
  75.         }
  76.        
  77.         return false;
  78.     }
  79.    
  80.     private void deleteGroup(StoneGroup group) {
  81.         Set<Pair<Integer, Integer>> stones = group.getStones();
  82.         for (Pair<Integer, Integer> stone : stones) {
  83.             ArrayList<Pair<Integer, Integer>> neighbours =
  84.                 getNeighbourhood(stone);
  85.            
  86.             /* Il faut mettre à jour la liberté des pierres ennemies adjacentes
  87.              * au groupe qui va disparaître.
  88.              */
  89.             for (Pair<Integer, Integer> s : neighbours) {
  90.                 if (!grid.get(s).equals(group.getColor())) {
  91.                     groups.get(s).upDof(s);
  92.                 }
  93.             }
  94.             groups.remove(stone);
  95.         }
  96.     }
  97.    
  98.     public boolean isVoid(Pair<Integer, Integer> position) {
  99.         return groups.get(position).equals(Color.VOID);
  100.     }
  101.    
  102.     public ArrayList<Pair<Integer, Integer>> getNeighbourhood(
  103.             Pair<Integer, Integer> position) {
  104.         ArrayList<Pair<Integer, Integer>> neighbours;
  105.         neighbours = new ArrayList<Pair<Integer, Integer>>();
  106.        
  107.         int offsets[][] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
  108.         for (int[] offset : offsets){
  109.             Pair<Integer, Integer> neighbour = Pair.of(
  110.                     position.first+offset[0], position.second+offset[1]
  111.                 );
  112.            
  113.             if (!isVoid(neighbour)) {
  114.                 neighbours.add(neighbour);
  115.             }
  116.         }
  117.        
  118.         return neighbours;
  119.     }
  120.    
  121.     public void display() {
  122.         for (int x = 0)
  123.     }
  124. }
Add Comment
Please, Sign In to add comment