From 8f9911d736ce1a194b7a54cbd79c4f5d59d57732 Mon Sep 17 00:00:00 2001 From: Josh Pham Date: Mon, 9 Mar 2020 21:42:02 -0400 Subject: [PATCH 1/9] Remove play button/Start timer only when a game is started/Refactor game difficulty selection --- src/kakuro/ButtonMenu.java | 17 ++++++++++------- src/kakuro/Chrono.java | 1 - src/kakuro/GameDifficulty.java | 15 +++++++++++++++ src/kakuro/GameDifficultyListItem.java | 19 +++++++++++++++++++ 4 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 src/kakuro/GameDifficulty.java create mode 100644 src/kakuro/GameDifficultyListItem.java diff --git a/src/kakuro/ButtonMenu.java b/src/kakuro/ButtonMenu.java index 8a254d2..24343fd 100644 --- a/src/kakuro/ButtonMenu.java +++ b/src/kakuro/ButtonMenu.java @@ -45,9 +45,8 @@ public ButtonMenu(JFrame appFrame, int x, int y, GameController gameController) chrono = new Chrono(); // Set up - mainPanel.add(play_button).setVisible(false); mainPanel.add(pause_button).setVisible(false); - mainPanel.add(restart_button); + mainPanel.add(restart_button).setVisible(false);; mainPanel.add(submit_button).setVisible(false); mainPanel.add(choose_game_button); mainPanel.add(save_button).setVisible(false); @@ -68,10 +67,10 @@ public void chronoSetUp() { } private void toggleMenu() { - play_button.setVisible(true); pause_button.setVisible(true); submit_button.setVisible(true); save_button.setVisible(true); + restart_button.setVisible(true); choose_game_button.setVisible(false); load_button.setVisible(false); @@ -161,14 +160,18 @@ public void actionPerformed(ActionEvent e) { //TODO: hardcoded for now and need other boards solution boards -iteration 2 UI - Object[] levels = {"1", "2", "3"}; - String chooseGame = (String) JOptionPane.showInputDialog(null, "Choose a level (1 - 3), 1 being easiest to 3 being the hardest", "Difficulty level", JOptionPane.PLAIN_MESSAGE, null, levels, levels[0]); + GameDifficultyListItem[] levels = {new GameDifficultyListItem("Easy", GameDifficulty.EASY), + new GameDifficultyListItem("Medium", GameDifficulty.MEDIUM), + new GameDifficultyListItem("Difficult", GameDifficulty.DIFFICULT)}; + + GameDifficultyListItem difficultyItem = (GameDifficultyListItem) JOptionPane.showInputDialog(null, "Choose a difficulty level", "Difficulty level", JOptionPane.PLAIN_MESSAGE, null, levels, levels[0]); - if(chooseGame != null) { - int gameLevel = Integer.parseInt(chooseGame); + if(difficultyItem != null) { + int gameLevel = GameDifficulty.GameDifficultyToInt(difficultyItem.getDifficulty()); toggleMenu(); gameController.loadPreconfiguredGame(gameLevel); + chrono.chronoStart(); } } }); diff --git a/src/kakuro/Chrono.java b/src/kakuro/Chrono.java index 43e2cd9..9b1e216 100644 --- a/src/kakuro/Chrono.java +++ b/src/kakuro/Chrono.java @@ -67,7 +67,6 @@ public void actionPerformed(ActionEvent e1) }; time = new Timer(delay,timerListener); - time.start(); } public void chronoPause() { diff --git a/src/kakuro/GameDifficulty.java b/src/kakuro/GameDifficulty.java new file mode 100644 index 0000000..9c69d68 --- /dev/null +++ b/src/kakuro/GameDifficulty.java @@ -0,0 +1,15 @@ +package kakuro; + +public enum GameDifficulty { + EASY, + MEDIUM, + DIFFICULT; + + public static int GameDifficultyToInt(GameDifficulty difficulty) { + switch(difficulty) { + case EASY: return 1; + case MEDIUM: return 2; + default: return 3; + } + } +} diff --git a/src/kakuro/GameDifficultyListItem.java b/src/kakuro/GameDifficultyListItem.java new file mode 100644 index 0000000..030549f --- /dev/null +++ b/src/kakuro/GameDifficultyListItem.java @@ -0,0 +1,19 @@ +package kakuro; + +public class GameDifficultyListItem { + private String description; + private GameDifficulty difficulty; + + public GameDifficultyListItem(String description, GameDifficulty difficulty) { + this.description = description; + this.difficulty = difficulty; + } + + public GameDifficulty getDifficulty() { + return difficulty; + } + + public String toString() { + return description; + } +} From df31694f2e3cc77a5ab491ea73cd6c129bc7fa2f Mon Sep 17 00:00:00 2001 From: Josh Pham Date: Tue, 10 Mar 2020 10:19:43 -0400 Subject: [PATCH 2/9] UI adjustments and refactor Added a UI for when the application first run (including moving frame initialization outside of board_ui()) Display the timer only when a game is in progress Adjust borders of UI elements --- src/kakuro/ButtonMenu.java | 3 ++- src/kakuro/Chrono.java | 12 +++++++++- src/kakuro/GameController.java | 3 ++- src/kakuro/GameView.java | 41 ++++++++++++++++++++++------------ 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/kakuro/ButtonMenu.java b/src/kakuro/ButtonMenu.java index 24343fd..221137c 100644 --- a/src/kakuro/ButtonMenu.java +++ b/src/kakuro/ButtonMenu.java @@ -37,7 +37,7 @@ public ButtonMenu(JFrame appFrame, int x, int y, GameController gameController) play_button = new JButton("Play"); submit_button = new JButton("Submit"); newGame_button = new JButton("New Game"); - choose_game_button = new JButton("Choose a game"); + choose_game_button = new JButton("New Game"); save_button = new JButton("Save"); restart_button = new JButton("Restart"); load_button = new JButton("Load Game"); @@ -172,6 +172,7 @@ public void actionPerformed(ActionEvent e) gameController.loadPreconfiguredGame(gameLevel); chrono.chronoStart(); + chrono.toggleTimerDisplay(); } } }); diff --git a/src/kakuro/Chrono.java b/src/kakuro/Chrono.java index 9b1e216..eb3196d 100644 --- a/src/kakuro/Chrono.java +++ b/src/kakuro/Chrono.java @@ -3,8 +3,10 @@ //@brief Timer class import javax.swing.*; +import javax.swing.border.EmptyBorder; import java.awt.BorderLayout; +import java.awt.Font; import java.awt.event.*; public class Chrono @@ -13,6 +15,7 @@ public class Chrono private int minutes=0; private int seconds=0; private int delay=1000; + private boolean isVisible=false; JLabel timerLabel; ActionListener timerListener; Timer time; @@ -20,7 +23,10 @@ public class Chrono /* Constructor : Creates all the UI components and add them on the panel. */ public Chrono() { - timerLabel = new JLabel("0:"+hours+":"+minutes+":"+seconds); + timerLabel = new JLabel("0:"+hours+":"+minutes+":"+seconds, SwingConstants.CENTER); + timerLabel.setFont(new Font(timerLabel.getFont().getFontName(), Font.PLAIN, 20)); + timerLabel.setVisible(false); + timerLabel.setBorder(new EmptyBorder(10,0,10,0));//top,left,bottom,right } public JLabel getTimerLabel() { @@ -108,5 +114,9 @@ public int getSeconds() { public void setSeconds(int seconds) { this.seconds = seconds; } + + public void toggleTimerDisplay() { + timerLabel.setVisible(!isVisible); + } } diff --git a/src/kakuro/GameController.java b/src/kakuro/GameController.java index afacc72..e685120 100644 --- a/src/kakuro/GameController.java +++ b/src/kakuro/GameController.java @@ -59,7 +59,8 @@ private void initGame(GameModel model) view.printStartup(); view.printBoard(false/*show answer values*/); if (gui){ - view.board_ui(); + //view.board_ui(); + view.start_ui(); view.settingUpMenu(); } } diff --git a/src/kakuro/GameView.java b/src/kakuro/GameView.java index cd6fdb1..57d3872 100644 --- a/src/kakuro/GameView.java +++ b/src/kakuro/GameView.java @@ -6,13 +6,16 @@ import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Font; import java.awt.GridLayout; import java.text.NumberFormat; import java.util.Scanner; import javax.swing.JFormattedTextField; import javax.swing.JFrame; +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; @@ -33,7 +36,8 @@ public class GameView private ButtonMenu buttonMenu; // TODO remove and use listeners to interact directly with model.board public JTextField[][] saveInput; - private JPanel currentPanel; //The reference to the current displaying pane (board UI) + private JPanel boardPanel; //The reference to the current displaying pane (board UI) + private JTextField gameTitle; public ButtonMenu getButtonMenu() { return buttonMenu; @@ -61,6 +65,14 @@ public GameView(final GameController controller, Boolean X11) frame = new JFrame("KAKURO"); buttonMenu = new ButtonMenu(frame, gridSizeX, gridSizeY, controller); saveInput = new JTextField[controller.model.rows][controller.model.columns]; + + //Initialize the application frame + int frameSize = 60; + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + int x = frameSize*gridSizeX; + int y = frameSize*gridSizeY; + frame.setSize(x, y); + frame.setResizable(false); } numberFormatter.setValueClass(Integer.class); @@ -106,15 +118,20 @@ public Object getNumberFormatterClassType() { return numberFormatter.getClass(); } - public int getMaxNumberValid() { return (int) numberFormatter.getMaximum(); } + public void start_ui() { + JLabel gameTitle = new JLabel("KAKURO", SwingConstants.CENTER); + gameTitle.setFont(new Font(gameTitle.getFont().getFontName(), Font.PLAIN, 120)); + + frame.getContentPane().add(gameTitle); + frame.setVisible(true); + } + //creates user interface of the game board public void board_ui() { - - int frameSize = 60; //creating grid of cells JPanel panel = new JPanel(new GridLayout(gridSizeX,gridSizeY)); //creating window of the game @@ -192,24 +209,17 @@ public void board_ui() { this.saveInput[row][column] = textField; } } - int x = frameSize*gridSizeX; - int y = frameSize*gridSizeY; - - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setSize(x, y); - frame.setResizable(false); //If a panel is already attached to the frame, remove it - if(currentPanel != null) - frame.getContentPane().remove(currentPanel); + if(boardPanel != null) + frame.getContentPane().remove(boardPanel); //Save a reference to the new panel frame.getContentPane().add(panel); - currentPanel = panel; + boardPanel = panel; // currentPanel = panel; frame.setVisible(true); - } public void settingTextField(JTextField txt) { @@ -218,6 +228,7 @@ public void settingTextField(JTextField txt) { txt.setBorder(javax.swing.BorderFactory.createEmptyBorder()); txt.setEditable(false); } + public void printBoard(Boolean showAnswerValues) { System.out.println("board:"); @@ -353,4 +364,6 @@ public void settingUpMenu() { buttonMenu.chronoSetUp(); buttonMenu.buttonsSetUp(); } + + } From c5a40b845559b18e9e8e20b3588f5ef24fecc037 Mon Sep 17 00:00:00 2001 From: Josh Pham Date: Thu, 12 Mar 2020 18:55:22 -0400 Subject: [PATCH 3/9] Refactor the code and fixed merge conglicts and commented out TestGameController --- resources/SQLiteKakuro.db | Bin 36864 -> 45056 bytes src/kakuro/GameDifficulty.java | 15 - src/kakuro/GameModel.java | 92 ----- src/kakuro/GameView.java | 369 ------------------ src/kakuro/Main.java | 4 +- .../AppController.java} | 99 +++-- src/kakuro/controllers/BoardController.java | 53 +++ .../controllers/ButtonMenuController.java | 57 +++ src/kakuro/controllers/ChronoController.java | 84 ++++ src/kakuro/{ => core}/BoardCell.java | 18 +- .../{utils => core}/DatabaseConnection.java | 2 +- src/kakuro/core/GameDifficulty.java | 26 ++ .../{ => core}/GameDifficultyListItem.java | 2 +- .../{line_panel.java => core/LinePanel.java} | 8 +- src/kakuro/{ => core}/Tools.java | 2 +- src/kakuro/{ => core}/UniquePartitions.java | 6 +- src/kakuro/game/dao/GameDao.java | 2 +- src/kakuro/game/dao/GameDaoImpl.java | 2 +- .../gameprogress/dao/GameProgressDao.java | 3 +- .../gameprogress/dao/GameProgressDaoImpl.java | 2 +- .../{Chrono.java => models/ChronoModel.java} | 83 ++-- src/kakuro/models/GameModel.java | 41 ++ src/kakuro/{ => models}/PlayerModel.java | 2 +- src/kakuro/views/BoardView.java | 149 +++++++ .../ButtonMenuView.java} | 109 ++---- src/kakuro/views/ChronoView.java | 28 ++ src/kakuro/views/GameConsole.java | 151 +++++++ src/kakuro/views/GameView.java | 118 ++++++ tests/kakuro/TestBoard.java | 10 +- tests/kakuro/TestBoardCell.java | 5 +- tests/kakuro/TestBoardUI.java | 4 +- tests/kakuro/TestChrono.java | 22 +- tests/kakuro/TestDBConnection.java | 2 +- tests/kakuro/TestGameController.java | 330 ++++++++-------- tests/kakuro/TestGameViewInput.java | 27 +- tests/kakuro/TestPlayerDao.java | 3 +- tests/kakuro/TestPlayerModel.java | 3 +- tests/kakuro/TestUniquePartitions.java | 2 + 38 files changed, 1082 insertions(+), 853 deletions(-) delete mode 100644 src/kakuro/GameDifficulty.java delete mode 100644 src/kakuro/GameModel.java delete mode 100644 src/kakuro/GameView.java rename src/kakuro/{GameController.java => controllers/AppController.java} (75%) create mode 100644 src/kakuro/controllers/BoardController.java create mode 100644 src/kakuro/controllers/ButtonMenuController.java create mode 100644 src/kakuro/controllers/ChronoController.java rename src/kakuro/{ => core}/BoardCell.java (68%) rename src/kakuro/{utils => core}/DatabaseConnection.java (99%) create mode 100644 src/kakuro/core/GameDifficulty.java rename src/kakuro/{ => core}/GameDifficultyListItem.java (95%) rename src/kakuro/{line_panel.java => core/LinePanel.java} (90%) rename src/kakuro/{ => core}/Tools.java (98%) rename src/kakuro/{ => core}/UniquePartitions.java (98%) rename src/kakuro/{Chrono.java => models/ChronoModel.java} (50%) create mode 100644 src/kakuro/models/GameModel.java rename src/kakuro/{ => models}/PlayerModel.java (96%) create mode 100644 src/kakuro/views/BoardView.java rename src/kakuro/{ButtonMenu.java => views/ButtonMenuView.java} (60%) create mode 100644 src/kakuro/views/ChronoView.java create mode 100644 src/kakuro/views/GameConsole.java create mode 100644 src/kakuro/views/GameView.java diff --git a/resources/SQLiteKakuro.db b/resources/SQLiteKakuro.db index 188eac6a8bbf9c8d64e1bd5b2a2f5fd942ddf3b0..ea1bdb4613fda8dbae6c79ab0aabdc40c5b12726 100644 GIT binary patch delta 311 zcmZozz|`=7X@WE_4+8@OHxP3HG2=uHb4H$x2}|`^`2-mF1U3sQu<>y(kYZ$KU^pPp zpcp-QVvh0T_%h+m339U;SwK1$IRs1$;F!$PATe1!L16Nq2JXq$2~!wNCSOcA1>$X9 zo~XslXg2v^o7mt$AZylb5zb{ zMy`<5;*x-z#LCp7jg40s8B-^-BrKjR(4G#|B{NySvu3hQ4iCZwd_6py{gOYi1I<%R a6hpEhfzcS~R-?%v1qwO5Oep4_W&{AR>TnML delta 67 zcmZp8z|^pSX@WE_Cj$cm2!p{y4Rc1$jR{NjS^3{F@W0zE7;u$;VgSF803#bnUNI!K PxFjGau`;!2kwX9g0oo9r diff --git a/src/kakuro/GameDifficulty.java b/src/kakuro/GameDifficulty.java deleted file mode 100644 index 9c69d68..0000000 --- a/src/kakuro/GameDifficulty.java +++ /dev/null @@ -1,15 +0,0 @@ -package kakuro; - -public enum GameDifficulty { - EASY, - MEDIUM, - DIFFICULT; - - public static int GameDifficultyToInt(GameDifficulty difficulty) { - switch(difficulty) { - case EASY: return 1; - case MEDIUM: return 2; - default: return 3; - } - } -} diff --git a/src/kakuro/GameModel.java b/src/kakuro/GameModel.java deleted file mode 100644 index 74cd1dd..0000000 --- a/src/kakuro/GameModel.java +++ /dev/null @@ -1,92 +0,0 @@ -// @author Vsevolod Ivanov -// @author ... -// @brief Game model class which handles the Kakuro game storage. - -package kakuro; - -import java.util.Enumeration; - -import javax.swing.JTextField; -import javax.swing.tree.TreeNode; - -public class GameModel -{ - - public final int columns; - public final int rows; - public BoardCell[][] board; - private static UniquePartitions partitions; - - public GameModel(final int columns, final int rows) - { - this.columns = columns; - this.rows = rows; - partitions = new UniquePartitions(); - } - - public void initBoard() - { - board = new BoardCell[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); - } - } - } - - //TODO: to remove - iteration2 - public void generateBoard10x10() - { - // second row - TreeNode eightBlocksTree = partitions.root.getChildAt(6 /* start at two blocks tree */); - // chose a random sum in children - TreeNode randEightBlockTree = eightBlocksTree.getChildAt(Tools.randomInt(0,7)); - int randEightBlockSum = Integer.parseInt(randEightBlockTree.toString()); - board[1][0] = new BoardCell(BoardCell.CellType.FILLED01, -1, randEightBlockSum); - // mark & fill the input cells - int[] array = Tools.childrenToArray(randEightBlockTree); - for(int column = 1; column <= 8; column++) - { - board[1][column] = new BoardCell(BoardCell.CellType.INPUT, -1, array[column-1]); - } - // row - 1 - eightBlocksTree = partitions.root.getChildAt(6 /* start at two blocks tree */); - // chose a random sum in children - randEightBlockTree = eightBlocksTree.getChildAt(Tools.randomInt(1,7)); - randEightBlockSum = Integer.parseInt(randEightBlockTree.toString()); - board[8][0] = new BoardCell(BoardCell.CellType.FILLED01, -1, randEightBlockSum); - // mark & fill the input cells - array = Tools.childrenToArray(randEightBlockTree); - for(int column = 8; column >=1; column--) - { - board[8][column] = new BoardCell(BoardCell.CellType.INPUT, -1, array[8-column]); - } - // second row - eightBlocksTree = partitions.root.getChildAt(6 /* start at two blocks tree */); - // chose a random sum in children - randEightBlockTree = eightBlocksTree.getChildAt(Tools.randomInt(0,7)); - randEightBlockSum = Integer.parseInt(randEightBlockTree.toString()); - // find sum for column that connect first row to row - 1 - for(Enumeration tree = eightBlocksTree.children(); tree.hasMoreElements();) - { - TreeNode candidateTree = tree.nextElement(); - int[] candidatesArray = Tools.childrenToArray(candidateTree); - // second column - if (candidatesArray[0] == board[1][1].getSecondValue() && candidatesArray[7] == board[1][8].getSecondValue()) - { - // put sum number - int sum = Integer.parseInt(candidateTree.toString()); - board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, sum); - // mark & fill the input cells - for(int row = 1; row < 8; row++) - { - board[row+1][1] = new BoardCell(BoardCell.CellType.INPUT, -1, candidatesArray[row]); - } - break; - } - } - } -} diff --git a/src/kakuro/GameView.java b/src/kakuro/GameView.java deleted file mode 100644 index 57d3872..0000000 --- a/src/kakuro/GameView.java +++ /dev/null @@ -1,369 +0,0 @@ -// @author Vsevolod Ivanov -// @author Isabelle Charette -// @brief Game view class which handles the Kakuro game interface. - -package kakuro; - -import java.awt.BorderLayout; -import java.awt.Color; -import java.awt.Font; -import java.awt.GridLayout; -import java.text.NumberFormat; -import java.util.Scanner; -import javax.swing.JFormattedTextField; -import javax.swing.JFrame; -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; - -/* - * SOURCES - * Examples on extending JPanel class and using paintComponent method // Examples for textfield formatting - * http://programmedlessons.org/java5/Notes/chap36/ch36_10.html - * https://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html - * https://docs.oracle.com/javase/8/docs/api/javax/swing/JFormattedTextField.html - */ - -import kakuro.GameController.UserActions; - -public class GameView -{ - private GameController controller; - private Scanner inputReader = new Scanner(System.in); - private ButtonMenu buttonMenu; - // TODO remove and use listeners to interact directly with model.board - public JTextField[][] saveInput; - private JPanel boardPanel; //The reference to the current displaying pane (board UI) - private JTextField gameTitle; - - public ButtonMenu getButtonMenu() { - return buttonMenu; - } - - public void setButtonMenu(ButtonMenu buttonMenu) { - this.buttonMenu = buttonMenu; - } - - JFrame frame; - int gridSizeX; - int gridSizeY; - NumberFormat numberFormat = NumberFormat.getInstance(); - NumberFormatter numberFormatter = new NumberFormatter(numberFormat); - - public GameView(final GameController controller, Boolean X11) - { - if (controller != null) - { - this.controller = controller; - gridSizeX = controller.model.rows; - gridSizeY = controller.model.columns; - } - if (X11) { - frame = new JFrame("KAKURO"); - buttonMenu = new ButtonMenu(frame, gridSizeX, gridSizeY, controller); - saveInput = new JTextField[controller.model.rows][controller.model.columns]; - - //Initialize the application frame - int frameSize = 60; - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - int x = frameSize*gridSizeX; - int y = frameSize*gridSizeY; - frame.setSize(x, y); - frame.setResizable(false); - } - - numberFormatter.setValueClass(Integer.class); - numberFormatter.setMinimum(1); - numberFormatter.setMaximum(9); - // TODO remove see definition comment - saveInput=new JTextField[controller.model.rows][controller.model.columns]; - } - - public void printStartup() - { - System.out.println("welcome to kakuro game!"); - System.out.println("=> use numbers between 1-9 to fill the cells;"); - } - - public void loadInputInModel() - { - for(int row = 0; row < controller.model.columns; row++) - { - for(int column = 0; column < controller.model.rows; column++) - { - BoardCell cell = controller.model.board[row][column]; - String value = this.saveInput[row][column].getText(); - if(!value.isEmpty()) { - switch (cell.getType()) - { - case INPUT: - controller.model.board[row][column].setFirstValue(Integer.parseInt(value)); - break; - default: - break; - } - } - } - } - } - - public int getMinNumberValid() { - return (int) numberFormatter.getMinimum(); - } - - public Object getNumberFormatterClassType() { - return numberFormatter.getClass(); - } - - public int getMaxNumberValid() { - return (int) numberFormatter.getMaximum(); - } - - public void start_ui() { - JLabel gameTitle = new JLabel("KAKURO", SwingConstants.CENTER); - gameTitle.setFont(new Font(gameTitle.getFont().getFontName(), Font.PLAIN, 120)); - - frame.getContentPane().add(gameTitle); - frame.setVisible(true); - } - - //creates user interface of the game board - public void board_ui() { - //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 < controller.model.rows; row++) - { - for(int column = 0; column < controller.model.columns; column++) - { - JFormattedTextField textField = null; - - //tracking the type of each cell - BoardCell cell = controller.model.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 line_panel(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 line_panel(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 line_panel(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.saveInput[row][column] = textField; - } - } - - //If a panel is already attached to the frame, remove it - if(boardPanel != null) - frame.getContentPane().remove(boardPanel); - - //Save a reference to the new panel - frame.getContentPane().add(panel); - boardPanel = panel; - - // currentPanel = panel; - frame.setVisible(true); - } - - public void settingTextField(JTextField txt) { - txt.setBackground(Color.black); - txt.setForeground(Color.white); - txt.setBorder(javax.swing.BorderFactory.createEmptyBorder()); - txt.setEditable(false); - } - - public void printBoard(Boolean showAnswerValues) - { - System.out.println("board:"); - for(int row = 0; row < controller.model.rows; row++) - { - for(int column = 0; column < controller.model.columns; column++) - { - int value = 0; - int value2 = 0; - BoardCell cell = controller.model.board[row][column]; - switch (cell.getType()) - { - case EMPTY: - System.out.print(" x "); - break; - case INPUT: - System.out.print(" " + - (showAnswerValues ? (cell.getSecondValue() != -1 ? cell.getSecondValue() : "_") : - (cell.getFirstValue() != -1 ? cell.getFirstValue() : "_")) + - " "); - break; - case FILLED11: - value = cell.getFirstValue(); - value2 = cell.getSecondValue(); - System.out.print(" " + value + "\\" + value2); - break; - case FILLED10: - value = cell.getFirstValue(); - System.out.print(" " + value + "\\"); - break; - case FILLED01: - value2 = cell.getSecondValue(); - System.out.print(" \\" + value2); - break; - default: - break; - } - if (value > 9 || value2 > 9) - System.out.print(" "); - } - System.out.println(); - } - } - - public UserActions printGetUserAction() - { - System.out.print("\nList of actions i=input s=solve a=answers\nChoose an action: "); - switch (inputReader.next()) - { - case "i": - return UserActions.INPUT; - - case "s": - return UserActions.SOLVE; - case "a": - return UserActions.ANSWERS; - default: - return UserActions.UNKNOWN; - } - } - - public void printGetInputNumber() - { - int row = -1; - int column = -1; - int number = -1; - while (true) - { - while (row < 1 || row > 10) - { - System.out.print("row: "); - try { - row = inputReader.nextInt(); - if (row < 1 || row > 10) - System.out.println("error: out of bounds"); - } - catch (java.util.InputMismatchException e) - { - System.out.println("error: invalid digit"); - inputReader.nextLine(); - } - } - while (column < 1 || column > 10) - { - System.out.print("column: "); - try { - column = inputReader.nextInt(); - if (column < 1 || column > 10) - System.out.println("error: out of bounds"); - } - catch (java.util.InputMismatchException e) - { - System.out.println("error: invalid digit"); - inputReader.nextLine(); - } - } - if (controller.model.board[row-1][column-1].getType() == BoardCell.CellType.INPUT) - break; - else { - System.out.println("error: not an input cell"); - row = -1; - column = -1; - } - } - while (number < 1 || number > 9) - { - try { - System.out.print("number: "); - number = inputReader.nextInt(); - if (number < 1 || number > 9) - System.out.println("error: out of bounds"); - } - catch (java.util.InputMismatchException e) - { - System.out.println("error: invalid digit"); - inputReader.nextLine(); - } - } - controller.model.board[row-1][column-1].setFirstValue(number); - } - - public void printSolveBoard() - { - Boolean success = controller.solveBoard(); - if (success) - System.out.println("The board is solved!"); - else - System.out.println("The solution is incorrect."); - } - - public void settingUpMenu() { - //chronoSetUp MUST be called before buttonsSetUp. - buttonMenu.chronoSetUp(); - buttonMenu.buttonsSetUp(); - } - - -} diff --git a/src/kakuro/Main.java b/src/kakuro/Main.java index 07f1403..3efaf2a 100644 --- a/src/kakuro/Main.java +++ b/src/kakuro/Main.java @@ -4,6 +4,8 @@ package kakuro; +import kakuro.controllers.AppController; + public class Main { public static void main(String[] args) @@ -11,7 +13,7 @@ public static void main(String[] args) int columns = 10; int rows = 10; - GameController game = new GameController(columns, rows, true/*GUI*/); + AppController game = new AppController(columns, rows, true/*GUI*/); game.loopGame(); } } diff --git a/src/kakuro/GameController.java b/src/kakuro/controllers/AppController.java similarity index 75% rename from src/kakuro/GameController.java rename to src/kakuro/controllers/AppController.java index e685120..9edd082 100644 --- a/src/kakuro/GameController.java +++ b/src/kakuro/controllers/AppController.java @@ -2,14 +2,19 @@ // @author Nalveer Moocheet // @brief Game controller class which handles the Kakuro game. -package kakuro; +package kakuro.controllers; +import kakuro.core.BoardCell; +import kakuro.core.DatabaseConnection; import kakuro.game.dao.GameDao; import kakuro.game.dao.GameDaoImpl; import kakuro.gameprogress.dao.GameProgressDao; import kakuro.gameprogress.dao.GameProgressDaoImpl; -import kakuro.utils.DatabaseConnection; +import kakuro.models.GameModel; +import kakuro.views.GameConsole; +import kakuro.views.GameView; +import java.awt.BorderLayout; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; @@ -17,7 +22,7 @@ import javax.swing.JTextField; -public class GameController +public class AppController { public DatabaseConnection database; public GameProgressDao gameProgress; @@ -26,6 +31,15 @@ public class GameController public GameView view; public GameModel model; private Boolean gui = true; + + //Sub-views controller + private ChronoController chronoController; + public BoardController boardController; + public ButtonMenuController buttonMenuController; + + public GameConsole console; + + public boolean isPaused = false; public enum UserActions { @@ -35,7 +49,7 @@ public enum UserActions ANSWERS } - public GameController(final int columns, final int rows, final Boolean gui) + public AppController(final int columns, final int rows, final Boolean gui) { this.model = new GameModel(columns, rows); this.gui = gui; @@ -54,34 +68,35 @@ private void initGame(GameModel model) model.initBoard(); - this.view = new GameView(this, gui); + //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); - view.printStartup(); - view.printBoard(false/*show answer values*/); - if (gui){ - //view.board_ui(); - view.start_ui(); - view.settingUpMenu(); - } + this.view = new GameView(this, gui, chronoController.getView(), boardController.getView(), buttonMenuController.getView());; + this.console = new GameConsole(this); + + console.printStartup(); + console.printBoard(false/*show answer values*/); } public void loopGame() { while (true) { - switch (view.printGetUserAction()) + switch (console.printGetUserAction()) { case INPUT: - view.printGetInputNumber(); - view.printBoard(false/*show answer values*/); + console.printGetInputNumber(); + console.printBoard(false/*show answer values*/); break; case SOLVE: if (gui) - view.loadInputInModel(); - view.printSolveBoard(); + loadInputInModel(false); + console.printSolveBoard(); break; case ANSWERS: - view.printBoard(true/*show answer values*/); + console.printBoard(true/*show answer values*/); break; case UNKNOWN: default: @@ -105,6 +120,8 @@ public void disconnectDatabase() { public void saveGame() { try { + chronoController.chronoPause(); + loadInputInModel(false); //TODO: fixed player and to fix in iteration 3 gameProgress.save(getDatabaseConnection(), "TestPlayer", model.board); @@ -123,11 +140,11 @@ public BoardCell[][] loadGame() { model.board = boardCell; System.out.println("Successfully loaded game progress"); - view.printStartup(); - view.printBoard(false); + console.printStartup(); + console.printBoard(false); if (gui){ - view.board_ui(); + view.updateView(boardController.loadGame()); } return model.board; @@ -145,14 +162,19 @@ public void loadPreconfiguredGame(int gameLevel) { try { ArrayList boardCells = game.loadAllPreconfiguredGames(getDatabaseConnection()); + //Load the new game model model.board = boardCells.get(gameLevel-1); - - view.printStartup(); - view.printBoard(false); + console.printStartup(); + console.printBoard(false); if (gui){ - view.board_ui(); + //Update the new view + view.updateView(boardController.loadGame()); + + //Start the timer + chronoController.show(); + chronoController.chronoStart(); } } catch(SQLException e) { @@ -306,7 +328,7 @@ public Boolean solveBoard() } public void loadInputInModel(boolean clearInput) { - JTextField[][] saveInput = view.saveInput; + JTextField[][] saveInput = boardController.getSavedInput(); String value; for(int row = 0; row < model.columns; row++) @@ -330,4 +352,29 @@ public void loadInputInModel(boolean clearInput) { } } } + + public void pause() { + isPaused = true; + chronoController.chronoPause(); + view.hideBoard(); + } + + public void resume() { + isPaused = false; + chronoController.chronoStart(); + view.showBoard(); + } + + public void restart() { + chronoController.resetTimer(); + chronoController.chronoStart(); + loadInputInModel(true); //Clear inputs + } + + public void submit() { + chronoController.chronoPause(); + loadInputInModel(false); //No clearing inputs + solveBoard(); + console.printSolveBoard(); + } } diff --git a/src/kakuro/controllers/BoardController.java b/src/kakuro/controllers/BoardController.java new file mode 100644 index 0000000..da57a43 --- /dev/null +++ b/src/kakuro/controllers/BoardController.java @@ -0,0 +1,53 @@ +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 { + AppController appController; + + int gridSizeX; + int gridSizeY; + + BoardView boardView; + JPanel currentPanel; + + public BoardController(int gridSizeX, int gridSizeY, AppController appController) { + this.gridSizeX = gridSizeX; + this.gridSizeY = gridSizeY; + this.appController = appController; + + boardView = new BoardView(10, 10, 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.getMaxNumberValid(); + } + + public Object getNumberFormatterClassType() { + return boardView.getNumberFormatterClassType(); + } + + public int getMaxNumberValid() { + return boardView.getMaxNumberValid(); + } +} diff --git a/src/kakuro/controllers/ButtonMenuController.java b/src/kakuro/controllers/ButtonMenuController.java new file mode 100644 index 0000000..a5965e0 --- /dev/null +++ b/src/kakuro/controllers/ButtonMenuController.java @@ -0,0 +1,57 @@ +package kakuro.controllers; + +import kakuro.core.BoardCell; +import kakuro.core.GameDifficulty; +import kakuro.views.ButtonMenuView; + +public class ButtonMenuController { + AppController appController; + + private ButtonMenuView buttonMenuView; + private boolean isPaused; + + public ButtonMenuController(AppController appController){ + this.appController = appController; + buttonMenuView = new ButtonMenuView(this); + } + + public ButtonMenuView getButtonMenuView() { + return buttonMenuView; + } + + public boolean isPaused() { + return appController.isPaused; + } + + public void pause() { + appController.pause(); + } + + public void resume() { + appController.resume(); + } + + public ButtonMenuView getView() { + return buttonMenuView; + } + + public void submit() { + appController.submit(); + } + + public void save() { + appController.saveGame(); + } + + public BoardCell[][] load() { + return appController.loadGame(); + } + + public void loadPreconfiguredGame(GameDifficulty gameLevel) { + appController.loadPreconfiguredGame(GameDifficulty.GameDifficultyToInt(gameLevel)); + } + + public void restart() { + appController.restart(); + } +} diff --git a/src/kakuro/controllers/ChronoController.java b/src/kakuro/controllers/ChronoController.java new file mode 100644 index 0000000..7394806 --- /dev/null +++ b/src/kakuro/controllers/ChronoController.java @@ -0,0 +1,84 @@ +package kakuro.controllers; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JLabel; +import javax.swing.Timer; + +import kakuro.models.ChronoModel; +import kakuro.views.ChronoView; + +public class ChronoController { + private ChronoModel chronoModel; + private ChronoView chronoView; + private ActionListener timerListener; + private Timer time; + private boolean isVisible; + + public ChronoController() { + chronoModel = new ChronoModel(); + chronoView = new ChronoView(); + + isVisible=false; + timerSetUp(); + } + + public void timerSetUp() { + timerListener= new ActionListener() + { + public void actionPerformed(ActionEvent e1) + { + chronoView.setTimerLabel(chronoModel.updateTime()); + } + }; + + chronoModel.getDelay(); + time = new Timer(chronoModel.getDelay(), timerListener); + } + + public void chronoPause() { + time.stop(); + } + + public void chronoStart() { + time.start(); + } + + public void show() { + chronoView.timerLabel.setVisible(true); + } + + public void hide() { + chronoView.timerLabel.setVisible(false); + } + + public JLabel getTimerLabel() { + return chronoView.timerLabel; + } + + public void toggleTimerDisplay() { + chronoView.timerLabel.setVisible(!isVisible); + } + + public void resetTimer() { + chronoModel.resetTimer(); + chronoView.timerLabel.setText("0:0:0:0"); + } + + public int getHours() { + return chronoModel.getHours(); + } + + public int getMinutes() { + return chronoModel.getMinutes(); + } + + public int getSeconds() { + return chronoModel.getSeconds(); + } + + public ChronoView getView() { + return chronoView; + } +} diff --git a/src/kakuro/BoardCell.java b/src/kakuro/core/BoardCell.java similarity index 68% rename from src/kakuro/BoardCell.java rename to src/kakuro/core/BoardCell.java index 9df167d..2382796 100644 --- a/src/kakuro/BoardCell.java +++ b/src/kakuro/core/BoardCell.java @@ -1,11 +1,11 @@ // @author Vsevolod Ivanov // @author ... -package kakuro; +package kakuro.core; public class BoardCell { - enum CellType + public enum CellType { EMPTY, /* | | */ INPUT, /* | | */ @@ -18,40 +18,40 @@ enum CellType private int value1 = -1; private int value2 = -1; - BoardCell(CellType type) + public BoardCell(CellType type) { this.type = type; }; - BoardCell(CellType type, int value1) + public BoardCell(CellType type, int value1) { this.type = type; this.value1 = value1; } - BoardCell(CellType type, int value1, int value2) + public BoardCell(CellType type, int value1, int value2) { this.type = type; this.value1 = value1; this.value2 = value2; } - int getFirstValue() + public int getFirstValue() { return this.value1; } - int getSecondValue() + public int getSecondValue() { return this.value2; } - void setFirstValue(int value) + public void setFirstValue(int value) { this.value1 = value; } - CellType getType() + public CellType getType() { return this.type; } diff --git a/src/kakuro/utils/DatabaseConnection.java b/src/kakuro/core/DatabaseConnection.java similarity index 99% rename from src/kakuro/utils/DatabaseConnection.java rename to src/kakuro/core/DatabaseConnection.java index e0c2051..5c7cd8a 100644 --- a/src/kakuro/utils/DatabaseConnection.java +++ b/src/kakuro/core/DatabaseConnection.java @@ -1,6 +1,6 @@ //@author Brian Gamboc-Javiniar and Nolan Mckay -package kakuro.utils; +package kakuro.core; import java.io.File; import java.sql.Connection; diff --git a/src/kakuro/core/GameDifficulty.java b/src/kakuro/core/GameDifficulty.java new file mode 100644 index 0000000..96b37fa --- /dev/null +++ b/src/kakuro/core/GameDifficulty.java @@ -0,0 +1,26 @@ +/* @author Hoang Thuan Pham + * Date created: March 9th, 2020 + * Enumerator: GameDifficulty + * Description: An enumerator for different levels of game difficulties +*/ + + +package kakuro.core; + +public enum GameDifficulty { + EASY, //Easy game difficulty + MEDIUM, //Medium game difficulty + DIFFICULT; //Difficult game difficulty + + /*Convert the enumerator to an integer. The method is to support database storage where enumerators are not supported. + * @param: an enumerator type + * @return: the corresponding integer to an enumerator value + * */ + public static int GameDifficultyToInt(GameDifficulty difficulty) { + switch(difficulty) { + case EASY: return 1; + case MEDIUM: return 2; + default: return 3; + } + } +} diff --git a/src/kakuro/GameDifficultyListItem.java b/src/kakuro/core/GameDifficultyListItem.java similarity index 95% rename from src/kakuro/GameDifficultyListItem.java rename to src/kakuro/core/GameDifficultyListItem.java index 030549f..5c16272 100644 --- a/src/kakuro/GameDifficultyListItem.java +++ b/src/kakuro/core/GameDifficultyListItem.java @@ -1,4 +1,4 @@ -package kakuro; +package kakuro.core; public class GameDifficultyListItem { private String description; diff --git a/src/kakuro/line_panel.java b/src/kakuro/core/LinePanel.java similarity index 90% rename from src/kakuro/line_panel.java rename to src/kakuro/core/LinePanel.java index 5ab2cd5..030f515 100644 --- a/src/kakuro/line_panel.java +++ b/src/kakuro/core/LinePanel.java @@ -3,7 +3,7 @@ // @brief class used to populate non-playable board cells: adds numbers and diagonal line // class extending JPanel to use the graphics with paintComponent method overriding -package kakuro; +package kakuro.core; import java.awt.BasicStroke; import java.awt.BorderLayout; @@ -23,10 +23,10 @@ * https://docs.oracle.com/javase/8/docs/api/javax/swing/JFormattedTextField.html */ -public class line_panel extends JPanel { +public class LinePanel extends JPanel { //constructor used if only one number in the cell - public line_panel(LayoutManager layout, JTextField textField, Boolean align) { + public LinePanel(LayoutManager layout, JTextField textField, Boolean align) { super(layout); this.setBackground(Color.black); settingTxt(textField); @@ -41,7 +41,7 @@ public line_panel(LayoutManager layout, JTextField textField, Boolean align) { } //constructor used if two numbers in the cell - public line_panel(LayoutManager layout, JTextField textFieldLEFT, JTextField textFieldRIGHT) { + public LinePanel(LayoutManager layout, JTextField textFieldLEFT, JTextField textFieldRIGHT) { super(layout); this.setBackground(Color.black); diff --git a/src/kakuro/Tools.java b/src/kakuro/core/Tools.java similarity index 98% rename from src/kakuro/Tools.java rename to src/kakuro/core/Tools.java index 31d593b..be1a618 100644 --- a/src/kakuro/Tools.java +++ b/src/kakuro/core/Tools.java @@ -1,7 +1,7 @@ //@author Vsevolod Ivanov //@author ... -package kakuro; +package kakuro.core; import java.util.Enumeration; import java.util.Random; diff --git a/src/kakuro/UniquePartitions.java b/src/kakuro/core/UniquePartitions.java similarity index 98% rename from src/kakuro/UniquePartitions.java rename to src/kakuro/core/UniquePartitions.java index 9d66c26..078c906 100644 --- a/src/kakuro/UniquePartitions.java +++ b/src/kakuro/core/UniquePartitions.java @@ -5,15 +5,15 @@ // Level 2 : possible sums for that number of cells // Level 3 : an array of its values representing consecutively cells values from smallest to largest -package kakuro; +package kakuro.core; import javax.swing.tree.DefaultMutableTreeNode; public class UniquePartitions { - DefaultMutableTreeNode root; + public DefaultMutableTreeNode root; - UniquePartitions() + public UniquePartitions() { this.root = new DefaultMutableTreeNode(0); fillCombinations(this.root); diff --git a/src/kakuro/game/dao/GameDao.java b/src/kakuro/game/dao/GameDao.java index 3acb354..bdeb2b9 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.BoardCell; +import kakuro.core.BoardCell; public interface GameDao { diff --git a/src/kakuro/game/dao/GameDaoImpl.java b/src/kakuro/game/dao/GameDaoImpl.java index 7893939..5056348 100644 --- a/src/kakuro/game/dao/GameDaoImpl.java +++ b/src/kakuro/game/dao/GameDaoImpl.java @@ -10,7 +10,7 @@ import com.google.gson.Gson; -import kakuro.BoardCell; +import kakuro.core.BoardCell; public class GameDaoImpl implements GameDao { private final String LOAD_ALL_PRECONFIGURED_GAMES = "SELECT cells FROM game"; diff --git a/src/kakuro/gameprogress/dao/GameProgressDao.java b/src/kakuro/gameprogress/dao/GameProgressDao.java index 2750c12..5949829 100644 --- a/src/kakuro/gameprogress/dao/GameProgressDao.java +++ b/src/kakuro/gameprogress/dao/GameProgressDao.java @@ -4,7 +4,8 @@ import java.sql.Connection; import java.sql.SQLException; -import kakuro.BoardCell; + +import kakuro.core.BoardCell; public interface GameProgressDao { /** diff --git a/src/kakuro/gameprogress/dao/GameProgressDaoImpl.java b/src/kakuro/gameprogress/dao/GameProgressDaoImpl.java index 3bdb93b..afa6133 100644 --- a/src/kakuro/gameprogress/dao/GameProgressDaoImpl.java +++ b/src/kakuro/gameprogress/dao/GameProgressDaoImpl.java @@ -8,7 +8,7 @@ import java.sql.SQLException; import com.google.gson.*; -import kakuro.BoardCell; +import kakuro.core.BoardCell; public class GameProgressDaoImpl implements GameProgressDao { private final String SAVE_GAME_PROGRESS = "UPDATE game_progress SET cells=? WHERE username=?"; diff --git a/src/kakuro/Chrono.java b/src/kakuro/models/ChronoModel.java similarity index 50% rename from src/kakuro/Chrono.java rename to src/kakuro/models/ChronoModel.java index eb3196d..113d456 100644 --- a/src/kakuro/Chrono.java +++ b/src/kakuro/models/ChronoModel.java @@ -1,4 +1,4 @@ -package kakuro; +package kakuro.models; //@author Audrey St-Louis //@brief Timer class @@ -9,41 +9,29 @@ import java.awt.Font; import java.awt.event.*; -public class Chrono +public class ChronoModel { - private int hours=0; - private int minutes=0; - private int seconds=0; - private int delay=1000; - private boolean isVisible=false; - JLabel timerLabel; - ActionListener timerListener; - Timer time; + private int hours; + private int minutes; + private int seconds; + private int delay; + /* Constructor : Creates all the UI components and add them on the panel. */ - public Chrono() + public ChronoModel() { - timerLabel = new JLabel("0:"+hours+":"+minutes+":"+seconds, SwingConstants.CENTER); - timerLabel.setFont(new Font(timerLabel.getFont().getFontName(), Font.PLAIN, 20)); - timerLabel.setVisible(false); - timerLabel.setBorder(new EmptyBorder(10,0,10,0));//top,left,bottom,right - } - - public JLabel getTimerLabel() { - - return timerLabel; + hours=0; + minutes=0; + seconds=0; + delay=1000; } - public void validation() { - - } - + /* Checks if the user have clicked on a button. If he clicks on pause, the timer will stop. If he clicks on start, the timer will start. If * he clicks on new game, the timer will restart and a new grid will appear on the screen. * The timer is set to start automatically when the application is launched since a game will already be ready to play. * Need to implement : Save feature */ - - public void checkTime() { + public String updateTime() { seconds++; if(seconds==60) { @@ -55,42 +43,15 @@ public void checkTime() { minutes=0; hours++; } - timerLabel.setText("0:"+hours+":"+minutes+":"+seconds); + + return "0:"+hours+":"+minutes+":"+seconds; } - + //Getters and setters public int getDelay() { return this.delay; } - - public void timerSetUp() { - timerListener= new ActionListener() - { - public void actionPerformed(ActionEvent e1) - { - checkTime(); - } - }; - - time = new Timer(delay,timerListener); - } - - public void chronoPause() { - time.stop(); - } - - public void chronoStart() { - time.start(); - } - - public void resetTimer() { - hours=0; - minutes=0; - seconds=0; - timerLabel.setText("0:"+hours+":"+minutes+":"+seconds); - } - public int getHours() { return hours; } @@ -114,9 +75,11 @@ public int getSeconds() { public void setSeconds(int seconds) { this.seconds = seconds; } - - public void toggleTimerDisplay() { - timerLabel.setVisible(!isVisible); - } + + public void resetTimer() { + hours=0; + minutes=0; + seconds=0; + } } diff --git a/src/kakuro/models/GameModel.java b/src/kakuro/models/GameModel.java new file mode 100644 index 0000000..8640558 --- /dev/null +++ b/src/kakuro/models/GameModel.java @@ -0,0 +1,41 @@ +// @author Vsevolod Ivanov +// @author ... +// @brief Game model class which handles the Kakuro game storage. + +package kakuro.models; + +import java.util.Enumeration; + +import javax.swing.JTextField; +import javax.swing.tree.TreeNode; + +import kakuro.core.*; + +public class GameModel +{ + + public final int columns; + public final int rows; + public BoardCell[][] board; + private static UniquePartitions partitions; + + public GameModel(final int columns, final int rows) + { + this.columns = columns; + this.rows = rows; + partitions = new UniquePartitions(); + } + + public void initBoard() + { + board = new BoardCell[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); + } + } + } +} diff --git a/src/kakuro/PlayerModel.java b/src/kakuro/models/PlayerModel.java similarity index 96% rename from src/kakuro/PlayerModel.java rename to src/kakuro/models/PlayerModel.java index 2c77c87..3c8b0b0 100644 --- a/src/kakuro/PlayerModel.java +++ b/src/kakuro/models/PlayerModel.java @@ -1,4 +1,4 @@ -package kakuro; +package kakuro.models; //TODO: iteration 3 work public class PlayerModel { diff --git a/src/kakuro/views/BoardView.java b/src/kakuro/views/BoardView.java new file mode 100644 index 0000000..f0b499c --- /dev/null +++ b/src/kakuro/views/BoardView.java @@ -0,0 +1,149 @@ +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 rows, int columns, 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/ButtonMenu.java b/src/kakuro/views/ButtonMenuView.java similarity index 60% rename from src/kakuro/ButtonMenu.java rename to src/kakuro/views/ButtonMenuView.java index 221137c..27659b4 100644 --- a/src/kakuro/ButtonMenu.java +++ b/src/kakuro/views/ButtonMenuView.java @@ -1,4 +1,4 @@ -package kakuro; +package kakuro.views; import java.awt.BorderLayout; import java.awt.event.ActionEvent; @@ -9,61 +9,47 @@ import javax.swing.JOptionPane; import javax.swing.JPanel; -public class ButtonMenu { +import kakuro.controllers.ChronoController; +import kakuro.controllers.AppController; +import kakuro.controllers.ButtonMenuController; +import kakuro.core.GameDifficulty; +import kakuro.core.GameDifficultyListItem; + +public class ButtonMenuView { + //Controller + ButtonMenuController buttonMenuController; + + //UI components JButton pause_button; - JButton play_button; JButton submit_button; - JButton newGame_button; JButton choose_game_button; JButton save_button; JButton restart_button; JButton load_button; JPanel mainPanel; - Chrono chrono; - public Chrono getChrono() { - return chrono; - } - - public void setChrono(Chrono chrono) { - this.chrono = chrono; - } - - GameController gameController; - - public ButtonMenu(JFrame appFrame, int x, int y, GameController gameController) { - this.gameController = gameController; + public ButtonMenuView(ButtonMenuController buttonMenuController) { + this.buttonMenuController = buttonMenuController; + + //Initialize the buttons pause_button = new JButton("Pause"); - play_button = new JButton("Play"); submit_button = new JButton("Submit"); - newGame_button = new JButton("New Game"); choose_game_button = new JButton("New Game"); save_button = new JButton("Save"); restart_button = new JButton("Restart"); load_button = new JButton("Load Game"); mainPanel = new JPanel(); - chrono = new Chrono(); - - // Set up + + //Set up the event listeners + buttonsSetUp(); + + //Set up visibility and add the buttons to a panel mainPanel.add(pause_button).setVisible(false); mainPanel.add(restart_button).setVisible(false);; mainPanel.add(submit_button).setVisible(false); mainPanel.add(choose_game_button); mainPanel.add(save_button).setVisible(false); mainPanel.add(load_button); - - if (appFrame != null) - { - appFrame.getContentPane().add(chrono.getTimerLabel(), BorderLayout.BEFORE_FIRST_LINE); - appFrame.getContentPane().add(mainPanel, BorderLayout.AFTER_LAST_LINE); - appFrame.pack(); - appFrame.setSize(x,y); - appFrame.setVisible(true); - } - } - - public void chronoSetUp() { - chrono.timerSetUp(); } private void toggleMenu() { @@ -83,26 +69,14 @@ public void buttonsSetUp() { { public void actionPerformed(ActionEvent e) { - chrono.chronoPause(); - } - }); - - /* With the use of an Action Listener to know if the user has clicked on the button, this part of the method will start the timer. */ - play_button.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent e) - { - chrono.chronoStart(); - } - }); - - /* With the use of an Action Listener to know if the user has clicked on the button, this part of the loop method will restart the timer. */ - newGame_button.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent e) - { - chrono.resetTimer(); - chrono.chronoStart(); + if(!buttonMenuController.isPaused()) { + pause_button.setText("Resume"); + buttonMenuController.pause(); + } + else { + pause_button.setText("Pause"); + buttonMenuController.resume(); + } } }); @@ -111,11 +85,7 @@ public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { - chrono.chronoPause(); - //loads data into model - gameController.loadInputInModel(false); //No clearing inputs - gameController.solveBoard(); - gameController.view.printSolveBoard(); + buttonMenuController.submit(); } }); @@ -126,10 +96,7 @@ public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { - chrono.chronoPause(); - gameController.view.loadInputInModel(); - - gameController.saveGame(); + buttonMenuController.save(); } }); @@ -141,11 +108,11 @@ public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { - if(gameController.loadGame() != null) { + if(buttonMenuController.load() != null) { JOptionPane.showMessageDialog(null, "Successfully loaded saved game!"); toggleMenu(); } else { - JOptionPane.showMessageDialog(null, "You do not have any saved game!", "Not Found", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(null, "You do not have any saved game!", "Not Found", JOptionPane.ERROR_MESSAGE); } } }); @@ -167,12 +134,8 @@ 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) { - int gameLevel = GameDifficulty.GameDifficultyToInt(difficultyItem.getDifficulty()); + buttonMenuController.loadPreconfiguredGame(difficultyItem.getDifficulty()); toggleMenu(); - - gameController.loadPreconfiguredGame(gameLevel); - chrono.chronoStart(); - chrono.toggleTimerDisplay(); } } }); @@ -181,9 +144,7 @@ public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { - chrono.resetTimer(); - chrono.chronoStart(); - gameController.loadInputInModel(true); //Clear inputs + buttonMenuController.restart(); } }); } diff --git a/src/kakuro/views/ChronoView.java b/src/kakuro/views/ChronoView.java new file mode 100644 index 0000000..6c212a7 --- /dev/null +++ b/src/kakuro/views/ChronoView.java @@ -0,0 +1,28 @@ +package kakuro.views; + +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JLabel; +import javax.swing.SwingConstants; +import javax.swing.Timer; +import javax.swing.border.EmptyBorder; + +import kakuro.models.ChronoModel; + +public class ChronoView { + + public JLabel timerLabel; + + public ChronoView() { + timerLabel = new JLabel("0:0:0:0", SwingConstants.CENTER); + timerLabel.setFont(new Font(timerLabel.getFont().getFontName(), Font.PLAIN, 20)); + timerLabel.setVisible(false); + timerLabel.setBorder(new EmptyBorder(10,0,10,0));//top,left,bottom,right + } + + public void setTimerLabel(String label) { + timerLabel.setText(label); + } +} diff --git a/src/kakuro/views/GameConsole.java b/src/kakuro/views/GameConsole.java new file mode 100644 index 0000000..1c4b08e --- /dev/null +++ b/src/kakuro/views/GameConsole.java @@ -0,0 +1,151 @@ +package kakuro.views; + +import java.util.Scanner; + +import kakuro.controllers.AppController; +import kakuro.controllers.AppController.UserActions; +import kakuro.core.*; + +public class GameConsole { + private AppController controller; + private Scanner inputReader = new Scanner(System.in); + + public GameConsole(final AppController controller) { + this.controller = controller; + } + + public void printStartup() + { + System.out.println("welcome to kakuro game!"); + System.out.println("=> use numbers between 1-9 to fill the cells;"); + } + + public void printBoard(Boolean showAnswerValues) + { + System.out.println("board:"); + for(int row = 0; row < controller.model.rows; row++) + { + for(int column = 0; column < controller.model.columns; column++) + { + int value = 0; + int value2 = 0; + BoardCell cell = controller.model.board[row][column]; + switch (cell.getType()) + { + case EMPTY: + System.out.print(" x "); + break; + case INPUT: + System.out.print(" " + + (showAnswerValues ? (cell.getSecondValue() != -1 ? cell.getSecondValue() : "_") : + (cell.getFirstValue() != -1 ? cell.getFirstValue() : "_")) + + " "); + break; + case FILLED11: + value = cell.getFirstValue(); + value2 = cell.getSecondValue(); + System.out.print(" " + value + "\\" + value2); + break; + case FILLED10: + value = cell.getFirstValue(); + System.out.print(" " + value + "\\"); + break; + case FILLED01: + value2 = cell.getSecondValue(); + System.out.print(" \\" + value2); + break; + default: + break; + } + if (value > 9 || value2 > 9) + System.out.print(" "); + } + System.out.println(); + } + } + + public UserActions printGetUserAction() + { + System.out.print("\nList of actions i=input s=solve a=answers\nChoose an action: "); + switch (inputReader.next()) + { + case "i": + return UserActions.INPUT; + + case "s": + return UserActions.SOLVE; + case "a": + return UserActions.ANSWERS; + default: + return UserActions.UNKNOWN; + } + } + + public void printGetInputNumber(){ + int row = -1; + int column = -1; + int number = -1; + while (true) + { + while (row < 1 || row > 10) + { + System.out.print("row: "); + try { + row = inputReader.nextInt(); + if (row < 1 || row > 10) + System.out.println("error: out of bounds"); + } + catch (java.util.InputMismatchException e) + { + System.out.println("error: invalid digit"); + inputReader.nextLine(); + } + } + while (column < 1 || column > 10) + { + System.out.print("column: "); + try { + column = inputReader.nextInt(); + if (column < 1 || column > 10) + System.out.println("error: out of bounds"); + } + catch (java.util.InputMismatchException e) + { + System.out.println("error: invalid digit"); + inputReader.nextLine(); + } + } + if (controller.model.board[row-1][column-1].getType() == BoardCell.CellType.INPUT) + break; + else { + System.out.println("error: not an input cell"); + row = -1; + column = -1; + } + } + while (number < 1 || number > 9) + { + try { + System.out.print("number: "); + number = inputReader.nextInt(); + if (number < 1 || number > 9) + System.out.println("error: out of bounds"); + } + catch (java.util.InputMismatchException e) + { + System.out.println("error: invalid digit"); + inputReader.nextLine(); + } + } + controller.model.board[row-1][column-1].setFirstValue(number); + } + + public void printSolveBoard() + { + Boolean success = controller.solveBoard(); + if (success) + System.out.println("The board is solved!"); + else + System.out.println("The solution is incorrect."); + } +} diff --git a/src/kakuro/views/GameView.java b/src/kakuro/views/GameView.java new file mode 100644 index 0000000..780e2a3 --- /dev/null +++ b/src/kakuro/views/GameView.java @@ -0,0 +1,118 @@ +// @author Vsevolod Ivanov +// @author Isabelle Charette +// @brief Game view class which handles the Kakuro game interface. + +package kakuro.views; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Font; +import java.awt.GridLayout; +import java.text.NumberFormat; +import java.util.Scanner; +import javax.swing.JFormattedTextField; +import javax.swing.JFrame; +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.controllers.AppController; +import kakuro.controllers.AppController.UserActions; +import kakuro.controllers.BoardController; +import kakuro.controllers.ButtonMenuController; +import kakuro.controllers.ChronoController; +import kakuro.core.BoardCell; +import kakuro.core.LinePanel; + +public class GameView +{ + private AppController controller; + + //Subviews + ChronoView chronoView; + BoardView boardView; + ButtonMenuView buttonMenuView; + + //UI components + JPanel currentBoard; + + //Main application frame and properties + JFrame frame; + int gridSizeX; + int gridSizeY; + + //UI support + + private ButtonMenuView buttonMenu; + + private JLabel startUI; + + public GameView(final AppController controller, Boolean X11, ChronoView chronoView, BoardView boardView, ButtonMenuView buttonMenuView) + { + if (controller != null) + { + this.controller = controller; + gridSizeX = controller.model.rows; + gridSizeY = controller.model.columns; + } + + if (X11) { + frame = new JFrame("KAKURO"); + //buttonMenu = new ButtonMenuView(frame, gridSizeX, gridSizeY, controller); + + //Initialize the application frame + int frameSize = 60; + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + int x = frameSize*gridSizeX; + int y = frameSize*gridSizeY; + 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); + //Main game UI stays at the center + currentBoard = boardView.boardPanel; + frame.getContentPane().add(currentBoard); + //Button menu stays at the bottom + frame.getContentPane().add(buttonMenuView.mainPanel, BorderLayout.AFTER_LAST_LINE); + + frame.pack(); + frame.setSize(x,y); + frame.setVisible(true); + } + } + + public void updateView(JPanel newBoardPanel) { + //If a panel is already attached to the frame, remove it + if(currentBoard != null) + frame.getContentPane().remove(currentBoard); + + //Save a reference to the new panel + frame.getContentPane().add(newBoardPanel); + currentBoard = newBoardPanel;; + } + + public void showBoard() { + currentBoard.setVisible(true); + } + + public void hideBoard() { + currentBoard.setVisible(false); + } + + //Methods for the menuButtons + public ButtonMenuView getButtonMenu() { + return buttonMenu; + } + + public void setButtonMenu(ButtonMenuView buttonMenu) { + this.buttonMenu = buttonMenu; + } +} diff --git a/tests/kakuro/TestBoard.java b/tests/kakuro/TestBoard.java index 79984d8..a0ad96a 100644 --- a/tests/kakuro/TestBoard.java +++ b/tests/kakuro/TestBoard.java @@ -9,6 +9,10 @@ import org.junit.Before; import org.junit.Test; +import kakuro.controllers.AppController; +import kakuro.core.BoardCell; +import kakuro.models.GameModel; + public class TestBoard{ private Boolean GUI = false; //disable GUI private Boolean solved = false; @@ -28,7 +32,7 @@ public void testSize(){ @Test public void testValidBoard(){ // Arrange - GameController controller = new GameController(10,10, GUI); + AppController controller = new AppController(10,10, GUI); GameModel model = new GameModel(10,10); model.initBoard(); //initialize board model.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); @@ -46,7 +50,7 @@ public void testValidBoard(){ @Test public void testNotValidBoardOneSumIsWrong(){ // Arrange - GameController controller = new GameController(10,10, GUI); + AppController controller = new AppController(10,10, GUI); GameModel model = new GameModel(10,10); model.initBoard(); model.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); @@ -64,7 +68,7 @@ public void testNotValidBoardOneSumIsWrong(){ @Test public void testNotValidBoardCorrectSumDuplicateEntries(){ // Arrange - GameController controller = new GameController(10,10, GUI); + AppController controller = new AppController(10,10, GUI); GameModel model = new GameModel(10,10); model.initBoard(); model.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); diff --git a/tests/kakuro/TestBoardCell.java b/tests/kakuro/TestBoardCell.java index 6cee191..c40efa3 100644 --- a/tests/kakuro/TestBoardCell.java +++ b/tests/kakuro/TestBoardCell.java @@ -9,7 +9,10 @@ import org.junit.BeforeClass; import org.junit.Test; -public class TestBoardCell{ +import kakuro.core.BoardCell; + +public class TestBoardCell +{ @Test public void testNonInputCell(){ // Arrange diff --git a/tests/kakuro/TestBoardUI.java b/tests/kakuro/TestBoardUI.java index a4426c6..065f9b3 100644 --- a/tests/kakuro/TestBoardUI.java +++ b/tests/kakuro/TestBoardUI.java @@ -10,6 +10,8 @@ import org.junit.BeforeClass; import org.junit.Test; +import kakuro.controllers.AppController; + public class TestBoardUI { @Test public void testBoardUIGeneration(){ @@ -17,7 +19,7 @@ public void testBoardUIGeneration(){ int rows = 3; int columns = 5; //Act - GameController gameController = new GameController(columns,rows, false); + AppController gameController = new AppController(columns,rows, false); //Assert assertEquals(columns, gameController.model.columns); assertEquals(rows, gameController.model.rows); diff --git a/tests/kakuro/TestChrono.java b/tests/kakuro/TestChrono.java index 746fa47..7b5bc75 100644 --- a/tests/kakuro/TestChrono.java +++ b/tests/kakuro/TestChrono.java @@ -10,6 +10,13 @@ import org.junit.BeforeClass; import org.junit.Test; +import kakuro.controllers.AppController; +import kakuro.controllers.BoardController; +import kakuro.controllers.ButtonMenuController; +import kakuro.controllers.ChronoController; +import kakuro.views.ButtonMenuView; +import kakuro.views.GameView; + public class TestChrono{ private int faultToleranceMs = 200; @@ -18,17 +25,16 @@ public class TestChrono{ public void testRunStopChronoAfter3Seconds() throws InterruptedException{ // Arrange int waitSeconds = 3; - GameController gameController = new GameController(10, 10, false); - GameView gameView = new GameView(gameController, false/*GUI*/); - gameView.setButtonMenu(new ButtonMenu(null, 0, 0, null)); + ChronoController chronoController = new ChronoController(); + // Act - gameView.settingUpMenu(); // chrono starts + //gameView.settingUpMenu(); // chrono starts Thread.sleep(waitSeconds * 1000/*ms*/ + faultToleranceMs); - gameView.getButtonMenu().getChrono().chronoPause(); + chronoController.chronoPause(); - int hours = gameView.getButtonMenu().getChrono().getHours(); - int minutes = gameView.getButtonMenu().getChrono().getMinutes(); - int seconds = gameView.getButtonMenu().getChrono().getSeconds(); + int hours = chronoController.getHours(); + int minutes = chronoController.getMinutes(); + int seconds = chronoController.getSeconds(); // Assert System.out.println("Chrono was run for " + seconds + " seconds"); assertTrue(waitSeconds == seconds); diff --git a/tests/kakuro/TestDBConnection.java b/tests/kakuro/TestDBConnection.java index a508721..7e22eba 100644 --- a/tests/kakuro/TestDBConnection.java +++ b/tests/kakuro/TestDBConnection.java @@ -10,7 +10,7 @@ import org.junit.Test; -import kakuro.utils.DatabaseConnection; +import kakuro.core.DatabaseConnection; public class TestDBConnection { @Test diff --git a/tests/kakuro/TestGameController.java b/tests/kakuro/TestGameController.java index d06d975..425e3d5 100644 --- a/tests/kakuro/TestGameController.java +++ b/tests/kakuro/TestGameController.java @@ -1,180 +1,180 @@ -// @author Brian Gamboc-Javiniar -// @author Nalveer Moocheet -// @author Vsevolod Ivanov -// @author Chang Liu -// @brief Test for the GameController class +// // @author Brian Gamboc-Javiniar +// // @author Nalveer Moocheet +// // @author Vsevolod Ivanov +// // @author Chang Liu +// // @brief Test for the GameController class -package kakuro; +// package kakuro; -import static org.junit.Assert.*; +// import static org.junit.Assert.*; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.ArrayList; +// import java.sql.Connection; +// import java.sql.SQLException; +// import java.util.ArrayList; -import org.junit.Test; +// import org.junit.Test; -import kakuro.game.dao.GameDao; -import kakuro.game.dao.GameDaoImpl; -import kakuro.gameprogress.dao.GameProgressDaoImpl; -import kakuro.utils.DatabaseConnection; +// import kakuro.game.dao.GameDao; +// import kakuro.game.dao.GameDaoImpl; +// import kakuro.gameprogress.dao.GameProgressDaoImpl; +// import kakuro.utils.DatabaseConnection; -public class TestGameController { - Boolean GUI = false; - Boolean solved = false; +// public class TestGameController { +// Boolean GUI = false; +// Boolean solved = false; - @Test - public void testGameController() { - //Arrange - int rows = 3; - int columns = 5; - //Act - GameController gameController = new GameController(columns,rows, false); - //Set GUI to false - //Assert - assertEquals(columns, gameController.model.columns); - assertEquals(rows, gameController.model.rows); - } +// @Test +// public void testGameController() { +// //Arrange +// int rows = 3; +// int columns = 5; +// //Act +// GameController gameController = new GameController(columns,rows, false); +// //Set GUI to false +// //Assert +// assertEquals(columns, gameController.model.columns); +// assertEquals(rows, gameController.model.rows); +// } - @Test - public void testGetDatabaseConnection() { - DatabaseConnection db = new DatabaseConnection(); - db.getConnection(); - } +// @Test +// public void testGetDatabaseConnection() { +// DatabaseConnection db = new DatabaseConnection(); +// db.getConnection(); +// } - @Test - public void testConnectDatabase() { - //Arrange - DatabaseConnection db = new DatabaseConnection(); - db.connect(); - //Act - Connection conn = db.getConnection(); - //Assert - assertEquals(conn!=null, true); - db.disconnect(); - } +// @Test +// public void testConnectDatabase() { +// //Arrange +// DatabaseConnection db = new DatabaseConnection(); +// db.connect(); +// //Act +// Connection conn = db.getConnection(); +// //Assert +// assertEquals(conn!=null, true); +// db.disconnect(); +// } - @Test - public void testDisconnectDatabase() { - //Arrange - DatabaseConnection db = new DatabaseConnection(); - db.connect(); - db.disconnect(); - //Act - Connection conn = db.getConnection(); - //Assert - try { - assertEquals(conn.isClosed(), true); - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } +// @Test +// public void testDisconnectDatabase() { +// //Arrange +// DatabaseConnection db = new DatabaseConnection(); +// db.connect(); +// db.disconnect(); +// //Act +// Connection conn = db.getConnection(); +// //Assert +// try { +// assertEquals(conn.isClosed(), true); +// } catch (SQLException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// } - @Test -// @brief Test Valid Board - public void testBoardSolveValidBoard() { - // Arrange - int columns = 10; - int rows = 10; - // Act - GameModel model = new GameModel(columns, rows); - // Assert - assertEquals(model.columns, columns); - assertEquals(model.rows, rows); - // Arrange - GameController controller1 = new GameController(10,10, GUI); - GameModel model1 = new GameModel(10,10); - model1.initBoard(); //initialize board - model1.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model1.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); - model1.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model1.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - model1.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); - controller1.model = model1; - // Act - solved = controller1.solveBoard(); - // Assert - assertEquals(solved, true); - } +// @Test +// // @brief Test Valid Board +// public void testBoardSolveValidBoard() { +// // Arrange +// int columns = 10; +// int rows = 10; +// // Act +// GameModel model = new GameModel(columns, rows); +// // Assert +// assertEquals(model.columns, columns); +// assertEquals(model.rows, rows); +// // Arrange +// GameController controller1 = new GameController(10,10, GUI); +// GameModel model1 = new GameModel(10,10); +// model1.initBoard(); //initialize board +// model1.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); +// model1.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); +// model1.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); +// model1.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); +// model1.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); +// controller1.model = model1; +// // Act +// solved = controller1.solveBoard(); +// // Assert +// assertEquals(solved, true); +// } - @Test -// @brief Test Invalid Board with one wrong vertical sum - public void testBoardSolveInvalidBoardWithOneWrongVerticalSum() { - // Arrange - int columns = 10; - int rows = 10; - // Act - GameModel model = new GameModel(columns, rows); - // Assert - assertEquals(model.columns, columns); - assertEquals(model.rows, rows); - GameController controller2 = new GameController(10,10, GUI); - GameModel model2 = new GameModel(10,10); - model2.initBoard(); - model2.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model2.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); - model2.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model2.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,6,-1); - model2.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); - controller2.model = model2; - // Act - solved = controller2.solveBoard(); - // Assert - assertEquals(solved, false); - } +// @Test +// // @brief Test Invalid Board with one wrong vertical sum +// public void testBoardSolveInvalidBoardWithOneWrongVerticalSum() { +// // Arrange +// int columns = 10; +// int rows = 10; +// // Act +// GameModel model = new GameModel(columns, rows); +// // Assert +// assertEquals(model.columns, columns); +// assertEquals(model.rows, rows); +// GameController controller2 = new GameController(10,10, GUI); +// GameModel model2 = new GameModel(10,10); +// model2.initBoard(); +// model2.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); +// model2.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); +// model2.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); +// model2.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,6,-1); +// model2.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); +// controller2.model = model2; +// // Act +// solved = controller2.solveBoard(); +// // Assert +// assertEquals(solved, false); +// } - @Test -// @brief Test Invalid Board with one wrong horizontal sum - public void testBoardSolveInvalidBoardWithOneWrongHorizontalSum() { - // Arrange - int columns = 10; - int rows = 10; - // Act - GameModel model = new GameModel(columns, rows); - // Assert - assertEquals(model.columns, columns); - assertEquals(model.rows, rows); - GameController controller2 = new GameController(10,10, GUI); - GameModel model2 = new GameModel(10,10); - model2.initBoard(); - model2.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model2.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,8); - model2.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model2.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - model2.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); - controller2.model = model2; - // Act - solved = controller2.solveBoard(); - // Assert - assertEquals(solved, false); - } +// @Test +// // @brief Test Invalid Board with one wrong horizontal sum +// public void testBoardSolveInvalidBoardWithOneWrongHorizontalSum() { +// // Arrange +// int columns = 10; +// int rows = 10; +// // Act +// GameModel model = new GameModel(columns, rows); +// // Assert +// assertEquals(model.columns, columns); +// assertEquals(model.rows, rows); +// GameController controller2 = new GameController(10,10, GUI); +// GameModel model2 = new GameModel(10,10); +// model2.initBoard(); +// model2.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); +// model2.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,8); +// model2.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); +// model2.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); +// model2.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); +// controller2.model = model2; +// // Act +// solved = controller2.solveBoard(); +// // Assert +// assertEquals(solved, false); +// } - @Test -// @brief Test Invalid Board with correct sum but duplicate entries - public void testBoardSolveInvalidBoardWithDuplicateEntries() { - // Arrange - int columns = 10; - int rows = 10; - // Act - GameModel model = new GameModel(columns, rows); - // Assert - assertEquals(model.columns, columns); - assertEquals(model.rows, rows); - // Arrange - GameController controller3 = new GameController(10,10, GUI); - GameModel model3 = new GameModel(10,10); - model3.initBoard(); - model3.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model3.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); - model3.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - model3.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model3.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - controller3.model = model3; - // Act - solved = controller3.solveBoard(); - // Assert - assertEquals(solved, false); - } -} +// @Test +// // @brief Test Invalid Board with correct sum but duplicate entries +// public void testBoardSolveInvalidBoardWithDuplicateEntries() { +// // Arrange +// int columns = 10; +// int rows = 10; +// // Act +// GameModel model = new GameModel(columns, rows); +// // Assert +// assertEquals(model.columns, columns); +// assertEquals(model.rows, rows); +// // Arrange +// GameController controller3 = new GameController(10,10, GUI); +// GameModel model3 = new GameModel(10,10); +// model3.initBoard(); +// model3.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); +// model3.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); +// model3.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); +// model3.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); +// model3.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,2,-1); +// controller3.model = model3; +// // Act +// solved = controller3.solveBoard(); +// // Assert +// assertEquals(solved, false); +// } +// } diff --git a/tests/kakuro/TestGameViewInput.java b/tests/kakuro/TestGameViewInput.java index 7f894a9..c0de346 100644 --- a/tests/kakuro/TestGameViewInput.java +++ b/tests/kakuro/TestGameViewInput.java @@ -12,19 +12,26 @@ import org.junit.BeforeClass; import org.junit.Test; -public class TestGameViewInput{ - - private static GameController gameController; - private static GameView gameView; +import kakuro.controllers.AppController; +import kakuro.controllers.BoardController; +import kakuro.views.GameView; + +public class TestGameViewInput +{ + + private static AppController gameController; + private static BoardController boardController; @BeforeClass - public static void onlyOnce(){ - gameController = new GameController(10, 10, false); - gameView = new GameView(gameController, false/*GUI*/); + public static void onlyOnce() + { + gameController = new AppController(10, 10, false); + boardController = new BoardController(10, 10, gameController); } - private boolean hasValidRange(final int value){ - return (value <= gameView.getMaxNumberValid() && value >= gameView.getMinNumberValid()); + private boolean hasValidRange(final int value) + { + return (value <= boardController.getMaxNumberValid() && value >= boardController.getMinNumberValid()); } @Test @@ -88,7 +95,7 @@ public void testInvalidInputString(){ String value = "yo"; Boolean isValid = false; // Act - isValid = gameView.getNumberFormatterClassType().equals(value.getClass()); + isValid = boardController.getNumberFormatterClassType().equals(value.getClass()); // Assert assertFalse(isValid); } diff --git a/tests/kakuro/TestPlayerDao.java b/tests/kakuro/TestPlayerDao.java index d183117..62dacce 100644 --- a/tests/kakuro/TestPlayerDao.java +++ b/tests/kakuro/TestPlayerDao.java @@ -4,10 +4,9 @@ package kakuro; +import kakuro.core.DatabaseConnection; import kakuro.player.dao.*; -import kakuro.utils.DatabaseConnection; - import static org.junit.Assert.assertEquals; import java.sql.SQLException; diff --git a/tests/kakuro/TestPlayerModel.java b/tests/kakuro/TestPlayerModel.java index ddd888b..d64d453 100644 --- a/tests/kakuro/TestPlayerModel.java +++ b/tests/kakuro/TestPlayerModel.java @@ -6,7 +6,8 @@ import static org.junit.Assert.assertEquals; import org.junit.Test; -import kakuro.PlayerModel; + +import kakuro.models.PlayerModel; //iteration 3 diff --git a/tests/kakuro/TestUniquePartitions.java b/tests/kakuro/TestUniquePartitions.java index 45dd509..9b3a7ba 100644 --- a/tests/kakuro/TestUniquePartitions.java +++ b/tests/kakuro/TestUniquePartitions.java @@ -12,6 +12,8 @@ import org.junit.BeforeClass; import org.junit.Test; +import kakuro.core.*; + public class TestUniquePartitions { private static UniquePartitions partitions; From 3b7877a2fa493368547d96a47623a07882242684 Mon Sep 17 00:00:00 2001 From: Josh Pham Date: Thu, 12 Mar 2020 19:29:14 -0400 Subject: [PATCH 4/9] Fixed test bug and renamed App Controller to Game Controller for consistency --- src/kakuro/Main.java | 4 ++-- src/kakuro/controllers/BoardController.java | 6 +++--- src/kakuro/controllers/ButtonMenuController.java | 4 ++-- .../{AppController.java => GameController.java} | 4 ++-- src/kakuro/views/BoardView.java | 2 +- src/kakuro/views/ButtonMenuView.java | 2 +- src/kakuro/views/GameConsole.java | 8 ++++---- src/kakuro/views/GameView.java | 8 ++++---- tests/kakuro/TestBoard.java | 8 ++++---- tests/kakuro/TestBoardUI.java | 4 ++-- tests/kakuro/TestChrono.java | 2 +- tests/kakuro/TestGameViewInput.java | 6 +++--- 12 files changed, 29 insertions(+), 29 deletions(-) rename src/kakuro/controllers/{AppController.java => GameController.java} (99%) diff --git a/src/kakuro/Main.java b/src/kakuro/Main.java index 3efaf2a..4cd5da8 100644 --- a/src/kakuro/Main.java +++ b/src/kakuro/Main.java @@ -4,7 +4,7 @@ package kakuro; -import kakuro.controllers.AppController; +import kakuro.controllers.GameController; public class Main { @@ -13,7 +13,7 @@ public static void main(String[] args) int columns = 10; int rows = 10; - AppController game = new AppController(columns, rows, true/*GUI*/); + GameController game = new GameController(columns, rows, true/*GUI*/); game.loopGame(); } } diff --git a/src/kakuro/controllers/BoardController.java b/src/kakuro/controllers/BoardController.java index da57a43..416679f 100644 --- a/src/kakuro/controllers/BoardController.java +++ b/src/kakuro/controllers/BoardController.java @@ -8,7 +8,7 @@ import kakuro.views.BoardView; public class BoardController { - AppController appController; + GameController appController; int gridSizeX; int gridSizeY; @@ -16,12 +16,12 @@ public class BoardController { BoardView boardView; JPanel currentPanel; - public BoardController(int gridSizeX, int gridSizeY, AppController appController) { + public BoardController(int gridSizeX, int gridSizeY, GameController appController) { this.gridSizeX = gridSizeX; this.gridSizeY = gridSizeY; this.appController = appController; - boardView = new BoardView(10, 10, gridSizeX, gridSizeY); + boardView = new BoardView(appController.model.columns, appController.model.rows, gridSizeX, gridSizeY); boardView.getBoardUI(appController.model.board); //boardView. } diff --git a/src/kakuro/controllers/ButtonMenuController.java b/src/kakuro/controllers/ButtonMenuController.java index a5965e0..d0bfcf7 100644 --- a/src/kakuro/controllers/ButtonMenuController.java +++ b/src/kakuro/controllers/ButtonMenuController.java @@ -5,12 +5,12 @@ import kakuro.views.ButtonMenuView; public class ButtonMenuController { - AppController appController; + GameController appController; private ButtonMenuView buttonMenuView; private boolean isPaused; - public ButtonMenuController(AppController appController){ + public ButtonMenuController(GameController appController){ this.appController = appController; buttonMenuView = new ButtonMenuView(this); } diff --git a/src/kakuro/controllers/AppController.java b/src/kakuro/controllers/GameController.java similarity index 99% rename from src/kakuro/controllers/AppController.java rename to src/kakuro/controllers/GameController.java index 9edd082..c024621 100644 --- a/src/kakuro/controllers/AppController.java +++ b/src/kakuro/controllers/GameController.java @@ -22,7 +22,7 @@ import javax.swing.JTextField; -public class AppController +public class GameController { public DatabaseConnection database; public GameProgressDao gameProgress; @@ -49,7 +49,7 @@ public enum UserActions ANSWERS } - public AppController(final int columns, final int rows, final Boolean gui) + public GameController(final int columns, final int rows, final Boolean gui) { this.model = new GameModel(columns, rows); this.gui = gui; diff --git a/src/kakuro/views/BoardView.java b/src/kakuro/views/BoardView.java index f0b499c..dd3945d 100644 --- a/src/kakuro/views/BoardView.java +++ b/src/kakuro/views/BoardView.java @@ -31,7 +31,7 @@ public class BoardView { NumberFormat numberFormat = NumberFormat.getInstance(); NumberFormatter numberFormatter = new NumberFormatter(numberFormat); - public BoardView(int rows, int columns, int gridSizeX, int gridSizeY) { + public BoardView(int columns, int rows, int gridSizeX, int gridSizeY) { cells = new JTextField[rows][columns]; this.gridSizeX = gridSizeX; this.gridSizeY = gridSizeY; diff --git a/src/kakuro/views/ButtonMenuView.java b/src/kakuro/views/ButtonMenuView.java index 27659b4..94d17ba 100644 --- a/src/kakuro/views/ButtonMenuView.java +++ b/src/kakuro/views/ButtonMenuView.java @@ -10,7 +10,7 @@ import javax.swing.JPanel; import kakuro.controllers.ChronoController; -import kakuro.controllers.AppController; +import kakuro.controllers.GameController; import kakuro.controllers.ButtonMenuController; import kakuro.core.GameDifficulty; import kakuro.core.GameDifficultyListItem; diff --git a/src/kakuro/views/GameConsole.java b/src/kakuro/views/GameConsole.java index 1c4b08e..82fb25d 100644 --- a/src/kakuro/views/GameConsole.java +++ b/src/kakuro/views/GameConsole.java @@ -2,15 +2,15 @@ import java.util.Scanner; -import kakuro.controllers.AppController; -import kakuro.controllers.AppController.UserActions; +import kakuro.controllers.GameController; +import kakuro.controllers.GameController.UserActions; import kakuro.core.*; public class GameConsole { - private AppController controller; + private GameController controller; private Scanner inputReader = new Scanner(System.in); - public GameConsole(final AppController controller) { + public GameConsole(final GameController controller) { this.controller = controller; } diff --git a/src/kakuro/views/GameView.java b/src/kakuro/views/GameView.java index 780e2a3..bf61477 100644 --- a/src/kakuro/views/GameView.java +++ b/src/kakuro/views/GameView.java @@ -19,8 +19,8 @@ import javax.swing.border.LineBorder; import javax.swing.text.NumberFormatter; -import kakuro.controllers.AppController; -import kakuro.controllers.AppController.UserActions; +import kakuro.controllers.GameController; +import kakuro.controllers.GameController.UserActions; import kakuro.controllers.BoardController; import kakuro.controllers.ButtonMenuController; import kakuro.controllers.ChronoController; @@ -29,7 +29,7 @@ public class GameView { - private AppController controller; + private GameController controller; //Subviews ChronoView chronoView; @@ -50,7 +50,7 @@ public class GameView private JLabel startUI; - public GameView(final AppController controller, Boolean X11, ChronoView chronoView, BoardView boardView, ButtonMenuView buttonMenuView) + public GameView(final GameController controller, Boolean X11, ChronoView chronoView, BoardView boardView, ButtonMenuView buttonMenuView) { if (controller != null) { diff --git a/tests/kakuro/TestBoard.java b/tests/kakuro/TestBoard.java index a0ad96a..78d7238 100644 --- a/tests/kakuro/TestBoard.java +++ b/tests/kakuro/TestBoard.java @@ -9,7 +9,7 @@ import org.junit.Before; import org.junit.Test; -import kakuro.controllers.AppController; +import kakuro.controllers.GameController; import kakuro.core.BoardCell; import kakuro.models.GameModel; @@ -32,7 +32,7 @@ public void testSize(){ @Test public void testValidBoard(){ // Arrange - AppController controller = new AppController(10,10, GUI); + 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); @@ -50,7 +50,7 @@ public void testValidBoard(){ @Test public void testNotValidBoardOneSumIsWrong(){ // Arrange - AppController controller = new AppController(10,10, GUI); + 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); @@ -68,7 +68,7 @@ public void testNotValidBoardOneSumIsWrong(){ @Test public void testNotValidBoardCorrectSumDuplicateEntries(){ // Arrange - AppController controller = new AppController(10,10, GUI); + 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); diff --git a/tests/kakuro/TestBoardUI.java b/tests/kakuro/TestBoardUI.java index 065f9b3..f94a351 100644 --- a/tests/kakuro/TestBoardUI.java +++ b/tests/kakuro/TestBoardUI.java @@ -10,7 +10,7 @@ import org.junit.BeforeClass; import org.junit.Test; -import kakuro.controllers.AppController; +import kakuro.controllers.GameController; public class TestBoardUI { @Test @@ -19,7 +19,7 @@ public void testBoardUIGeneration(){ int rows = 3; int columns = 5; //Act - AppController gameController = new AppController(columns,rows, false); + GameController gameController = new GameController(columns,rows, false); //Assert assertEquals(columns, gameController.model.columns); assertEquals(rows, gameController.model.rows); diff --git a/tests/kakuro/TestChrono.java b/tests/kakuro/TestChrono.java index 7b5bc75..2a0c9a4 100644 --- a/tests/kakuro/TestChrono.java +++ b/tests/kakuro/TestChrono.java @@ -10,7 +10,7 @@ import org.junit.BeforeClass; import org.junit.Test; -import kakuro.controllers.AppController; +import kakuro.controllers.GameController; import kakuro.controllers.BoardController; import kakuro.controllers.ButtonMenuController; import kakuro.controllers.ChronoController; diff --git a/tests/kakuro/TestGameViewInput.java b/tests/kakuro/TestGameViewInput.java index c0de346..57abfde 100644 --- a/tests/kakuro/TestGameViewInput.java +++ b/tests/kakuro/TestGameViewInput.java @@ -12,20 +12,20 @@ import org.junit.BeforeClass; import org.junit.Test; -import kakuro.controllers.AppController; +import kakuro.controllers.GameController; import kakuro.controllers.BoardController; import kakuro.views.GameView; public class TestGameViewInput { - private static AppController gameController; + private static GameController gameController; private static BoardController boardController; @BeforeClass public static void onlyOnce() { - gameController = new AppController(10, 10, false); + gameController = new GameController(10, 10, false); boardController = new BoardController(10, 10, gameController); } From f734787cf0485b7de05d69d06e9ea5fae8fbb760 Mon Sep 17 00:00:00 2001 From: Josh Pham Date: Thu, 12 Mar 2020 19:39:35 -0400 Subject: [PATCH 5/9] Fixed more test cases --- src/kakuro/controllers/BoardController.java | 2 +- tests/kakuro/TestChrono.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kakuro/controllers/BoardController.java b/src/kakuro/controllers/BoardController.java index 416679f..8fd9df6 100644 --- a/src/kakuro/controllers/BoardController.java +++ b/src/kakuro/controllers/BoardController.java @@ -40,7 +40,7 @@ public JTextField[][] getSavedInput() { //Number formatter methods public int getMinNumberValid() { - return boardView.getMaxNumberValid(); + return boardView.getMinNumberValid(); } public Object getNumberFormatterClassType() { diff --git a/tests/kakuro/TestChrono.java b/tests/kakuro/TestChrono.java index 2a0c9a4..d8adbdc 100644 --- a/tests/kakuro/TestChrono.java +++ b/tests/kakuro/TestChrono.java @@ -28,7 +28,7 @@ public void testRunStopChronoAfter3Seconds() throws InterruptedException{ ChronoController chronoController = new ChronoController(); // Act - //gameView.settingUpMenu(); // chrono starts + chronoController.chronoStart(); Thread.sleep(waitSeconds * 1000/*ms*/ + faultToleranceMs); chronoController.chronoPause(); From b141c02e241abc35797d35288b98c66d78489df0 Mon Sep 17 00:00:00 2001 From: changlmasaki <59718659+changlmasaki@users.noreply.github.com> Date: Thu, 12 Mar 2020 21:11:22 -0400 Subject: [PATCH 6/9] uncommented all, refactored and fixed errors --- tests/kakuro/TestGameController.java | 334 ++++++++++++++------------- 1 file changed, 169 insertions(+), 165 deletions(-) diff --git a/tests/kakuro/TestGameController.java b/tests/kakuro/TestGameController.java index 425e3d5..11aa652 100644 --- a/tests/kakuro/TestGameController.java +++ b/tests/kakuro/TestGameController.java @@ -1,180 +1,184 @@ -// // @author Brian Gamboc-Javiniar -// // @author Nalveer Moocheet -// // @author Vsevolod Ivanov -// // @author Chang Liu -// // @brief Test for the GameController class +// @author Brian Gamboc-Javiniar +// @author Nalveer Moocheet +// @author Vsevolod Ivanov +// @author Chang Liu +// @brief Test for the GameController class -// package kakuro; +package kakuro; -// import static org.junit.Assert.*; +import static org.junit.Assert.*; -// import java.sql.Connection; -// import java.sql.SQLException; -// import java.util.ArrayList; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; -// import org.junit.Test; +import org.junit.Test; -// import kakuro.game.dao.GameDao; -// import kakuro.game.dao.GameDaoImpl; -// import kakuro.gameprogress.dao.GameProgressDaoImpl; -// import kakuro.utils.DatabaseConnection; +import kakuro.game.dao.GameDao; +import kakuro.game.dao.GameDaoImpl; +import kakuro.gameprogress.dao.GameProgressDaoImpl; +import kakuro.models.GameModel; +import kakuro.controllers.GameController; +import kakuro.core.BoardCell; +import kakuro.core.DatabaseConnection; -// public class TestGameController { -// Boolean GUI = false; -// Boolean solved = false; +public class TestGameController { + Boolean GUI = false; + Boolean solved = false; -// @Test -// public void testGameController() { -// //Arrange -// int rows = 3; -// int columns = 5; -// //Act -// GameController gameController = new GameController(columns,rows, false); -// //Set GUI to false -// //Assert -// assertEquals(columns, gameController.model.columns); -// assertEquals(rows, gameController.model.rows); -// } + @Test + public void testGameController() { + //Arrange + int rows = 3; + int columns = 5; + //Act + GameController gameController = new GameController(columns,rows, false); + //Set GUI to false + //Assert + assertEquals(columns, gameController.model.columns); + assertEquals(rows, gameController.model.rows); + } -// @Test -// public void testGetDatabaseConnection() { -// DatabaseConnection db = new DatabaseConnection(); -// db.getConnection(); -// } + @Test + public void testGetDatabaseConnection() { + DatabaseConnection db = new DatabaseConnection(); + db.getConnection(); + } -// @Test -// public void testConnectDatabase() { -// //Arrange -// DatabaseConnection db = new DatabaseConnection(); -// db.connect(); -// //Act -// Connection conn = db.getConnection(); -// //Assert -// assertEquals(conn!=null, true); -// db.disconnect(); -// } + @Test + public void testConnectDatabase() { + //Arrange + DatabaseConnection db = new DatabaseConnection(); + db.connect(); + //Act + Connection conn = db.getConnection(); + //Assert + assertEquals(conn!=null, true); + db.disconnect(); + } -// @Test -// public void testDisconnectDatabase() { -// //Arrange -// DatabaseConnection db = new DatabaseConnection(); -// db.connect(); -// db.disconnect(); -// //Act -// Connection conn = db.getConnection(); -// //Assert -// try { -// assertEquals(conn.isClosed(), true); -// } catch (SQLException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// } + @Test + public void testDisconnectDatabase() { + //Arrange + DatabaseConnection db = new DatabaseConnection(); + db.connect(); + db.disconnect(); + //Act + Connection conn = db.getConnection(); + //Assert + try { + assertEquals(conn.isClosed(), true); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } -// @Test -// // @brief Test Valid Board -// public void testBoardSolveValidBoard() { -// // Arrange -// int columns = 10; -// int rows = 10; -// // Act -// GameModel model = new GameModel(columns, rows); -// // Assert -// assertEquals(model.columns, columns); -// assertEquals(model.rows, rows); -// // Arrange -// GameController controller1 = new GameController(10,10, GUI); -// GameModel model1 = new GameModel(10,10); -// model1.initBoard(); //initialize board -// model1.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); -// model1.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); -// model1.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); -// model1.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); -// model1.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); -// controller1.model = model1; -// // Act -// solved = controller1.solveBoard(); -// // Assert -// assertEquals(solved, true); -// } + @Test + // @brief Test Valid Board + public void testBoardSolveValidBoard() { + //Arrange + int columns = 10; + int rows = 10; + //Act + GameModel model = new GameModel(columns, rows); + //Assert + assertEquals(model.columns, columns); + assertEquals(model.rows, rows); + //Arrange + GameController controller1 = new GameController(10,10, GUI); + GameModel model1 = new GameModel(10,10); + model1.initBoard(); //initialize board + model1.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); + model1.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); + model1.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); + model1.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); + model1.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); + controller1.model = model1; + //Act + solved = controller1.solveBoard(); + //Assert + assertEquals(solved, true); + } -// @Test -// // @brief Test Invalid Board with one wrong vertical sum -// public void testBoardSolveInvalidBoardWithOneWrongVerticalSum() { -// // Arrange -// int columns = 10; -// int rows = 10; -// // Act -// GameModel model = new GameModel(columns, rows); -// // Assert -// assertEquals(model.columns, columns); -// assertEquals(model.rows, rows); -// GameController controller2 = new GameController(10,10, GUI); -// GameModel model2 = new GameModel(10,10); -// model2.initBoard(); -// model2.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); -// model2.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); -// model2.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); -// model2.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,6,-1); -// model2.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); -// controller2.model = model2; -// // Act -// solved = controller2.solveBoard(); -// // Assert -// assertEquals(solved, false); -// } + @Test + // @brief Test Invalid Board with one wrong vertical sum + public void testBoardSolveInvalidBoardWithOneWrongVerticalSum() { + //Arrange + int columns = 10; + int rows = 10; + //Act + GameModel model = new GameModel(columns, rows); + //Assert + assertEquals(model.columns, columns); + assertEquals(model.rows, rows); + GameController controller2 = new GameController(10,10, GUI); + GameModel model2 = new GameModel(10,10); + model2.initBoard(); + model2.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); + model2.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); + model2.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); + model2.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,6,-1); + model2.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); + controller2.model = model2; + //Act + solved = controller2.solveBoard(); + //Assert + assertEquals(solved, false); + } -// @Test -// // @brief Test Invalid Board with one wrong horizontal sum -// public void testBoardSolveInvalidBoardWithOneWrongHorizontalSum() { -// // Arrange -// int columns = 10; -// int rows = 10; -// // Act -// GameModel model = new GameModel(columns, rows); -// // Assert -// assertEquals(model.columns, columns); -// assertEquals(model.rows, rows); -// GameController controller2 = new GameController(10,10, GUI); -// GameModel model2 = new GameModel(10,10); -// model2.initBoard(); -// model2.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); -// model2.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,8); -// model2.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); -// model2.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); -// model2.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); -// controller2.model = model2; -// // Act -// solved = controller2.solveBoard(); -// // Assert -// assertEquals(solved, false); -// } + @Test + // @brief Test Invalid Board with one wrong horizontal sum + public void testBoardSolveInvalidBoardWithOneWrongHorizontalSum() { + //Arrange + int columns = 10; + int rows = 10; + //Act + GameModel model = new GameModel(columns, rows); + //Assert + assertEquals(model.columns, columns); + assertEquals(model.rows, rows); + GameController controller2 = new GameController(10,10, GUI); + GameModel model2 = new GameModel(10,10); + model2.initBoard(); + model2.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); + model2.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,8); + model2.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); + model2.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); + model2.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); + controller2.model = model2; + //Act + solved = controller2.solveBoard(); + //Assert + assertEquals(solved, false); + } -// @Test -// // @brief Test Invalid Board with correct sum but duplicate entries -// public void testBoardSolveInvalidBoardWithDuplicateEntries() { -// // Arrange -// int columns = 10; -// int rows = 10; -// // Act -// GameModel model = new GameModel(columns, rows); -// // Assert -// assertEquals(model.columns, columns); -// assertEquals(model.rows, rows); -// // Arrange -// GameController controller3 = new GameController(10,10, GUI); -// GameModel model3 = new GameModel(10,10); -// model3.initBoard(); -// model3.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); -// model3.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); -// model3.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); -// model3.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); -// model3.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,2,-1); -// controller3.model = model3; -// // Act -// solved = controller3.solveBoard(); -// // Assert -// assertEquals(solved, false); -// } -// } + @Test + // @brief Test Invalid Board with correct sum but duplicate entries + public void testBoardSolveInvalidBoardWithDuplicateEntries() { + //Arrange + int columns = 10; + int rows = 10; + //Act + GameModel model = new GameModel(columns, rows); + //Assert + assertEquals(model.columns, columns); + assertEquals(model.rows, rows); + //Arrange + GameController controller3 = new GameController(10,10, GUI); + GameModel model3 = new GameModel(10,10); + model3.initBoard(); + model3.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); + model3.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); + model3.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); + model3.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); + model3.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,2,-1); + controller3.model = model3; + //Act + solved = controller3.solveBoard(); + //Assert + assertEquals(solved, false); + } +} From ec6c235c74bdfda5baa60ca7e3fa9331826dbf85 Mon Sep 17 00:00:00 2001 From: changlmasaki <59718659+changlmasaki@users.noreply.github.com> Date: Thu, 12 Mar 2020 21:59:50 -0400 Subject: [PATCH 7/9] Update TestGameController.java --- tests/kakuro/TestGameController.java | 36 ---------------------------- 1 file changed, 36 deletions(-) diff --git a/tests/kakuro/TestGameController.java b/tests/kakuro/TestGameController.java index 11aa652..4fb10c8 100644 --- a/tests/kakuro/TestGameController.java +++ b/tests/kakuro/TestGameController.java @@ -10,13 +10,7 @@ import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; - import org.junit.Test; - -import kakuro.game.dao.GameDao; -import kakuro.game.dao.GameDaoImpl; -import kakuro.gameprogress.dao.GameProgressDaoImpl; import kakuro.models.GameModel; import kakuro.controllers.GameController; import kakuro.core.BoardCell; @@ -78,14 +72,6 @@ public void testDisconnectDatabase() { @Test // @brief Test Valid Board public void testBoardSolveValidBoard() { - //Arrange - int columns = 10; - int rows = 10; - //Act - GameModel model = new GameModel(columns, rows); - //Assert - assertEquals(model.columns, columns); - assertEquals(model.rows, rows); //Arrange GameController controller1 = new GameController(10,10, GUI); GameModel model1 = new GameModel(10,10); @@ -106,13 +92,6 @@ public void testBoardSolveValidBoard() { // @brief Test Invalid Board with one wrong vertical sum public void testBoardSolveInvalidBoardWithOneWrongVerticalSum() { //Arrange - int columns = 10; - int rows = 10; - //Act - GameModel model = new GameModel(columns, rows); - //Assert - assertEquals(model.columns, columns); - assertEquals(model.rows, rows); GameController controller2 = new GameController(10,10, GUI); GameModel model2 = new GameModel(10,10); model2.initBoard(); @@ -132,13 +111,6 @@ public void testBoardSolveInvalidBoardWithOneWrongVerticalSum() { // @brief Test Invalid Board with one wrong horizontal sum public void testBoardSolveInvalidBoardWithOneWrongHorizontalSum() { //Arrange - int columns = 10; - int rows = 10; - //Act - GameModel model = new GameModel(columns, rows); - //Assert - assertEquals(model.columns, columns); - assertEquals(model.rows, rows); GameController controller2 = new GameController(10,10, GUI); GameModel model2 = new GameModel(10,10); model2.initBoard(); @@ -157,14 +129,6 @@ public void testBoardSolveInvalidBoardWithOneWrongHorizontalSum() { @Test // @brief Test Invalid Board with correct sum but duplicate entries public void testBoardSolveInvalidBoardWithDuplicateEntries() { - //Arrange - int columns = 10; - int rows = 10; - //Act - GameModel model = new GameModel(columns, rows); - //Assert - assertEquals(model.columns, columns); - assertEquals(model.rows, rows); //Arrange GameController controller3 = new GameController(10,10, GUI); GameModel model3 = new GameModel(10,10); From a03f54de6a042404dfda28176942cb17659b8169 Mon Sep 17 00:00:00 2001 From: Josh Pham Date: Thu, 12 Mar 2020 22:24:49 -0400 Subject: [PATCH 8/9] Apply the correct flow and changed the variable name --- tests/kakuro/TestGameController.java | 76 +++++++++++++--------------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/tests/kakuro/TestGameController.java b/tests/kakuro/TestGameController.java index 4fb10c8..5729aca 100644 --- a/tests/kakuro/TestGameController.java +++ b/tests/kakuro/TestGameController.java @@ -73,17 +73,16 @@ public void testDisconnectDatabase() { // @brief Test Valid Board public void testBoardSolveValidBoard() { //Arrange - GameController controller1 = new GameController(10,10, GUI); - GameModel model1 = new GameModel(10,10); - model1.initBoard(); //initialize board - model1.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model1.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); - model1.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model1.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - model1.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); - controller1.model = model1; + 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); //Act - solved = controller1.solveBoard(); + solved = controller.solveBoard(); //Assert assertEquals(solved, true); } @@ -92,17 +91,16 @@ public void testBoardSolveValidBoard() { // @brief Test Invalid Board with one wrong vertical sum public void testBoardSolveInvalidBoardWithOneWrongVerticalSum() { //Arrange - GameController controller2 = new GameController(10,10, GUI); - GameModel model2 = new GameModel(10,10); - model2.initBoard(); - model2.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model2.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); - model2.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model2.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,6,-1); - model2.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); - controller2.model = model2; + 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); //Act - solved = controller2.solveBoard(); + solved = controller.solveBoard(); //Assert assertEquals(solved, false); } @@ -111,17 +109,16 @@ public void testBoardSolveInvalidBoardWithOneWrongVerticalSum() { // @brief Test Invalid Board with one wrong horizontal sum public void testBoardSolveInvalidBoardWithOneWrongHorizontalSum() { //Arrange - GameController controller2 = new GameController(10,10, GUI); - GameModel model2 = new GameModel(10,10); - model2.initBoard(); - model2.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model2.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,8); - model2.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model2.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - model2.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,1,-1); - controller2.model = model2; + 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); //Act - solved = controller2.solveBoard(); + solved = controller.solveBoard(); //Assert assertEquals(solved, false); } @@ -130,17 +127,16 @@ public void testBoardSolveInvalidBoardWithOneWrongHorizontalSum() { // @brief Test Invalid Board with correct sum but duplicate entries public void testBoardSolveInvalidBoardWithDuplicateEntries() { //Arrange - GameController controller3 = new GameController(10,10, GUI); - GameModel model3 = new GameModel(10,10); - model3.initBoard(); - model3.board[0][1] = new BoardCell(BoardCell.CellType.FILLED10, 5); - model3.board[1][0] = new BoardCell(BoardCell.CellType.FILLED01,-1,4); - model3.board[1][1] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - model3.board[2][1] = new BoardCell(BoardCell.CellType.INPUT,3,-1); - model3.board[1][2] = new BoardCell(BoardCell.CellType.INPUT,2,-1); - controller3.model = model3; + 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); //Act - solved = controller3.solveBoard(); + solved = controller.solveBoard(); //Assert assertEquals(solved, false); } From d2d79b6ab79ae637e19749643c2a0287d7423ef8 Mon Sep 17 00:00:00 2001 From: Josh Pham Date: Fri, 13 Mar 2020 11:55:34 -0400 Subject: [PATCH 9/9] 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); }