|
|
|
@ -1,5 +1,174 @@
|
|
|
|
|
package de.hs_mannheim.informatik.pr2projekt.domain;
|
|
|
|
|
|
|
|
|
|
public class HitoriMain {
|
|
|
|
|
public class HitoriMain extends JFrame implements AcrtionListener{
|
|
|
|
|
|
|
|
|
|
private static String[] filepath = new String[2];
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws FileNotFoundException{
|
|
|
|
|
filepath[0] = "";
|
|
|
|
|
filepath[1] = "";
|
|
|
|
|
new HitoriMain();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HitoriMain() throws FileNotFoundException {
|
|
|
|
|
Stack<String> madeMoves = new Stack<>();
|
|
|
|
|
MenuGUI.getPath(filepath);
|
|
|
|
|
int i = 0;
|
|
|
|
|
while(filepath[0].equals("")){
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
String[][] data = getData(filepath[0], Integer.parseInt(filepath[1]));
|
|
|
|
|
String[][] colors = makeColorArray(data.length);
|
|
|
|
|
JButton[][] buttons = makeButtonArray(data);
|
|
|
|
|
GameGUI.paintGame(buttons, colors, madeMoves, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ArrayList<String> readFromFile(String path){
|
|
|
|
|
ArrayList<String> lines = new ArrayList<>();
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
lines.add(line);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String[][] getData(String filepath, int rows) throws FileNotFoundException{
|
|
|
|
|
String filepath0 = filepath;
|
|
|
|
|
Scanner sc = new Scanner(new File(filepath0));
|
|
|
|
|
String[][] data = new String[rows][rows];
|
|
|
|
|
int rowInt = 0;
|
|
|
|
|
while (sc.hasNextLine() && rowInt < rows) {
|
|
|
|
|
String line = sc.nextLine();
|
|
|
|
|
data[rowInt] = line.split(",");
|
|
|
|
|
rowInt++;
|
|
|
|
|
}
|
|
|
|
|
sc.close();
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String[][] makeColorArray(int size){
|
|
|
|
|
String[][] colors = new String[size][size];
|
|
|
|
|
for(int i=0;i<size;i++){
|
|
|
|
|
for(int j=0;j<size;j++){
|
|
|
|
|
colors[i][j] = "W";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return colors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static JButton[][] makeButtonArray(String[][] data){
|
|
|
|
|
JButton[][] buttons = new JButton[data.length][data.length];
|
|
|
|
|
for(int i=0;i<data.length;i++){
|
|
|
|
|
for(int j=0;j<data.length;j++){
|
|
|
|
|
String number = data[i][j];
|
|
|
|
|
JButton b0 = new JButton(number);
|
|
|
|
|
buttons[i][j] = b0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return buttons;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
String[][] ergebnis1 = getErgebnisArray(result, filteredData);
|
|
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
for(int i=0;i<result.length;i++){
|
|
|
|
|
for(int j=0;j<result.length;j++){
|
|
|
|
|
if(result[i][j].equals(ergebnis1[i][j])){
|
|
|
|
|
} else {
|
|
|
|
|
count--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(count <0){
|
|
|
|
|
abgabeMöglich = false;
|
|
|
|
|
} else {
|
|
|
|
|
abgabeMöglich = true;
|
|
|
|
|
}
|
|
|
|
|
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 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|