Advertisement
Xtremecake123

Untitled

Oct 1st, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. private ArrayList<Move> withinGroup(Move inGroup, Player[][] localBoard) {
  2.        
  3.         //'whose' contains a 'Player' enumeration that represents whether the group is of white or black stones.
  4.         Player whose = localBoard[inGroup.y][inGroup.x];
  5.         int whoseInt;
  6.        
  7.         if (whose.equals(Player.BLACK)){
  8.                 whoseInt = 1;
  9.         }else if(whose.equals(Player.WHITE)) {
  10.                 whoseInt = 2;
  11.         }else return null;
  12.        
  13.         //'whoseInt' contains an integer value, either 1 or 2, that represents which color the group is, 1 for black, and 2 for white
  14.        
  15.         ArrayList<Move> toVisit = new ArrayList<Move>();
  16.        
  17.         //toVisit will contain all unvisited stones in the group
  18.        
  19.         ArrayList<Move> visited = new ArrayList<Move>();
  20.        
  21.         //visited will contain all visited stones in the group
  22.        
  23.         Move currentMove = inGroup;
  24.         toVisit.add(inGroup);
  25.        
  26.        
  27.         //this loop finds all stones within the group, and adds them to the arraylists.
  28.        
  29.         while(toVisit.size() > 0) {
  30.            
  31.             currentMove = toVisit.get(0);
  32.            
  33.             int[] adjTiles = adjacentTiles(currentMove, localBoard);
  34.             Move moveToCheck = new Move(currentMove.x + 1,currentMove.y);
  35.             if (adjTiles[0] == whoseInt && !toVisit.contains(moveToCheck) && !visited.contains(moveToCheck)){
  36.                 toVisit.add(moveToCheck);
  37.             }
  38.             moveToCheck = new Move(currentMove.x,currentMove.y - 1);
  39.             if (adjTiles[1] == whoseInt && !toVisit.contains(moveToCheck) && !visited.contains(moveToCheck)){
  40.                 toVisit.add(moveToCheck);
  41.             }
  42.             moveToCheck = new Move(currentMove.x - 1,currentMove.y);
  43.             if (adjTiles[2] == whoseInt && !toVisit.contains(moveToCheck) && !visited.contains(moveToCheck)){
  44.                 toVisit.add(moveToCheck);
  45.             }
  46.             moveToCheck = new Move(currentMove.x,currentMove.y + 1);
  47.             if (adjTiles[3] == whoseInt && !toVisit.contains(moveToCheck) && !visited.contains(moveToCheck)){
  48.                 toVisit.add(moveToCheck);
  49.             }
  50.             visited.add(currentMove);
  51.             toVisit.remove(toVisit.indexOf(currentMove));
  52.         }
  53.        
  54.         return visited;    
  55.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement