addet totalResetButton, getResult, getSolution and getErgebnisArray
methodsdomainBranch
parent
c9be82826b
commit
f3da7c44f7
|
@ -73,7 +73,7 @@ public class HitoriMain extends JFrame implements AcrtionListener{
|
|||
return buttons;
|
||||
}
|
||||
|
||||
private static boolean abgabeMöglich(String path, String[][] data, String[][] colors) throws FileNotFoundException{
|
||||
public static boolean abgabeMöglich(String path, String[][] data, String[][] colors) throws FileNotFoundException{
|
||||
boolean abgabeMöglich = false;
|
||||
String[][] result = getResult(data, colors);
|
||||
ArrayList<String> filteredData = getSolution(path, result);
|
||||
|
@ -95,4 +95,137 @@ public class HitoriMain extends JFrame implements AcrtionListener{
|
|||
}
|
||||
return abgabeMöglich;
|
||||
}
|
||||
|
||||
public static String[] getCords(int i, int j){
|
||||
String yKoordinate = String.valueOf(i);
|
||||
String xKoordinate = String.valueOf(j);
|
||||
String[] pos = {yKoordinate, xKoordinate};
|
||||
return pos;
|
||||
}
|
||||
|
||||
public static void backOneStep(Stack<String> movesMade, JButton[][] buttons, String[][] colors, JPanel grid){
|
||||
try {
|
||||
String move = movesMade.pop();
|
||||
String[] line = move.split("\\.");
|
||||
String y = line[0];
|
||||
String x = line[1];
|
||||
String color = line[2];
|
||||
if(color.equals("W")){
|
||||
int i = Integer.parseInt(y);
|
||||
int j = Integer.parseInt(x);
|
||||
JButton b0 = buttons[i][j];
|
||||
b0.setOpaque(true);
|
||||
b0.setForeground(Color.WHITE);
|
||||
b0.setContentAreaFilled(true);
|
||||
b0.setBorderPainted(false);
|
||||
b0.setFocusPainted(false);
|
||||
b0.setBackground(Color.BLACK);
|
||||
buttons[i][j] = b0;
|
||||
String str = colors[i][j];
|
||||
String str0 = str.substring(0, str.length() - 1);
|
||||
colors[i][j] = str0;
|
||||
grid.repaint();
|
||||
gridUpdate(grid, buttons);
|
||||
}else if(color.equals("G")){
|
||||
int i = Integer.parseInt(y);
|
||||
int j = Integer.parseInt(x);
|
||||
JButton b0 = buttons[i][j];
|
||||
b0.setOpaque(true);
|
||||
b0.setForeground(Color.BLACK);
|
||||
b0.setContentAreaFilled(true);
|
||||
b0.setBorderPainted(false);
|
||||
b0.setFocusPainted(false);
|
||||
b0.setBackground(Color.WHITE);
|
||||
buttons[i][j] = b0;
|
||||
String str = colors[i][j];
|
||||
String str0 = str.substring(0, str.length() - 1);
|
||||
colors[i][j] = str0;
|
||||
gridUpdate(grid, buttons);
|
||||
} else if(color.equals("B")){
|
||||
int i = Integer.parseInt(y);
|
||||
int j = Integer.parseInt(x);
|
||||
JButton b0 = buttons[i][j];
|
||||
b0.setOpaque(true);
|
||||
b0.setForeground(Color.BLACK);
|
||||
b0.setContentAreaFilled(true);
|
||||
b0.setBorderPainted(false);
|
||||
b0.setFocusPainted(false);
|
||||
b0.setBackground(Color.lightGray);
|
||||
buttons[i][j] = b0;
|
||||
String str = colors[i][j];
|
||||
String str0 = str.substring(0, str.length() - 1);
|
||||
colors[i][j] = str0;
|
||||
gridUpdate(grid, buttons);
|
||||
}
|
||||
} catch(EmptyStackException e) {}
|
||||
}
|
||||
|
||||
public static void totalResetButton(JButton[][] buttons, String[][] colors,Stack<String> madeMoves,String[][] data){
|
||||
madeMoves.clear();
|
||||
for(int i = 0; i<data.length;i++){
|
||||
for(int j = 0; j<data.length;j++){
|
||||
colors[i][j] = "W";
|
||||
buttons[i][j] = null;
|
||||
}
|
||||
JButton[][] buttons0 = makeButtonArray(data);
|
||||
GameGUI.paintGame(buttons0, colors, madeMoves, data);
|
||||
}
|
||||
}
|
||||
|
||||
public static String[][] getResult(String[][] data, String[][] colors){
|
||||
String[][] result = new String[data.length][data.length];
|
||||
for(int i=0;i<data.length;i++){
|
||||
for(int j=0;j<data.length;j++){
|
||||
String farben = colors[i][j];
|
||||
String lastColor = String.valueOf(farben.charAt(farben.length() - 1));
|
||||
if(lastColor.equals("G")){
|
||||
lastColor = "W";
|
||||
}
|
||||
result[i][j] = lastColor;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ArrayList<String> getSolution(String path, String[][] result) throws FileNotFoundException{
|
||||
Scanner sc = new Scanner(new File(path));
|
||||
ArrayList<String> filteredData = new ArrayList<>();
|
||||
boolean isUnderComment = false;
|
||||
while (sc.hasNextLine()) {
|
||||
String line = sc.nextLine().trim();
|
||||
if (line.equals("//Lösung (schwarze Felder)")) {
|
||||
isUnderComment = true;
|
||||
continue;
|
||||
}
|
||||
if (isUnderComment && !line.isEmpty()) {
|
||||
String[] lineArray = line.split(",");
|
||||
int num1 = Integer.parseInt(lineArray[0]);
|
||||
int num2 = Integer.parseInt(lineArray[1]);
|
||||
String newNum1 = String.valueOf(num1-1);
|
||||
String newNum2 = String.valueOf(num2-1);
|
||||
String newLine = newNum1+","+newNum2;
|
||||
filteredData.add(newLine);
|
||||
}
|
||||
}
|
||||
sc.close();
|
||||
return filteredData;
|
||||
}
|
||||
|
||||
public static String[][] getErgebnisArray(String[][] result, ArrayList<String> filteredData){
|
||||
String[][] ergebnis = new String[result.length][result.length];
|
||||
for(int i=0;i<result.length;i++){
|
||||
for(int j=0;j<result.length;j++){
|
||||
ergebnis[i][j] = "W";
|
||||
}
|
||||
}
|
||||
for (String index : filteredData) {
|
||||
String[] parts = index.split(",");
|
||||
int row = Integer.parseInt(parts[0].trim());
|
||||
int col = Integer.parseInt(parts[1].trim());
|
||||
if (row >= 0 && row < ergebnis.length && col >= 0 && col < ergebnis[row].length) {
|
||||
ergebnis[row][col] = "B";
|
||||
}
|
||||
}
|
||||
return ergebnis;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue