From d2d79b6ab79ae637e19749643c2a0287d7423ef8 Mon Sep 17 00:00:00 2001 From: Josh Pham Date: Fri, 13 Mar 2020 11:55:34 -0400 Subject: [PATCH] Refactor Merge BoardController with GameController Rename Button Menu to Menu Bar Rename BoardCell to Cell --- src/kakuro/controllers/BoardController.java | 53 ------ src/kakuro/controllers/ChronoController.java | 5 +- src/kakuro/controllers/GameController.java | 49 ++++-- ...Controller.java => MenuBarController.java} | 22 +-- src/kakuro/core/{BoardCell.java => Cell.java} | 8 +- src/kakuro/game/dao/GameDao.java | 4 +- src/kakuro/game/dao/GameDaoImpl.java | 8 +- .../gameprogress/dao/GameProgressDao.java | 6 +- .../gameprogress/dao/GameProgressDaoImpl.java | 8 +- src/kakuro/models/GameModel.java | 6 +- src/kakuro/views/BoardView.java | 149 ----------------- src/kakuro/views/GameConsole.java | 4 +- src/kakuro/views/GameView.java | 156 ++++++++++++++---- .../{ButtonMenuView.java => MenuBarView.java} | 28 ++-- tests/kakuro/TestBoard.java | 32 ++-- tests/kakuro/TestBoardCell.java | 20 +-- tests/kakuro/TestChrono.java | 5 +- tests/kakuro/TestGameController.java | 42 ++--- tests/kakuro/TestGameViewInput.java | 7 +- 19 files changed, 259 insertions(+), 353 deletions(-) delete mode 100644 src/kakuro/controllers/BoardController.java rename src/kakuro/controllers/{ButtonMenuController.java => MenuBarController.java} (68%) rename src/kakuro/core/{BoardCell.java => Cell.java} (83%) delete mode 100644 src/kakuro/views/BoardView.java rename src/kakuro/views/{ButtonMenuView.java => MenuBarView.java} (86%) diff --git a/src/kakuro/controllers/BoardController.java b/src/kakuro/controllers/BoardController.java deleted file mode 100644 index 8fd9df6..0000000 --- a/src/kakuro/controllers/BoardController.java +++ /dev/null @@ -1,53 +0,0 @@ -package kakuro.controllers; - -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.JTextField; - -import kakuro.core.BoardCell; -import kakuro.views.BoardView; - -public class BoardController { - GameController appController; - - int gridSizeX; - int gridSizeY; - - BoardView boardView; - JPanel currentPanel; - - public BoardController(int gridSizeX, int gridSizeY, GameController appController) { - this.gridSizeX = gridSizeX; - this.gridSizeY = gridSizeY; - this.appController = appController; - - boardView = new BoardView(appController.model.columns, appController.model.rows, gridSizeX, gridSizeY); - boardView.getBoardUI(appController.model.board); - //boardView. - } - - public JPanel loadGame() { - return boardView.getBoardUI(appController.model.board); - } - - public BoardView getView() { - return boardView; - } - - public JTextField[][] getSavedInput() { - return boardView.cells; - } - - //Number formatter methods - public int getMinNumberValid() { - return boardView.getMinNumberValid(); - } - - public Object getNumberFormatterClassType() { - return boardView.getNumberFormatterClassType(); - } - - public int getMaxNumberValid() { - return boardView.getMaxNumberValid(); - } -} diff --git a/src/kakuro/controllers/ChronoController.java b/src/kakuro/controllers/ChronoController.java index 7394806..7a28ece 100644 --- a/src/kakuro/controllers/ChronoController.java +++ b/src/kakuro/controllers/ChronoController.java @@ -3,6 +3,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.Timer; @@ -78,7 +79,7 @@ public int getSeconds() { return chronoModel.getSeconds(); } - public ChronoView getView() { - return chronoView; + public JComponent getView() { + return chronoView.timerLabel; } } diff --git a/src/kakuro/controllers/GameController.java b/src/kakuro/controllers/GameController.java index c024621..27b5f8b 100644 --- a/src/kakuro/controllers/GameController.java +++ b/src/kakuro/controllers/GameController.java @@ -4,7 +4,7 @@ package kakuro.controllers; -import kakuro.core.BoardCell; +import kakuro.core.Cell; import kakuro.core.DatabaseConnection; import kakuro.game.dao.GameDao; import kakuro.game.dao.GameDaoImpl; @@ -34,8 +34,8 @@ public class GameController //Sub-views controller private ChronoController chronoController; - public BoardController boardController; - public ButtonMenuController buttonMenuController; + //public BoardController boardController; + public MenuBarController buttonMenuController; public GameConsole console; @@ -70,10 +70,10 @@ private void initGame(GameModel model) //Currently the view must be loaded the first to populate the data chronoController = new ChronoController(); - boardController = new BoardController(model.rows, model.columns, this); - buttonMenuController = new ButtonMenuController(this); + //boardController = new BoardController(model.rows, model.columns, this); + buttonMenuController = new MenuBarController(this); - this.view = new GameView(this, gui, chronoController.getView(), boardController.getView(), buttonMenuController.getView());; + this.view = new GameView(this, gui, chronoController.getView(), buttonMenuController.getView());; this.console = new GameConsole(this); console.printStartup(); @@ -131,10 +131,10 @@ public void saveGame() { } } - public BoardCell[][] loadGame() { + public Cell[][] loadGame() { try { //TODO: fixed player and to fix in iteration 3 - BoardCell[][] boardCell = gameProgress.load(getDatabaseConnection(), "TestPlayer"); + Cell[][] boardCell = gameProgress.load(getDatabaseConnection(), "TestPlayer"); if(boardCell != null) { model.board = boardCell; @@ -144,7 +144,7 @@ public BoardCell[][] loadGame() { console.printBoard(false); if (gui){ - view.updateView(boardController.loadGame()); + view.updateView(); } return model.board; @@ -160,7 +160,7 @@ public BoardCell[][] loadGame() { public void loadPreconfiguredGame(int gameLevel) { try { - ArrayList boardCells = game.loadAllPreconfiguredGames(getDatabaseConnection()); + ArrayList boardCells = game.loadAllPreconfiguredGames(getDatabaseConnection()); //Load the new game model model.board = boardCells.get(gameLevel-1); @@ -170,7 +170,7 @@ public void loadPreconfiguredGame(int gameLevel) { if (gui){ //Update the new view - view.updateView(boardController.loadGame()); + view.updateView(); //Start the timer chronoController.show(); @@ -204,7 +204,7 @@ public Boolean solveBoard() int sum = 0; map = new HashMap(); //continues to add horizontally until next cell is not an INPUT type - while(column <= model.columns && model.board[i][column].getType()==BoardCell.CellType.INPUT) { //horizontal sum check + while(column <= model.columns && model.board[i][column].getType()==Cell.CellType.INPUT) { //horizontal sum check int cell = model.board[i][column].getFirstValue(); //getting cell value @@ -235,7 +235,7 @@ public Boolean solveBoard() int sum = 0; map = new HashMap(); - while(row <= model.rows && model.board[row][j].getType()==BoardCell.CellType.INPUT) { + while(row <= model.rows && model.board[row][j].getType()==Cell.CellType.INPUT) { int cell = model.board[row][j].getFirstValue(); @@ -266,7 +266,7 @@ public Boolean solveBoard() int sumColumns = 0; map = new HashMap(); //checking row sum - while(column <= model.columns && model.board[i][column].getType()==BoardCell.CellType.INPUT) { //horizontal sum check + while(column <= model.columns && model.board[i][column].getType()==Cell.CellType.INPUT) { //horizontal sum check int cell = model.board[i][column].getFirstValue(); @@ -283,7 +283,7 @@ public Boolean solveBoard() map.clear(); //checking column sum - while(row <= model.rows && model.board[row][j].getType()==BoardCell.CellType.INPUT) { //vertical sum check + while(row <= model.rows && model.board[row][j].getType()==Cell.CellType.INPUT) { //vertical sum check int cell = model.board[row][j].getFirstValue(); @@ -328,16 +328,16 @@ public Boolean solveBoard() } public void loadInputInModel(boolean clearInput) { - JTextField[][] saveInput = boardController.getSavedInput(); + JTextField[][] saveInput = view.getSavedInput(); String value; for(int row = 0; row < model.columns; row++) { for(int column = 0; column < model.rows; column++) { - BoardCell cell = model.board[row][column]; + Cell cell = model.board[row][column]; - if (cell.getType() == BoardCell.CellType.INPUT) + if (cell.getType() == Cell.CellType.INPUT) { if(clearInput) { saveInput[row][column].setText(""); @@ -377,4 +377,17 @@ public void submit() { solveBoard(); console.printSolveBoard(); } + + //Number formatter + public int getMinNumberValid() { + return view.getMinNumberValid(); + } + + public Object getNumberFormatterClassType() { + return view.getNumberFormatterClassType(); + } + + public int getMaxNumberValid() { + return view.getMaxNumberValid(); + } } diff --git a/src/kakuro/controllers/ButtonMenuController.java b/src/kakuro/controllers/MenuBarController.java similarity index 68% rename from src/kakuro/controllers/ButtonMenuController.java rename to src/kakuro/controllers/MenuBarController.java index d0bfcf7..8fa04e4 100644 --- a/src/kakuro/controllers/ButtonMenuController.java +++ b/src/kakuro/controllers/MenuBarController.java @@ -1,21 +1,23 @@ package kakuro.controllers; -import kakuro.core.BoardCell; +import javax.swing.JComponent; + +import kakuro.core.Cell; import kakuro.core.GameDifficulty; -import kakuro.views.ButtonMenuView; +import kakuro.views.MenuBarView; -public class ButtonMenuController { +public class MenuBarController { GameController appController; - private ButtonMenuView buttonMenuView; + private MenuBarView buttonMenuView; private boolean isPaused; - public ButtonMenuController(GameController appController){ + public MenuBarController(GameController appController){ this.appController = appController; - buttonMenuView = new ButtonMenuView(this); + buttonMenuView = new MenuBarView(this); } - public ButtonMenuView getButtonMenuView() { + public MenuBarView getButtonMenuView() { return buttonMenuView; } @@ -31,8 +33,8 @@ public void resume() { appController.resume(); } - public ButtonMenuView getView() { - return buttonMenuView; + public JComponent getView() { + return buttonMenuView.mainPanel; } public void submit() { @@ -43,7 +45,7 @@ public void save() { appController.saveGame(); } - public BoardCell[][] load() { + public Cell[][] load() { return appController.loadGame(); } diff --git a/src/kakuro/core/BoardCell.java b/src/kakuro/core/Cell.java similarity index 83% rename from src/kakuro/core/BoardCell.java rename to src/kakuro/core/Cell.java index 2382796..be5a8b8 100644 --- a/src/kakuro/core/BoardCell.java +++ b/src/kakuro/core/Cell.java @@ -3,7 +3,7 @@ package kakuro.core; -public class BoardCell +public class Cell { public enum CellType { @@ -18,18 +18,18 @@ public enum CellType private int value1 = -1; private int value2 = -1; - public BoardCell(CellType type) + public Cell(CellType type) { this.type = type; }; - public BoardCell(CellType type, int value1) + public Cell(CellType type, int value1) { this.type = type; this.value1 = value1; } - public BoardCell(CellType type, int value1, int value2) + public Cell(CellType type, int value1, int value2) { this.type = type; this.value1 = value1; diff --git a/src/kakuro/game/dao/GameDao.java b/src/kakuro/game/dao/GameDao.java index bdeb2b9..a74bbfd 100644 --- a/src/kakuro/game/dao/GameDao.java +++ b/src/kakuro/game/dao/GameDao.java @@ -6,7 +6,7 @@ import java.sql.SQLException; import java.util.ArrayList; -import kakuro.core.BoardCell; +import kakuro.core.Cell; public interface GameDao { @@ -17,5 +17,5 @@ public interface GameDao { * - the database connection * @return BoardCell object */ - ArrayList loadAllPreconfiguredGames(Connection conn) throws SQLException; + ArrayList loadAllPreconfiguredGames(Connection conn) throws SQLException; } diff --git a/src/kakuro/game/dao/GameDaoImpl.java b/src/kakuro/game/dao/GameDaoImpl.java index 5056348..62aa8e2 100644 --- a/src/kakuro/game/dao/GameDaoImpl.java +++ b/src/kakuro/game/dao/GameDaoImpl.java @@ -10,15 +10,15 @@ import com.google.gson.Gson; -import kakuro.core.BoardCell; +import kakuro.core.Cell; public class GameDaoImpl implements GameDao { private final String LOAD_ALL_PRECONFIGURED_GAMES = "SELECT cells FROM game"; @Override - public ArrayList loadAllPreconfiguredGames(Connection conn) throws SQLException { + public ArrayList loadAllPreconfiguredGames(Connection conn) throws SQLException { Gson gson = new Gson(); - ArrayList boardCells = new ArrayList(); + ArrayList boardCells = new ArrayList(); PreparedStatement pstmt = conn.prepareStatement(LOAD_ALL_PRECONFIGURED_GAMES); @@ -27,7 +27,7 @@ public ArrayList loadAllPreconfiguredGames(Connection conn) throw while(rs.next()) { String cells = rs.getString("cells"); - boardCells.add(gson.fromJson(cells, BoardCell[][].class)); + boardCells.add(gson.fromJson(cells, Cell[][].class)); } return boardCells; diff --git a/src/kakuro/gameprogress/dao/GameProgressDao.java b/src/kakuro/gameprogress/dao/GameProgressDao.java index 5949829..0ee6aa1 100644 --- a/src/kakuro/gameprogress/dao/GameProgressDao.java +++ b/src/kakuro/gameprogress/dao/GameProgressDao.java @@ -5,7 +5,7 @@ import java.sql.Connection; import java.sql.SQLException; -import kakuro.core.BoardCell; +import kakuro.core.Cell; public interface GameProgressDao { /** @@ -16,7 +16,7 @@ public interface GameProgressDao { * @param uid * - the username of the player */ - void save(Connection conn, String uid, BoardCell[][] board) throws SQLException; + void save(Connection conn, String uid, Cell[][] board) throws SQLException; /** * @@ -27,5 +27,5 @@ public interface GameProgressDao { * - the username of the player * @return BoardCell object */ - BoardCell[][] load(Connection conn, String uid) throws SQLException; + Cell[][] load(Connection conn, String uid) throws SQLException; } diff --git a/src/kakuro/gameprogress/dao/GameProgressDaoImpl.java b/src/kakuro/gameprogress/dao/GameProgressDaoImpl.java index afa6133..35e5921 100644 --- a/src/kakuro/gameprogress/dao/GameProgressDaoImpl.java +++ b/src/kakuro/gameprogress/dao/GameProgressDaoImpl.java @@ -8,14 +8,14 @@ import java.sql.SQLException; import com.google.gson.*; -import kakuro.core.BoardCell; +import kakuro.core.Cell; public class GameProgressDaoImpl implements GameProgressDao { private final String SAVE_GAME_PROGRESS = "UPDATE game_progress SET cells=? WHERE username=?"; private final String LOAD_GAME_PROGRESS = "SELECT cells FROM game_progress WHERE username=?"; @Override - public void save(Connection conn, String uid, BoardCell[][] board) throws SQLException { + public void save(Connection conn, String uid, Cell[][] board) throws SQLException { Gson gson = new Gson(); String boardCellJSON = gson.toJson(board); @@ -27,7 +27,7 @@ public void save(Connection conn, String uid, BoardCell[][] board) throws SQLExc } @Override - public BoardCell[][] load(Connection conn, String uid) throws SQLException { + public Cell[][] load(Connection conn, String uid) throws SQLException { Gson gson = new Gson(); PreparedStatement pstmt = conn.prepareStatement(LOAD_GAME_PROGRESS); @@ -38,7 +38,7 @@ public BoardCell[][] load(Connection conn, String uid) throws SQLException { if(rs.next()) { String cells = rs.getString("cells"); - return gson.fromJson(cells, BoardCell[][].class); + return gson.fromJson(cells, Cell[][].class); } return null; diff --git a/src/kakuro/models/GameModel.java b/src/kakuro/models/GameModel.java index 8640558..b2c0dd4 100644 --- a/src/kakuro/models/GameModel.java +++ b/src/kakuro/models/GameModel.java @@ -16,7 +16,7 @@ public class GameModel public final int columns; public final int rows; - public BoardCell[][] board; + public Cell[][] board; private static UniquePartitions partitions; public GameModel(final int columns, final int rows) @@ -28,13 +28,13 @@ public GameModel(final int columns, final int rows) public void initBoard() { - board = new BoardCell[this.rows][this.columns]; + board = new Cell[this.rows][this.columns]; for(int row = 0; row < this.rows; row++) { for(int column = 0; column < this.columns; column++) { - board[row][column] = new BoardCell(BoardCell.CellType.EMPTY); + board[row][column] = new Cell(Cell.CellType.EMPTY); } } } diff --git a/src/kakuro/views/BoardView.java b/src/kakuro/views/BoardView.java deleted file mode 100644 index dd3945d..0000000 --- a/src/kakuro/views/BoardView.java +++ /dev/null @@ -1,149 +0,0 @@ -package kakuro.views; - -import java.awt.BorderLayout; -import java.awt.Color; -import java.awt.Font; -import java.awt.GridLayout; -import java.awt.Panel; -import java.text.NumberFormat; - -import javax.swing.JFormattedTextField; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.JTextField; -import javax.swing.SwingConstants; -import javax.swing.border.LineBorder; -import javax.swing.text.NumberFormatter; - -import kakuro.core.BoardCell; -import kakuro.core.LinePanel; - -public class BoardView { - int gridSizeX; - int gridSizeY; - int rows; - int columns; - - public JTextField[][] cells; // TODO remove and use listeners to interact directly with model.board - public JPanel boardPanel; //The reference to the current displaying pane (board UI) - - //Number formatter for displaying - NumberFormat numberFormat = NumberFormat.getInstance(); - NumberFormatter numberFormatter = new NumberFormatter(numberFormat); - - public BoardView(int columns, int rows, int gridSizeX, int gridSizeY) { - cells = new JTextField[rows][columns]; - this.gridSizeX = gridSizeX; - this.gridSizeY = gridSizeY; - this.rows = rows; - this.columns = columns; - - numberFormatter.setValueClass(Integer.class); - numberFormatter.setMinimum(1); - numberFormatter.setMaximum(9); - } - - public JPanel getBoardUI(BoardCell[][] board) { - //creating grid of cells - JPanel panel = new JPanel(new GridLayout(gridSizeX,gridSizeY)); - //creating window of the game - - //this allows temporary invalid input, particularly to be able to delete and try again - //if invalid input, when clicking onto another cell, the input will be deleted - numberFormatter.setAllowsInvalid(true); - - //identifies type of each cell and populates it - //input or non-playable - for(int row = 0; row < rows; row++) - { - for(int column = 0; column < columns; column++) - { - JFormattedTextField textField = null; - - //tracking the type of each cell - BoardCell cell = board[row][column]; - //adding extra panel that will overlay the cells that are non-playable with game level numbers and diagonal line - JPanel diagonalPanel = null; - - //according to type of cell, populate - switch (cell.getType()) - { - case EMPTY: - textField = new JFormattedTextField(numberFormatter); - settingTextField(textField); - textField.setBorder(new LineBorder(Color.GRAY,1)); - panel.add(textField); - break; - - case INPUT: - textField = new JFormattedTextField(numberFormatter); - textField.setHorizontalAlignment(JTextField.CENTER); - textField.setBorder(new LineBorder(Color.GRAY,1)); - //When you load a game, there is some data exists. We have to check to make sure we are displaying the saved input - if(cell.getFirstValue()!=-1) - textField.setValue(cell.getFirstValue()); - panel.add(textField); - break; - - case FILLED01: - textField = new JFormattedTextField(cell.getSecondValue()); - settingTextField(textField); - //adding diagonal line in board cell - diagonalPanel = new LinePanel(new BorderLayout(), textField, true); - diagonalPanel.setBorder(new LineBorder(Color.GRAY,1)); - panel.add(diagonalPanel); - break; - - case FILLED10: - textField = new JFormattedTextField(cell.getFirstValue()); - settingTextField(textField); - //adding diagonal line in board cell - diagonalPanel = new LinePanel(new BorderLayout(), textField, false); - diagonalPanel.setBorder(new LineBorder(Color.GRAY,1)); - panel.add(diagonalPanel); - break; - - case FILLED11: - textField = new JFormattedTextField(cell.getFirstValue()); - JFormattedTextField textFieldRIGHT = new JFormattedTextField(cell.getSecondValue()); - settingTextField(textField); - settingTextField(textFieldRIGHT); - //using constructor that expects two text values to place in board cell - diagonalPanel = new LinePanel(new BorderLayout(), textField, textFieldRIGHT); - diagonalPanel.setBorder(new LineBorder(Color.GRAY,1)); - panel.add(diagonalPanel); - break; - - default: - break; - } - //placing textfield value in input array to track user input - this.cells[row][column] = textField; - } - } - - boardPanel = panel; - - return panel; - } - - public void settingTextField(JTextField txt) { - txt.setBackground(Color.black); - txt.setForeground(Color.white); - txt.setBorder(javax.swing.BorderFactory.createEmptyBorder()); - txt.setEditable(false); - } - - //Number formatter methods - public int getMinNumberValid() { - return (int) numberFormatter.getMinimum(); - } - - public Object getNumberFormatterClassType() { - return numberFormatter.getClass(); - } - - public int getMaxNumberValid() { - return (int) numberFormatter.getMaximum(); - } -} diff --git a/src/kakuro/views/GameConsole.java b/src/kakuro/views/GameConsole.java index 82fb25d..481acf4 100644 --- a/src/kakuro/views/GameConsole.java +++ b/src/kakuro/views/GameConsole.java @@ -29,7 +29,7 @@ public void printBoard(Boolean showAnswerValues) { int value = 0; int value2 = 0; - BoardCell cell = controller.model.board[row][column]; + Cell cell = controller.model.board[row][column]; switch (cell.getType()) { case EMPTY: @@ -115,7 +115,7 @@ public void printGetInputNumber(){ inputReader.nextLine(); } } - if (controller.model.board[row-1][column-1].getType() == BoardCell.CellType.INPUT) + if (controller.model.board[row-1][column-1].getType() == Cell.CellType.INPUT) break; else { System.out.println("error: not an input cell"); diff --git a/src/kakuro/views/GameView.java b/src/kakuro/views/GameView.java index bf61477..cbf673f 100644 --- a/src/kakuro/views/GameView.java +++ b/src/kakuro/views/GameView.java @@ -10,6 +10,8 @@ import java.awt.GridLayout; import java.text.NumberFormat; import java.util.Scanner; + +import javax.swing.JComponent; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; @@ -21,37 +23,35 @@ import kakuro.controllers.GameController; import kakuro.controllers.GameController.UserActions; -import kakuro.controllers.BoardController; -import kakuro.controllers.ButtonMenuController; +import kakuro.controllers.MenuBarController; import kakuro.controllers.ChronoController; -import kakuro.core.BoardCell; +import kakuro.core.Cell; import kakuro.core.LinePanel; public class GameView { private GameController controller; - - //Subviews - ChronoView chronoView; - BoardView boardView; - ButtonMenuView buttonMenuView; - + //UI components + JTextField[][] cells; JPanel currentBoard; //Main application frame and properties JFrame frame; int gridSizeX; int gridSizeY; - - //UI support - private ButtonMenuView buttonMenu; - - private JLabel startUI; + //Number formatter for displaying + NumberFormat numberFormat = NumberFormat.getInstance(); + NumberFormatter numberFormatter = new NumberFormatter(numberFormat); - public GameView(final GameController controller, Boolean X11, ChronoView chronoView, BoardView boardView, ButtonMenuView buttonMenuView) + public GameView(final GameController controller, Boolean X11, JComponent chronoLabel, JComponent menuBar) { + //Initialize number formatter + numberFormatter.setValueClass(Integer.class); + numberFormatter.setMinimum(1); + numberFormatter.setMaximum(9); + if (controller != null) { this.controller = controller; @@ -71,17 +71,13 @@ public GameView(final GameController controller, Boolean X11, ChronoView chronoV frame.setSize(x, y); frame.setResizable(false); - this.chronoView = chronoView; - this.boardView = boardView; - this.buttonMenuView = buttonMenuView; - //Create a timer at the top - frame.getContentPane().add(chronoView.timerLabel, BorderLayout.BEFORE_FIRST_LINE); + frame.getContentPane().add(chronoLabel, BorderLayout.BEFORE_FIRST_LINE); //Main game UI stays at the center - currentBoard = boardView.boardPanel; + currentBoard = getBoardUI(controller.model.board); frame.getContentPane().add(currentBoard); //Button menu stays at the bottom - frame.getContentPane().add(buttonMenuView.mainPanel, BorderLayout.AFTER_LAST_LINE); + frame.getContentPane().add(menuBar, BorderLayout.AFTER_LAST_LINE); frame.pack(); frame.setSize(x,y); @@ -89,8 +85,104 @@ public GameView(final GameController controller, Boolean X11, ChronoView chronoV } } - public void updateView(JPanel newBoardPanel) { - //If a panel is already attached to the frame, remove it + public JPanel getBoardUI(Cell[][] board) { + //creating grid of cells + cells = new JTextField[gridSizeX][gridSizeY]; + JPanel panel = new JPanel(new GridLayout(gridSizeX,gridSizeY)); + //creating window of the game + + //this allows temporary invalid input, particularly to be able to delete and try again + //if invalid input, when clicking onto another cell, the input will be deleted + numberFormatter.setAllowsInvalid(true); + + //identifies type of each cell and populates it + //input or non-playable + for(int row = 0; row < gridSizeX; row++) + { + for(int column = 0; column < gridSizeY; column++) + { + JFormattedTextField textField = null; + + //tracking the type of each cell + Cell cell = board[row][column]; + //adding extra panel that will overlay the cells that are non-playable with game level numbers and diagonal line + JPanel diagonalPanel = null; + + //according to type of cell, populate + switch (cell.getType()) + { + case EMPTY: + textField = new JFormattedTextField(numberFormatter); + settingTextField(textField); + textField.setBorder(new LineBorder(Color.GRAY,1)); + panel.add(textField); + break; + + case INPUT: + textField = new JFormattedTextField(numberFormatter); + textField.setHorizontalAlignment(JTextField.CENTER); + textField.setBorder(new LineBorder(Color.GRAY,1)); + //When you load a game, there is some data exists. We have to check to make sure we are displaying the saved input + if(cell.getFirstValue()!=-1) + textField.setValue(cell.getFirstValue()); + panel.add(textField); + break; + + case FILLED01: + textField = new JFormattedTextField(cell.getSecondValue()); + settingTextField(textField); + //adding diagonal line in board cell + diagonalPanel = new LinePanel(new BorderLayout(), textField, true); + diagonalPanel.setBorder(new LineBorder(Color.GRAY,1)); + panel.add(diagonalPanel); + break; + + case FILLED10: + textField = new JFormattedTextField(cell.getFirstValue()); + settingTextField(textField); + //adding diagonal line in board cell + diagonalPanel = new LinePanel(new BorderLayout(), textField, false); + diagonalPanel.setBorder(new LineBorder(Color.GRAY,1)); + panel.add(diagonalPanel); + break; + + case FILLED11: + textField = new JFormattedTextField(cell.getFirstValue()); + JFormattedTextField textFieldRIGHT = new JFormattedTextField(cell.getSecondValue()); + settingTextField(textField); + settingTextField(textFieldRIGHT); + //using constructor that expects two text values to place in board cell + diagonalPanel = new LinePanel(new BorderLayout(), textField, textFieldRIGHT); + diagonalPanel.setBorder(new LineBorder(Color.GRAY,1)); + panel.add(diagonalPanel); + break; + + default: + break; + } + + //placing textfield value in input array to track user input + this.cells[row][column] = textField; + } + } + + return panel; + } + + public JTextField[][] getSavedInput(){ + return cells; + } + + public void settingTextField(JTextField txt) { + txt.setBackground(Color.black); + txt.setForeground(Color.white); + txt.setBorder(javax.swing.BorderFactory.createEmptyBorder()); + txt.setEditable(false); + } + + public void updateView() { + //If a panel is already attached to the frame, remove it + JPanel newBoardPanel = getBoardUI(controller.model.board); if(currentBoard != null) frame.getContentPane().remove(currentBoard); @@ -107,12 +199,16 @@ public void hideBoard() { currentBoard.setVisible(false); } - //Methods for the menuButtons - public ButtonMenuView getButtonMenu() { - return buttonMenu; + //Number formatter methods + public int getMinNumberValid() { + return (int) numberFormatter.getMinimum(); } - - public void setButtonMenu(ButtonMenuView buttonMenu) { - this.buttonMenu = buttonMenu; + + public Object getNumberFormatterClassType() { + return numberFormatter.getClass(); + } + + public int getMaxNumberValid() { + return (int) numberFormatter.getMaximum(); } } diff --git a/src/kakuro/views/ButtonMenuView.java b/src/kakuro/views/MenuBarView.java similarity index 86% rename from src/kakuro/views/ButtonMenuView.java rename to src/kakuro/views/MenuBarView.java index 94d17ba..163afc0 100644 --- a/src/kakuro/views/ButtonMenuView.java +++ b/src/kakuro/views/MenuBarView.java @@ -11,13 +11,13 @@ import kakuro.controllers.ChronoController; import kakuro.controllers.GameController; -import kakuro.controllers.ButtonMenuController; +import kakuro.controllers.MenuBarController; import kakuro.core.GameDifficulty; import kakuro.core.GameDifficultyListItem; -public class ButtonMenuView { +public class MenuBarView { //Controller - ButtonMenuController buttonMenuController; + MenuBarController menuBarController; //UI components JButton pause_button; @@ -26,10 +26,10 @@ public class ButtonMenuView { JButton save_button; JButton restart_button; JButton load_button; - JPanel mainPanel; + public JPanel mainPanel; - public ButtonMenuView(ButtonMenuController buttonMenuController) { - this.buttonMenuController = buttonMenuController; + public MenuBarView(MenuBarController menuBarController) { + this.menuBarController = menuBarController; //Initialize the buttons pause_button = new JButton("Pause"); @@ -69,13 +69,13 @@ public void buttonsSetUp() { { public void actionPerformed(ActionEvent e) { - if(!buttonMenuController.isPaused()) { + if(!menuBarController.isPaused()) { pause_button.setText("Resume"); - buttonMenuController.pause(); + menuBarController.pause(); } else { pause_button.setText("Pause"); - buttonMenuController.resume(); + menuBarController.resume(); } } }); @@ -85,7 +85,7 @@ public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { - buttonMenuController.submit(); + menuBarController.submit(); } }); @@ -96,7 +96,7 @@ public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { - buttonMenuController.save(); + menuBarController.save(); } }); @@ -108,7 +108,7 @@ public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { - if(buttonMenuController.load() != null) { + if(menuBarController.load() != null) { JOptionPane.showMessageDialog(null, "Successfully loaded saved game!"); toggleMenu(); } else { @@ -134,7 +134,7 @@ public void actionPerformed(ActionEvent e) GameDifficultyListItem difficultyItem = (GameDifficultyListItem) JOptionPane.showInputDialog(null, "Choose a difficulty level", "Difficulty level", JOptionPane.PLAIN_MESSAGE, null, levels, levels[0]); if(difficultyItem != null) { - buttonMenuController.loadPreconfiguredGame(difficultyItem.getDifficulty()); + menuBarController.loadPreconfiguredGame(difficultyItem.getDifficulty()); toggleMenu(); } } @@ -144,7 +144,7 @@ public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { - buttonMenuController.restart(); + menuBarController.restart(); } }); } diff --git a/tests/kakuro/TestBoard.java b/tests/kakuro/TestBoard.java index 78d7238..473ab5e 100644 --- a/tests/kakuro/TestBoard.java +++ b/tests/kakuro/TestBoard.java @@ -10,7 +10,7 @@ import org.junit.Test; import kakuro.controllers.GameController; -import kakuro.core.BoardCell; +import kakuro.core.Cell; import kakuro.models.GameModel; public class TestBoard{ @@ -35,11 +35,11 @@ public void testValidBoard(){ GameController controller = new GameController(10,10, GUI); GameModel model = new GameModel(10,10); model.initBoard(); //initialize board - model.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); - model.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - model.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); + model.board[0][1] = new Cell(Cell.CellType.FILLED10, 5); + model.board[1][0] = new Cell(Cell.CellType.FILLED01,-1,4); + model.board[1][1] = new Cell(Cell.CellType.INPUT,3,-1); + model.board[2][1] = new Cell(Cell.CellType.INPUT,2,-1); + model.board[1][2] = new Cell(Cell.CellType.INPUT,1,-1); controller.model = model; // Act solved = controller.solveBoard(); @@ -53,11 +53,11 @@ public void testNotValidBoardOneSumIsWrong(){ GameController controller = new GameController(10,10, GUI); GameModel model = new GameModel(10,10); model.initBoard(); - model.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); - model.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - model.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,5,-1); + model.board[0][1] = new Cell(Cell.CellType.FILLED10, 5); + model.board[1][0] = new Cell(Cell.CellType.FILLED01,-1,4); + model.board[1][1] = new Cell(Cell.CellType.INPUT,3,-1); + model.board[2][1] = new Cell(Cell.CellType.INPUT,2,-1); + model.board[1][2] = new Cell(Cell.CellType.INPUT,5,-1); controller.model = model; // Act solved = controller.solveBoard(); @@ -71,11 +71,11 @@ public void testNotValidBoardCorrectSumDuplicateEntries(){ GameController controller = new GameController(10,10, GUI); GameModel model = new GameModel(10,10); model.initBoard(); - model.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); - model.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - model.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,2,-1); + model.board[0][1] = new Cell(Cell.CellType.FILLED10, 5); + model.board[1][0] = new Cell(Cell.CellType.FILLED01,-1,4); + model.board[1][1] = new Cell(Cell.CellType.INPUT,2,-1); + model.board[2][1] = new Cell(Cell.CellType.INPUT,3,-1); + model.board[1][2] = new Cell(Cell.CellType.INPUT,2,-1); controller.model = model; // Act solved = controller.solveBoard(); diff --git a/tests/kakuro/TestBoardCell.java b/tests/kakuro/TestBoardCell.java index c40efa3..7bab8f0 100644 --- a/tests/kakuro/TestBoardCell.java +++ b/tests/kakuro/TestBoardCell.java @@ -9,30 +9,30 @@ import org.junit.BeforeClass; import org.junit.Test; -import kakuro.core.BoardCell; +import kakuro.core.Cell; public class TestBoardCell { @Test public void testNonInputCell(){ // Arrange - BoardCell.CellType cellType = BoardCell.CellType.EMPTY; + Cell.CellType cellType = Cell.CellType.EMPTY; // Act - BoardCell cell = new BoardCell(cellType); + Cell cell = new Cell(cellType); // Assert - assertEquals(cell.getType(), BoardCell.CellType.EMPTY); + assertEquals(cell.getType(), Cell.CellType.EMPTY); } @Test public void testInputCell(){ // Arrange int inputNumber = 1; - BoardCell.CellType cellType = BoardCell.CellType.EMPTY; + Cell.CellType cellType = Cell.CellType.EMPTY; // Act - BoardCell cell = new BoardCell(cellType, inputNumber); + Cell cell = new Cell(cellType, inputNumber); // Assert assertEquals(cell.getFirstValue(), inputNumber); - assertEquals(cell.getType(), BoardCell.CellType.EMPTY); + assertEquals(cell.getType(), Cell.CellType.EMPTY); } @Test @@ -40,12 +40,12 @@ public void testInputAnswerCell(){ // Arrange int inputNumber = 1; int answerNumber = 3; - BoardCell.CellType cellType = BoardCell.CellType.EMPTY; + Cell.CellType cellType = Cell.CellType.EMPTY; // Act - BoardCell cell = new BoardCell(cellType, inputNumber, answerNumber); + Cell cell = new Cell(cellType, inputNumber, answerNumber); // Assert assertEquals(cell.getFirstValue(), inputNumber); assertEquals(cell.getSecondValue(), answerNumber); - assertEquals(cell.getType(), BoardCell.CellType.EMPTY); + assertEquals(cell.getType(), Cell.CellType.EMPTY); } } diff --git a/tests/kakuro/TestChrono.java b/tests/kakuro/TestChrono.java index d8adbdc..4893b11 100644 --- a/tests/kakuro/TestChrono.java +++ b/tests/kakuro/TestChrono.java @@ -11,10 +11,9 @@ import org.junit.Test; import kakuro.controllers.GameController; -import kakuro.controllers.BoardController; -import kakuro.controllers.ButtonMenuController; +import kakuro.controllers.MenuBarController; import kakuro.controllers.ChronoController; -import kakuro.views.ButtonMenuView; +import kakuro.views.MenuBarView; import kakuro.views.GameView; diff --git a/tests/kakuro/TestGameController.java b/tests/kakuro/TestGameController.java index 5729aca..0d107ee 100644 --- a/tests/kakuro/TestGameController.java +++ b/tests/kakuro/TestGameController.java @@ -13,7 +13,7 @@ import org.junit.Test; import kakuro.models.GameModel; import kakuro.controllers.GameController; -import kakuro.core.BoardCell; +import kakuro.core.Cell; import kakuro.core.DatabaseConnection; public class TestGameController { @@ -76,11 +76,11 @@ public void testBoardSolveValidBoard() { GameController controller = new GameController(10,10, GUI); GameModel model = controller.model; model.initBoard(); //initialize board - model.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); - model.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - model.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); + model.board[0][1] = new Cell(Cell.CellType.FILLED10, 5); + model.board[1][0] = new Cell(Cell.CellType.FILLED01,-1,4); + model.board[1][1] = new Cell(Cell.CellType.INPUT,3,-1); + model.board[2][1] = new Cell(Cell.CellType.INPUT,2,-1); + model.board[1][2] = new Cell(Cell.CellType.INPUT,1,-1); //Act solved = controller.solveBoard(); //Assert @@ -94,11 +94,11 @@ public void testBoardSolveInvalidBoardWithOneWrongVerticalSum() { GameController controller = new GameController(10,10, GUI); GameModel model = controller.model; model.initBoard(); //initialize board - model.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); - model.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,6,-1); - model.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); + model.board[0][1] = new Cell(Cell.CellType.FILLED10, 5); + model.board[1][0] = new Cell(Cell.CellType.FILLED01,-1,4); + model.board[1][1] = new Cell(Cell.CellType.INPUT,3,-1); + model.board[2][1] = new Cell(Cell.CellType.INPUT,6,-1); + model.board[1][2] = new Cell(Cell.CellType.INPUT,1,-1); //Act solved = controller.solveBoard(); //Assert @@ -112,11 +112,11 @@ public void testBoardSolveInvalidBoardWithOneWrongHorizontalSum() { GameController controller = new GameController(10,10, GUI); GameModel model = controller.model; model.initBoard(); //initialize board - model.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,8); - model.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - model.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); + model.board[0][1] = new Cell(Cell.CellType.FILLED10, 5); + model.board[1][0] = new Cell(Cell.CellType.FILLED01,-1,8); + model.board[1][1] = new Cell(Cell.CellType.INPUT,3,-1); + model.board[2][1] = new Cell(Cell.CellType.INPUT,2,-1); + model.board[1][2] = new Cell(Cell.CellType.INPUT,1,-1); //Act solved = controller.solveBoard(); //Assert @@ -130,11 +130,11 @@ public void testBoardSolveInvalidBoardWithDuplicateEntries() { GameController controller = new GameController(10,10, GUI); GameModel model = controller.model; model.initBoard(); //initialize board - model.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); - model.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - model.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,2,-1); + model.board[0][1] = new Cell(Cell.CellType.FILLED10, 5); + model.board[1][0] = new Cell(Cell.CellType.FILLED01,-1,4); + model.board[1][1] = new Cell(Cell.CellType.INPUT,2,-1); + model.board[2][1] = new Cell(Cell.CellType.INPUT,3,-1); + model.board[1][2] = new Cell(Cell.CellType.INPUT,2,-1); //Act solved = controller.solveBoard(); //Assert diff --git a/tests/kakuro/TestGameViewInput.java b/tests/kakuro/TestGameViewInput.java index 57abfde..9184ff7 100644 --- a/tests/kakuro/TestGameViewInput.java +++ b/tests/kakuro/TestGameViewInput.java @@ -13,25 +13,22 @@ import org.junit.Test; import kakuro.controllers.GameController; -import kakuro.controllers.BoardController; import kakuro.views.GameView; public class TestGameViewInput { private static GameController gameController; - private static BoardController boardController; @BeforeClass public static void onlyOnce() { gameController = new GameController(10, 10, false); - boardController = new BoardController(10, 10, gameController); } private boolean hasValidRange(final int value) { - return (value <= boardController.getMaxNumberValid() && value >= boardController.getMinNumberValid()); + return (value <= gameController.getMaxNumberValid() && value >= gameController.getMinNumberValid()); } @Test @@ -95,7 +92,7 @@ public void testInvalidInputString(){ String value = "yo"; Boolean isValid = false; // Act - isValid = boardController.getNumberFormatterClassType().equals(value.getClass()); + isValid = gameController.getNumberFormatterClassType().equals(value.getClass()); // Assert assertFalse(isValid); }