FloodIt Game Java I have attached a URL with the instructions of what to do and
ID: 3805035 • Letter: F
Question
FloodIt Game Java
I have attached a URL with the instructions of what to do and I have attached all of my classes
http://www.site.uottawa.ca/~gvj/Courses/ITI1121/assignments/04/index.pdf
DOTBUTTON.JAVA
import javax.swing.*;
import java.awt.*;
public class DotButton extends JButton {
private ImageIcon greyM;
private ImageIcon orangeM;
private ImageIcon blueM;
private ImageIcon greenM;
private ImageIcon purpleM;
private ImageIcon redM;
private ImageIcon greyS;
private ImageIcon orangeS;
private ImageIcon blueS;
private ImageIcon greenS;
private ImageIcon purpleS;
private ImageIcon redS;
private ImageIcon greyL;
private ImageIcon orangeL;
private ImageIcon blueL;
private ImageIcon greenL;
private ImageIcon purpleL;
private ImageIcon redL;
private int row;
private int column;
private int color;
private final int iconSize;
public DotButton(int row, int column, int color, int iconSize) {
this.row = row;
this.column = column;
this.color = color;
this.iconSize = iconSize;
if (iconSize == 0) {
this.setPreferredSize(new Dimension(11, 11));
if (color == 0) {
greyS = new ImageIcon("data/S/ball-0.png");
setIcon(greyS);
}else if (color == 1) {
orangeS = new ImageIcon("data/S/ball-1.png");
setIcon(orangeS);
}else if (color == 2) {
blueS = new ImageIcon("data/S/ball-2.png");
setIcon(blueS);
}else if (color == 3) {
greenS = new ImageIcon("data/S/ball-3.png");
setIcon(greenS);
}else if (color == 4) {
purpleS = new ImageIcon("data/S/ball-4.png");
setIcon(purpleS);
}else if (color == 5) {
redS = new ImageIcon("data/S/ball-5.png");
setIcon(redS);}
}else if (iconSize == 1){
this.setPreferredSize(new Dimension(28, 28));
if (color == 0) {
greyM = new ImageIcon("data/M/ball-0.png");
setIcon(greyM);
}else if (color == 1) {
orangeM = new ImageIcon("data/M/ball-1.png");
setIcon(orangeM);
}else if (color == 2) {
blueM = new ImageIcon("data/M/ball-2.png");
setIcon(blueM);
}else if (color == 3) {
greenM = new ImageIcon("data/M/ball-3.png");
setIcon(greenM);
}else if (color == 4) {
purpleM = new ImageIcon("data/M/ball-4.png");
setIcon(purpleM);
}else if (color == 5) {
redM = new ImageIcon("data/M/ball-5.png");
setIcon(redM);
}
}
setOpaque(true);
setBorderPainted(false);
setFocusPainted(false);
setContentAreaFilled(false);
setBackground(Color.WHITE);
}public DotButton(int color, int iconSize) {
this.color = color;
this.iconSize = iconSize;
this.setPreferredSize(new Dimension(40, 40));
if (color == 0) {
greyL = new ImageIcon("data/L/ball-0.png");
setIcon(greyL);
setActionCommand("0");
}else if (color == 1) {
orangeL = new ImageIcon("data/L/ball-1.png");
setIcon(orangeL);
setActionCommand("1");
}else if (color == 2) {
blueL = new ImageIcon("data/L/ball-2.png");
setIcon(blueL);
setActionCommand("2");
}else if (color == 3) {
greenL = new ImageIcon("data/L/ball-3.png");
setIcon(greenL);
setActionCommand("3");
}else if (color == 4) {
purpleL = new ImageIcon("data/L/ball-4.png");
setIcon(purpleL);
setActionCommand("4");
}else if (color == 5) {
redL = new ImageIcon("data/L/ball-5.png");
setIcon(redL);
setActionCommand("5");
}
setContentAreaFilled(false);
setBackground(Color.WHITE);
setOpaque(true);
setBorderPainted(false);
}public void setColor(int color) {
this.color = color;
}public int getColor() {
return color;
}public int getRow() {
return row;
}public int getColumn() {
return column;
}
}
DOTINFO.JAVA
public class DotInfo {
private final int x;
private final int y;
protected int color;
private boolean captured;
public DotInfo(int x, int y, int color) {
this.x = x;
this.y = y;
this.color = color;
}public int getX() {
return x;
}public int getY() {
return y;
}public boolean isCaptured() {
return captured;
}public void setCaptured(boolean captured) {
this.captured = captured;
}public int getColor() {
return color;
}
}
FLOODIT.JAVA
public class FloodIt {
public static void main(String[] args) {
StudentInfo.display();
if (args.length > 0) {
if (Integer.parseInt(args[0]) < 10) {
GameController controller = new GameController(12);
} else {
GameController controller = new GameController(Integer.parseInt(args[0]));
}
} else {
GameController controller = new GameController(12);
}
}
}
GAMECONTROLLER.JAVA
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static javax.swing.JOptionPane.NO_OPTION;
import static javax.swing.JOptionPane.YES_OPTION;
public class GameController implements ActionListener, Stack<DotInfo> {
private final GameModel gameModel;
private final DotInfo[] stack;
private final GameView gameView;
public GameController(int size) {
gameModel = new GameModel(size);
gameView = new GameView(gameModel, this);
stack = new DotInfo[size * size];
selectColor(-1);
}public void reset() {
gameModel.reset();
selectColor(-1);
}public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "0":
selectColor(0);
break;
case "1":
selectColor(1);
break;
case "2":
selectColor(2);
break;
case "3":
selectColor(3);
break;
case "4":
selectColor(4);
break;
case "5":
selectColor(5);
break;
case "Reset":
reset();
break;
case "Quit":
System.exit(0);
break;
}
}public void selectColor(int color) {
if (!gameModel.isFinished()) {
if (color != -1 && color != gameModel.getCurrentSelectedColor()) {
gameModel.setCurrentSelectedColor(color);
gameModel.step();
for (int i = 0; (i < gameModel.getSize()); i++) {
for (int j = 0; (j < gameModel.getSize()); j++) {
if (gameModel.isCaptured(i, j)) {
gameModel.get(i, j).color = gameModel.getCurrentSelectedColor();
push(gameModel.get(i, j));
}
}
}
} else if (color == -1) {
push(gameModel.get(0, 0));
}DotInfo verify;
while (!isEmpty()) {
boolean westExists = true;
boolean eastExists = true;
boolean southExists = true;
boolean northExists = true;
verify = pop();
if (verify.isCaptured()) {
if (verify.getY() == 0) {
westExists = false;
}if (verify.getY() + 1 == gameModel.getSize()) {
eastExists = false;
}if (verify.getX() + 1 == gameModel.getSize()) {
southExists = false;
}if (verify.getX() == 0) {
northExists = false;
}if (westExists && (verify.getColor() == gameModel.getColor(verify.getX(), verify.getY() - 1))
&& !gameModel.isCaptured(verify.getX(), verify.getY() - 1)) {
gameModel.capture(verify.getX(), verify.getY() - 1);
push(gameModel.get(verify.getX(), verify.getY() - 1));
}if (eastExists && (verify.getColor() == gameModel.getColor(verify.getX(), verify.getY() + 1))
&& !gameModel.isCaptured(verify.getX(), verify.getY() + 1)) {
gameModel.capture(verify.getX(), verify.getY() + 1);
push(gameModel.get(verify.getX(), verify.getY() + 1));
}if (southExists && (verify.getColor() == gameModel.getColor(verify.getX() + 1, verify.getY()))
&& !gameModel.isCaptured(verify.getX() + 1, verify.getY())) {
gameModel.capture(verify.getX() + 1, verify.getY());
push(gameModel.get(verify.getX() + 1, verify.getY()));
}if (northExists && (verify).getColor() == gameModel.getColor(verify.getX() - 1, verify.getY())
&& !gameModel.isCaptured(verify.getX() - 1, verify.getY())) {
gameModel.capture(verify.getX() - 1, verify.getY());
push(gameModel.get(verify.getX() - 1, verify.getY()));
}
}
}
}
gameView.update();
if (gameModel.isFinished()) {
int selectedValue = JOptionPane.showConfirmDialog(gameView,
"You won! You used " + gameModel.getNumberOfSteps() + " steps! Would you like to start a new game?", "Congratulations",
JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (selectedValue == YES_OPTION) {
reset();
}else if (selectedValue == NO_OPTION) {
System.exit(0);
}
}else if (gameModel.getNumberOfSteps() == 20) {
int selectedValue = JOptionPane.showConfirmDialog(gameView,
"You've reached the maximum number of steps. Would you like to try again?", "Game Over",
JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (selectedValue == YES_OPTION) {
reset();
}else if (selectedValue == NO_OPTION) {
System.exit(0);
}
}
}public boolean isEmpty() {
for (int i = 0; i < stack.length; i++) {
if (stack[i] != null) {
return false;
}
}
return true;
}
public DotInfo peek() {
DotInfo lastElement = null;
for (int i = 1; i < stack.length; i++) {
if (stack[i] == null) {
lastElement = stack[i - 1];
break;
}
}return lastElement;
}public DotInfo pop() {
DotInfo lastElement = null;
for (int i = 1; i < stack.length; i++) {
if (stack[i] == null) {
lastElement = stack[i - 1];
stack[i - 1] = null;
break;
}
}return lastElement;
}public void push(DotInfo element) {
for (int i = 0; i < stack.length; i++) {
if (stack[i] == null) {
stack[i] = element;
break;
}
}
}
}
GAMEMODEL.JAVA
import java.util.*;
public class GameModel {
public static final int COLOR_0 = 0;
public static final int COLOR_1 = 1;
public static final int COLOR_2 = 2;
public static final int COLOR_3 = 3;
public static final int COLOR_4 = 4;
public static final int COLOR_5 = 5;
public static final int NUMBER_OF_COLORS = 6;
final Random random;
final int size;
int numberOfSteps;
int currentSelectedColor;
final DotInfo[][] board;
public GameModel(int size) {
this.size = size;
random = new Random();
board = new DotInfo[size][size];
reset();
}public int getSize() {
return size;
}public int getColor(int i, int j) {
return board[i][j].getColor();
}public boolean isCaptured(int i, int j) {
return board[i][j].isCaptured();
}public void capture(int i, int j) {
board[i][j].setCaptured(true);
}public int getNumberOfSteps() {
return numberOfSteps;
}public void reset() {
numberOfSteps = 0;
currentSelectedColor = -1;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = new DotInfo(i, j, random.nextInt(NUMBER_OF_COLORS));
board[i][j].setCaptured(false);
}
}
board[0][0].setCaptured(true);
}public int getCurrentSelectedColor() {
return currentSelectedColor;
}public void setCurrentSelectedColor(int val) {
currentSelectedColor = val;
}public void step() {
numberOfSteps++;
}public DotInfo get(int i, int j) {
return board[i][j];
}public boolean isFinished() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size - 1; j++) {
if (board[i][j].getColor() != board[i][j + 1].getColor()) {
return false;
}
}
}return true;
}public String toString() {
String captured = "";
String color = "";
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
captured += isCaptured(i, j) + " ";
color += getColor(i, j) + " ";
if (j == board.length - 1) {
captured += " ";
color += " ";
}
}
}return color;
}
}
GAMEVIEW.JAVA
import javax.swing.*;
import java.awt.*;
public class GameView extends JFrame {
private final GameModel model;
private final GameController gameController;
private final JButton reset;
private final JButton quit;
private final JPanel boardPanel;
private final JPanel colorSelectionPanel;
private final JPanel controlPanel;
private final JPanel southPanel;
private final DotButton greyP;
private final DotButton orangeP;
private final DotButton blueP;
private final DotButton greenP;
private final DotButton purpleP;
private final DotButton redP;
private final DotButton[][] board;
private int iconSize;
public GameView(GameModel model, GameController gameController) {
setTitle("ITI1121 FloodIt");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.model = model;
this.gameController = gameController;
reset = new JButton("reset");
reset.addActionListener(this.gameController);
reset.setContentAreaFilled(false);
reset.setBackground(Color.WHITE);
reset.setOpaque(true);
quit = new JButton("quit");
quit.addActionListener(this.gameController);
quit.setContentAreaFilled(false);
quit.setBackground(Color.WHITE);
quit.setOpaque(true);
boardPanel = new JPanel(new GridLayout(model.getSize(), model.getSize()));
colorSelectionPanel = new JPanel();
controlPanel = new JPanel();
southPanel = new JPanel(new BorderLayout());
iconSize = -1;
board = new DotButton[model.getSize()][model.getSize()];
if (model.getSize() <= 25 && model.getSize() >= 10) {
iconSize = 1;
} else if (model.getSize() > 25) {
iconSize = 0;
}
for (int i = 0; i < model.getSize(); i++) {
for (int j = 0; j < model.getSize(); j++) {
board[i][j] = new DotButton(model.get(i, j).getX(), model.get(i, j).getY(), model.getColor(i, j), iconSize);
boardPanel.add(board[i][j]);
}
}
controlPanel.setBackground(Color.WHITE);
controlPanel.setOpaque(true);
controlPanel.add(new JLabel("Number of steps Done:" + Integer.toString(model.getNumberOfSteps())));
controlPanel.add(reset);
controlPanel.add(quit);
colorSelectionPanel.setBackground(Color.WHITE);
colorSelectionPanel.setOpaque(true);
greenP = new DotButton(3, 2);
purpleP = new DotButton(4, 2);
redP = new DotButton(5, 2);
greyP = new DotButton(0, 2);
orangeP = new DotButton(1, 2);
blueP = new DotButton(2, 2);
greenP.addActionListener(this.gameController);
purpleP.addActionListener(this.gameController);
redP.addActionListener(this.gameController);
greyP.addActionListener(this.gameController);
orangeP.addActionListener(this.gameController);
blueP.addActionListener(this.gameController);
colorSelectionPanel.setBackground(Color.WHITE);
colorSelectionPanel.setOpaque(true);
colorSelectionPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 3));
colorSelectionPanel.add(greenP);
colorSelectionPanel.add(purpleP);
colorSelectionPanel.add(redP);
colorSelectionPanel.add(greyP);
colorSelectionPanel.add(orangeP);
colorSelectionPanel.add(blueP);
southPanel.add(colorSelectionPanel, BorderLayout.CENTER);
southPanel.add(controlPanel, BorderLayout.SOUTH);
add(boardPanel, BorderLayout.CENTER);
add(southPanel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
public void update() {
boardPanel.removeAll();
for (int i = 0; i < model.getSize(); i++) {
for (int j = 0; j < model.getSize(); j++) {
board[i][j] = new DotButton(i, j, model.getColor(i, j), iconSize);
boardPanel.add(board[i][j]);
}
}
controlPanel.removeAll();
controlPanel.add(new JLabel("Number of steps Done: "));
controlPanel.add(new JLabel(Integer.toString(model.getNumberOfSteps())));
controlPanel.add(reset);
controlPanel.add(quit);
revalidate();
repaint();
}
}
STACK.JAVA
public interface Stack<E> {
public abstract boolean isEmpty();
public abstract E peek();
public abstract E pop();
public abstract void push(E element);
}
Explanation / Answer
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static javax.swing.JOptionPane.NO_OPTION;
import static javax.swing.JOptionPane.YES_OPTION;
public class GameController implements ActionListener, Stack<DotInfo> {
private final GameModel gameModel;
private final DotInfo[] stack;
private final GameView gameView;
public GameController(int size) {
gameModel = new GameModel(size);
gameView = new GameView(gameModel, this);
stack = new DotInfo[size * size];
selectColor(-1);
}public void reset() {
gameModel.reset();
selectColor(-1);
}public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "0":
selectColor(0);
break;
case "1":
selectColor(1);
break;
case "2":
selectColor(2);
break;
case "3":
selectColor(3);
break;
case "4":
selectColor(4);
break;
case "5":
selectColor(5);
break;
case "Reset":
reset();
break;
case "Quit":
System.exit(0);
break;
}
}public void selectColor(int color) {
if (!gameModel.isFinished()) {
if (color != -1 && color != gameModel.getCurrentSelectedColor()) {
gameModel.setCurrentSelectedColor(color);
gameModel.step();
for (int i = 0; (i < gameModel.getSize()); i++) {
for (int j = 0; (j < gameModel.getSize()); j++) {
if (gameModel.isCaptured(i, j)) {
gameModel.get(i, j).color = gameModel.getCurrentSelectedColor();
push(gameModel.get(i, j));
}
}
}
} else if (color == -1) {
push(gameModel.get(0, 0));
}DotInfo verify;
while (!isEmpty()) {
boolean westExists = true;
boolean eastExists = true;
boolean southExists = true;
boolean northExists = true;
verify = pop();
if (verify.isCaptured()) {
if (verify.getY() == 0) {
westExists = false;
}if (verify.getY() + 1 == gameModel.getSize()) {
eastExists = false;
}if (verify.getX() + 1 == gameModel.getSize()) {
southExists = false;
}if (verify.getX() == 0) {
northExists = false;
}if (westExists && (verify.getColor() == gameModel.getColor(verify.getX(), verify.getY() - 1))
&& !gameModel.isCaptured(verify.getX(), verify.getY() - 1)) {
gameModel.capture(verify.getX(), verify.getY() - 1);
push(gameModel.get(verify.getX(), verify.getY() - 1));
}if (eastExists && (verify.getColor() == gameModel.getColor(verify.getX(), verify.getY() + 1))
&& !gameModel.isCaptured(verify.getX(), verify.getY() + 1)) {
gameModel.capture(verify.getX(), verify.getY() + 1);
push(gameModel.get(verify.getX(), verify.getY() + 1));
}if (southExists && (verify.getColor() == gameModel.getColor(verify.getX() + 1, verify.getY()))
&& !gameModel.isCaptured(verify.getX() + 1, verify.getY())) {
gameModel.capture(verify.getX() + 1, verify.getY());
push(gameModel.get(verify.getX() + 1, verify.getY()));
}if (northExists && (verify).getColor() == gameModel.getColor(verify.getX() - 1, verify.getY())
&& !gameModel.isCaptured(verify.getX() - 1, verify.getY())) {
gameModel.capture(verify.getX() - 1, verify.getY());
push(gameModel.get(verify.getX() - 1, verify.getY()));
}
}
}
}
gameView.update();
if (gameModel.isFinished()) {
int selectedValue = JOptionPane.showConfirmDialog(gameView,
"You won! You used " + gameModel.getNumberOfSteps() + " steps! Would you like to start a new game?", "Congratulations",
JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (selectedValue == YES_OPTION) {
reset();
}else if (selectedValue == NO_OPTION) {
System.exit(0);
}
}else if (gameModel.getNumberOfSteps() == 20) {
int selectedValue = JOptionPane.showConfirmDialog(gameView,
"You've reached the maximum number of steps. Would you like to try again?", "Game Over",
JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (selectedValue == YES_OPTION) {
reset();
}else if (selectedValue == NO_OPTION) {
System.exit(0);
}
}
}public boolean isEmpty() {
for (int i = 0; i < stack.length; i++) {
if (stack[i] != null) {
return false;
}
}
return true;
}
public DotInfo peek() {
DotInfo lastElement = null;
for (int i = 1; i < stack.length; i++) {
if (stack[i] == null) {
lastElement = stack[i - 1];
break;
}
}return lastElement;
}public DotInfo pop() {
DotInfo lastElement = null;
for (int i = 1; i < stack.length; i++) {
if (stack[i] == null) {
lastElement = stack[i - 1];
stack[i - 1] = null;
break;
}
}return lastElement;
}public void push(DotInfo element) {
for (int i = 0; i < stack.length; i++) {
if (stack[i] == null) {
stack[i] = element;
break;
}
}
}
}
GAMEMODEL.JAVA
import java.util.*;
public class GameModel {
public static final int COLOR_0 = 0;
public static final int COLOR_1 = 1;
public static final int COLOR_2 = 2;
public static final int COLOR_3 = 3;
public static final int COLOR_4 = 4;
public static final int COLOR_5 = 5;
public static final int NUMBER_OF_COLORS = 6;
final Random random;
final int size;
int numberOfSteps;
int currentSelectedColor;
final DotInfo[][] board;
public GameModel(int size) {
this.size = size;
random = new Random();
board = new DotInfo[size][size];
reset();
}public int getSize() {
return size;
}public int getColor(int i, int j) {
return board[i][j].getColor();
}public boolean isCaptured(int i, int j) {
return board[i][j].isCaptured();
}public void capture(int i, int j) {
board[i][j].setCaptured(true);
}public int getNumberOfSteps() {
return numberOfSteps;
}public void reset() {
numberOfSteps = 0;
currentSelectedColor = -1;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = new DotInfo(i, j, random.nextInt(NUMBER_OF_COLORS));
board[i][j].setCaptured(false);
}
}
board[0][0].setCaptured(true);
}public int getCurrentSelectedColor() {
return currentSelectedColor;
}public void setCurrentSelectedColor(int val) {
currentSelectedColor = val;
}public void step() {
numberOfSteps++;
}public DotInfo get(int i, int j) {
return board[i][j];
}public boolean isFinished() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size - 1; j++) {
if (board[i][j].getColor() != board[i][j + 1].getColor()) {
return false;
}
}
}return true;
}public String toString() {
String captured = "";
String color = "";
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
captured += isCaptured(i, j) + " ";
color += getColor(i, j) + " ";
if (j == board.length - 1) {
captured += " ";
color += " ";
}
}
}return color;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.