Skip to content
Snippets Groups Projects
Commit 134b1b2f authored by pm3g19's avatar pm3g19
Browse files

Deleted/untracked unneeded files.

parent a1d69e4b
No related branches found
No related tags found
No related merge requests found
public class CircularActionRecorder<E> implements ActionRecorderBase<E> {
private final int maxSize = 3;
E[] actions = new E[maxSize];
int head = 0; // the actual index in the internal array reflecting the beginning of the list
int size = 0; // total number of elements, both undoable and redoable (max as specified)
int nRedoable = 0;
@Override
public void record(E action) {
if (size == maxSize) {
head = loopForward(head,1);
size--;
}
size -= nRedoable;
actions[loopForward(head, size)] = action;
size++;
}
@Override
public E undo() {
assert (canUndo());
E a = actions[loopForward(head, size - 1)];
nRedoable++;
return a;
}
@Override
public E redo() {
assert (canRedo());
E a = actions[loopForward(head, size - 1)];
nRedoable++;
return a;
}
@Override
public boolean canUndo() {
return (size != nRedoable);
}
@Override
public boolean canRedo() {
return nRedoable != 0;
}
}
//a singleton class
public class GameContext {
private int canvasPixelWidth;
private int boardWidth;
private int squarePixelWidth;
private static GameContext instance;
private GameContext(int canvasPixelWidth, int boardWidth) {
this.canvasPixelWidth = canvasPixelWidth;
this.boardWidth = boardWidth;
squarePixelWidth = canvasPixelWidth / boardWidth;
}
public static void init(int canvasPixelWidth, int boardWidth) {
instance = new GameContext(canvasPixelWidth, boardWidth);
}
public static GameContext getInstance() throws NoContextException{
if (instance == null) {
throw new NoContextException();
}
return instance;
}
public int getCanvasPixelWidth() {
return canvasPixelWidth;
}
public int getBoardWidth() {
return boardWidth;
}
public int getSquarePixelWidth() {
return squarePixelWidth;
}
}
class NoContextException extends RuntimeException{
NoContextException() {
super("You must first create a game context before any resource tries to obtain it.");
}
}
package com.patryk.mathdoku.cage;
import com.patryk.mathdoku.global.BoardPosVec;
import com.patryk.mathdoku.UserData;
import java.util.BitSet;
public class Generator {
public interface BoardReadyFunc {
void onBoardReady(UserData board);
}
private BitSet[] colForbidden;
private BitSet[] rowForbidden;
private int size;
private UserData data;
private BoardReadyFunc boardReadyFunc;
private static BitSet[] getBitsSetArray(int size) {
BitSet[] a = new BitSet[size];
for (int i = 0; i < size; i++) {
a[i] = new BitSet(size);
}
return a;
}
private Generator(int size, BoardReadyFunc boardReadyFunc) {
this.size = size;
this.data = new UserData(size);
this.boardReadyFunc = boardReadyFunc;
colForbidden = getBitsSetArray(size);
rowForbidden = getBitsSetArray(size);
}
public static void perform(int size, BoardReadyFunc boardReadyFunc) {
new Generator(size, boardReadyFunc).f(BoardPosVec.zero());
}
private void f(BoardPosVec cell) {
//get bit vector of forbidden values for that row and column
BitSet forbiddenNums = new BitSet();
forbiddenNums.or(colForbidden[cell.c]);
forbiddenNums.or(rowForbidden[cell.r]);
for (int i = 1; i <= size; i++) {
//if that value is not forbidden
if (!forbiddenNums.get(i)) {
//give current cell that number
data.setValueAtCell(cell, i);
if (cell.isLast()) {
boardReadyFunc.onBoardReady(data);
return;
}
//make this number forbidden in rows and columns
colForbidden[cell.c].set(i, true);
rowForbidden[cell.r].set(i, true);
//go to next cell and recursively do the same
f(cell.next());
//after that is done, make this number no longer forbidden
colForbidden[cell.c].set(i, false);
rowForbidden[cell.r].set(i, false);
//assert(forbiddenNums.equals(colForbidden[cell.c].or(rowForbidden[cell.r])));
//but don't remove the number; it will be ignored and later overwritten anyway
}
}
}
}
\ No newline at end of file
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class MathDoku extends Application {
private static int canvasPixelWidth = 600;
private static int boardWidth = 6;
UI ui;
private InitialData initialData;
private UserData userData;
public static ActionRecorderBase<Action> actionRecorder = new CircularActionRecorder();
public static int getCanvasPixelWidth() {
return canvasPixelWidth;
}
public static int getBoardWidth() {
return boardWidth;
}
public static int getSquarePixelWidth() {
return canvasPixelWidth / boardWidth;
}
@Override
public void start(Stage primaryStage) throws Exception{
//GameContext.init(canvasPixelWidth, boardWidth);
//create the canvas and put it in the window
primaryStage.setTitle("Mathdoku");
//create the constant game data
initialData = new InitialData("example.txt");
userData = new UserData();
ui = new UI(initialData, userData);
markedCellProperty.addListener(new ChangeListener<Util.BoardPosVec>() {
@Override
public void changed(ObservableValue<? extends Util.BoardPosVec> observableValue, Util.BoardPosVec oldPos, Util.BoardPosVec newPos) {
redrawMarkedCell();
}
});
ui.getScene().setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
switch (keyEvent.g)
});
ui.undoButton.addEventHandler(ActionEvent.ANY, new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
undo();
}
});
ui.redoButton.addEventHandler(ActionEvent.ANY, new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
redo();
}
});
ui.clearButton.addEventHandler(ActionEvent.ANY, new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
clear();
}
});
userDataDrawer.draw();
primaryStage.setScene(ui.getScene());
primaryStage.show();
}
/**
* also checks if the digit is in range
* @param cell
* @param digit
*/
private void appendDigitToCell(Util.BoardPosVec cell, char digit) {
int newValue = Util.charToInt(digit);
if (newValue <= GameContext.getInstance().getBoardWidth())
setValueAtCell(cell, newValue);
}
private Util.BoardPosVec getSelectedCell() {
return this.markedCellProperty.get();
}
private void redrawMarkedCell() {
//TODO set colours, line width
//clear canvas
//re-draw the marked cell
//drawSquare(markedCellCanvas.getGraphicsContext(), markedCellProperty.getValue());
selectedCellDrawer.draw();
}
private void setSelectedCell(Util.BoardPosVec pos) {
//Util.BoardPosVec newValue = markedCellProperty.get().add(direction.vector);
//if change has occured to the value
if (!pos.clampToArea()) {
markedCellProperty.set(pos);
}
}
private void undo() {
if (actionRecorder.canUndo()) {
Action action = actionRecorder.undo();
if (action.isFlipped()) {
redoAction(action);
} else {
undoAction(action);
}
}
}
private void redo() {
if(actionRecorder.canRedo()) {
Action action = actionRecorder.redo();
if (action.isFlipped()) {
undoAction(action);
} else {
redoAction(action);
}
}
}
public void redoAction(Action action) {
switch (action.getType()) {
case CHANGE_CELL_VALUE:
redoSpecificAction((CellValueChangeAction) action);
break;
default:
} }
public void undoAction(Action action) {
switch (action.getType()) {
case CHANGE_CELL_VALUE:
undoSpecificAction((CellValueChangeAction) action);
break;
default:
}
}
private void undoSpecificAction(CellValueChangeAction action) {
userData.setValueAtCell(action.getCell(), action.getOldValue());
userDataDrawer.draw();
}
public void redoSpecificAction(CellValueChangeAction action) {
userData.setValueAtCell(action.getCell(), action.getNewValue());
userDataDrawer.draw();
}
public static void main(String[] args) {
launch(args);
}
}
public class SampleAction extends Action {
String message;
public SampleAction(String message) {
super(Type.SAMPLE);
this.message = message;
}
@Override
public String toString() {
return message;
}
}
public class TestMain {
public static void main(String[] args) {
LimitedStack stack = new LimitedStack(3);
stack.push(new SampleAction("aardvark"));
stack.push(new SampleAction("pig"));
stack.push(new SampleAction("dog"));
stack.push(new SampleAction("sheep"));
stack.push(new SampleAction("wolf"));
stack.push(new SampleAction("horse"));
Action temp = stack.pop();
temp = stack.pop();
temp = stack.pop();
}
}
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug (Launch) - Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "Debug (Launch)-Permutations<trivial_ee8fd3d>",
"request": "launch",
"mainClass": "Permutations",
"projectName": "trivial_ee8fd3d"
}
]
}
\ No newline at end of file
{
"python.pythonPath": "/sbin/python3.8"
}
\ No newline at end of file
import java.util.BitSet;
public class CageGenerator {
public interface BoardReadyFunc {
void onBoardReady(UserData board);
}
private BitSet[] colForbidden;
private BitSet[] rowForbidden;
private int size;
private UserData data;
private BoardReadyFunc boardReadyFunc;
public void perform(UserData data, BoardReadyFunc boardReadyFunc) {
this.size = data.size;
this.data = data;
this.boardReadyFunc = boardReadyFunc;
colForbidden = new BitSet[size];
rowForbidden = new BitSet[size];
f(Util.BoardPosVec.zero());
}
private void f(Util.BoardPosVec cell) {
//get bit vector of forbidden values for that row and column
BitSet forbiddenNums = colForbidden[cell.c].or(rowForbidden[cell.r]);
for (int i = 1; i <= size; i++) {
//if that value is not forbidden
if (!forbiddenNums.get(i)) {
//give current cell that number
data.setValueAtCell(cell, i);
if (cell.isLast()) {
boardReadyFunc.onBoardReady(data);
return;
}
//make this number forbidden in rows and columns
colForbidden[cell.c].set(i, true);
rowForbidden[cell.r].set(i, true);
//go to next cell and recursively do the same
f(cell.next());
//after that is done, make this number no longer forbidden
colForbidden[cell.c].set(i, false);
rowForbidden[cell.r].set(i, false);
assert(forbiddenNums.equals(colForbidden[cell.c].or(rowForbidden[cell.r])));
//but don't remove the number; it will be ignored and later overwritten anyway
}
}
}
}
\ No newline at end of file
File deleted
File deleted
File deleted
public class Permutations {
public enum Sign {PLUS, MINUS, MULTIPLY, DIVIDE};
//public boolean fits(int target, int[] nums) {
/*checks if target can be formed by applying op1 with sign sign to any integer in the list or by also
additionally recursively applying each result to the remainders in the list.
*/
private int operation(int operand1, int operand2, Sign operator) {
switch (operator) {
case PLUS:
return operand1 + operand2;
case MINUS:
return operand1 - operand2;
case MULTIPLY:
return operand1 * operand2;
case DIVIDE:
return Math.floorDiv(operand1, operand2);
default:
return -1;
}
}
boolean permute;
Sign operator;
int target = 240;
private boolean f(int result,int[] cageList, int startIndex, int depth) {
//System.out.println("Analyzing " + result);
if (result == target)
return true;
for (int i = startIndex; i < cageList.length; i++) {
if (cageList[i] != -1) {
int e = cageList[i];
int newResult = depth == 0 ? e : operation(result, e, operator);
//int newResult = e;
if (operator == Sign.DIVIDE && result % e != 0) {
//System.out.println(e + " Not divisible so ignoring");
continue;
}
cageList[i] = -1;
boolean outcome = f(newResult, cageList, permute ? 0 : i + 1, depth + 1);
cageList[i] = e;
if (outcome)
return true;
}
}
//System.out.println("Done analyzing " + result);
return false;
}
//!!!not thread safe!!!
public boolean testSign(int[] cageList, int target, Sign operator) {
this.target = target;
this.operator = operator;
this.permute = (operator == Sign.MINUS || operator == Sign.DIVIDE);
return f(0, cageList, 0, 0);
}
public static void main(String[] args) {
System.out.println( new Permutations().testSign(new int[]{9, 1, 2, 3}, 8, Sign.MINUS));
}
}
\ No newline at end of file
outSet = set()
runs = 0
def testAdd(cage : list, start: int, permute = False):
global runs
if (len(cage) == 0):
print()
return
for i, e in enumerate(cage):
result = start + e
runs+=1
print(e, end=' ')
#outSet.add(result)
if permute:
newList = cage[:i] + cage[i + 1:]
else:
newList = cage[i + 1:]
testAdd(newList, result)
permute = True
operator = lambda x, y: x / y
def f(result, cageList, startindex = 0):
print("Analyzing", result)
for i, e in enumerate(cageList):
if startindex == 0:
newResult = e
else:
newResult = operator(result, e)
if permute:
newList = cageList[:i] + cageList[i + 1:]
else:
newList = cageList[i + 1:]
f(newResult, newList, 1)
print("Done analyzing", result)
f(0, [6, 1, 12])
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment