From e81406253ee8dc60c05da679c70589db3fa58943 Mon Sep 17 00:00:00 2001 From: Gustav Schoelin Date: Thu, 15 Feb 2018 17:43:31 -0800 Subject: [PATCH 1/9] GS/KW Created a launcher class and reformated all classes --- .../games/flood_it/view/FloodItGUI.java | 464 ++++++++---------- .../games/flood_it/view/FloodItGrid.java | 72 +-- .../flood_it/view/FloodItInstructGui.java | 108 ++-- .../games/flood_it/view/FloodItLauncher.java | 43 ++ 4 files changed, 341 insertions(+), 346 deletions(-) create mode 100644 src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java index 0641de8..d704764 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java @@ -10,6 +10,7 @@ /** * Class for the Flood it game JFrame * includes the main method + * * @author Daniel Ben-Naim * @author Dylan Hanson * @author Sophia Mao @@ -17,10 +18,10 @@ * @author Chris Luo * @author Kevin Briggs */ -public class FloodItGUI extends JFrame{ - +public class FloodItGUI extends JFrame { + //private variables for all the GUI components - + private JFrame frame; private Container textContainer; private FloodItGrid gridBoard; @@ -30,288 +31,239 @@ public class FloodItGUI extends JFrame{ private JPanel buttonPanel; private JTextField countdown; private JLabel movesLeft; - private int [][] grid; - private final Color[] colors = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW, - Color.MAGENTA, Color.CYAN,Color.ORANGE,Color.BLACK}; - private String[] colorNames = {"Red","Blue","Green","Yellow","Magenta","Cyan","Orange","Black"}; + private int[][] grid; + private final Color[] colors = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW, + Color.MAGENTA, Color.CYAN, Color.ORANGE, Color.BLACK}; + private String[] colorNames = {"Red", "Blue", "Green", "Yellow", "Magenta", "Cyan", "Orange", "Black"}; private int numColors; private int dimension; private int difficultyLevel; private Integer MOVES_LEFT; - + /** - * FloodItGUI constructor creates an instance of the game - * - * @param dimension the grid will be dimension x dimension - * @param numColors the number of colors available - * @param difficultyLevel how difficult the game is - */ - public FloodItGUI(int dimension, int numColors, int difficultyLevel){ - this.dimension = dimension; - this.numColors = numColors; - this.difficultyLevel = difficultyLevel; + * FloodItGUI constructor creates an instance of the game + * + * @param dimension the grid will be dimension x dimension + * @param numColors the number of colors available + * @param difficultyLevel how difficult the game is + */ + public FloodItGUI(int dimension, int numColors, int difficultyLevel) { + this.dimension = dimension; + this.numColors = numColors; + this.difficultyLevel = difficultyLevel; } /** - * init initializes the game and draws the board. - * - */ - public void init(){ - //set MovesLeft (scales number of moves based on number of colors and dimension - //selected using 25 moves for a 6 color, 14x14 grid as a baseline. - MOVES_LEFT = (int)Math.floor(dimension*numColors*25/84);//Thanks, autoboxing! - if(difficultyLevel == 1) MOVES_LEFT = (int)Math.floor(MOVES_LEFT*.8); - if(difficultyLevel == 3) MOVES_LEFT = (int)Math.floor(MOVES_LEFT * 2.33); - //set JFrame properties - frame = new JFrame("Flood It! by SM and KJ and KB and CL and DH and DBN"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setSize(1000,800); - grid = PopulateGrid(dimension, numColors, difficultyLevel); - gridBoard = new FloodItGrid(grid, colors); - buttonInstruction = new JButton("Instructions"); - //set JTextArea properties for the big message returning box - messageArea = new JTextArea(40,20); - messageArea.setEditable(false); - JScrollPane messageScroller = new JScrollPane(messageArea); - messageScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); - //set JTextField properties for countdown box - countdown = new JTextField(MOVES_LEFT.toString(),2); - countdown.setEditable(false); - //JLabel for the countdown JTextArea - movesLeft = new JLabel("moves left:"); - //Panel that holds the color buttons and countdown - buttonPanel = new JPanel(); - //add Countdown components to buttonPanel - buttonPanel.add(movesLeft); - buttonPanel.add(countdown); - //ALL button properties - for(int i=0; i1) - previousColor = result[i-1][j]; - } - result[i][j] = previousColor; - } - } - return result; + * PopulateGrid populates the grid of a given size and difficulty level + * + * @param dimension the grid will be dimension x dimension + * @param numColors the number of colors in the grid + * @param difficultyLevel a number 1,2,3 representing easy medium or hard + * @return dimensionxdimension matrix of ints represnting the game board + */ + public int[][] PopulateGrid(int dimension, int numColors, int difficultyLevel) { + if (difficultyLevel == 1) + return PopulateGridEasy(dimension, numColors); + if (difficultyLevel == 2) + return PopulateGridMedium(dimension, numColors); + if (difficultyLevel == 3) + return PopulateGridHard(dimension, numColors); + return PopulateGridMedium(dimension, numColors); } /** - * PopulateGridEasy populates a medium grid of a given size - * - * @param dimension the grid will be dimension x dimension - * @param numColors the number of colors in the grid - * @return dimensionxdimension matrix of ints represnting the game board - */ - public int[][] PopulateGridMedium(int dimension, int numColors){ - int[][] result = new int[dimension][dimension]; - for(int i=0; i 1) + previousColor = result[i - 1][j]; + } + result[i][j] = previousColor; + } + } + return result; } - /** - * PopulateGridEasy populates a hard grid of a given size - * - * @param dimension the grid will be dimension x dimension - * @param numColors the number of colors in the grid - * @return dimensionxdimension matrix of ints represnting the game board - */ - public int[][] PopulateGridHard(int dimension, int numColors){ - int currentColor = (int)(Math.random()*numColors); - int [][] result = new int[dimension][dimension]; - for(int i=0; i16) - { - System.out.println("Invalid input"); - dimension = S.nextInt(); - } - System.out.println("Enter the number of colors you want: (between 3 and 8)"); - numColors = S.nextInt(); - while(numColors<3||numColors>8) - { - numColors = S.nextInt(); - System.out.println("Invalid input"); - } - System.out.println("Enter 1 for easy, 2 for medium, 3 for hard."); - difficultyLevel = S.nextInt(); - while(difficultyLevel<1||difficultyLevel>3) - { - difficultyLevel = S.nextInt(); - System.out.println("Invalid input"); - } - FloodItGUI game = new FloodItGUI(dimension, numColors, difficultyLevel); - game.init(); + * floodIt redraws the matrix after the player makes a move + * it is adapted from Wikipedia's algorithm: en.wikipedia.org/wiki/Flood_fill + * + * @param x the x location in the matrix + * @param y the y location in the matrix + * @param newColor the new color being painted + * @param oldColor the color to be repainted + */ + public void floodIt(int x, int y, int newColor, int oldColor) { + if (x < 0 || y < 0 || x >= grid.length || y >= grid.length) return; + if (grid[x][y] != oldColor) return; + grid[x][y] = newColor; + floodIt(x, y + 1, newColor, oldColor); + floodIt(x, y - 1, newColor, oldColor); + floodIt(x + 1, y, newColor, oldColor); + floodIt(x - 1, y, newColor, oldColor); + return; } - + /** - * floodIt redraws the matrix after the player makes a move - * it is adapted from Wikipedia's algorithm: en.wikipedia.org/wiki/Flood_fill - * - * @param x the x location in the matrix - * @param y the y location in the matrix - * @param newColor the new color being painted - * @param oldColor the color to be repainted - */ - public void floodIt(int x, int y, int newColor, int oldColor){ - if(x<0 || y<0 || x>=grid.length || y>= grid.length) return; - if(grid[x][y] != oldColor) return; - grid[x][y] = newColor; - floodIt(x, y+1, newColor, oldColor); - floodIt(x, y-1, newColor, oldColor); - floodIt(x+1, y, newColor, oldColor); - floodIt(x-1, y, newColor, oldColor); - return; - } + * checkWin checks if the player has won the game + * + * @return true if win, false if not. + */ + public boolean checkWin() { + for (int i = 0; i < grid.length; i++) + for (int j = 0; j < grid.length; j++) + if (grid[i][j] != grid[0][0]) return false; + return true; - /** - * checkWin checks if the player has won the game - * - * @return true if win, false if not. - */ - public boolean checkWin(){ - for(int i=0; i + * This is the utility function of the class for redrawing the * grid when the user makes a move. - * @param grid A NxN Matrix of integers representing colors in an array + * + * @param grid A NxN Matrix of integers representing colors in an array * @param colors an array of Colors */ - public void redrawLabel(int [][] grid, Color[] colors){ - removeAll(); - - setLayout(new GridLayout(grid.length, grid.length)); - for(int j=0; j 16) { + System.out.println("Invalid input"); + dimension = S.nextInt(); + } + System.out.println("Enter the number of colors you want: (between 3 and 8)"); + numColors = S.nextInt(); + while (numColors < 3 || numColors > 8) { + numColors = S.nextInt(); + System.out.println("Invalid input"); + } + System.out.println("Enter 1 for easy, 2 for medium, 3 for hard."); + difficultyLevel = S.nextInt(); + while (difficultyLevel < 1 || difficultyLevel > 3) { + difficultyLevel = S.nextInt(); + System.out.println("Invalid input"); + } + FloodItGUI game = new FloodItGUI(dimension, numColors, difficultyLevel); + game.init(); + } + +} From 0db075a48cb7ac8824e4f7bf4ac80195475bba8a Mon Sep 17 00:00:00 2001 From: Gustav Schoelin Date: Thu, 15 Feb 2018 17:57:07 -0800 Subject: [PATCH 2/9] GS/KW Created a Controller class --- .../flood_it/view/FloodItController.java | 144 ++++++++++++++++++ .../games/flood_it/view/FloodItGUI.java | 124 +-------------- 2 files changed, 145 insertions(+), 123 deletions(-) create mode 100644 src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java new file mode 100644 index 0000000..a2cffc2 --- /dev/null +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java @@ -0,0 +1,144 @@ +package edu.ucsb.cs56.projects.games.flood_it.view; + +public class FloodItController { + + private int[][] grid; + + /** + * FloodItGUI constructor creates an instance of the game + * + * @param dimension the grid will be dimension x dimension + * @param numColors the number of colors available + * @param difficultyLevel how difficult the game is + */ + public FloodItGUI(int dimension, int numColors, int difficultyLevel) { + this.dimension = dimension; + this.numColors = numColors; + this.difficultyLevel = difficultyLevel; + } + + /** + * PopulateGrid populates the grid of a given size and difficulty level + * + * @param dimension the grid will be dimension x dimension + * @param numColors the number of colors in the grid + * @param difficultyLevel a number 1,2,3 representing easy medium or hard + * @return dimensionxdimension matrix of ints represnting the game board + */ + public int[][] PopulateGrid(int dimension, int numColors, int difficultyLevel) { + if (difficultyLevel == 1) + return PopulateGridEasy(dimension, numColors); + if (difficultyLevel == 2) + return PopulateGridMedium(dimension, numColors); + if (difficultyLevel == 3) + return PopulateGridHard(dimension, numColors); + return PopulateGridMedium(dimension, numColors); + } + + /** + * PopulateGridEasy populates an easy grid of a given size + * + * @param dimension the grid will be dimension x dimension + * @param numColors the number of colors in the grid + * @return dimensionxdimension matrix of ints represnting the game board + */ + public int[][] PopulateGridEasy(int dimension, int numColors) { + int previousColor = (int) (Math.random() * numColors); + int[][] result = new int[dimension][dimension]; + for (int i = 0; i < dimension; i++) { + for (int j = 0; j < dimension; j++) { + if (Math.random() < .5) { + previousColor = (int) (Math.random() * numColors); + } else if (Math.random() < .5) { + if (i > 1) + previousColor = result[i - 1][j]; + } + result[i][j] = previousColor; + } + } + return result; + } + + /** + * PopulateGridEasy populates a medium grid of a given size + * + * @param dimension the grid will be dimension x dimension + * @param numColors the number of colors in the grid + * @return dimensionxdimension matrix of ints represnting the game board + */ + public int[][] PopulateGridMedium(int dimension, int numColors) { + int[][] result = new int[dimension][dimension]; + for (int i = 0; i < dimension; i++) { + for (int j = 0; j < dimension; j++) { + result[i][j] = (int) (Math.random() * numColors); + } + } + return result; + } + + + /** + * PopulateGridEasy populates a hard grid of a given size + * + * @param dimension the grid will be dimension x dimension + * @param numColors the number of colors in the grid + * @return dimensionxdimension matrix of ints represnting the game board + */ + public int[][] PopulateGridHard(int dimension, int numColors) { + int currentColor = (int) (Math.random() * numColors); + int[][] result = new int[dimension][dimension]; + for (int i = 0; i < dimension; i++) { + for (int j = 0; j < dimension; j++) { + if (i == 0 && j == 0) result[i][j] = (int) (Math.random() * numColors); + if (i != 0 && j != 0) { + while (currentColor == result[i - 1][j] || currentColor == result[i][j - 1]) + currentColor = (int) (Math.random() * numColors); + result[i][j] = currentColor; + } else if (i != 0) { + while (currentColor == result[i - 1][j]) + currentColor = (int) (Math.random() * numColors); + result[i][j] = currentColor; + } else if (j != 0) { + while (currentColor == result[i][j - 1]) + currentColor = (int) (Math.random() * numColors); + result[i][j] = currentColor; + } + } + } + return result; + } + + + /** + * floodIt redraws the matrix after the player makes a move + * it is adapted from Wikipedia's algorithm: en.wikipedia.org/wiki/Flood_fill + * + * @param x the x location in the matrix + * @param y the y location in the matrix + * @param newColor the new color being painted + * @param oldColor the color to be repainted + */ + public void floodIt(int x, int y, int newColor, int oldColor) { + if (x < 0 || y < 0 || x >= grid.length || y >= grid.length) return; + if (grid[x][y] != oldColor) return; + grid[x][y] = newColor; + floodIt(x, y + 1, newColor, oldColor); + floodIt(x, y - 1, newColor, oldColor); + floodIt(x + 1, y, newColor, oldColor); + floodIt(x - 1, y, newColor, oldColor); + return; + } + + /** + * checkWin checks if the player has won the game + * + * @return true if win, false if not. + */ + public boolean checkWin() { + for (int i = 0; i < grid.length; i++) + for (int j = 0; j < grid.length; j++) + if (grid[i][j] != grid[0][0]) return false; + return true; + + } +} diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java index d704764..ffef073 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java @@ -66,6 +66,7 @@ public void init() { frame = new JFrame("Flood It! by SM and KJ and KB and CL and DH and DBN"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(1000, 800); + FloodItController controller = new FloodItController(dimension, numColors, difficultyLevel); grid = PopulateGrid(dimension, numColors, difficultyLevel); gridBoard = new FloodItGrid(grid, colors); buttonInstruction = new JButton("Instructions"); @@ -142,128 +143,5 @@ public Integer decrementAMove() { } } - /** - * PopulateGrid populates the grid of a given size and difficulty level - * - * @param dimension the grid will be dimension x dimension - * @param numColors the number of colors in the grid - * @param difficultyLevel a number 1,2,3 representing easy medium or hard - * @return dimensionxdimension matrix of ints represnting the game board - */ - public int[][] PopulateGrid(int dimension, int numColors, int difficultyLevel) { - if (difficultyLevel == 1) - return PopulateGridEasy(dimension, numColors); - if (difficultyLevel == 2) - return PopulateGridMedium(dimension, numColors); - if (difficultyLevel == 3) - return PopulateGridHard(dimension, numColors); - return PopulateGridMedium(dimension, numColors); - } - - /** - * PopulateGridEasy populates an easy grid of a given size - * - * @param dimension the grid will be dimension x dimension - * @param numColors the number of colors in the grid - * @return dimensionxdimension matrix of ints represnting the game board - */ - public int[][] PopulateGridEasy(int dimension, int numColors) { - int previousColor = (int) (Math.random() * numColors); - int[][] result = new int[dimension][dimension]; - for (int i = 0; i < dimension; i++) { - for (int j = 0; j < dimension; j++) { - if (Math.random() < .5) { - previousColor = (int) (Math.random() * numColors); - } else if (Math.random() < .5) { - if (i > 1) - previousColor = result[i - 1][j]; - } - result[i][j] = previousColor; - } - } - return result; - } - - /** - * PopulateGridEasy populates a medium grid of a given size - * - * @param dimension the grid will be dimension x dimension - * @param numColors the number of colors in the grid - * @return dimensionxdimension matrix of ints represnting the game board - */ - public int[][] PopulateGridMedium(int dimension, int numColors) { - int[][] result = new int[dimension][dimension]; - for (int i = 0; i < dimension; i++) { - for (int j = 0; j < dimension; j++) { - result[i][j] = (int) (Math.random() * numColors); - } - } - return result; - } - - /** - * PopulateGridEasy populates a hard grid of a given size - * - * @param dimension the grid will be dimension x dimension - * @param numColors the number of colors in the grid - * @return dimensionxdimension matrix of ints represnting the game board - */ - public int[][] PopulateGridHard(int dimension, int numColors) { - int currentColor = (int) (Math.random() * numColors); - int[][] result = new int[dimension][dimension]; - for (int i = 0; i < dimension; i++) { - for (int j = 0; j < dimension; j++) { - if (i == 0 && j == 0) result[i][j] = (int) (Math.random() * numColors); - if (i != 0 && j != 0) { - while (currentColor == result[i - 1][j] || currentColor == result[i][j - 1]) - currentColor = (int) (Math.random() * numColors); - result[i][j] = currentColor; - } else if (i != 0) { - while (currentColor == result[i - 1][j]) - currentColor = (int) (Math.random() * numColors); - result[i][j] = currentColor; - } else if (j != 0) { - while (currentColor == result[i][j - 1]) - currentColor = (int) (Math.random() * numColors); - result[i][j] = currentColor; - } - } - } - return result; - } - - - /** - * floodIt redraws the matrix after the player makes a move - * it is adapted from Wikipedia's algorithm: en.wikipedia.org/wiki/Flood_fill - * - * @param x the x location in the matrix - * @param y the y location in the matrix - * @param newColor the new color being painted - * @param oldColor the color to be repainted - */ - public void floodIt(int x, int y, int newColor, int oldColor) { - if (x < 0 || y < 0 || x >= grid.length || y >= grid.length) return; - if (grid[x][y] != oldColor) return; - grid[x][y] = newColor; - floodIt(x, y + 1, newColor, oldColor); - floodIt(x, y - 1, newColor, oldColor); - floodIt(x + 1, y, newColor, oldColor); - floodIt(x - 1, y, newColor, oldColor); - return; - } - - /** - * checkWin checks if the player has won the game - * - * @return true if win, false if not. - */ - public boolean checkWin() { - for (int i = 0; i < grid.length; i++) - for (int j = 0; j < grid.length; j++) - if (grid[i][j] != grid[0][0]) return false; - return true; - - } } From c66e3e4ddadb1c30f9fbd98fdc24e07770a9fb1c Mon Sep 17 00:00:00 2001 From: Gustav Schoelin Date: Thu, 22 Feb 2018 17:52:59 -0800 Subject: [PATCH 3/9] GS/KW Continued refactoring, fixed input validation --- build.xml | 2 +- .../flood_it/view/FloodItController.java | 42 +++++++++++++------ .../games/flood_it/view/FloodItGUI.java | 29 ++++++------- .../games/flood_it/view/FloodItLauncher.java | 7 ++-- 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/build.xml b/build.xml index ba63387..493ad64 100644 --- a/build.xml +++ b/build.xml @@ -8,7 +8,7 @@ updated by: Chris Luo & Kevin Briggs --> - + diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java index a2cffc2..df9387d 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java @@ -2,6 +2,10 @@ public class FloodItController { + private int numColors; + private int dimension; + private int difficultyLevel; + private int[][] grid; /** @@ -11,38 +15,38 @@ public class FloodItController { * @param numColors the number of colors available * @param difficultyLevel how difficult the game is */ - public FloodItGUI(int dimension, int numColors, int difficultyLevel) { + public FloodItController(int dimension, int numColors, int difficultyLevel) { this.dimension = dimension; this.numColors = numColors; this.difficultyLevel = difficultyLevel; } /** - * PopulateGrid populates the grid of a given size and difficulty level + * populateGrid populates the grid of a given size and difficulty level * * @param dimension the grid will be dimension x dimension * @param numColors the number of colors in the grid * @param difficultyLevel a number 1,2,3 representing easy medium or hard * @return dimensionxdimension matrix of ints represnting the game board */ - public int[][] PopulateGrid(int dimension, int numColors, int difficultyLevel) { + public int[][] populateGrid(int dimension, int numColors, int difficultyLevel) { if (difficultyLevel == 1) - return PopulateGridEasy(dimension, numColors); + return populateGridEasy(dimension, numColors); if (difficultyLevel == 2) - return PopulateGridMedium(dimension, numColors); + return populateGridMedium(dimension, numColors); if (difficultyLevel == 3) - return PopulateGridHard(dimension, numColors); - return PopulateGridMedium(dimension, numColors); + return populateGridHard(dimension, numColors); + return populateGridMedium(dimension, numColors); } /** - * PopulateGridEasy populates an easy grid of a given size + * populateGridEasy populates an easy grid of a given size * * @param dimension the grid will be dimension x dimension * @param numColors the number of colors in the grid * @return dimensionxdimension matrix of ints represnting the game board */ - public int[][] PopulateGridEasy(int dimension, int numColors) { + public int[][] populateGridEasy(int dimension, int numColors) { int previousColor = (int) (Math.random() * numColors); int[][] result = new int[dimension][dimension]; for (int i = 0; i < dimension; i++) { @@ -60,13 +64,13 @@ public int[][] PopulateGridEasy(int dimension, int numColors) { } /** - * PopulateGridEasy populates a medium grid of a given size + * populateGridEasy populates a medium grid of a given size * * @param dimension the grid will be dimension x dimension * @param numColors the number of colors in the grid * @return dimensionxdimension matrix of ints represnting the game board */ - public int[][] PopulateGridMedium(int dimension, int numColors) { + public int[][] populateGridMedium(int dimension, int numColors) { int[][] result = new int[dimension][dimension]; for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { @@ -78,13 +82,13 @@ public int[][] PopulateGridMedium(int dimension, int numColors) { /** - * PopulateGridEasy populates a hard grid of a given size + * populateGridEasy populates a hard grid of a given size * * @param dimension the grid will be dimension x dimension * @param numColors the number of colors in the grid * @return dimensionxdimension matrix of ints represnting the game board */ - public int[][] PopulateGridHard(int dimension, int numColors) { + public int[][] populateGridHard(int dimension, int numColors) { int currentColor = (int) (Math.random() * numColors); int[][] result = new int[dimension][dimension]; for (int i = 0; i < dimension; i++) { @@ -141,4 +145,16 @@ public boolean checkWin() { return true; } + + public int getDifficultyLevel() { + return difficultyLevel; + } + + public int getDimension() { + return dimension; + } + + public int getNumColors() { + return numColors; + } } diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java index ffef073..9215825 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java @@ -4,7 +4,6 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; -import java.util.Scanner; /** @@ -22,6 +21,7 @@ public class FloodItGUI extends JFrame { //private variables for all the GUI components + private FloodItController controller; private JFrame frame; private Container textContainer; private FloodItGrid gridBoard; @@ -35,28 +35,24 @@ public class FloodItGUI extends JFrame { private final Color[] colors = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW, Color.MAGENTA, Color.CYAN, Color.ORANGE, Color.BLACK}; private String[] colorNames = {"Red", "Blue", "Green", "Yellow", "Magenta", "Cyan", "Orange", "Black"}; - private int numColors; - private int dimension; - private int difficultyLevel; private Integer MOVES_LEFT; /** * FloodItGUI constructor creates an instance of the game * - * @param dimension the grid will be dimension x dimension - * @param numColors the number of colors available - * @param difficultyLevel how difficult the game is + * @param controller */ - public FloodItGUI(int dimension, int numColors, int difficultyLevel) { - this.dimension = dimension; - this.numColors = numColors; - this.difficultyLevel = difficultyLevel; + public FloodItGUI(FloodItController controller) { + this.controller = controller; } /** * init initializes the game and draws the board. */ public void init() { + int dimension = controller.getDimension(); + int numColors = controller.getNumColors(); + int difficultyLevel = controller.getDifficultyLevel(); //set MovesLeft (scales number of moves based on number of colors and dimension //selected using 25 moves for a 6 color, 14x14 grid as a baseline. MOVES_LEFT = (int) Math.floor(dimension * numColors * 25 / 84);//Thanks, autoboxing! @@ -66,8 +62,7 @@ public void init() { frame = new JFrame("Flood It! by SM and KJ and KB and CL and DH and DBN"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(1000, 800); - FloodItController controller = new FloodItController(dimension, numColors, difficultyLevel); - grid = PopulateGrid(dimension, numColors, difficultyLevel); + grid = controller.populateGrid(dimension, numColors, difficultyLevel); gridBoard = new FloodItGrid(grid, colors); buttonInstruction = new JButton("Instructions"); //set JTextArea properties for the big message returning box @@ -93,16 +88,16 @@ public void init() { buttonPanel.add(currentButton); currentButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - if (MOVES_LEFT != 0 && !checkWin() && grid[0][0] != k) { + if (MOVES_LEFT != 0 && !controller.checkWin() && grid[0][0] != k) { countdown.setText(decrementAMove().toString()); messageArea.append(colorNames[k] + "\n"); - floodIt(0, 0, k, grid[0][0]); + controller.floodIt(0, 0, k, grid[0][0]); gridBoard.redrawLabel(grid, colors); - if (checkWin()) + if (controller.checkWin()) messageArea.append("You Win :D\n"); else if (MOVES_LEFT == 0) messageArea.append("You Lose :(\n"); - } else if (MOVES_LEFT != 0 && !checkWin()) + } else if (MOVES_LEFT != 0 && !controller.checkWin()) messageArea.append("Invalid move.\n"); } }); diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java index a062eb6..49ffdf7 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java @@ -27,16 +27,17 @@ public static void main(String args[]) { System.out.println("Enter the number of colors you want: (between 3 and 8)"); numColors = S.nextInt(); while (numColors < 3 || numColors > 8) { - numColors = S.nextInt(); System.out.println("Invalid input"); + numColors = S.nextInt(); } System.out.println("Enter 1 for easy, 2 for medium, 3 for hard."); difficultyLevel = S.nextInt(); while (difficultyLevel < 1 || difficultyLevel > 3) { - difficultyLevel = S.nextInt(); System.out.println("Invalid input"); + difficultyLevel = S.nextInt(); } - FloodItGUI game = new FloodItGUI(dimension, numColors, difficultyLevel); + FloodItController controller = new FloodItController(dimension, numColors, difficultyLevel); + FloodItGUI game = new FloodItGUI(controller); game.init(); } From 129e06671bd6531e2341afbc8db80bf37058e4e9 Mon Sep 17 00:00:00 2001 From: Gustav Schoelin Date: Sun, 25 Feb 2018 10:52:39 -0800 Subject: [PATCH 4/9] GS Continued refactoring, possibly finished, updated gitignore --- .gitignore | 6 +++ .../flood_it/view/FloodItController.java | 38 +++++++++++++---- .../games/flood_it/view/FloodItGUI.java | 41 ++++++++----------- .../games/flood_it/view/FloodItLauncher.java | 4 ++ 4 files changed, 57 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 0be1402..987e05b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,11 @@ *.class +.DS_Store +.idea/ +.metadata/ +cs56-games-flood-it.iml +out/ + # Package Files # *.war *.ear diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java index df9387d..65f2505 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java @@ -1,15 +1,22 @@ package edu.ucsb.cs56.projects.games.flood_it.view; +/** + * Class for Flood it game Controller + * + * @author Gustav Schoelin + * @author Karl Wang + */ + public class FloodItController { private int numColors; private int dimension; private int difficultyLevel; - + private int movesLeft; private int[][] grid; /** - * FloodItGUI constructor creates an instance of the game + * FloodItController constructor creates an instance of the game * * @param dimension the grid will be dimension x dimension * @param numColors the number of colors available @@ -19,6 +26,12 @@ public FloodItController(int dimension, int numColors, int difficultyLevel) { this.dimension = dimension; this.numColors = numColors; this.difficultyLevel = difficultyLevel; + populateGrid(dimension, numColors, difficultyLevel); + //set MovesLeft (scales number of moves based on number of colors and dimension + //selected using 25 moves for a 6 color, 14x14 grid as a baseline. + movesLeft = (int) Math.floor(dimension * numColors * 25 / 84); + if (difficultyLevel == 1) movesLeft = (int) Math.floor(movesLeft * .8); + if (difficultyLevel == 3) movesLeft = (int) Math.floor(movesLeft * 2.33); } /** @@ -29,14 +42,13 @@ public FloodItController(int dimension, int numColors, int difficultyLevel) { * @param difficultyLevel a number 1,2,3 representing easy medium or hard * @return dimensionxdimension matrix of ints represnting the game board */ - public int[][] populateGrid(int dimension, int numColors, int difficultyLevel) { + public void populateGrid(int dimension, int numColors, int difficultyLevel) { if (difficultyLevel == 1) - return populateGridEasy(dimension, numColors); + grid = populateGridEasy(dimension, numColors); if (difficultyLevel == 2) - return populateGridMedium(dimension, numColors); + grid = populateGridMedium(dimension, numColors); if (difficultyLevel == 3) - return populateGridHard(dimension, numColors); - return populateGridMedium(dimension, numColors); + grid = populateGridHard(dimension, numColors); } /** @@ -157,4 +169,16 @@ public int getDimension() { public int getNumColors() { return numColors; } + + public int[][] getGrid() { + return grid; + } + + public Integer getMovesLeft() { + return movesLeft; + } + + public void setMovesLeft(int movesLeft) { + this.movesLeft = movesLeft; + } } diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java index 9215825..af9a089 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java @@ -8,7 +8,6 @@ /** * Class for the Flood it game JFrame - * includes the main method * * @author Daniel Ben-Naim * @author Dylan Hanson @@ -16,6 +15,8 @@ * @author Kai Jann * @author Chris Luo * @author Kevin Briggs + * @author Gustav Schoelin + * @author Karl Wang */ public class FloodItGUI extends JFrame { @@ -31,11 +32,9 @@ public class FloodItGUI extends JFrame { private JPanel buttonPanel; private JTextField countdown; private JLabel movesLeft; - private int[][] grid; private final Color[] colors = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW, Color.MAGENTA, Color.CYAN, Color.ORANGE, Color.BLACK}; private String[] colorNames = {"Red", "Blue", "Green", "Yellow", "Magenta", "Cyan", "Orange", "Black"}; - private Integer MOVES_LEFT; /** * FloodItGUI constructor creates an instance of the game @@ -50,20 +49,11 @@ public FloodItGUI(FloodItController controller) { * init initializes the game and draws the board. */ public void init() { - int dimension = controller.getDimension(); - int numColors = controller.getNumColors(); - int difficultyLevel = controller.getDifficultyLevel(); - //set MovesLeft (scales number of moves based on number of colors and dimension - //selected using 25 moves for a 6 color, 14x14 grid as a baseline. - MOVES_LEFT = (int) Math.floor(dimension * numColors * 25 / 84);//Thanks, autoboxing! - if (difficultyLevel == 1) MOVES_LEFT = (int) Math.floor(MOVES_LEFT * .8); - if (difficultyLevel == 3) MOVES_LEFT = (int) Math.floor(MOVES_LEFT * 2.33); //set JFrame properties - frame = new JFrame("Flood It! by SM and KJ and KB and CL and DH and DBN"); + frame = new JFrame("Flood It! by SM and KJ and KB and CL and DH and DBN and GS and KW"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(1000, 800); - grid = controller.populateGrid(dimension, numColors, difficultyLevel); - gridBoard = new FloodItGrid(grid, colors); + gridBoard = new FloodItGrid(controller.getGrid(), colors); buttonInstruction = new JButton("Instructions"); //set JTextArea properties for the big message returning box messageArea = new JTextArea(40, 20); @@ -71,33 +61,33 @@ public void init() { JScrollPane messageScroller = new JScrollPane(messageArea); messageScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); //set JTextField properties for countdown box - countdown = new JTextField(MOVES_LEFT.toString(), 2); + countdown = new JTextField(controller.getMovesLeft().toString(), 2); countdown.setEditable(false); //JLabel for the countdown JTextArea - movesLeft = new JLabel("moves left:"); + movesLeft = new JLabel("Moves left:"); //Panel that holds the color buttons and countdown buttonPanel = new JPanel(); //add Countdown components to buttonPanel buttonPanel.add(movesLeft); buttonPanel.add(countdown); //ALL button properties - for (int i = 0; i < numColors; i++) { + for (int i = 0; i < controller.getNumColors(); i++) { final int k = i; JButton currentButton = new JButton(colorNames[i]); currentButton.setBackground(colors[i]); buttonPanel.add(currentButton); currentButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - if (MOVES_LEFT != 0 && !controller.checkWin() && grid[0][0] != k) { + if (controller.getMovesLeft() != 0 && !controller.checkWin() && controller.getGrid()[0][0] != k) { countdown.setText(decrementAMove().toString()); messageArea.append(colorNames[k] + "\n"); - controller.floodIt(0, 0, k, grid[0][0]); - gridBoard.redrawLabel(grid, colors); + controller.floodIt(0, 0, k, controller.getGrid()[0][0]); + gridBoard.redrawLabel(controller.getGrid(), colors); if (controller.checkWin()) messageArea.append("You Win :D\n"); - else if (MOVES_LEFT == 0) + else if (controller.getMovesLeft() == 0) messageArea.append("You Lose :(\n"); - } else if (MOVES_LEFT != 0 && !controller.checkWin()) + } else if (controller.getMovesLeft() != 0 && !controller.checkWin()) messageArea.append("Invalid move.\n"); } }); @@ -114,7 +104,7 @@ else if (MOVES_LEFT == 0) buttonInstruction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { instructions = new FloodItInstructGui(); - messageArea.append("you have clicked the instructions\n"); + messageArea.append("You have clicked the instructions\n"); } }); //add textContainer to JFrame @@ -129,11 +119,12 @@ public void actionPerformed(ActionEvent e) { * @return numMoves an integer representing the number of moves */ public Integer decrementAMove() { - if (MOVES_LEFT <= 0) { + if (controller.getMovesLeft() <= 0) { messageArea.append("Out of moves!\n"); return 0; } else { - Integer numMoves = new Integer(--MOVES_LEFT); + int numMoves = controller.getMovesLeft() - 1; + controller.setMovesLeft(numMoves); return numMoves; } } diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java index 49ffdf7..921592b 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java @@ -6,7 +6,11 @@ public class FloodItLauncher { /** + * Class for the Flood it game Launcher * Main method for the game + * + * @author Gustav Schoelin + * @author Karl Wang */ public static void main(String args[]) { //maybe error prone From 9520aeefff3ef3c07463fea67d2ca00e4864b5b5 Mon Sep 17 00:00:00 2001 From: Gustav Schoelin Date: Tue, 27 Feb 2018 00:49:02 -0800 Subject: [PATCH 5/9] GS Implemented a graphical start menu for the game --- .../games/flood_it/view/FloodItGUI.java | 6 +- ...structGui.java => FloodItInstructGUI.java} | 4 +- .../games/flood_it/view/FloodItLauncher.java | 15 ++- .../flood_it/view/FloodItStartMenuGUI.java | 110 ++++++++++++++++++ 4 files changed, 126 insertions(+), 9 deletions(-) rename src/edu/ucsb/cs56/projects/games/flood_it/view/{FloodItInstructGui.java => FloodItInstructGUI.java} (96%) create mode 100644 src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItStartMenuGUI.java diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java index af9a089..0fa8b83 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java @@ -26,7 +26,7 @@ public class FloodItGUI extends JFrame { private JFrame frame; private Container textContainer; private FloodItGrid gridBoard; - private FloodItInstructGui instructions; + private FloodItInstructGUI instructions; private JTextArea messageArea; private JButton buttonInstruction; private JPanel buttonPanel; @@ -51,7 +51,7 @@ public FloodItGUI(FloodItController controller) { public void init() { //set JFrame properties frame = new JFrame("Flood It! by SM and KJ and KB and CL and DH and DBN and GS and KW"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(1000, 800); gridBoard = new FloodItGrid(controller.getGrid(), colors); buttonInstruction = new JButton("Instructions"); @@ -103,7 +103,7 @@ else if (controller.getMovesLeft() == 0) textContainer.add(buttonInstruction); buttonInstruction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - instructions = new FloodItInstructGui(); + instructions = new FloodItInstructGUI(); messageArea.append("You have clicked the instructions\n"); } }); diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItInstructGui.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItInstructGUI.java similarity index 96% rename from src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItInstructGui.java rename to src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItInstructGUI.java index c397cf6..e5b6ea9 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItInstructGui.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItInstructGUI.java @@ -18,8 +18,8 @@ * @version 2/28/14 for proj 1, cs56, W14 */ -public class FloodItInstructGui { - public FloodItInstructGui() { +public class FloodItInstructGUI { + public FloodItInstructGUI() { //initialize and set JFrame visible and size diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java index 921592b..910930b 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java @@ -14,10 +14,17 @@ public class FloodItLauncher { */ public static void main(String args[]) { //maybe error prone - + FloodItStartMenuGUI start = new FloodItStartMenuGUI(); + while(!start.isGameStarted()) { + try { + Thread.sleep(200); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } //int dimension = (int) args[0]; //int numColors = (int) args[1]; - int dimension; + /* int dimension; int numColors = 0; int difficultyLevel = 0; Scanner S = new Scanner(System.in); @@ -39,8 +46,8 @@ public static void main(String args[]) { while (difficultyLevel < 1 || difficultyLevel > 3) { System.out.println("Invalid input"); difficultyLevel = S.nextInt(); - } - FloodItController controller = new FloodItController(dimension, numColors, difficultyLevel); + }*/ + FloodItController controller = new FloodItController(start.getDimensions(), start.getNumColors(), start.getDifficulty()); FloodItGUI game = new FloodItGUI(controller); game.init(); } diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItStartMenuGUI.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItStartMenuGUI.java new file mode 100644 index 0000000..d09dd93 --- /dev/null +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItStartMenuGUI.java @@ -0,0 +1,110 @@ +package edu.ucsb.cs56.projects.games.flood_it.view; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowEvent; + +/** + * Class for Flood it game StartMenuGUI + * + * @author Gustav Schoelin + */ + +public class FloodItStartMenuGUI { + + private JFrame frame; + private JComboBox dimensionsChooser; + private JComboBox numColorsChooser; + private JComboBox difficultyChooser; + private boolean gameStarted = false; + private int dimensions; + private int numColors; + private int difficulty; + + public FloodItStartMenuGUI() { + frame = new JFrame("Welcome to FloodIt!"); + frame.setSize(600, 200); + Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); + frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/3-frame.getSize().height/2); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + + JPanel textPanel = new JPanel(); + textPanel.add(new JLabel("
Welcome to FloodIt!
Choose size, number of colors and difficulty

")); + frame.getContentPane().add(BorderLayout.NORTH, textPanel); + + dimensionsChooser = new JComboBox(); + for (int i = 4; i <= 16; i++) { + dimensionsChooser.addItem(i + "x" + i); + } + + numColorsChooser = new JComboBox(); + for (int i = 3; i <= 8; i++) { + numColorsChooser.addItem(i); + } + + String[] difficulties = {"Easy", "Medium", "Hard"}; + difficultyChooser = new JComboBox(difficulties); + + JPanel chooserPanel = new JPanel(); + chooserPanel.add(new JLabel("Size:")); + chooserPanel.add(dimensionsChooser); + chooserPanel.add(new JLabel("Number of colors:")); + chooserPanel.add(numColorsChooser); + chooserPanel.add(new JLabel("Difficulty:")); + chooserPanel.add(difficultyChooser); + frame.getContentPane().add(BorderLayout.CENTER, chooserPanel); + + JPanel buttonPanel = new JPanel(); + JButton startButton = new JButton("Start Game"); + startButton.addActionListener(new startButtonListener()); + JButton exitButton = new JButton(("Exit")); + exitButton.addActionListener(new exitButtonListener()); + + buttonPanel.add(startButton); + buttonPanel.add(exitButton); + + frame.getContentPane().add(BorderLayout.SOUTH, buttonPanel); + + frame.setVisible(true); + } + + public boolean isGameStarted() { + return gameStarted; + } + + public int getDimensions() { + return dimensions; + } + + public int getNumColors() { + return numColors; + } + + public int getDifficulty() { + return difficulty; + } + + class startButtonListener implements ActionListener { + public void actionPerformed(ActionEvent e) { + dimensions = dimensionsChooser.getSelectedIndex() + 4; + numColors = numColorsChooser.getSelectedIndex() + 3; + difficulty = difficultyChooser.getSelectedIndex() + 1; + + gameStarted = true; + frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); + } + } + + class exitButtonListener implements ActionListener { + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + } + +// public int[] getStartValues() { +// int[] startValues = new int[3]; +// //startValues[0] = +// } +} From dc78b7e1ae9f11dd546ba49a8579cb538d0926d2 Mon Sep 17 00:00:00 2001 From: Gustav Schoelin Date: Tue, 27 Feb 2018 15:14:13 -0800 Subject: [PATCH 6/9] GS Code cleanup --- .../games/flood_it/view/FloodItGUI.java | 2 +- .../games/flood_it/view/FloodItLauncher.java | 26 ------------------- .../flood_it/view/FloodItStartMenuGUI.java | 8 ++---- 3 files changed, 3 insertions(+), 33 deletions(-) diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java index 0fa8b83..50949d7 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItGUI.java @@ -96,7 +96,7 @@ else if (controller.getMovesLeft() == 0) frame.getContentPane().add(BorderLayout.SOUTH, buttonPanel); //Container for text and instructions button textContainer = new Container(); - textContainer.setLayout(new BoxLayout(textContainer, BoxLayout.Y_AXIS)); + textContainer.setLayout(new BoxLayout(textContainer, BoxLayout.PAGE_AXIS)); //add Components to textContainer //textContainer.add(messageScroller); textContainer.add(messageArea); diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java index 910930b..3508792 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItLauncher.java @@ -13,7 +13,6 @@ public class FloodItLauncher { * @author Karl Wang */ public static void main(String args[]) { - //maybe error prone FloodItStartMenuGUI start = new FloodItStartMenuGUI(); while(!start.isGameStarted()) { try { @@ -22,31 +21,6 @@ public static void main(String args[]) { e.printStackTrace(); } } - //int dimension = (int) args[0]; - //int numColors = (int) args[1]; - /* int dimension; - int numColors = 0; - int difficultyLevel = 0; - Scanner S = new Scanner(System.in); - System.out.println("Welcome to FloodIt!"); - System.out.println("Enter the dimension you want: (between 4 and 16)"); - dimension = S.nextInt(); - while (dimension < 4 || dimension > 16) { - System.out.println("Invalid input"); - dimension = S.nextInt(); - } - System.out.println("Enter the number of colors you want: (between 3 and 8)"); - numColors = S.nextInt(); - while (numColors < 3 || numColors > 8) { - System.out.println("Invalid input"); - numColors = S.nextInt(); - } - System.out.println("Enter 1 for easy, 2 for medium, 3 for hard."); - difficultyLevel = S.nextInt(); - while (difficultyLevel < 1 || difficultyLevel > 3) { - System.out.println("Invalid input"); - difficultyLevel = S.nextInt(); - }*/ FloodItController controller = new FloodItController(start.getDimensions(), start.getNumColors(), start.getDifficulty()); FloodItGUI game = new FloodItGUI(controller); game.init(); diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItStartMenuGUI.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItStartMenuGUI.java index d09dd93..7a43540 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItStartMenuGUI.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItStartMenuGUI.java @@ -93,7 +93,8 @@ public void actionPerformed(ActionEvent e) { difficulty = difficultyChooser.getSelectedIndex() + 1; gameStarted = true; - frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); + frame.setVisible(false); + frame.dispose(); } } @@ -102,9 +103,4 @@ public void actionPerformed(ActionEvent e) { System.exit(0); } } - -// public int[] getStartValues() { -// int[] startValues = new int[3]; -// //startValues[0] = -// } } From 8037833cb9213ae1d14aa43c782113072b3eeded Mon Sep 17 00:00:00 2001 From: Karl Wang Date: Tue, 27 Feb 2018 21:27:28 -0800 Subject: [PATCH 7/9] fix comment --- .../cs56/projects/games/flood_it/view/FloodItController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java index 65f2505..3591da9 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java @@ -76,7 +76,7 @@ public int[][] populateGridEasy(int dimension, int numColors) { } /** - * populateGridEasy populates a medium grid of a given size + * populateGridMedium populates a medium grid of a given size * * @param dimension the grid will be dimension x dimension * @param numColors the number of colors in the grid @@ -94,7 +94,7 @@ public int[][] populateGridMedium(int dimension, int numColors) { /** - * populateGridEasy populates a hard grid of a given size + * populateGridHard populates a hard grid of a given size * * @param dimension the grid will be dimension x dimension * @param numColors the number of colors in the grid From 5a4c412cfc5b4b991000adf26a0da63234dd901a Mon Sep 17 00:00:00 2001 From: Karl Wang Date: Tue, 27 Feb 2018 21:30:06 -0800 Subject: [PATCH 8/9] adjust movesLeft --- .../cs56/projects/games/flood_it/view/FloodItController.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java index 3591da9..68383ec 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java @@ -27,9 +27,12 @@ public FloodItController(int dimension, int numColors, int difficultyLevel) { this.numColors = numColors; this.difficultyLevel = difficultyLevel; populateGrid(dimension, numColors, difficultyLevel); + //set MovesLeft (scales number of moves based on number of colors and dimension //selected using 25 moves for a 6 color, 14x14 grid as a baseline. - movesLeft = (int) Math.floor(dimension * numColors * 25 / 84); + int row = dimension; + int col = dimension; + movesLeft = (int) Math.floor( 25 * (row + col) * numColors / (14 + 14) * 6 ); if (difficultyLevel == 1) movesLeft = (int) Math.floor(movesLeft * .8); if (difficultyLevel == 3) movesLeft = (int) Math.floor(movesLeft * 2.33); } From 51b2d74618edb1c2f07928764d4a812d6bedcc70 Mon Sep 17 00:00:00 2001 From: Karl Wang Date: Wed, 28 Feb 2018 00:00:50 -0800 Subject: [PATCH 9/9] fixed moves left algorithm --- .../flood_it/view/FloodItController.java | 104 +++++++++++++++--- 1 file changed, 91 insertions(+), 13 deletions(-) diff --git a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java index 68383ec..a7ef06a 100644 --- a/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java +++ b/src/edu/ucsb/cs56/projects/games/flood_it/view/FloodItController.java @@ -1,5 +1,8 @@ package edu.ucsb.cs56.projects.games.flood_it.view; +import java.util.Collections; +import java.util.Arrays; + /** * Class for Flood it game Controller * @@ -27,14 +30,13 @@ public FloodItController(int dimension, int numColors, int difficultyLevel) { this.numColors = numColors; this.difficultyLevel = difficultyLevel; populateGrid(dimension, numColors, difficultyLevel); - - //set MovesLeft (scales number of moves based on number of colors and dimension - //selected using 25 moves for a 6 color, 14x14 grid as a baseline. - int row = dimension; - int col = dimension; - movesLeft = (int) Math.floor( 25 * (row + col) * numColors / (14 + 14) * 6 ); - if (difficultyLevel == 1) movesLeft = (int) Math.floor(movesLeft * .8); - if (difficultyLevel == 3) movesLeft = (int) Math.floor(movesLeft * 2.33); + + if (difficultyLevel == 1) + this.movesLeft = (int) (calculateBaselineMovesLeft(dimension * dimension, dimension)); + if (difficultyLevel == 2) + this.movesLeft = (int) (calculateBaselineMovesLeft(dimension * dimension, dimension / 2)); + if (difficultyLevel == 3) + this.movesLeft = (int) (calculateBaselineMovesLeft(dimension * dimension, 1)); } /** @@ -127,6 +129,41 @@ public int[][] populateGridHard(int dimension, int numColors) { return result; } + /** + * calculates the number of moves given to the player + * by running random simulations on the grid + * the average of the top results is provided as the recommended number of moves + * + * @return recommended number of moves, not adjusted for difficulty + */ + private double calculateBaselineMovesLeft(int iterations, int tops){ + if(tops > iterations){ + throw new IllegalArgumentException("requesting the average of more result than that is generated"); + } + int[] testResults = new int[iterations]; //the choice of 30 iterations is completely arbitrary. Feel free to change it. + + for(int i = 0; i < testResults.length; i++){ + int[][] testGrid = gridCopy(this.grid); + int movesUsed = 0; + while(!checkWin(testGrid)){ + int newColor; + do{ + newColor = (int) (Math.random() * numColors); + }while(newColor == testGrid[0][0]); + floodIt(0, 0, newColor, testGrid[0][0], testGrid); + movesUsed++; + } + testResults[i] = movesUsed; + } + + Arrays.sort(testResults); + + int sum = 0; + for (int i = 0; i < tops; i++) { + sum += testResults[i]; + } + return (double)sum / tops; + } /** * floodIt redraws the matrix after the player makes a move @@ -138,13 +175,30 @@ public int[][] populateGridHard(int dimension, int numColors) { * @param oldColor the color to be repainted */ public void floodIt(int x, int y, int newColor, int oldColor) { + floodIt(x, y, newColor, oldColor, this.grid); + } + + /** + * floodIt redraws the matrix after the player makes a move + * it is adapted from Wikipedia's algorithm: en.wikipedia.org/wiki/Flood_fill + * + * @param x the x location in the matrix + * @param y the y location in the matrix + * @param newColor the new color being painted + * @param oldColor the color to be repainted + * @param grid the grid to be worked on + */ + private void floodIt(int x, int y, int newColor, int oldColor, int[][] grid) { + if(newColor == oldColor){ + throw new IllegalArgumentException("newColor and oldColor should not be the same!"); + } if (x < 0 || y < 0 || x >= grid.length || y >= grid.length) return; if (grid[x][y] != oldColor) return; grid[x][y] = newColor; - floodIt(x, y + 1, newColor, oldColor); - floodIt(x, y - 1, newColor, oldColor); - floodIt(x + 1, y, newColor, oldColor); - floodIt(x - 1, y, newColor, oldColor); + floodIt(x, y + 1, newColor, oldColor, grid); + floodIt(x, y - 1, newColor, oldColor, grid); + floodIt(x + 1, y, newColor, oldColor, grid); + floodIt(x - 1, y, newColor, oldColor, grid); return; } @@ -154,11 +208,20 @@ public void floodIt(int x, int y, int newColor, int oldColor) { * @return true if win, false if not. */ public boolean checkWin() { + return checkWin(this.grid); + } + + /** + * checkWin checks if the player has won the game + * + * @return true if win, false if not. + * @param grid the grid to be worked on + */ + private boolean checkWin(int[][] grid) { for (int i = 0; i < grid.length; i++) for (int j = 0; j < grid.length; j++) if (grid[i][j] != grid[0][0]) return false; return true; - } public int getDifficultyLevel() { @@ -184,4 +247,19 @@ public Integer getMovesLeft() { public void setMovesLeft(int movesLeft) { this.movesLeft = movesLeft; } + + /** + * utility method for copying a grid + * to be used in calculateMovesLeft() + * + * @return a copy of the provided grid + * @param grid the grid to be worked on + */ + private int[][] gridCopy(int[][] grid){ + int [][] newGrid = new int[grid.length][]; + for(int i = 0; i < grid.length; i++){ + newGrid[i] = grid[i].clone(); + } + return newGrid; + } }