Merged. Also Added an Enum for Button Types.
Extended the GameController with additional functions for the view.
This commit is contained in:
parent
163e64810e
commit
54b9de3e22
13 changed files with 231 additions and 167 deletions
|
@ -7,8 +7,8 @@ import java.util.List;
|
|||
|
||||
import tu_darmstadt.sudoku.game.CellConflict;
|
||||
import tu_darmstadt.sudoku.game.CellConflictList;
|
||||
import tu_darmstadt.sudoku.game.GameBoard;
|
||||
import tu_darmstadt.sudoku.game.GameCell;
|
||||
import tu_darmstadt.sudoku.game.GameField;
|
||||
import tu_darmstadt.sudoku.game.GameType;
|
||||
import tu_darmstadt.sudoku.game.solver.Solver;
|
||||
import tu_darmstadt.sudoku.game.solver.ISolver;
|
||||
|
@ -21,7 +21,7 @@ public class GameController {
|
|||
private int size;
|
||||
private int sectionHeight;
|
||||
private int sectionWidth;
|
||||
private GameField gameField;
|
||||
private GameBoard gameBoard;
|
||||
private ISolver solver;
|
||||
private GameType gameType;
|
||||
private int selectedRow;
|
||||
|
@ -42,31 +42,18 @@ public class GameController {
|
|||
}
|
||||
|
||||
public GameController(GameType type, SharedPreferences pref) {
|
||||
this.gameType = type;
|
||||
setGameType(type);
|
||||
gameField = new GameField(size, sectionHeight, sectionWidth);
|
||||
gameBoard = new GameBoard(size, sectionHeight, sectionWidth);
|
||||
setSettings(pref);
|
||||
setValue(0, 1, 8); setValue(0, 4, 2);
|
||||
setValue(0, 5, 6); setValue(0, 6, 7);
|
||||
setValue(0, 7, 3); setValue(0, 8, 4);
|
||||
setNote(6, 3, 1); setNote(6, 3, 2); setNote(6, 3, 3); setNote(6, 3, 4);
|
||||
setNote(6, 3, 5); setNote(6, 3, 6); setNote(6, 3, 7); setNote(6, 3, 8);
|
||||
setNote(6, 3, 9);
|
||||
|
||||
setNote(7, 3, 2); setNote(7, 3, 3); setNote(7, 3, 4);
|
||||
setNote(7, 3, 5); setNote(7, 3, 7); setNote(7, 3, 8);
|
||||
setNote(7, 3, 9);
|
||||
}
|
||||
|
||||
public void loadLevel(int size, int sectionHeight, int sectionWidth, int[] fixedValues, int[] setValues, int[][] setNotes) {
|
||||
this.size = size;
|
||||
this.sectionHeight = sectionHeight;
|
||||
this.sectionWidth = sectionWidth;
|
||||
this.gameField = new GameField(size, sectionHeight, sectionWidth);
|
||||
public void loadLevel(GameType type, int[] fixedValues, int[] setValues, int[][] setNotes) {
|
||||
setGameType(type);
|
||||
this.gameBoard = new GameBoard(size, sectionHeight, sectionWidth);
|
||||
|
||||
if(fixedValues == null) throw new IllegalArgumentException("fixedValues may not be null.");
|
||||
|
||||
gameField.initCells(fixedValues);
|
||||
gameBoard.initCells(fixedValues);
|
||||
|
||||
// now set the values that are not fixed
|
||||
if (setValues != null) {
|
||||
|
@ -87,30 +74,31 @@ public class GameController {
|
|||
settings = pref;
|
||||
}
|
||||
|
||||
private GameField solve(GameField gameField) {
|
||||
public GameBoard solve(GameBoard gameBoard) {
|
||||
switch(gameType) {
|
||||
case Default_9x9:
|
||||
case Default_6x6:
|
||||
case Default_12x12:
|
||||
solver = new Solver(gameField);
|
||||
solver = new Solver(gameBoard);
|
||||
break;
|
||||
default:
|
||||
throw new UnsupportedOperationException("No Solver for this GameType defined.");
|
||||
}
|
||||
|
||||
if(solver.solve()) {
|
||||
return solver.getGameField();
|
||||
return solver.getGameBoard();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/*public boolean loadLevel(GameField level) {
|
||||
if(GameField.isValid(level)) {
|
||||
gameField = level;
|
||||
/*public boolean loadLevel(GameBoard level) {
|
||||
if(GameBoard.isValid(level)) {
|
||||
gameBoard = level;
|
||||
}
|
||||
}*/
|
||||
|
||||
private void setGameType(GameType type) {
|
||||
this.gameType = type;
|
||||
switch(type) {
|
||||
case Default_9x9:
|
||||
this.size = 9;
|
||||
|
@ -139,24 +127,25 @@ public class GameController {
|
|||
/** Use with care.
|
||||
*/
|
||||
public GameCell getGameCell(int row, int col) {
|
||||
return gameField.getCell(row, col);
|
||||
return gameBoard.getCell(row, col);
|
||||
}
|
||||
|
||||
public boolean isSolved() {
|
||||
return gameField.isSolved(new LinkedList<CellConflict>());
|
||||
errorList = new CellConflictList();
|
||||
return gameBoard.isSolved(errorList);
|
||||
}
|
||||
|
||||
public void setValue(int row, int col, int value) {
|
||||
GameCell cell = gameField.getCell(row, col);
|
||||
GameCell cell = gameBoard.getCell(row, col);
|
||||
if (!cell.isFixed() && isValidNumber(value)) {
|
||||
cell.deleteNotes();
|
||||
cell.setValue(value);
|
||||
|
||||
if(settings != null && settings.getBoolean("pref_automatic_note_deletion",true)) {
|
||||
LinkedList<GameCell> updateList = new LinkedList<GameCell>();
|
||||
updateList.addAll(gameField.getRow(cell.getRow()));
|
||||
updateList.addAll(gameField.getColumn(cell.getCol()));
|
||||
updateList.addAll(gameField.getSection(cell.getRow(), cell.getCol()));
|
||||
updateList.addAll(gameBoard.getRow(cell.getRow()));
|
||||
updateList.addAll(gameBoard.getColumn(cell.getCol()));
|
||||
updateList.addAll(gameBoard.getSection(cell.getRow(), cell.getCol()));
|
||||
deleteNotes(updateList, value);
|
||||
}
|
||||
}
|
||||
|
@ -165,12 +154,12 @@ public class GameController {
|
|||
public LinkedList<GameCell> getConnectedCells(int row, int col, boolean connectedRow, boolean connectedCol, boolean connectedSec) {
|
||||
LinkedList<GameCell> list = new LinkedList<>();
|
||||
|
||||
if(connectedRow) list.addAll(gameField.getRow(row));
|
||||
list.remove(gameField.getCell(row, col));
|
||||
if(connectedCol) list.addAll(gameField.getColumn(col));
|
||||
list.remove(gameField.getCell(row, col));
|
||||
if(connectedSec) list.addAll(gameField.getSection(row, col));
|
||||
list.remove(gameField.getCell(row, col));
|
||||
if(connectedRow) list.addAll(gameBoard.getRow(row));
|
||||
list.remove(gameBoard.getCell(row, col));
|
||||
if(connectedCol) list.addAll(gameBoard.getColumn(col));
|
||||
list.remove(gameBoard.getCell(row, col));
|
||||
if(connectedSec) list.addAll(gameBoard.getSection(row, col));
|
||||
list.remove(gameBoard.getCell(row, col));
|
||||
|
||||
return list;
|
||||
}
|
||||
|
@ -182,7 +171,7 @@ public class GameController {
|
|||
}
|
||||
|
||||
public int getValue(int row, int col) {
|
||||
GameCell cell = gameField.getCell(row, col);
|
||||
GameCell cell = gameBoard.getCell(row, col);
|
||||
return cell.getValue();
|
||||
}
|
||||
|
||||
|
@ -205,12 +194,12 @@ public class GameController {
|
|||
}
|
||||
|
||||
public void resetLevel() {
|
||||
gameField.reset();
|
||||
gameBoard.reset();
|
||||
//notifyListeners();
|
||||
}
|
||||
|
||||
public boolean deleteValue(int row, int col) {
|
||||
GameCell c = gameField.getCell(row,col);
|
||||
GameCell c = gameBoard.getCell(row,col);
|
||||
if(!c.isFixed()) {
|
||||
c.setValue(0);
|
||||
//notifyListeners();
|
||||
|
@ -220,24 +209,24 @@ public class GameController {
|
|||
}
|
||||
|
||||
public void setNote(int row, int col, int value) {
|
||||
GameCell c = gameField.getCell(row,col);
|
||||
GameCell c = gameBoard.getCell(row,col);
|
||||
c.setNote(value);
|
||||
//notifyListeners();
|
||||
}
|
||||
|
||||
public boolean[] getNotes(int row, int col) {
|
||||
GameCell c = gameField.getCell(row,col);
|
||||
GameCell c = gameBoard.getCell(row,col);
|
||||
return c.getNotes().clone();
|
||||
}
|
||||
|
||||
public void deleteNote(int row, int col, int value) {
|
||||
GameCell c = gameField.getCell(row,col);
|
||||
GameCell c = gameBoard.getCell(row,col);
|
||||
c.deleteNote(value);
|
||||
//notifyListeners();
|
||||
}
|
||||
|
||||
public void toggleNote(int row, int col, int value) {
|
||||
GameCell c = gameField.getCell(row,col);
|
||||
GameCell c = gameBoard.getCell(row,col);
|
||||
c.toggleNote(value);
|
||||
//notifyListeners();
|
||||
}
|
||||
|
@ -247,7 +236,7 @@ public class GameController {
|
|||
* @return the Field represented as a String
|
||||
*/
|
||||
public String getFieldAsString() {
|
||||
return gameField.toString();
|
||||
return gameBoard.toString();
|
||||
}
|
||||
|
||||
public int getSelectedRow() {
|
||||
|
|
|
@ -1,29 +1,26 @@
|
|||
package tu_darmstadt.sudoku.game;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Christopher Beckmann on 06.11.2015.
|
||||
*/
|
||||
public class GameField implements Cloneable {
|
||||
public class GameBoard implements Cloneable {
|
||||
|
||||
//private int id;
|
||||
private int sectionHeight;
|
||||
private int sectionWidth;
|
||||
//private List additionalSections
|
||||
private CellConflictList errorList = new CellConflictList();
|
||||
private int size;
|
||||
private GameCell[][] field;
|
||||
|
||||
public GameField(int size, int sectionHeight, int sectionWidth) {
|
||||
public GameBoard(int size, int sectionHeight, int sectionWidth) {
|
||||
this.sectionHeight = sectionHeight;
|
||||
this.sectionWidth = sectionWidth;
|
||||
this.size = size;
|
||||
|
||||
field = new GameCell[size][size];
|
||||
initCells(new int[][]{{1}});
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
|
@ -36,29 +33,16 @@ public class GameField implements Cloneable {
|
|||
}, true);
|
||||
}
|
||||
|
||||
public void initCells(int[][] level) {
|
||||
// TODO: this is a placeholder, because we don't have real levels yet.
|
||||
int[][] placeholder = {{ 5, 0, 1, 9, 0, 0, 0, 0, 0 },
|
||||
{ 2, 0, 0, 0, 0, 4, 9, 5, 0 },
|
||||
{ 3, 9, 0, 7, 0, 0, 0, 2, 6 },
|
||||
|
||||
{ 0, 3, 0, 0, 0, 1, 0, 7, 2 },
|
||||
{ 0, 0, 6, 0, 5, 7, 0, 0, 0 },
|
||||
{ 0, 7, 2, 0, 0, 9, 0, 4, 1 },
|
||||
|
||||
{ 0, 0, 0, 0, 7, 0, 4, 0, 9 },
|
||||
{ 6, 4, 0, 0, 0, 0, 0, 0, 0 },
|
||||
{ 7, 0, 0, 0, 1, 0, 3, 0, 5 }};
|
||||
|
||||
/*public void initCells(int[][] level) {
|
||||
// Initit the game field
|
||||
int[] oneDimension = new int[size*size];
|
||||
for(int i = 0; i < size; i++) {
|
||||
for(int j = 0; j < size; j++) {
|
||||
oneDimension[i*size+j] = placeholder[i][j];
|
||||
oneDimension[i*size+j] = level[i][j];
|
||||
}
|
||||
}
|
||||
initCells(oneDimension);
|
||||
}
|
||||
}*/
|
||||
|
||||
public void initCells(int[] level) {
|
||||
int count = 0;
|
||||
|
@ -163,7 +147,6 @@ public class GameField implements Cloneable {
|
|||
*/
|
||||
private boolean checkList(final List<GameCell> list, final List<CellConflict> errorList) {
|
||||
boolean isNothingEmpty = true;
|
||||
CellConflict lastFound = null;
|
||||
|
||||
for(int i = 0; i < list.size(); i++) {
|
||||
for(int j = i + 1; j < list.size(); j++) {
|
||||
|
@ -187,8 +170,8 @@ public class GameField implements Cloneable {
|
|||
}
|
||||
|
||||
@Override
|
||||
public GameField clone() throws CloneNotSupportedException {
|
||||
GameField clone = (GameField) super.clone();
|
||||
public GameBoard clone() throws CloneNotSupportedException {
|
||||
GameBoard clone = (GameBoard) super.clone();
|
||||
|
||||
GameCell[][] cloneField = new GameCell[size][size];
|
||||
for(int i = 0; i < size; i++) {
|
||||
|
@ -205,7 +188,7 @@ public class GameField implements Cloneable {
|
|||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append("[GameField: \n");
|
||||
sb.append("[GameBoard: \n");
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
|
|
@ -6,7 +6,6 @@ package tu_darmstadt.sudoku.game;
|
|||
public enum GameType {
|
||||
Unspecified,
|
||||
Default_9x9,
|
||||
Default_9x6,
|
||||
Default_12x12,
|
||||
Default_6x6
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package tu_darmstadt.sudoku.game.solver;
|
||||
|
||||
import tu_darmstadt.sudoku.game.GameField;
|
||||
import tu_darmstadt.sudoku.game.GameBoard;
|
||||
|
||||
/**
|
||||
* Created by Chris on 11.11.2015.
|
||||
|
@ -11,6 +11,6 @@ public interface ISolver {
|
|||
|
||||
public boolean calculateNextPossibleStep();
|
||||
|
||||
public GameField getGameField();
|
||||
public GameBoard getGameBoard();
|
||||
|
||||
}
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
package tu_darmstadt.sudoku.game.solver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import tu_darmstadt.sudoku.game.CellConflict;
|
||||
import tu_darmstadt.sudoku.game.GameBoard;
|
||||
import tu_darmstadt.sudoku.game.GameCell;
|
||||
import tu_darmstadt.sudoku.game.GameField;
|
||||
import tu_darmstadt.sudoku.game.ICellAction;
|
||||
|
||||
/**
|
||||
|
@ -15,42 +12,42 @@ import tu_darmstadt.sudoku.game.ICellAction;
|
|||
*/
|
||||
public class Solver implements ISolver {
|
||||
|
||||
private GameField gameField = null;
|
||||
private GameBoard gameBoard = null;
|
||||
|
||||
public Solver(GameField gf) {
|
||||
public Solver(GameBoard gf) {
|
||||
try {
|
||||
if(gf == null) {
|
||||
throw new IllegalArgumentException("GameField may not be null.");
|
||||
throw new IllegalArgumentException("GameBoard may not be null.");
|
||||
}
|
||||
|
||||
gameField = gf.clone();
|
||||
gameBoard = gf.clone();
|
||||
} catch(CloneNotSupportedException e) {
|
||||
throw new IllegalArgumentException("This GameField is not cloneable.", e);
|
||||
throw new IllegalArgumentException("This GameBoard is not cloneable.", e);
|
||||
}
|
||||
|
||||
gameField.reset();
|
||||
if(!isSolvable(gameField)) {
|
||||
throw new IllegalArgumentException("This GameField is not solveable.");
|
||||
gameBoard.reset();
|
||||
if(!isSolvable(gameBoard)) {
|
||||
throw new IllegalArgumentException("This GameBoard is not solveable.");
|
||||
}
|
||||
|
||||
setNotes(gameField);
|
||||
setNotes(gameBoard);
|
||||
}
|
||||
|
||||
public void setNotes(GameField gameField) {
|
||||
for(int i = 0; i < gameField.getSize(); i++) {
|
||||
for(int j = 0; j < gameField.getSize(); j++) {
|
||||
for(int k = 0; k < gameField.getSize(); k++) {
|
||||
gameField.getCell(i,j).setNote(k);
|
||||
public void setNotes(GameBoard gameBoard) {
|
||||
for(int i = 0; i < gameBoard.getSize(); i++) {
|
||||
for(int j = 0; j < gameBoard.getSize(); j++) {
|
||||
for(int k = 0; k < gameBoard.getSize(); k++) {
|
||||
gameBoard.getCell(i,j).setNote(k);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSolvable(GameField gameField) {
|
||||
for(int i = 0; i < gameField.getSize(); i++) {
|
||||
if(hasErrors(gameField.getRow(i))) return false;
|
||||
if(hasErrors(gameField.getColumn(i))) return false;
|
||||
if(hasErrors(gameField.getSection(i))) return false;
|
||||
public boolean isSolvable(GameBoard gameBoard) {
|
||||
for(int i = 0; i < gameBoard.getSize(); i++) {
|
||||
if(hasErrors(gameBoard.getRow(i))) return false;
|
||||
if(hasErrors(gameBoard.getColumn(i))) return false;
|
||||
if(hasErrors(gameBoard.getSection(i))) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -68,7 +65,7 @@ public class Solver implements ISolver {
|
|||
|
||||
public boolean solve() {
|
||||
|
||||
if(gameField.isSolved(new LinkedList<CellConflict>())) {
|
||||
if(gameBoard.isSolved(new LinkedList<CellConflict>())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -97,21 +94,21 @@ public class Solver implements ISolver {
|
|||
return false;
|
||||
}
|
||||
|
||||
public GameField getGameField() {
|
||||
return gameField;
|
||||
public GameBoard getGameBoard() {
|
||||
return gameBoard;
|
||||
}
|
||||
|
||||
public boolean showPossibles() {
|
||||
LinkedList<GameCell> list = new LinkedList<GameCell>();
|
||||
for(int i = 0; i < gameField.getSize(); i++) {
|
||||
for(int j = 0; j < gameField.getSize(); j++) {
|
||||
GameCell gc = gameField.getCell(i,j);
|
||||
for(int i = 0; i < gameBoard.getSize(); i++) {
|
||||
for(int j = 0; j < gameBoard.getSize(); j++) {
|
||||
GameCell gc = gameBoard.getCell(i,j);
|
||||
if(!gc.hasValue()) {
|
||||
list.clear();
|
||||
list.addAll(gameField.getRow(i));
|
||||
list.addAll(gameField.getColumn(j));
|
||||
list.addAll(gameField.getSection(i,j));
|
||||
for(int k = 0; k < gameField.getSize(); k++) {
|
||||
list.addAll(gameBoard.getRow(i));
|
||||
list.addAll(gameBoard.getColumn(j));
|
||||
list.addAll(gameBoard.getSection(i,j));
|
||||
for(int k = 0; k < gameBoard.getSize(); k++) {
|
||||
for(GameCell c : list) {
|
||||
gc.deleteNote(c.getValue());
|
||||
}
|
||||
|
@ -128,14 +125,14 @@ public class Solver implements ISolver {
|
|||
|
||||
LinkedList<GameCell> list = new LinkedList<>();
|
||||
|
||||
for(int i = 0; i < gameField.getSize()*3; i++) {
|
||||
for(int i = 0; i < gameBoard.getSize()*3; i++) {
|
||||
|
||||
int index = i % gameField.getSize();
|
||||
int listSelector = (int)Math.floor(i / gameField.getSize());
|
||||
int index = i % gameBoard.getSize();
|
||||
int listSelector = (int)Math.floor(i / gameBoard.getSize());
|
||||
|
||||
if(listSelector == 0) list = gameField.getRow(index);
|
||||
if(listSelector == 1) list = gameField.getColumn(index);
|
||||
if(listSelector == 2) list = gameField.getSection(index);
|
||||
if(listSelector == 0) list = gameBoard.getRow(index);
|
||||
if(listSelector == 1) list = gameBoard.getColumn(index);
|
||||
if(listSelector == 2) list = gameBoard.getSection(index);
|
||||
|
||||
LinkedList<Integer[]> possibles = new LinkedList<>();
|
||||
LinkedList<Integer> notPossibles = new LinkedList<Integer>();
|
||||
|
@ -147,7 +144,7 @@ public class Solver implements ISolver {
|
|||
continue;
|
||||
}
|
||||
|
||||
for(int k = 0; k < gameField.getSize(); k++) { // check all nodes
|
||||
for(int k = 0; k < gameBoard.getSize(); k++) { // check all nodes
|
||||
if(c.getNotes()[k]) { // if k note active
|
||||
for(int p = 0; p < possibles.size(); p++) { // check possible list
|
||||
if(possibles.get(p)[2] == k+1) { // is value in possible list?
|
||||
|
@ -177,7 +174,7 @@ public class Solver implements ISolver {
|
|||
}
|
||||
// if this is still possible then SET IT :D YAY
|
||||
if(possible) {
|
||||
gameField.getCell(possibles.get(p)[0],possibles.get(p)[1]).setValue(possibles.get(p)[2]);
|
||||
gameBoard.getCell(possibles.get(p)[0],possibles.get(p)[1]).setValue(possibles.get(p)[2]);
|
||||
foundHiddenSingles = true;
|
||||
}
|
||||
}
|
||||
|
@ -203,12 +200,12 @@ public class Solver implements ISolver {
|
|||
}
|
||||
|
||||
private boolean checkSolvedCells() {
|
||||
return gameField.actionOnCells(new ICellAction<Boolean>() {
|
||||
return gameBoard.actionOnCells(new ICellAction<Boolean>() {
|
||||
@Override
|
||||
public Boolean action(GameCell gc, Boolean existing) {
|
||||
int value = -1;
|
||||
if(!gc.hasValue() && gc.getNoteCount() == 1) {
|
||||
for(int i = 0; i < gameField.getSize(); i++) {
|
||||
for(int i = 0; i < gameBoard.getSize(); i++) {
|
||||
if(gc.getNotes()[i]) {
|
||||
value = i;
|
||||
break;
|
||||
|
|
|
@ -16,6 +16,7 @@ import android.view.MenuItem;
|
|||
import android.widget.GridLayout;
|
||||
|
||||
import tu_darmstadt.sudoku.controller.GameController;
|
||||
import tu_darmstadt.sudoku.game.GameType;
|
||||
import tu_darmstadt.sudoku.ui.view.R;
|
||||
import tu_darmstadt.sudoku.ui.view.SudokuFieldLayout;
|
||||
import tu_darmstadt.sudoku.ui.view.SudokuButton;
|
||||
|
@ -41,6 +42,32 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
|
|||
//Create new GameField
|
||||
layout = (SudokuFieldLayout)findViewById(R.id.sudokuLayout);
|
||||
gameController = new GameController(sharedPref);
|
||||
/*gameController.loadLevel(GameType.Default_9x9,
|
||||
new int[]{5, 0, 1, 9, 0, 0, 0, 0, 0,
|
||||
2, 0, 0, 0, 0, 4, 9, 5, 0,
|
||||
3, 9, 0, 7, 0, 0, 0, 2, 6,
|
||||
0, 3, 0, 0, 0, 1, 0, 7, 2,
|
||||
0, 0, 6, 0, 5, 7, 0, 0, 0,
|
||||
0, 7, 2, 0, 0, 9, 0, 4, 1,
|
||||
0, 0, 0, 0, 7, 0, 4, 0, 9,
|
||||
6, 4, 0, 0, 0, 0, 0, 0, 0,
|
||||
7, 0, 0, 0, 1, 0, 3, 0, 5}
|
||||
, null, null);*/
|
||||
gameController.loadLevel(GameType.Default_12x12,
|
||||
new int[] {0, 2, 1, 0, 0, 6, 0, 0, 0, 8, 9, 0,
|
||||
10, 0,12, 0, 0, 2, 1,11, 0, 0, 0, 6,
|
||||
6, 0, 0, 4, 0,12, 0, 0, 0, 0, 2, 1,
|
||||
0, 0, 0, 5, 0, 0, 0, 4,11,10, 0, 0,
|
||||
0,10, 0, 1, 0, 0, 6, 0, 0, 0, 0, 0,
|
||||
0, 7, 0, 0,11, 0, 0, 0, 0,12, 8, 9,
|
||||
2, 1,11, 0, 0, 0, 0, 7, 0, 0, 6, 0,
|
||||
0, 0, 0, 0, 0, 5, 0, 0, 4, 0,10, 0,
|
||||
0, 0, 7, 3, 9, 0, 0, 0, 1, 0, 0, 0,
|
||||
1, 5, 0, 0, 0, 0, 4, 0,10, 0, 0,11,
|
||||
9, 0, 0, 0, 1,10, 2, 0, 0, 6, 0, 7,
|
||||
0, 6,10, 0, 0, 0, 8, 0, 0, 1,12, 0}
|
||||
,null, null);
|
||||
|
||||
layout.setGame(gameController);
|
||||
layout.setSettings(sharedPref);
|
||||
|
||||
|
@ -52,8 +79,8 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
|
|||
keyboard.setRowCount(2);
|
||||
Point p = new Point();
|
||||
getWindowManager().getDefaultDisplay().getSize(p);
|
||||
int width = p.x;
|
||||
keyboard.setKeyBoard(gameController.getSize(),p.x);
|
||||
//int width = p.x;
|
||||
keyboard.setKeyBoard(gameController.getSize(), p.x);
|
||||
/*
|
||||
// DEBUG
|
||||
String debug = gameController.getFieldAsString();
|
||||
|
@ -98,8 +125,8 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
|
|||
|
||||
if (id == R.id.nav_newgame) {
|
||||
//create new game
|
||||
//intent = new Intent(this, NewGameActivity.class);
|
||||
//startActivity(intent);
|
||||
intent = new Intent(this, GameActivity.class);
|
||||
startActivity(intent);
|
||||
|
||||
} else if (id == R.id.nav_mainmenu) {
|
||||
//go to main menu
|
||||
|
|
|
@ -12,18 +12,17 @@ import android.widget.Button;
|
|||
|
||||
public class SudokuButton extends Button {
|
||||
|
||||
private int i = 100;
|
||||
private int value = -1;
|
||||
private SudokuButtonType type = SudokuButtonType.Unspecified;
|
||||
|
||||
public SudokuButton(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public void setVal(int i){
|
||||
this.i = i;
|
||||
}
|
||||
public int getValue () {
|
||||
return i;
|
||||
}
|
||||
public void setValue(int value) { this.value = value; }
|
||||
public void setType(SudokuButtonType type) { this.type = type; }
|
||||
public int getValue () { return value; }
|
||||
public SudokuButtonType getType() { return type; }
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package tu_darmstadt.sudoku.ui.view;
|
||||
|
||||
/**
|
||||
* Created by Chris on 15.11.2015.
|
||||
*/
|
||||
public enum SudokuButtonType {
|
||||
Unspecified,
|
||||
Value,
|
||||
Do,
|
||||
Undo,
|
||||
Hint,
|
||||
NoteToggle,
|
||||
NumberOrCellFirst,
|
||||
Delete
|
||||
}
|
|
@ -110,15 +110,16 @@ public class SudokuFieldLayout extends RelativeLayout {
|
|||
p.setColor(Color.BLACK);
|
||||
p.setStrokeWidth(2);
|
||||
|
||||
// TODO: Draw Borders
|
||||
for(int i = 0; i <= (gameController.getSize()/sectionWidth); i++) {
|
||||
int horizontalSections = gameController.getSize() / sectionWidth;
|
||||
for(int i = 0; i <= horizontalSections; i++) {
|
||||
for(int j = -2; j < 2; j++) {
|
||||
canvas.drawLine((i * getWidth() / sectionWidth) + j, 0, (i * getWidth() / sectionWidth) + j, getHeight(), p);
|
||||
canvas.drawLine((i * getWidth() / horizontalSections) + j, 0, (i * getWidth() / horizontalSections) + j, getHeight(), p);
|
||||
}
|
||||
}
|
||||
for(int i = 0; i <= (gameController.getSize()/sectionHeight); i++) {
|
||||
int verticalSections = (gameController.getSize()/sectionHeight);
|
||||
for(int i = 0; i <= verticalSections; i++) {
|
||||
for(int j = -2; j < 2; j++) {
|
||||
canvas.drawLine(0, (i * getHeight() / sectionHeight) + j, getHeight(), (i * getHeight() / sectionHeight) + j, p);
|
||||
canvas.drawLine(0, (i * getHeight() / verticalSections) + j, getHeight(), (i * getHeight() / verticalSections) + j, p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,14 +3,9 @@ package tu_darmstadt.sudoku.ui.view;
|
|||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.Display;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.GridLayout;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import tu_darmstadt.sudoku.controller.GameController;
|
||||
|
||||
|
@ -29,13 +24,40 @@ public class SudokuKeyboardLayout extends GridLayout {
|
|||
OnClickListener listener = new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if(v instanceof SudokuButton) {
|
||||
SudokuButton btn = (SudokuButton)v;
|
||||
int i = btn.getValue();
|
||||
|
||||
switch(btn.getType()) {
|
||||
case Value:
|
||||
if(notesEnabled) {
|
||||
//TODO: set notes funktion erst noch im GameController Schreiben
|
||||
}else {
|
||||
gameController.toggleSelectedNote(btn.getValue());
|
||||
} else {
|
||||
gameController.setSelectedValue(btn.getValue());
|
||||
}
|
||||
break;
|
||||
case Delete:
|
||||
gameController.deleteSelectedValue();
|
||||
break;
|
||||
case NoteToggle:
|
||||
notesEnabled = !notesEnabled;
|
||||
break;
|
||||
case Do:
|
||||
// TODO: not implemented
|
||||
break;
|
||||
case Undo:
|
||||
// TODO: not implemented
|
||||
break;
|
||||
case Hint:
|
||||
// TODO: not implemented
|
||||
break;
|
||||
case NumberOrCellFirst:
|
||||
// TODO: not implemented
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -67,8 +89,9 @@ public class SudokuKeyboardLayout extends GridLayout {
|
|||
buttons[i].setLayoutParams(p);
|
||||
buttons[i].setGravity(Gravity.CENTER);
|
||||
if (number<size) {
|
||||
buttons[i].setType(SudokuButtonType.Value);
|
||||
buttons[i].setText(String.valueOf(number + 1));
|
||||
buttons[i].setVal(number + 1);
|
||||
buttons[i].setValue(number + 1);
|
||||
buttons[i].setOnClickListener(listener);
|
||||
} else {
|
||||
//TODO: Set Enum for fixed Buttons maybe also pictures
|
||||
|
|
|
@ -1,18 +1,22 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
android:layout_centerHorizontal="true"
|
||||
tools:context="tu_darmstadt.sudoku.activity.MainActivity">
|
||||
|
||||
<!-- TODO .. add menu -->
|
||||
|
||||
<TextView android:id="@+id/testString"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:editable="true"
|
||||
android:clickable="true"
|
||||
android:onClick="onClicktext"
|
||||
android:text="@string/app_name"/>
|
||||
|
||||
</RelativeLayout>
|
||||
</LinearLayout>
|
||||
|
|
|
@ -3,6 +3,8 @@ package tu_darmstadt.sudoku.controller;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import tu_darmstadt.sudoku.game.GameType;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
|
@ -11,23 +13,36 @@ import static org.junit.Assert.*;
|
|||
public class GameControllerTest {
|
||||
|
||||
GameController controller;
|
||||
GameController controller2;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
controller = new GameController();
|
||||
int[][] level = {{ 5, 0, 1, 9, 0, 0, 0, 0, 0 },
|
||||
{ 2, 0, 0, 0, 0, 4, 9, 5, 0 },
|
||||
{ 3, 9, 0, 7, 0, 0, 0, 2, 6 },
|
||||
|
||||
{ 0, 3, 0, 0, 0, 1, 0, 7, 2 },
|
||||
{ 0, 0, 6, 0, 5, 7, 0, 0, 0 },
|
||||
{ 0, 7, 2, 0, 0, 9, 0, 4, 1 },
|
||||
|
||||
{ 0, 0, 0, 0, 7, 0, 4, 0, 9 },
|
||||
{ 6, 4, 0, 0, 0, 0, 0, 0, 0 },
|
||||
{ 7, 0, 0, 0, 1, 0, 3, 0, 5 }};
|
||||
|
||||
|
||||
controller. loadLevel(GameType.Default_9x9,
|
||||
new int[]{ 5, 0, 1, 9, 0, 0, 0, 0, 0,
|
||||
2, 0, 0, 0, 0, 4, 9, 5, 0,
|
||||
3, 9, 0, 7, 0, 0, 0, 2, 6,
|
||||
0, 3, 0, 0, 0, 1, 0, 7, 2,
|
||||
0, 0, 6, 0, 5, 7, 0, 0, 0,
|
||||
0, 7, 2, 0, 0, 9, 0, 4, 1,
|
||||
0, 0, 0, 0, 7, 0, 4, 0, 9,
|
||||
6, 4, 0, 0, 0, 0, 0, 0, 0,
|
||||
7, 0, 0, 0, 1, 0, 3, 0, 5}, null, null);
|
||||
controller2 = new GameController();
|
||||
controller2.loadLevel(GameType.Default_12x12,
|
||||
new int[] {0, 2, 1, 0, 0, 6, 0, 0, 0, 8, 9, 0,
|
||||
10, 0,12, 0, 0, 2, 1,11, 0, 0, 0, 6,
|
||||
6, 0, 0, 4, 0,12, 0, 0, 0, 0, 2, 1,
|
||||
0, 0, 0, 5, 0, 0, 0, 4,11,10, 0, 0,
|
||||
0,10, 0, 1, 0, 0, 6, 0, 0, 0, 0, 0,
|
||||
0, 7, 0, 0,11, 0, 0, 0, 0,12, 8, 9,
|
||||
2, 1,11, 0, 0, 0, 0, 7, 0, 0, 6, 0,
|
||||
0, 0, 0, 0, 0, 5, 0, 0, 4, 0,10, 0,
|
||||
0, 0, 7, 3, 9, 0, 0, 0, 1, 0, 0, 0,
|
||||
1, 5, 0, 0, 0, 0, 4, 0,10, 0, 0,11,
|
||||
9, 0, 0, 0, 1,10, 2, 0, 0, 6, 0, 7,
|
||||
0, 6,10, 0, 0, 0, 8, 0, 0, 1,12, 0}
|
||||
,null, null);
|
||||
}
|
||||
|
||||
|
||||
|
@ -183,12 +198,20 @@ public class GameControllerTest {
|
|||
@Test
|
||||
public void toggleNoteTest() {
|
||||
controller.toggleNote(1,2,5);
|
||||
controller.toggleNote(1,2,9);
|
||||
controller.toggleNote(1,2,5);
|
||||
controller.toggleNote(1,2,4);
|
||||
controller.toggleNote(1, 2, 9);
|
||||
controller.toggleNote(1, 2, 5);
|
||||
controller.toggleNote(1, 2, 4);
|
||||
|
||||
boolean[] result = {false, false, false, true, false, false, false, false, true};
|
||||
|
||||
assertArrayEquals(result, controller.getNotes(1, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectCellTest() {
|
||||
|
||||
controller.selectCell(0, 1);
|
||||
assertEquals(1, controller.getSelectedCol());
|
||||
assertEquals(0, controller.getSelectedRow());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import org.junit.Before;
|
|||
import org.junit.Test;
|
||||
|
||||
import tu_darmstadt.sudoku.controller.GameController;
|
||||
import tu_darmstadt.sudoku.game.GameType;
|
||||
|
||||
/**
|
||||
* Created by Chris on 12.11.2015.
|
||||
|
@ -27,7 +28,10 @@ public class SolverTest {
|
|||
{ 6, 4, 0, 0, 0, 0, 0, 0, 0 },
|
||||
{ 7, 0, 0, 0, 1, 0, 3, 0, 5 }};
|
||||
|
||||
controller.loadLevel(9,3,3,new int[]{0,0,0,0,4,1,0,0,0,0,6,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,3,2,0,6,0,0,0,0,0,0,0,0,0,5,0,0,4,1,7,0,0,0,0,0,0,0,0,0,0,0,2,0,0,3,0,0,0,4,8,0,0,0,0,0,0,5,0,1,0,0,0,0,0,0},null,null);
|
||||
controller.loadLevel(GameType.Default_9x9,
|
||||
new int[]{0,0,0,0,4,1,0,0,0,0,6,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,3,2,0,6,0,0,0,0,0,0,0,0,0,5,0,0,4,1,7,0,0,0,0,0,0,0,0,0,0,0,2,0,0,3,0,0,0,4,8,0,0,0,0,0,0,5,0,1,0,0,0,0,0,0},
|
||||
null,
|
||||
null);
|
||||
}
|
||||
|
||||
//000041000060000200000000000320600000000050041700000000000200300048000000501000000
|
||||
|
@ -36,6 +40,6 @@ public class SolverTest {
|
|||
|
||||
@Test
|
||||
public void solveTest() {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue