Skip to content

Commit

Permalink
Merge pull request #44 from binarytrails/epic/iteration2
Browse files Browse the repository at this point in the history
Iteration 2: everything
  • Loading branch information
binarytrails committed Mar 6, 2020
2 parents 4713455 + d12031c commit f115610
Show file tree
Hide file tree
Showing 25 changed files with 866 additions and 21 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/sqlite-jdbc-3.30.1.jar"/>
<classpathentry kind="lib" path="lib/gson-2.8.6.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
4 changes: 2 additions & 2 deletions .github/workflows/junit-ant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ name: Java Unit Test CI
on:
push:
paths:
- 'src/kakuro/*.java'
- 'tests/kakuro/*.java'
- 'src/**'
- 'tests/**'

jobs:
build:
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Filled cells notation means:

## UI

![iteration1-ui](resources/images/iteration1-ui.png)
![iteration1-ui](resources/images/iteration2-ui.png)
![iteration1-ui](resources/images/iteration2-ui-1.png)
![iteration1-ui](resources/images/iteration2-ui-2.png)

## Console

Expand Down
3 changes: 2 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<project name="junit-example">
<property name="main.build.dir" value="build/main"/>
<property name="main.src.dir" value="src/kakuro"/>
<property name="main.src.dir" value="src"/>
<property name="test.build.dir" value="build/test"/>
<property name="test.src.dir" value="tests/kakuro"/>

<path id="classpath.test">
<pathelement location="lib/junit-4.12.0.jar"/>
<pathelement location="lib/org.hamcrest.core_1.3.0.jar"/>
<pathelement location="lib/gson-2.8.6.jar"/>
<pathelement location="lib/sqlite-jdbc-3.30.1.jar"/>
<pathelement location="${main.build.dir}"/>
</path>

Expand Down
Binary file added lib/sqlite-jdbc-3.30.1.jar
Binary file not shown.
Binary file added resources/SQLiteKakuro.db
Binary file not shown.
Binary file added resources/images/iteration2-ui-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/images/iteration2-ui-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/images/iteration2-ui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 78 additions & 6 deletions src/kakuro/ButtonMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

public class ButtonMenu {
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() {
Expand All @@ -34,14 +37,21 @@ 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");
save_button = new JButton("Save");
restart_button = new JButton("Restart");
load_button = new JButton("Load Game");
mainPanel = new JPanel();
chrono = new Chrono();

// Set up
mainPanel.add(play_button);
mainPanel.add(pause_button);
mainPanel.add(submit_button);
mainPanel.add(play_button).setVisible(false);
mainPanel.add(pause_button).setVisible(false);
mainPanel.add(restart_button);
mainPanel.add(submit_button).setVisible(false);
mainPanel.add(choose_game_button);
mainPanel.add(save_button).setVisible(false);
mainPanel.add(load_button);

if (appFrame != null)
{
Expand All @@ -56,6 +66,16 @@ public ButtonMenu(JFrame appFrame, int x, int y, GameController gameController)
public void chronoSetUp() {
chrono.timerSetUp();
}

private void toggleMenu() {
play_button.setVisible(true);
pause_button.setVisible(true);
submit_button.setVisible(true);
save_button.setVisible(true);

choose_game_button.setVisible(false);
load_button.setVisible(false);
}

public void buttonsSetUp() {

Expand Down Expand Up @@ -94,7 +114,7 @@ public void actionPerformed(ActionEvent e)
{
chrono.chronoPause();
//loads data into model
gameController.view.loadInputInModel();
gameController.loadInputInModel(false); //No clearing inputs
gameController.solveBoard();
gameController.view.printSolveBoard();
}
Expand All @@ -107,7 +127,59 @@ public void actionPerformed(ActionEvent e)
{
public void actionPerformed(ActionEvent e)
{
// Call save method
chrono.chronoPause();
gameController.view.loadInputInModel();

gameController.saveGame();
}
});

/*
* With the use of an Action Listener to know if the user has clicked on the button, this part of the method will load a saved game of a user
*
**/
load_button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(gameController.loadGame() != 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);
}
}
});

/*
* With the use of an Action Listener to know if the user has clicked on the button, this part of the method will pop a dialog with preconfigured games
*
**/
choose_game_button.addActionListener(new ActionListener()
{
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]);

if(chooseGame != null) {
int gameLevel = Integer.parseInt(chooseGame);
toggleMenu();

gameController.loadPreconfiguredGame(gameLevel);
}
}
});

restart_button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
chrono.resetTimer();
chrono.chronoStart();
gameController.loadInputInModel(true); //Clear inputs
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/kakuro/Chrono.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ 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 button
* Need to implement : Save feature
*/

public void checkTime() {
Expand Down
128 changes: 121 additions & 7 deletions src/kakuro/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@

package kakuro;

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 java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JTextField;

public class GameController
{
public DatabaseConnection database;
public GameProgressDao gameProgress;
public GameDao game;

public GameView view;
public GameModel model;
private Boolean gui = true;
Expand All @@ -25,15 +39,23 @@ public GameController(final int columns, final int rows, final Boolean gui)
{
this.model = new GameModel(columns, rows);
this.gui = gui;

database = new DatabaseConnection();
connectDatabase();

gameProgress = new GameProgressDaoImpl();
game = new GameDaoImpl();

initGame(model);
}

private void initGame(GameModel model)
{
model.initBoard();
if (model.columns == 10 && model.rows == 10)
model.generateBoard10x10();

model.initBoard();

this.view = new GameView(this, gui);

view.printStartup();
view.printBoard(false/*show answer values*/);
if (gui){
Expand Down Expand Up @@ -66,6 +88,76 @@ public void loopGame()
}
}
}

public Connection getDatabaseConnection() {
return database.getConnection();
}

public void connectDatabase() {
database.connect();
}

public void disconnectDatabase() {
database.disconnect();
}

public void saveGame() {

try {
//TODO: fixed player and to fix in iteration 3
gameProgress.save(getDatabaseConnection(), "TestPlayer", model.board);

System.out.println("Successfully saved game progress");
} catch(SQLException e) {
System.err.println("Failed to save game");
}
}

public BoardCell[][] loadGame() {
try {
//TODO: fixed player and to fix in iteration 3
BoardCell[][] boardCell = gameProgress.load(getDatabaseConnection(), "TestPlayer");

if(boardCell != null) {
model.board = boardCell;
System.out.println("Successfully loaded game progress");

view.printStartup();
view.printBoard(false);

if (gui){
view.board_ui();
}

return model.board;
}


} catch(SQLException e) {
System.err.println("Failed to load game");
}

return null;
}

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

model.board = boardCells.get(gameLevel-1);


view.printStartup();
view.printBoard(false);

if (gui){
view.board_ui();
}

} catch(SQLException e) {
System.err.println("Failed to load preconfigred game");
}
}

public Boolean solveBoard()
{
Expand Down Expand Up @@ -210,9 +302,31 @@ public Boolean solveBoard()
return true;
else
return false;




}

public void loadInputInModel(boolean clearInput) {
JTextField[][] saveInput = view.saveInput;
String value;

for(int row = 0; row < model.columns; row++)
{
for(int column = 0; column < model.rows; column++)
{
BoardCell cell = model.board[row][column];

if (cell.getType() == BoardCell.CellType.INPUT)
{
if(clearInput) {
saveInput[row][column].setText("");
model.board[row][column].setFirstValue(-1);
}
else {
value = saveInput[row][column].getText();
if(!value.isEmpty())
model.board[row][column].setFirstValue(Integer.parseInt(value));
}
}
}
}
}
}
1 change: 1 addition & 0 deletions src/kakuro/GameModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void initBoard()
}
}

//TODO: to remove - iteration2
public void generateBoard10x10()
{
// second row
Expand Down
Loading

0 comments on commit f115610

Please sign in to comment.