Browse Source

implemented a model to do file works and history

remotes/origin/develop
fdai6499 2 years ago
parent
commit
66380a4adf
  1. 3
      build-project.sh
  2. 5
      src/main/java/src/GameField.java
  3. 115
      src/main/java/src/Model.java
  4. 4
      src/test/java/GameFieldTest.java
  5. 188
      src/test/java/ModelTest.java

3
build-project.sh

@ -1,3 +1,6 @@
#!/bin/bash #!/bin/bash
mvn clean
mvn compile
mvn compile test mvn compile test
mvn test

5
src/main/java/src/GameField.java

@ -14,7 +14,7 @@ public class GameField extends JPanel {
private JPanel activePanel = null; private JPanel activePanel = null;
private static final Color colorDefault = new Color(0, 0, 255, 255);
private static final Color colorDefault = new Color(100, 100, 255, 255);
private static final Color colorOnEntered = new Color(0, 0, 255, 100); private static final Color colorOnEntered = new Color(0, 0, 255, 100);
private static final Color colorOnWhenActive = new Color(0, 255, 150, 200); private static final Color colorOnWhenActive = new Color(0, 255, 150, 200);
@ -34,6 +34,7 @@ public class GameField extends JPanel {
return activePanel; return activePanel;
} }
/** /**
* represents the game filed of sudoku * represents the game filed of sudoku
* @param widthHeight describes the field size in pixels which will be seperated into single sudoku fields * @param widthHeight describes the field size in pixels which will be seperated into single sudoku fields
@ -69,7 +70,7 @@ public class GameField extends JPanel {
j * size + border*(j+1), j * size + border*(j+1),
i * size + border*(i+1), i * size + border*(i+1),
size, size); size, size);
gameField[i][j].setBackground(Color.BLUE);
gameField[i][j].setBackground(colorDefault);
gameField[i][j].setVisible(true); gameField[i][j].setVisible(true);
this.add(gameField[i][j]); this.add(gameField[i][j]);

115
src/main/java/src/Model.java

@ -0,0 +1,115 @@
package src;
import javax.swing.*;
import java.util.List;
public class Model {
private static int SIZE = 9;
public int[][] model;
public int difficulty;
public int getDifficulty() {
return difficulty;
}
public void setDifficulty(int difficulty) {
this.difficulty = difficulty;
}
public Model(GameField gameField) {
model = new int[SIZE][];
for (int i = 0; i < SIZE; i++) {
model[i] = new int[SIZE];
}
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
setField(i, j, gameField.getValue(i, j));
}
}
}
public void setField(int x, int y, int value) {
model[y][x] = value;
}
public int getField(int x, int y) {
return model[y][x];
}
//fields are 3x3 fields and their numeration starts at 0 to 8.
//0 is the upper right corner and 8 the lower left corner. 1 is the 3x3 square in the
//middle of the first upper row
public int[] get3x3Field(int fieldNr) {
int[] ints = new int[9];
int x = 0, y = 0;
for (int i = 0, counter = 0; i < 3; i++) {
for (int j = 0; j < 3; j++, counter++) {
ints[counter] = getField(i + 3*(fieldNr/3), j + 3*(fieldNr%3));
}
}
return ints;
}
public void safeFieldAsCurrentGame(List<Model> history) {
}
public void loadAllData() {
}
public void loadNewEasy() {
}
public void loadNewMedium() {
}
public void loadNewHard() {
}
public void safeNewSandboxGame() {
}
public void showAllGames() {
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("X 0 1 2 3 4 5 6 7 8\n");
sb.append(" ------------------------\n");
for (int i = 0; i < SIZE; i++) {
sb.append(i + " | ");
for (int j = 0; j < SIZE; j++) {
sb.append(model[j][i] + ((j+1==SIZE)?"\n":" "));
if ((j+1) % 3 == 0 && j+1 != SIZE) {
sb.append(" ");
}
}
if ((i+1) % 3 == 0 && i+1 != SIZE){
sb.append(" |" + "\n");
}
}
return sb.toString();
}
}

4
src/test/java/GameFieldTest.java

@ -1,13 +1,9 @@
import org.junit.Before;
import src.GameField; import src.GameField;
import src.Main;
import src.Notes; import src.Notes;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import java.awt.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;

188
src/test/java/ModelTest.java

@ -0,0 +1,188 @@
import org.junit.jupiter.api.Test;
import src.GameField;
import src.MainFrame;
import src.Model;
import static org.junit.jupiter.api.Assertions.*;
public class ModelTest extends MainFrame {
@Test
public void test_toString() {
GameField gameField = new GameField(360);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
gameField.setValue(i, j, i);
}
}
Model model = new Model(gameField);
System.out.println(model);
}
@Test
public void test_getDifficulty() {
}
@Test
public void test_setDifficulty() {
}
@Test
public void test_setAndGetField() {
GameField gameField = new GameField(360);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
gameField.setValue(i, j, i);
}
}
Model model = new Model(gameField);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (i == 0) {
model.setField(i, j, 1);
}
if (j == 8) {
model.setField(i, j, 2);
}
if (i == 8) {
model.setField(i, j, 3);
}
if (j == 0) {
model.setField(i, j, 4);
}
if (i == 0 && j == 0 ||
i == 0 && j == 8 ||
i == 8 && j == 8 ||
i == 8 && j == 0) {
model.setField(i, j, i+j);
}
}
}
boolean a = true;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
int field = model.getField(i, j);
if (i == 0 && j == 0 ||
i == 0 && j == 8 ||
i == 8 && j == 8 ||
i == 8 && j == 0) {
if(model.getField(i, j) != i+j) {
a = false;
}
} else {
if (i == 0) {
if (field != 1) {
a = false;
}
}
if (j == 8) {
if (field != 2) {
a = false;
}
}
if (i == 8) {
if (field != 3) {
a = false;
}
}
if (j == 0) {
if (field != 4) {
a = false;
}
}
}
}
}
assertTrue(a);
}
@Test
public void test_get3x3Field() {
GameField gameField = new GameField(360);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
gameField.setValue(i, j, i);
}
}
Model model = new Model(gameField);
int[] ints1 = {0, 0, 0, 1, 1, 1, 2, 2, 2};
int[] ints2 = {3, 3, 3, 4, 4, 4, 5, 5, 5};
int[] ints3 = {6, 6, 6, 7, 7, 7, 8, 8, 8};
int[] x3Field0 = model.get3x3Field(0);
int[] x3Field1 = model.get3x3Field(1);
int[] x3Field2 = model.get3x3Field(2);
int[] x3Field3 = model.get3x3Field(3);
int[] x3Field4 = model.get3x3Field(4);
int[] x3Field5 = model.get3x3Field(5);
int[] x3Field6 = model.get3x3Field(6);
int[] x3Field7 = model.get3x3Field(7);
int[] x3Field8 = model.get3x3Field(8);
assertArrayEquals(x3Field0, ints1);
assertArrayEquals(x3Field1, ints1);
assertArrayEquals(x3Field2, ints1);
assertArrayEquals(x3Field3, ints2);
assertArrayEquals(x3Field4, ints2);
assertArrayEquals(x3Field5, ints2);
assertArrayEquals(x3Field6, ints3);
assertArrayEquals(x3Field7, ints3);
assertArrayEquals(x3Field8, ints3);
}
@Test
public void test_safeFieldAsCurrentGame() {
}
@Test
public void test_loadAllData() {
}
@Test
public void test_loadNewEasy() {
}
@Test
public void test_loadNewMedium() {
}
@Test
public void test_loadNewHard() {
}
@Test
public void test_safeNewSandboxGame() {
}
@Test
public void test_showAllGames() {
}
}
Loading…
Cancel
Save