package controller;

import boundary.PlayerApplication;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

import model.Board;
import model.CellType;
import model.PlayerModel;
import model.Tile;
import model.Variation;

/**
 * @author Eli Skeggs, and Nick Chaput
 */
public class PlayerScrambleCtrl implements ActionListener {
  PlayerApplication app;
  PlayerModel model;

  public PlayerScrambleCtrl(PlayerApplication app, PlayerModel model) {
    this.app = app;
    this.model = model;
  }

  public void actionPerformed(ActionEvent event) {
    // TODO: generic implementation
    // model.variation.specialMove();
	 // System.out.println("Start Scramble");
	  ArrayList<Tile> tiles = new ArrayList<Tile>();
	  for (int x = 0; x < Board.width; x++) {
		  for (int y = 0; y < Board.height; y++) {
			  if (model.level.currentBoard.grid[x][y].type != CellType.INERT
					  && model.level.currentBoard.grid[x][y].type != CellType.BUCKET
					  && model.level.currentBoard.grid[x][y].tile != null
					  && (model.level.currentBoard.grid[x][y].tile.number != 5
							  && model.level.rules.variation != Variation.RELEASE)) {
				  tiles.add(model.level.currentBoard.grid[x][y].tile);
				  model.level.currentBoard.grid[x][y].tile = null;
			  }
		  }
	  }
	  Collections.shuffle(tiles);
	  for (int x = 0; x < Board.width; x++) {
		  for (int y = 0; y < Board.height; y++) {
			  if (model.level.currentBoard.grid[x][y].type != CellType.INERT
					  && model.level.currentBoard.grid[x][y].type != CellType.BUCKET
					  && model.level.currentBoard.grid[x][y].tile == null) {
				  if (tiles.get(0) != null) {
					  model.level.currentBoard.grid[x][y].tile = tiles.get(0);
					  tiles.remove(0);
				  }
				  else {
					  System.out.println("Nick's brain has failed and you should laugh at him.");
					  return;
				  }
			  }
		  }
	  }
	  model.variation.createCtrl(app, model).scramble();
	  app.getView().repaint();
  }
}

@@ -629,122 +629,57 @@
     
-// TODO: generic implementation%0A    // model.variation.specialMove();%0A%09 // System.out.println(%22Start Scramble%22);%0A%09
+ArrayList%3CCell%3E cells = new ArrayList%3CCell%3E();%0A  
   Ar
@@ -723,17 +723,18 @@
 ile%3E();%0A
-%09
+  
   for (i
@@ -759,36 +759,38 @@
 d.width; x++) %7B%0A
-%09%09
+   
   
+ 
 for (int y = 0; 
@@ -810,33 +810,44 @@
 ght; y++) %7B%0A
-%09%09%09  if (
+        Cell cell = 
 model.level.
@@ -861,32 +861,50 @@
 Board.grid%5Bx%5D%5By%5D
+;%0A        if (cell
 .type != CellTyp
@@ -915,183 +915,105 @@
 ERT%0A
-%09%09%09%09%09  && model.level.currentBoard.grid%5Bx%5D%5By%5D.type != CellType.BUCKET%0A%09%09%09%09%09  && model.level.currentBoard.grid%5Bx%5D%5By%5D.tile != null%0A%09%09%09%09%09  && (model.level.currentBoard.grid%5Bx%5D%5By%5D
+            && cell.type != CellType.BUCKET%0A            && cell.tile != null%0A            && (cell
 .til
@@ -1030,15 +1030,18 @@
 = 5%0A
-%09%09%09%09%09%09%09
+          
   &&
@@ -1098,727 +1098,273 @@
 ) %7B%0A
-%09%09%09%09  tiles.add(model.level.currentBoard.grid%5Bx%5D%5By%5D.tile);%0A%09%09%09%09  model.level.currentBoard.grid%5Bx%5D%5By%5D.tile = null;%0A%09%09%09  %7D%0A%09%09  %7D%0A%09  %7D%0A%09  Collections.shuffle(
+          cells.add(cell);%0A          tiles.add(cell.
 tile
-s
 );%0A
-%09
   
-for (int x = 0; x %3C Board.width; x++) %7B%0A%09%09  for (int y = 0; y %3C Board.height; y++) %7B%0A%09%09%09  if (model.level.currentBoard.grid%5Bx%5D%5By%5D.type != CellType.INERT%0A%09%09%09%09%09  && model.level.currentBoard.grid%5Bx%5D%5By%5D.type != CellType.BUCKET%0A%09%09%09%09%09  && model.level.currentBoard.grid%5Bx%5D%5By%5D.tile == null) %7B%0A%09%09%09%09  if (tiles.get(0) != null) %7B%0A%09%09%09%09%09  model.level.currentBoard.grid%5Bx%5D%5By%5D.tile =
+      %7D%0A      %7D%0A    %7D%0A    // TODO: should use seed%0A    Collections.shuffle(tiles);%0A    Iterator%3CTile%3E
  tile
-s.get(0);%0A%09%09%09%09%09  tiles.remove(0);%0A%09%09%09%09  %7D%0A%09%09%09%09  else %7B%0A%09%09%09%09%09  System.out.println(%22Nick's brain has failed and you should laugh at him.%22);%0A%09%09%09%09%09  return;%0A%09%09%09%09  %7D%0A%09%09%09  %7D%0A%09%09  %7D%0A%09  %7D%0A%09
+Iter = tiles.iterator();%0A    for (Cell cell : cells) %7B%0A      cell.tile = tileIter.next();%0A    %7D%0A  
   mo
@@ -1416,9 +1416,10 @@
 ();%0A
-%09
+  
   ap

Improve scramble algorithm