Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Merge BoardController with GameController
Rename Button Menu to Menu Bar
Rename BoardCell to Cell
  • Loading branch information
joshpham97 committed Mar 13, 2020
1 parent a03f54d commit d2d79b6
Show file tree
Hide file tree
Showing 19 changed files with 259 additions and 353 deletions.
53 changes: 0 additions & 53 deletions src/kakuro/controllers/BoardController.java

This file was deleted.

5 changes: 3 additions & 2 deletions src/kakuro/controllers/ChronoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -78,7 +79,7 @@ public int getSeconds() {
return chronoModel.getSeconds();
}

public ChronoView getView() {
return chronoView;
public JComponent getView() {
return chronoView.timerLabel;
}
}
49 changes: 31 additions & 18 deletions src/kakuro/controllers/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -144,7 +144,7 @@ public BoardCell[][] loadGame() {
console.printBoard(false);

if (gui){
view.updateView(boardController.loadGame());
view.updateView();
}

return model.board;
Expand All @@ -160,7 +160,7 @@ public BoardCell[][] loadGame() {

public void loadPreconfiguredGame(int gameLevel) {
try {
ArrayList<BoardCell[][]> boardCells = game.loadAllPreconfiguredGames(getDatabaseConnection());
ArrayList<Cell[][]> boardCells = game.loadAllPreconfiguredGames(getDatabaseConnection());

//Load the new game model
model.board = boardCells.get(gameLevel-1);
Expand All @@ -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();
Expand Down Expand Up @@ -204,7 +204,7 @@ public Boolean solveBoard()
int sum = 0;
map = new HashMap<Integer,Integer>();
//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
Expand Down Expand Up @@ -235,7 +235,7 @@ public Boolean solveBoard()
int sum = 0;
map = new HashMap<Integer,Integer>();

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();

Expand Down Expand Up @@ -266,7 +266,7 @@ public Boolean solveBoard()
int sumColumns = 0;
map = new HashMap<Integer,Integer>();
//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();

Expand All @@ -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();

Expand Down Expand Up @@ -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("");
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -31,8 +33,8 @@ public void resume() {
appController.resume();
}

public ButtonMenuView getView() {
return buttonMenuView;
public JComponent getView() {
return buttonMenuView.mainPanel;
}

public void submit() {
Expand All @@ -43,7 +45,7 @@ public void save() {
appController.saveGame();
}

public BoardCell[][] load() {
public Cell[][] load() {
return appController.loadGame();
}

Expand Down
8 changes: 4 additions & 4 deletions src/kakuro/core/BoardCell.java → src/kakuro/core/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package kakuro.core;

public class BoardCell
public class Cell
{
public enum CellType
{
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/kakuro/game/dao/GameDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.sql.SQLException;
import java.util.ArrayList;

import kakuro.core.BoardCell;
import kakuro.core.Cell;

public interface GameDao {

Expand All @@ -17,5 +17,5 @@ public interface GameDao {
* - the database connection
* @return BoardCell object
*/
ArrayList<BoardCell[][]> loadAllPreconfiguredGames(Connection conn) throws SQLException;
ArrayList<Cell[][]> loadAllPreconfiguredGames(Connection conn) throws SQLException;
}
8 changes: 4 additions & 4 deletions src/kakuro/game/dao/GameDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BoardCell[][]> loadAllPreconfiguredGames(Connection conn) throws SQLException {
public ArrayList<Cell[][]> loadAllPreconfiguredGames(Connection conn) throws SQLException {
Gson gson = new Gson();
ArrayList<BoardCell[][]> boardCells = new ArrayList<BoardCell[][]>();
ArrayList<Cell[][]> boardCells = new ArrayList<Cell[][]>();

PreparedStatement pstmt = conn.prepareStatement(LOAD_ALL_PRECONFIGURED_GAMES);

Expand All @@ -27,7 +27,7 @@ public ArrayList<BoardCell[][]> 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;
Expand Down
6 changes: 3 additions & 3 deletions src/kakuro/gameprogress/dao/GameProgressDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.sql.Connection;
import java.sql.SQLException;

import kakuro.core.BoardCell;
import kakuro.core.Cell;

public interface GameProgressDao {
/**
Expand All @@ -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;

/**
*
Expand All @@ -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;
}
Loading

0 comments on commit d2d79b6

Please sign in to comment.