diff --git a/.classpath b/.classpath
index 15ae0b6..a966583 100644
--- a/.classpath
+++ b/.classpath
@@ -8,6 +8,7 @@
+
diff --git a/.github/workflows/junit-ant.yml b/.github/workflows/junit-ant.yml
index 53de4d4..55580d7 100644
--- a/.github/workflows/junit-ant.yml
+++ b/.github/workflows/junit-ant.yml
@@ -3,8 +3,8 @@ name: Java Unit Test CI
on:
push:
paths:
- - 'src/kakuro/*.java'
- - 'tests/kakuro/*.java'
+ - 'src/**'
+ - 'tests/**'
jobs:
build:
diff --git a/README.md b/README.md
index ad23395..78373bb 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/build.xml b/build.xml
index 5cbed06..20c4f46 100644
--- a/build.xml
+++ b/build.xml
@@ -1,6 +1,6 @@
-
+
@@ -8,6 +8,7 @@
+
diff --git a/lib/sqlite-jdbc-3.30.1.jar b/lib/sqlite-jdbc-3.30.1.jar
new file mode 100644
index 0000000..8583737
Binary files /dev/null and b/lib/sqlite-jdbc-3.30.1.jar differ
diff --git a/resources/SQLiteKakuro.db b/resources/SQLiteKakuro.db
new file mode 100644
index 0000000..188eac6
Binary files /dev/null and b/resources/SQLiteKakuro.db differ
diff --git a/resources/images/iteration2-ui-1.png b/resources/images/iteration2-ui-1.png
new file mode 100644
index 0000000..1e2fc41
Binary files /dev/null and b/resources/images/iteration2-ui-1.png differ
diff --git a/resources/images/iteration2-ui-2.png b/resources/images/iteration2-ui-2.png
new file mode 100644
index 0000000..27285ed
Binary files /dev/null and b/resources/images/iteration2-ui-2.png differ
diff --git a/resources/images/iteration2-ui.png b/resources/images/iteration2-ui.png
new file mode 100644
index 0000000..424a9ef
Binary files /dev/null and b/resources/images/iteration2-ui.png differ
diff --git a/src/kakuro/ButtonMenu.java b/src/kakuro/ButtonMenu.java
index fc98500..8a254d2 100644
--- a/src/kakuro/ButtonMenu.java
+++ b/src/kakuro/ButtonMenu.java
@@ -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() {
@@ -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)
{
@@ -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() {
@@ -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();
}
@@ -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
}
});
}
diff --git a/src/kakuro/Chrono.java b/src/kakuro/Chrono.java
index 8e70bf5..43e2cd9 100644
--- a/src/kakuro/Chrono.java
+++ b/src/kakuro/Chrono.java
@@ -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() {
diff --git a/src/kakuro/GameController.java b/src/kakuro/GameController.java
index 2f614ef..afacc72 100644
--- a/src/kakuro/GameController.java
+++ b/src/kakuro/GameController.java
@@ -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;
@@ -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){
@@ -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 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()
{
@@ -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));
+ }
+ }
+ }
+ }
}
}
diff --git a/src/kakuro/GameModel.java b/src/kakuro/GameModel.java
index 8300ce9..74cd1dd 100644
--- a/src/kakuro/GameModel.java
+++ b/src/kakuro/GameModel.java
@@ -37,6 +37,7 @@ public void initBoard()
}
}
+ //TODO: to remove - iteration2
public void generateBoard10x10()
{
// second row
diff --git a/src/kakuro/GameView.java b/src/kakuro/GameView.java
index 5362580..cd6fdb1 100644
--- a/src/kakuro/GameView.java
+++ b/src/kakuro/GameView.java
@@ -33,6 +33,7 @@ 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)
public ButtonMenu getButtonMenu() {
return buttonMenu;
@@ -50,16 +51,18 @@ public void setButtonMenu(ButtonMenu buttonMenu) {
public GameView(final GameController controller, Boolean X11)
{
- if (X11)
- frame = new JFrame("KAKURO");
if (controller != null)
{
this.controller = controller;
gridSizeX = controller.model.rows;
gridSizeY = controller.model.columns;
}
- if (X11)
+ if (X11) {
+ frame = new JFrame("KAKURO");
buttonMenu = new ButtonMenu(frame, gridSizeX, gridSizeY, controller);
+ saveInput = new JTextField[controller.model.rows][controller.model.columns];
+ }
+
numberFormatter.setValueClass(Integer.class);
numberFormatter.setMinimum(1);
numberFormatter.setMaximum(9);
@@ -147,6 +150,9 @@ public void board_ui() {
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;
@@ -192,7 +198,16 @@ public void board_ui() {
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);
+
+ //Save a reference to the new panel
frame.getContentPane().add(panel);
+ currentPanel = panel;
+
+ // currentPanel = panel;
frame.setVisible(true);
}
diff --git a/src/kakuro/PlayerModel.java b/src/kakuro/PlayerModel.java
new file mode 100644
index 0000000..2c77c87
--- /dev/null
+++ b/src/kakuro/PlayerModel.java
@@ -0,0 +1,29 @@
+package kakuro;
+
+//TODO: iteration 3 work
+public class PlayerModel {
+ private String username;
+ private String password;
+
+ public PlayerModel(String username, String password) {
+ this.username = username;
+ this.password = password;
+ }
+
+ public String getPlayerUsername() {
+ return this.username;
+ }
+
+ //TODO: remove later when user is implemented properly in iteration 3
+ public String getPlayerPassword() {
+ return this.password;
+ }
+
+ public void setPlayerUsername(String username) {
+ this.username = username;
+ }
+
+ public void setPlayerPassword(String password) {
+ this.password = password;
+ }
+}
diff --git a/src/kakuro/game/dao/GameDao.java b/src/kakuro/game/dao/GameDao.java
new file mode 100644
index 0000000..3acb354
--- /dev/null
+++ b/src/kakuro/game/dao/GameDao.java
@@ -0,0 +1,21 @@
+//@author Brian Gamboc-Javiniar
+
+package kakuro.game.dao;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.ArrayList;
+
+import kakuro.BoardCell;
+
+public interface GameDao {
+
+ /**
+ *
+ * Deserialize the JSON object and returns all pre-configured BoardCell object
+ * @param conn
+ * - the database connection
+ * @return BoardCell object
+ */
+ ArrayList loadAllPreconfiguredGames(Connection conn) throws SQLException;
+}
diff --git a/src/kakuro/game/dao/GameDaoImpl.java b/src/kakuro/game/dao/GameDaoImpl.java
new file mode 100644
index 0000000..7893939
--- /dev/null
+++ b/src/kakuro/game/dao/GameDaoImpl.java
@@ -0,0 +1,36 @@
+//@author Brian Gamboc-Javiniar
+
+package kakuro.game.dao;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+
+import com.google.gson.Gson;
+
+import kakuro.BoardCell;
+
+public class GameDaoImpl implements GameDao {
+ private final String LOAD_ALL_PRECONFIGURED_GAMES = "SELECT cells FROM game";
+
+ @Override
+ public ArrayList loadAllPreconfiguredGames(Connection conn) throws SQLException {
+ Gson gson = new Gson();
+ ArrayList boardCells = new ArrayList();
+
+ PreparedStatement pstmt = conn.prepareStatement(LOAD_ALL_PRECONFIGURED_GAMES);
+
+ ResultSet rs = pstmt.executeQuery();
+
+
+ while(rs.next()) {
+ String cells = rs.getString("cells");
+ boardCells.add(gson.fromJson(cells, BoardCell[][].class));
+ }
+
+ return boardCells;
+ }
+
+}
diff --git a/src/kakuro/gameprogress/dao/GameProgressDao.java b/src/kakuro/gameprogress/dao/GameProgressDao.java
new file mode 100644
index 0000000..2750c12
--- /dev/null
+++ b/src/kakuro/gameprogress/dao/GameProgressDao.java
@@ -0,0 +1,30 @@
+//@author Brian Gamboc-Javiniar
+
+package kakuro.gameprogress.dao;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import kakuro.BoardCell;
+
+public interface GameProgressDao {
+ /**
+ *
+ * Serialize the BoardCell object to JSON and stores a JSON object to the gameprogress table in the database
+ * @param conn
+ * - the database connection
+ * @param uid
+ * - the username of the player
+ */
+ void save(Connection conn, String uid, BoardCell[][] board) throws SQLException;
+
+ /**
+ *
+ * Deserialize the JSON object and returns a BoardCell object
+ * @param conn
+ * - the database connection
+ * @param uid
+ * - the username of the player
+ * @return BoardCell object
+ */
+ BoardCell[][] load(Connection conn, String uid) throws SQLException;
+}
diff --git a/src/kakuro/gameprogress/dao/GameProgressDaoImpl.java b/src/kakuro/gameprogress/dao/GameProgressDaoImpl.java
new file mode 100644
index 0000000..3bdb93b
--- /dev/null
+++ b/src/kakuro/gameprogress/dao/GameProgressDaoImpl.java
@@ -0,0 +1,47 @@
+//@author Brian Gamboc-Javiniar
+
+package kakuro.gameprogress.dao;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import com.google.gson.*;
+
+import kakuro.BoardCell;
+
+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 {
+ Gson gson = new Gson();
+
+ String boardCellJSON = gson.toJson(board);
+ PreparedStatement pstmt = conn.prepareStatement(SAVE_GAME_PROGRESS);
+
+ pstmt.setString(1, boardCellJSON);
+ pstmt.setString(2, uid);
+ pstmt.executeUpdate();
+ }
+
+ @Override
+ public BoardCell[][] load(Connection conn, String uid) throws SQLException {
+ Gson gson = new Gson();
+
+ PreparedStatement pstmt = conn.prepareStatement(LOAD_GAME_PROGRESS);
+
+ pstmt.setString(1, uid);
+ ResultSet rs = pstmt.executeQuery();
+
+
+ if(rs.next()) {
+ String cells = rs.getString("cells");
+ return gson.fromJson(cells, BoardCell[][].class);
+ }
+
+ return null;
+ }
+
+}
diff --git a/src/kakuro/player/dao/PlayerDao.java b/src/kakuro/player/dao/PlayerDao.java
new file mode 100644
index 0000000..c426bca
--- /dev/null
+++ b/src/kakuro/player/dao/PlayerDao.java
@@ -0,0 +1,34 @@
+//@author Brian Gamboc-Javiniar
+
+package kakuro.player.dao;
+
+import java.sql.SQLException;
+import java.sql.Connection;
+
+
+public interface PlayerDao {
+
+ /**
+ * Logins when player is found in the database
+ * @param conn
+ * - the database connection
+ * @param uid
+ * - the username of the player
+ * @param pwd
+ * - the password of the player
+ * @return true if player if found and false if player is not found
+ */
+ boolean login(Connection conn, String uid, String pwd) throws SQLException;
+
+ /**
+ * Registers the player and inserts them to the database
+ * @param conn
+ * - the database connection
+ * @param uid
+ * - the username of the player
+ * @param pwd
+ * - the password of the player
+ * @return true if successfully registering and false if failing to register
+ */
+ boolean register(Connection conn, String uid, String pwd) throws SQLException;
+}
diff --git a/src/kakuro/player/dao/PlayerDaoImpl.java b/src/kakuro/player/dao/PlayerDaoImpl.java
new file mode 100644
index 0000000..0fe8801
--- /dev/null
+++ b/src/kakuro/player/dao/PlayerDaoImpl.java
@@ -0,0 +1,72 @@
+//@author Brian Gamboc-Javiniar
+
+package kakuro.player.dao;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+public class PlayerDaoImpl implements PlayerDao {
+
+ private final String SELECT_A_PLAYER_QUERY = "SELECT * FROM player WHERE username=? AND password=? ";
+ private final String VERIFY_IF_PLAYER_EXIST_QUERY = "SELECT * FROM player WHERE username=? ";
+ private final String INSERT_PLAYER_QUERY = "INSERT INTO player(username, password) VALUES(?, ?)";
+
+ @Override
+ public boolean login(Connection conn, String uid, String pwd) throws SQLException {
+
+ PreparedStatement psmt = conn.prepareStatement(SELECT_A_PLAYER_QUERY);
+
+ psmt.setString(1, uid);
+ psmt.setString(2, pwd);
+
+ ResultSet rs = psmt.executeQuery();
+
+ int count = 0;
+
+ while (rs.next()) {
+ count++;
+ }
+
+ if (count == 1) {
+ return true;
+ }
+
+ return false;
+ }
+
+ @Override
+ public boolean register(Connection conn, String uid, String pwd) throws SQLException {
+
+
+ PreparedStatement checkUserPsmt = conn.prepareStatement(VERIFY_IF_PLAYER_EXIST_QUERY);
+
+ checkUserPsmt.setString(1, uid);
+
+ ResultSet rs = checkUserPsmt.executeQuery();
+
+ int count = 0;
+
+ while (rs.next()) {
+ count++;
+ }
+
+ if (count > 0) {
+ System.err.println("User already exist");
+ return false;
+
+ } else {
+ PreparedStatement registerPsmt = conn.prepareStatement(INSERT_PLAYER_QUERY);
+
+ registerPsmt.setString(1, uid);
+ registerPsmt.setString(2, pwd);
+
+ registerPsmt.executeUpdate();
+
+ System.out.println("Succesfully registered");
+ return true;
+ }
+
+ }
+}
diff --git a/src/kakuro/utils/DatabaseConnection.java b/src/kakuro/utils/DatabaseConnection.java
new file mode 100644
index 0000000..e0c2051
--- /dev/null
+++ b/src/kakuro/utils/DatabaseConnection.java
@@ -0,0 +1,136 @@
+//@author Brian Gamboc-Javiniar and Nolan Mckay
+
+package kakuro.utils;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+
+public class DatabaseConnection {
+ private Connection connection = null;
+ private final static String DATABASE_FILE_PATH = "resources/SQLiteKakuro.db";
+ private final String CREATE_GAME_TABLE_QUERY = "CREATE TABLE IF NOT EXISTS game"
+ + "(gameID INTEGER PRIMARY KEY AUTOINCREMENT,"
+ + "username VARCHAR(60)," + "time INTEGER," + "cells TEXT);";
+
+ private final String CREATE_GAME_PROGRESS_TABLE_QUERY = "CREATE TABLE IF NOT EXISTS game_progress"
+ + "(gameProgressID INTEGER PRIMARY KEY AUTOINCREMENT,"
+ + "time INTEGER," + "cells TEXT," + "gameID INTEGER,"
+ + "username VARCHAR(60),"
+ + "FOREIGN KEY(gameID) REFERENCES game (gameID),"
+ + "FOREIGN KEY(username) REFERENCES player (username)" + ");";
+
+ private final String CREATE_PLAYER_TABLE_QUERY = "CREATE TABLE IF NOT EXISTS player"
+ + "(username VARCHAR(60) PRIMARY KEY," + "password VARCHAR(60)"
+ + ");";
+
+ //TODO: remove hardcoded player in iteration 3
+ private final String INSERT_PLAYER_QUERY = "INSERT INTO player(username,password) VALUES(?,?)";
+ //TODO: remove this in iteration 3
+ private final String INSERT_PLAYER_DATA_QUERY = "INSERT INTO game_progress(username) VALUES(?)";
+ private final String INSERT_PRECONFIGURED_GAME_DATA_QUERY = "INSERT INTO game(cells) VALUES(?)";
+
+ public void connect() {
+
+ try {
+ File file = new File(System.getProperty("user.dir") + "/" + DATABASE_FILE_PATH);
+
+ if(!file.exists()) {
+
+ connection = DriverManager.getConnection("jdbc:sqlite:" + DATABASE_FILE_PATH);
+ System.out.println("Success! Connected to SQLite database");
+
+ createGameTable();
+ createGameProgressTable();
+ createPlayerTable();
+ //TODO: remove hardcoded player in iteration 3
+ insertMainPlayer();
+ //TODO: remove hardcoded player in iteration 3
+ insertPlayerData();
+ insertPreconfiguredGames();
+ }
+ else {
+
+ connection = DriverManager.getConnection("jdbc:sqlite:" + DATABASE_FILE_PATH);
+ System.out.println("Success! Connected to SQLite database");
+ }
+
+
+ } catch (Exception e) {
+ System.err.println("Failed to connect to SQLite database");
+ }
+ }
+
+ public void disconnect() {
+ try {
+ if (connection != null) {
+ connection.close();
+
+ System.out
+ .println("Success! Disconnected from SQLite database");
+ }
+ } catch (Exception e) {
+ System.err.println("Failed to disconnect from SQLite database");
+ }
+
+ }
+
+ public Connection getConnection() {
+ return this.connection;
+ }
+
+ private void createGameTable() throws SQLException {
+ Statement stmt = connection.createStatement();
+ stmt.execute(CREATE_GAME_TABLE_QUERY);
+ }
+
+ private void createGameProgressTable() throws SQLException {
+ Statement stmt = connection.createStatement();
+ stmt.execute(CREATE_GAME_PROGRESS_TABLE_QUERY);
+ }
+
+ private void createPlayerTable() throws SQLException {
+ Statement stmt = connection.createStatement();
+ stmt.execute(CREATE_PLAYER_TABLE_QUERY);
+ }
+
+ //TODO: remove hardcoded player in iteration 3
+ private void insertMainPlayer() throws SQLException {
+ PreparedStatement pstmt = connection.prepareStatement(INSERT_PLAYER_QUERY);
+
+ pstmt.setString(1, "TestPlayer");
+ pstmt.setString(2, "123");
+
+ pstmt.executeUpdate();
+ }
+
+ //TODO: remove in iteration 3
+ private void insertPlayerData() throws SQLException {
+ PreparedStatement pstmt = connection.prepareStatement(INSERT_PLAYER_DATA_QUERY);
+
+ pstmt.setString(1, "TestPlayer");
+
+ pstmt.executeUpdate();
+ }
+
+ private void insertPreconfiguredGames() throws SQLException {
+ String preconfiguredGame1 = "[[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"FILLED10\",\"value1\":37,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"FILLED01\",\"value1\":-1,\"value2\":43},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":3},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":4},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":6},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":7},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":8},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":9},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":2},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":3},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":4},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":6},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":7},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"FILLED01\",\"value1\":-1,\"value2\":39},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":9},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":8},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":7},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":4},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":3},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":2},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}]]";
+ String preconfiguredGame2 = "[[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"FILLED10\",\"value1\":24,\"value2\":-1},{\"type\":\"FILLED10\",\"value1\":23,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":6},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":6},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":6},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":6},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"FILLED10\",\"value1\":8,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":6},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":2},{\"type\":\"FILLED10\",\"value1\":70,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":7},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":3},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":9},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":8},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":7},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"FILLED01\",\"value1\":-1,\"value2\":90},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}]]";
+ String preconfiguredGame3 = "[[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"FILLED10\",\"value1\":50,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"FILLED10\",\"value1\":5,\"value2\":-1},{\"type\":\"FILLED10\",\"value1\":10,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"FILLED01\",\"value1\":-1,\"value2\":33},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":2},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":3},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":4},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":6},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":7},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":8},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":9},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":3},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":8},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":9},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":4},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"FILLED11\",\"value1\":3,\"value2\":12},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":6},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":7},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":4},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":6},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":7},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":8},{\"type\":\"FILLED10\",\"value1\":8,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":6},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":8},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":9},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":7},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":8},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":8},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":7},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":5},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":4},{\"type\":\"INPUT\",\"value1\":-1,\"value2\":3},{\"type\":\"FILLED11\",\"value1\":21,\"value2\":12},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}],[{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1},{\"type\":\"EMPTY\",\"value1\":-1,\"value2\":-1}]]";
+
+ String[] listOfPreconfiguredGames = new String[] {preconfiguredGame1, preconfiguredGame2, preconfiguredGame3};
+
+ PreparedStatement pstmt = connection.prepareStatement(INSERT_PRECONFIGURED_GAME_DATA_QUERY);
+
+ for(int i = 0; i < listOfPreconfiguredGames.length; i++) {
+ pstmt.setString(1, listOfPreconfiguredGames[i]);
+ pstmt.addBatch();
+ }
+
+ pstmt.executeBatch();
+ }
+}
diff --git a/tests/kakuro/TestDBConnection.java b/tests/kakuro/TestDBConnection.java
new file mode 100644
index 0000000..edf6eb4
--- /dev/null
+++ b/tests/kakuro/TestDBConnection.java
@@ -0,0 +1,53 @@
+package kakuro;
+
+import static org.junit.Assert.assertEquals;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import org.junit.Test;
+
+import kakuro.utils.DatabaseConnection;
+
+public class TestDBConnection {
+
+
+ @Test
+ public void testConnect() {
+
+
+ DatabaseConnection db = new DatabaseConnection();
+
+ db.connect();
+
+ Connection conn = db.getConnection();
+
+ assertEquals(conn!=null, true);
+
+ db.disconnect();
+
+ }
+
+ @Test
+ public void testDisconnect() {
+
+ DatabaseConnection db = new DatabaseConnection();
+
+ db.connect();
+
+ db.disconnect();
+
+ Connection conn = db.getConnection();
+
+ try {
+ assertEquals(conn.isClosed(), true);
+ } catch (SQLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+
+ }
+
+
+
+}
diff --git a/tests/kakuro/TestPlayerDao.java b/tests/kakuro/TestPlayerDao.java
new file mode 100644
index 0000000..e1b0080
--- /dev/null
+++ b/tests/kakuro/TestPlayerDao.java
@@ -0,0 +1,128 @@
+package kakuro;
+
+import kakuro.player.dao.*;
+
+import kakuro.utils.DatabaseConnection;
+
+import static org.junit.Assert.assertEquals;
+
+import java.sql.SQLException;
+
+import org.junit.Test;
+
+public class TestPlayerDao {
+
+
+ @Test
+ public void testRegisterAlreadyRegistered() {
+
+ DatabaseConnection db = new DatabaseConnection(); //has hard coded player "TestPlayer" password: "123" already in db
+ db.connect();
+
+ String name = "TestPlayer";
+ String password = "123";
+
+ PlayerDaoImpl playerDoa = new PlayerDaoImpl();
+
+ boolean registered = true;
+
+ try {
+
+ registered = playerDoa.register(db.getConnection(),name,password);
+
+ } catch (SQLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ assertEquals(registered,false);
+
+ db.disconnect();
+ }
+
+
+ // TODO change once can unregister a user
+// @Test
+// public void testRegisterFirstTime() {
+//
+// DatabaseConnection db = new DatabaseConnection();
+// db.connect();
+//
+// String name = "Player1"; //new user
+// String password = "password1";
+//
+// PlayerDaoImpl playerDoa = new PlayerDaoImpl();
+//
+// boolean registered = false;
+//
+// try {
+//
+// registered = playerDoa.register(db.getConnection(),name,password);
+//
+// } catch (SQLException e) {
+// e.printStackTrace();
+// }
+//
+// assertEquals(registered,true);
+//
+// db.disconnect();
+// }
+
+ @Test
+ public void testLoginRegistered() {
+
+ DatabaseConnection db = new DatabaseConnection();
+ db.connect();
+
+ String name = "TestPlayer"; //hard coded in table
+ String password = "123";
+
+ PlayerDaoImpl playerDoa = new PlayerDaoImpl();
+
+ boolean login = false;
+
+ try {
+
+ login = playerDoa.login(db.getConnection(),name,password);
+
+ } catch (SQLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ assertEquals(login,true);
+
+ db.disconnect();
+ }
+
+ @Test
+ public void testLoginNotRegistered() {
+
+ DatabaseConnection db = new DatabaseConnection();
+ db.connect();
+
+ String name = "NotInDB"; //hard coded in table
+ String password = "NOPass";
+
+ PlayerDaoImpl playerDoa = new PlayerDaoImpl();
+
+ boolean login = true;
+
+ try {
+
+ login = playerDoa.login(db.getConnection(),name,password);
+
+ } catch (SQLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ assertEquals(login,false);
+
+ db.disconnect();
+ }
+
+
+
+
+}
diff --git a/tests/kakuro/TestPlayerModel.java b/tests/kakuro/TestPlayerModel.java
new file mode 100644
index 0000000..b13b4cd
--- /dev/null
+++ b/tests/kakuro/TestPlayerModel.java
@@ -0,0 +1,53 @@
+package kakuro;
+
+import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+import kakuro.PlayerModel;
+
+//iteration 3
+
+public class TestPlayerModel {
+
+
+ @Test
+ public void testConstructor() {
+
+ String trueUsername = "name1";
+ String truePassword = "password1";
+
+ PlayerModel plaMod = new PlayerModel(trueUsername, truePassword);
+
+ assertEquals(plaMod.getPlayerUsername(),trueUsername);
+ assertEquals(plaMod.getPlayerPassword(),truePassword);
+
+ }
+
+ @Test
+ public void testGetterMethod() {
+
+ PlayerModel plaMod = new PlayerModel("name1", "password1");
+
+ assertEquals(plaMod.getPlayerUsername(),"name1");
+ assertEquals(plaMod.getPlayerPassword(),"password1");
+
+ }
+
+ @Test
+ public void testSetterMethod() {
+
+ PlayerModel plaMod = new PlayerModel("dummyName", "dummyPassword");
+
+ String newName = "name1";
+ String newPassword = "password1";
+
+ plaMod.setPlayerUsername(newName);
+ plaMod.setPlayerPassword(newPassword);
+
+ assertEquals(plaMod.getPlayerUsername(),newName);
+ assertEquals(plaMod.getPlayerPassword(),newPassword);
+
+ }
+
+
+
+}