Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
1 result

Target

Select target project
  • pm3g19/mathdoku
1 result
Select Git revision
  • master
1 result
Show changes
Commits on Source (2)
Showing
with 176 additions and 134 deletions
package com.patryk.mathdoku;
public class ArrayConversions {
public static float[] toNative(Float[] arr) {
float[] out = new float[arr.length];
for(int i = 0; i < arr.length; ++i) {
out[i] = arr[i];
}
return out;
}
public static int[] toNative(Object[] arr) {
int[] out = new int[arr.length];
for(int i = 0; i < arr.length; ++i) {
out[i] = (Integer)arr[i];
}
return out;
}
public static double[] toDouble(float[] arr) {
double[] out = new double[arr.length];
for(int i = 0; i < arr.length; ++i) {
out[i] = arr[i];
}
return out;
}
public static Float[] fromNative(float[] arr) {
Float[] out = new Float[arr.length];
for(int i = 0; i < arr.length; ++i) {
out[i] = arr[i];
}
return out;
}
}
File mode changed from 100644 to 100755
package com.patryk.mathdoku;
import com.patryk.mathdoku.actions.*;
import com.patryk.mathdoku.cageData.CageData;
import com.patryk.mathdoku.cageData.DataFormatException;
import com.patryk.mathdoku.errorChecking.UserErrorChecker;
import com.patryk.mathdoku.util.BoardPosVec;
/**
* Represents one status of the playing of the game e.g. the cage data, what the user has entered
*/
public class GameContext {
private int boardWidth;
private CageData cageData;
private UserData userData;
private UserErrorChecker userErrorChecker;
StackActionRecorder<Action> actionRecorder;
public StackActionRecorder<Action> getActionRecorder() {
return actionRecorder;
}
UserData.ChangeListener updateErrorState = (UserData.ChangeListener.ChangeData changeData) -> {
userErrorChecker.onGridChange(changeData);
};
public GameContext(String data) throws DataFormatException {
cageData = new CageData(data);
this.boardWidth = cageData.getWidth();
BoardPosVec.setBoardWidth(boardWidth);
Action.setGameContext(this);
userData = new UserData(boardWidth);
actionRecorder = new StackActionRecorder<>(5);
//userData.fill();
userErrorChecker = new UserErrorChecker(boardWidth, userData, cageData);
userData.addChangeListener(updateErrorState);
}
public CageData getCageData() {
return cageData;
}
public UserData getUserData() {
return userData;
}
public void setValueAtCell(BoardPosVec cell, int value, boolean record ) {
int oldValue = userData.getValueAtCell(cell);
userData.setValueAtCell(cell, value);
if (record)
actionRecorder.record(new CellValueChangeAction(cell, oldValue, value));
}
public void clear(boolean record) {
UserData copy = UserData.move(userData);
if(record) {
actionRecorder.record(new ClearAction(copy));
}
}
public int getBoardWidth() {
return boardWidth;
}
public void undo() {
actionRecorder.undo().undo();
}
public void redo() {
actionRecorder.redo().redo();
UndoRedoButtonManager.me().onRedoButtonPressed();
}
public UserErrorChecker getErrorChecker() {
return userErrorChecker;
}
public boolean isWon() {
if (userData.isFull()) {
if (userErrorChecker.noErrors()) {
return true;
}
}
return false;
}
}
\ No newline at end of file
package com.patryk.mathdoku; package com.patryk.mathdoku;
import com.patryk.mathdoku.cageData.Cage;
import com.patryk.mathdoku.cageData.CageData;
import com.patryk.mathdoku.cageData.DataFormatException; import com.patryk.mathdoku.cageData.DataFormatException;
import com.patryk.mathdoku.solver.CageSolver;
import com.patryk.mathdoku.gui.GameUI; import com.patryk.mathdoku.gui.GameUI;
import com.patryk.mathdoku.gui.ManualGameInputDialog; import com.patryk.mathdoku.gui.ManualGameInputDialog;
import com.patryk.mathdoku.gui.RandGameDialog;
import com.patryk.mathdoku.randomGame.RandomGame;
import com.patryk.mathdoku.solver.Solver;
import javafx.application.Application; import javafx.application.Application;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
...@@ -16,7 +22,9 @@ import java.io.File; ...@@ -16,7 +22,9 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Random;
public class MathDoku extends Application { public class MathDoku extends Application {
GameUI gameUI; GameUI gameUI;
...@@ -65,6 +73,29 @@ public class MathDoku extends Application { ...@@ -65,6 +73,29 @@ public class MathDoku extends Application {
}; };
EventHandler<ActionEvent> onRanGameButtonPressed = (event) -> {
RandGameDialog dialog = new RandGameDialog();
Optional<RandGameDialog.RanGameInfo> result = dialog.showAndWait();
if (result.isPresent()) {
int seed;
seed = result.get().seed;
int size = result.get().width;
CageData cageData = RandomGame.randomGame(seed, size);
setGameContext(new GameContext(cageData));
}
};
private void onSolveButtonPressed(ActionEvent event) {
gameContext.userData.clear();
gameUI.wonBefore = true;
Solver.solve(gameContext.getUserData(), gameContext.getCageData());
}
//key event handler //key event handler
...@@ -124,6 +155,8 @@ public class MathDoku extends Application { ...@@ -124,6 +155,8 @@ public class MathDoku extends Application {
gameUI.checkButton.addEventHandler(ActionEvent.ANY,(event) -> gameUI.gameGridView.showErrors()); gameUI.checkButton.addEventHandler(ActionEvent.ANY,(event) -> gameUI.gameGridView.showErrors());
gameUI.fileLoadButton.addEventHandler(ActionEvent.ANY, onFileLoadButtonPressed); gameUI.fileLoadButton.addEventHandler(ActionEvent.ANY, onFileLoadButtonPressed);
gameUI.textLoadButton.addEventHandler(ActionEvent.ANY, onManualInputButtonPressed); gameUI.textLoadButton.addEventHandler(ActionEvent.ANY, onManualInputButtonPressed);
gameUI.ranGameButton.addEventHandler(ActionEvent.ANY, onRanGameButtonPressed);
gameUI.solveButton.addEventHandler(ActionEvent.ANY, this::onSolveButtonPressed);
gameUI.setNumberButtonCallback((digit) -> gameUI.gameGridView.getInputHandler().injectNumberKey(digit)); gameUI.setNumberButtonCallback((digit) -> gameUI.gameGridView.getInputHandler().injectNumberKey(digit));
...@@ -136,13 +169,15 @@ public class MathDoku extends Application { ...@@ -136,13 +169,15 @@ public class MathDoku extends Application {
} }
public boolean wantsToExit() { public boolean wantsToExit() {
if (shouldDisplayExitDialog()) { //todo debug
//show confirmation dialog
return gameUI.showConfirmExitDialog();
}
return true; return true;
// if (shouldDisplayExitDialog()) {
// //show confirmation dialog
//
//
// return gameUI.showConfirmExitDialog();
// }
// return true;
} }
public boolean shouldDisplayExitDialog() { public boolean shouldDisplayExitDialog() {
......
...@@ -5,9 +5,7 @@ import com.patryk.mathdoku.util.BoardPosVec; ...@@ -5,9 +5,7 @@ import com.patryk.mathdoku.util.BoardPosVec;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.*;
import java.util.LinkedList;
import java.util.List;
public class UserData { public class UserData {
public interface ChangeListener { public interface ChangeListener {
...@@ -98,6 +96,10 @@ public class UserData { ...@@ -98,6 +96,10 @@ public class UserData {
notifyListener(new ChangeListener.MultipleCellChange(false)); notifyListener(new ChangeListener.MultipleCellChange(false));
} }
public int getBoardWidth() {
return boardWidth;
}
public int getPopulationCount() { public int getPopulationCount() {
return populationCount; return populationCount;
} }
...@@ -122,17 +124,20 @@ public class UserData { ...@@ -122,17 +124,20 @@ public class UserData {
public void setValueAtCell(BoardPosVec cell, int value) { public void setValueAtCell(BoardPosVec cell, int value) {
int oldValue = data[cell.toIndex()]; setValueAtCell(cell.toIndex(), value);
}
public void setValueAtCell(int index, int value) {
int oldValue = data[index];
if (oldValue != value) { if (oldValue != value) {
data[cell.toIndex()] = value; data[index] = value;
if (oldValue == 0) if (oldValue == 0)
populationCount++; populationCount++;
if (value == 0) { if (value == 0) {
populationCount--; populationCount--;
} }
notifyListener(new ChangeListener.SingleCellChange(cell, oldValue, value)); notifyListener(new ChangeListener.SingleCellChange(new BoardPosVec(index), oldValue, value));
} }
} }
...@@ -167,6 +172,28 @@ public class UserData { ...@@ -167,6 +172,28 @@ public class UserData {
return builder.toString(); return builder.toString();
} }
public Set<Integer> getAllowedDigits(int index) {
BoardPosVec posVec = new BoardPosVec(index);
int r = posVec.r;
int c = posVec.c;
Set<Integer> allDigits = new HashSet<>();
for(int i = 1; i <= boardWidth; i++) {
allDigits.add(i);
}
//remove row digits
for (int vcol = 0; vcol < c; vcol++) {
allDigits.remove(Integer.valueOf(getValueAtCell(new BoardPosVec(r, vcol))));
}
//remove column digits
for (int vrow = 0; vrow < r; vrow++) {
allDigits.remove(Integer.valueOf(getValueAtCell(new BoardPosVec(vrow, c))));
}
return allDigits;
}
public void fill() { public void fill() {
for (int i = 0; i < fullSize;i ++) { for (int i = 0; i < fullSize;i ++) {
int val = (int)( 5 * Math.random()) + 1; int val = (int)( 5 * Math.random()) + 1;
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
package com.patryk.mathdoku.cageData; package com.patryk.mathdoku.cageData;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Cage { public class Cage {
int target; int target;
Cage.Operator operator; Cage.Operator operator = Operator.ADD;
int markedCell; int markedCell = 0;
private final List<Integer> memberCells; private List<Integer> memberCells = new ArrayList<>();
public Cage() {}
public Cage(int target, Cage.Operator operator, int markedCell, List<Integer> memberCells) { public Cage(int target, Cage.Operator operator, int markedCell, List<Integer> memberCells) {
this.target = target; this.target = target;
...@@ -39,6 +42,18 @@ public class Cage { ...@@ -39,6 +42,18 @@ public class Cage {
return s; return s;
} }
public void setTarget(int target) {
this.target = target;
}
public void setOperator(Operator operator) {
this.operator = operator;
}
public void setMemberCells(List<Integer> memberCells) {
this.memberCells = memberCells;
}
public enum Operator { public enum Operator {
ADD('+'), SUBTRACT('-'), MULTIPLY('x'), DIVIDE('÷'); ADD('+'), SUBTRACT('-'), MULTIPLY('x'), DIVIDE('÷');
...@@ -72,7 +87,7 @@ public class Cage { ...@@ -72,7 +87,7 @@ public class Cage {
case MULTIPLY: case MULTIPLY:
return operand1 * operand2; return operand1 * operand2;
case DIVIDE: case DIVIDE:
return Math.floorDiv(operand1, operand2); return Math.max(1, Math.floorDiv(operand1, operand2));
default: default:
return -1; return -1;
} }
......
...@@ -8,18 +8,34 @@ import java.util.*; ...@@ -8,18 +8,34 @@ import java.util.*;
public class CageData { public class CageData {
int width; int width;
Cell[] data; Cell[] data;
public List<Cage> getCageList() {
return cageList;
}
List<Cage> cageList = new ArrayList<Cage>(); List<Cage> cageList = new ArrayList<Cage>();
int cageCount; int cageCount;
public CageData(int width) {
this.width = width;
data = new Cell[width * width];
for (int i = 0; i < width * width; i++) {
data[i] = new Cell(-1);
}
this.cageList = new ArrayList<>();
}
public CageData(String data) throws DataFormatException{ public CageData(String data) throws DataFormatException{
parseData(data); parseData(data);
} }
public Cell[] getData() {
return data;
}
public List<Cage> getCages() {return cageList; } public List<Cage> getCages() {return cageList; }
public boolean cellConnectsTo(BoardPosVec pos, Direction direction) { public boolean cellConnectsTo(BoardPosVec pos, Direction direction) {
Cell nextCell = getCellAt(pos.add(direction.vector)); Cell nextCell = getCellAt(pos.add(direction.vector));
...@@ -45,7 +61,7 @@ public class CageData { ...@@ -45,7 +61,7 @@ public class CageData {
if (size > 81) if (size > 81)
throw new DataFormatException(-1, "Too many cells! Max is 81."); throw new DataFormatException(-1, "Too many cells! Max is 81.");
initData(); initData();
cageCount = parser.parseData(new Scanner(rawData), data, cageList); parser.parseData(new Scanner(rawData), data, cageList);
} }
...@@ -71,17 +87,11 @@ public class CageData { ...@@ -71,17 +87,11 @@ public class CageData {
} }
public int getCageCount() { public int getCageCount() {
return cageCount; return cageList.size();
} }
}
class Cell {
private int cageID;
public Cell (int cageID) { public void setCageList(List<Cage> cageList) {
this.cageID = cageID; this.cageList = cageList;
} }
public int getCageId() {return cageID; }
} }
File mode changed from 100644 to 100755
package com.patryk.mathdoku.cageData;
public class Cell {
private int cageID;
public Cell(int cageID) {
this.cageID = cageID;
}
public int getCageId() {
return cageID;
}
public void setCageId(int cageID) {
this.cageID = cageID;
}
}
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
...@@ -45,7 +45,7 @@ public class CageInfo { ...@@ -45,7 +45,7 @@ public class CageInfo {
populationCount++; populationCount++;
if (isFull()) { if (isFull()) {
int[] cageMemberData = getMembersOfCage(); int[] cageMemberData = getMembersOfCage();
isInvalid = !RecursiveSolver.testSign(cageMemberData, cage.getTarget(), cage.getOperator()); isInvalid = !RecursiveChecker.testSign(cageMemberData, cage.getTarget(), cage.getOperator());
} }
} }
......