PR2-Gruppenprojekt/PR2Projekt/site/jacoco/de.hs_mannheim.informatik.mvn/Main.java.html

714 lines
48 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>Main.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MvnDemo</a> &gt; <a href="index.source.html" class="el_package">de.hs_mannheim.informatik.mvn</a> &gt; <span class="el_source">Main.java</span></div><h1>Main.java</h1><pre class="source lang-java linenums">package de.hs_mannheim.informatik.mvn;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.EmptyStackException;
import java.util.Scanner;
import java.util.Stack;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Main extends JFrame implements ActionListener {
<span class="fc" id="L36"> private static String[] filepath = {&quot;&quot;, &quot;&quot;};</span>
<span class="fc" id="L37"> private static CardLayout cl = new CardLayout();</span>
<span class="fc" id="L38"> private static JPanel main = new JPanel(cl);</span>
<span class="fc" id="L39"> private static Timer timer = new Timer(); </span>
private static long startTime;
private static long elapsedSeconds;
public static void main(String[] args) throws FileNotFoundException {
<span class="nc" id="L44"> new Main();</span>
<span class="nc" id="L45"> }</span>
<span class="fc" id="L47"> public Main(){</span>
<span class="fc" id="L48"> JPanel menuPanel = new JPanel(new BorderLayout());</span>
<span class="fc" id="L49"> JPanel buttonPanel = new JPanel(new GridLayout(7,1,10,10));</span>
<span class="fc" id="L50"> String[] buttons = {</span>
&quot;4x4 - leicht&quot;,
&quot;5x5 - leicht&quot;,
&quot;8x8 - leicht&quot;,
&quot;8x8 - medium&quot;,
&quot;10x10 - medium&quot;,
&quot;15x15 - medium&quot;
};
<span class="fc bfc" id="L58" title="All 2 branches covered."> for(int i=0;i&lt;buttons.length;i++){</span>
<span class="fc" id="L59"> JButton b0 = new JButton(buttons[i]);</span>
<span class="fc" id="L60"> buttonPanel.add(b0);</span>
<span class="fc" id="L61"> int[] count = {i};</span>
<span class="fc" id="L62"> String[] num = buttons[i].split(&quot;x&quot;); </span>
<span class="fc" id="L63"> b0.addActionListener(e -&gt; {</span>
<span class="nc" id="L64"> int j = count[0];</span>
<span class="nc" id="L65"> String currentDirectory = Paths.get(&quot;&quot;).toAbsolutePath().toString();</span>
<span class="nc" id="L66"> System.out.println(&quot;Current Working Directory: &quot; + currentDirectory);</span>
<span class="nc" id="L67"> String[] paths = {</span>
&quot;C:/Users/Berat/Desktop/hitoriNew/src/main/java/de/hs_mannheim/informatik/mvn/Hitori_Spielfelder/Hitori4x4_leicht.csv&quot;,
&quot;C:/Users/Berat/Desktop/hitoriNew/src/main/java/de/hs_mannheim/informatik/mvn/Hitori_Spielfelder/Hitori5x5leicht.csv&quot;,
&quot;C:/Users/Berat/Desktop/hitoriNew/src/main/java/de/hs_mannheim/informatik/mvn/Hitori_Spielfelder/Hitori8x8leicht.csv&quot;,
&quot;C:/Users/Berat/Desktop/hitoriNew/src/main/java/de/hs_mannheim/informatik/mvn/Hitori_Spielfelder/Hitori8x8medium.csv&quot;,
&quot;C:/Users/Berat/Desktop/hitoriNew/src/main/java/de/hs_mannheim/informatik/mvn/Hitori_Spielfelder/Hitori10x10medium.csv&quot;,
&quot;C:/Users/Berat/Desktop/hitoriNew/src/main/java/de/hs_mannheim/informatik/mvn/Hitori_Spielfelder/Hitori15x15_medium.csv&quot;
};
<span class="nc" id="L76"> System.out.println(&quot;Current Working Directory: &quot; + Paths.get(&quot;&quot;).toAbsolutePath());</span>
<span class="nc bnc" id="L78" title="All 2 branches missed."> for (String path : paths) {</span>
<span class="nc" id="L79"> File file = new File(path);</span>
<span class="nc bnc" id="L80" title="All 2 branches missed."> if (file.exists()) {</span>
<span class="nc" id="L81"> System.out.println(&quot;File found: &quot; + file.getAbsolutePath());</span>
} else {
<span class="nc" id="L83"> System.out.println(&quot;File not found: &quot; + file.getAbsolutePath());</span>
}
}
<span class="nc" id="L88"> filepath[0] = paths[j];</span>
<span class="nc" id="L89"> filepath[1] = num[0];</span>
try{
<span class="nc" id="L91"> ablauf(filepath);</span>
<span class="nc" id="L92"> } catch (FileNotFoundException s){</span>
<span class="nc" id="L93"> System.out.println(&quot;Fehler: &quot; + s.getMessage());</span>
<span class="nc" id="L94"> }</span>
<span class="nc" id="L95"> });</span>
}
<span class="fc" id="L97"> JButton b = new JButton(&quot;Highscores&quot;);</span>
<span class="fc" id="L98"> b.addActionListener(e -&gt; {</span>
<span class="nc" id="L99"> highscoreScreen();</span>
<span class="nc" id="L100"> });</span>
<span class="fc" id="L101"> buttonPanel.add(b);</span>
<span class="fc" id="L102"> menuPanel.add(buttonPanel, BorderLayout.CENTER);</span>
<span class="fc" id="L103"> JLabel text0 = new JLabel(&quot;Wählen Sie ein Level aus!&quot;);</span>
<span class="fc" id="L104"> menuPanel.add(text0, BorderLayout.NORTH);</span>
//add(menuPanel);
<span class="fc" id="L106"> setVisible(true);</span>
<span class="fc" id="L107"> setSize(600, 600);</span>
<span class="fc" id="L108"> setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span>
<span class="fc" id="L109"> add(main);</span>
<span class="fc" id="L110"> main.add(menuPanel, &quot;HAUPT&quot;);</span>
<span class="fc" id="L111"> cl.show(main, &quot;HAUPT&quot;);</span>
<span class="fc" id="L112"> }</span>
public static void ablauf(String[] filepath) throws FileNotFoundException{
<span class="nc" id="L115"> Stack&lt;String&gt; madeMoves = new Stack&lt;&gt;();</span>
<span class="nc" id="L116"> String[][] data = getData(filepath[0], Integer.parseInt(filepath[1]));</span>
<span class="nc" id="L117"> String[][] colors = makeColorArray(data.length);</span>
<span class="nc" id="L118"> JButton[][] buttons = makeButtonArray(data);</span>
<span class="nc" id="L119"> paintGame(filepath, buttons, colors, madeMoves, data);</span>
<span class="nc" id="L120"> }</span>
private static void highscoreScreen(){
<span class="nc" id="L123"> JPanel highscorePanel = new JPanel(new BorderLayout());</span>
<span class="nc" id="L124"> JPanel buttonPanel = new JPanel(new GridLayout(7,1,10,10));</span>
<span class="nc" id="L125"> String[] buttons = {</span>
&quot;Highscore 4x4 - leicht&quot;,
&quot;Highscore 5x5 - leicht&quot;,
&quot;Highscore 8x8 - leicht&quot;,
&quot;Highscore 8x8 - medium&quot;,
&quot;Highscore 10x10 - medium&quot;,
&quot;Highscore 15x15 - medium&quot;
};
<span class="nc bnc" id="L133" title="All 2 branches missed."> for(int i=0; i&lt;buttons.length;i++){</span>
<span class="nc" id="L134"> JButton b0 = new JButton(buttons[i]);</span>
<span class="nc" id="L135"> buttonPanel.add(b0);</span>
<span class="nc" id="L136"> int[] count = {i};</span>
<span class="nc" id="L137"> b0.addActionListener(e -&gt; {</span>
<span class="nc" id="L138"> int j = count[0];</span>
<span class="nc" id="L139"> String[] paths = {</span>
&quot;C:/Users/Berat/Desktop/hitoriNew/src/main/java/de/hs_mannheim/informatik/mvn/Hitori_Highscores/Hitori4x4_leicht.txt&quot;,
&quot;C:/Users/Berat/Desktop/hitoriNew/src/main/java/de/hs_mannheim/informatik/mvn/Hitori_Highscores/Hitori5x5leicht.txt&quot;,
&quot;C:/Users/Berat/Desktop/hitoriNew/src/main/java/de/hs_mannheim/informatik/mvn/Hitori_Highscores/Hitori8x8leicht.txt&quot;,
&quot;C:/Users/Berat/Desktop/hitoriNew/src/main/java/de/hs_mannheim/informatik/mvn/Hitori_Highscores/Hitori8x8medium.txt&quot;,
&quot;C:/Users/Berat/Desktop/hitoriNew/src/main/java/de/hs_mannheim/informatik/mvn/Hitori_Highscores/Hitori10x10medium.txt&quot;,
&quot;C:/Users/Berat/Desktop/hitoriNew/src/main/java/de/hs_mannheim/informatik/mvn/Hitori_Highscores/Hitori15x15_medium.txt&quot;
};
try {
<span class="nc" id="L148"> showHighscores(paths[j]);</span>
<span class="nc" id="L149"> } catch (FileNotFoundException e1) {</span>
<span class="nc" id="L150"> e1.printStackTrace();</span>
<span class="nc" id="L151"> } catch (IOException e1) {</span>
<span class="nc" id="L152"> e1.printStackTrace();</span>
<span class="nc" id="L153"> }</span>
<span class="nc" id="L154"> });</span>
}
<span class="nc" id="L156"> JButton b = new JButton(&quot;Zurück&quot;);</span>
<span class="nc" id="L157"> b.addActionListener(e -&gt; {</span>
<span class="nc" id="L158"> cl.show(main, &quot;HAUPT&quot;); </span>
<span class="nc" id="L159"> });</span>
<span class="nc" id="L160"> buttonPanel.add(b);</span>
<span class="nc" id="L161"> highscorePanel.setVisible(true);</span>
<span class="nc" id="L162"> highscorePanel.setSize(600,600);</span>
<span class="nc" id="L163"> highscorePanel.add(buttonPanel, BorderLayout.CENTER);</span>
<span class="nc" id="L164"> JLabel text0 = new JLabel(&quot;Level für Highscore Liste auswählen!&quot;);</span>
<span class="nc" id="L165"> highscorePanel.add(text0, BorderLayout.NORTH);</span>
<span class="nc" id="L166"> main.add(highscorePanel, &quot;HIGHSCORES&quot;);</span>
<span class="nc" id="L167"> cl.show(main, &quot;HIGHSCORES&quot;);</span>
<span class="nc" id="L168"> }</span>
private static void sortByTime(String path) throws FileNotFoundException, IOException{
<span class="nc" id="L171"> File file = new File(path);</span>
<span class="nc" id="L172"> ArrayList&lt;Entry&gt; entries = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L174"> try (BufferedReader br = new BufferedReader(new FileReader(file))) {</span>
String line;
<span class="nc bnc" id="L176" title="All 2 branches missed."> while ((line = br.readLine()) != null) {</span>
<span class="nc" id="L177"> String[] parts = line.split(&quot; &quot;, 2); </span>
<span class="nc" id="L178"> String timeStr = parts[0];</span>
<span class="nc bnc" id="L179" title="All 2 branches missed."> String name = parts.length &gt; 1 ? parts[1] : &quot;&quot;;</span>
<span class="nc" id="L181"> String[] timeParts = timeStr.split(&quot;:&quot;);</span>
<span class="nc" id="L182"> int hour = Integer.parseInt(timeParts[0]);</span>
<span class="nc" id="L183"> int minute = Integer.parseInt(timeParts[1]);</span>
<span class="nc" id="L184"> LocalTime time = LocalTime.of(hour, minute);</span>
<span class="nc" id="L186"> entries.add(new Entry(time, name));</span>
<span class="nc" id="L187"> }</span>
}
<span class="nc" id="L190"> entries.sort(Comparator.comparing(e -&gt; e.time));</span>
<span class="nc" id="L192"> try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {</span>
<span class="nc bnc" id="L193" title="All 2 branches missed."> for (Entry e : entries) {</span>
<span class="nc" id="L194"> String formattedTime = String.format(&quot;%02d:%02d&quot;, e.time.getHour(), e.time.getMinute());</span>
<span class="nc" id="L195"> bw.write(formattedTime + &quot; &quot; + e.name);</span>
<span class="nc" id="L196"> bw.newLine();</span>
<span class="nc" id="L197"> }</span>
}
<span class="nc" id="L199"> }</span>
private static void showHighscores(String path) throws FileNotFoundException, IOException{
<span class="nc" id="L202"> sortByTime(path);</span>
<span class="nc" id="L203"> JPanel mainPanel = new JPanel(new BorderLayout());</span>
<span class="nc" id="L204"> File file = new File(path);</span>
<span class="nc" id="L205"> int i = 0;</span>
<span class="nc" id="L206"> try (Scanner scanner = new Scanner(file)) {</span>
<span class="nc bnc" id="L207" title="All 2 branches missed."> while (scanner.hasNextLine()) {</span>
<span class="nc" id="L208"> scanner.nextLine();</span>
<span class="nc" id="L209"> i++;</span>
}
<span class="nc" id="L211"> } catch (FileNotFoundException e) {</span>
<span class="nc" id="L212"> e.printStackTrace();</span>
<span class="nc" id="L213"> }</span>
<span class="nc" id="L214"> JPanel highscorePanel = new JPanel(new GridLayout(i,1,10,10));</span>
<span class="nc bnc" id="L215" title="All 2 branches missed."> if(i&gt;0){</span>
<span class="nc" id="L216"> ArrayList&lt;String&gt; lines = readFromFile(path);</span>
<span class="nc bnc" id="L217" title="All 2 branches missed."> for(String s: lines){</span>
<span class="nc" id="L218"> JLabel text = new JLabel(s);</span>
<span class="nc" id="L219"> highscorePanel.add(text);</span>
<span class="nc" id="L220"> }</span>
<span class="nc" id="L221"> JButton b = new JButton(&quot;Zurück&quot;);</span>
<span class="nc" id="L222"> b.addActionListener(e -&gt; {</span>
<span class="nc" id="L223"> cl.show(main, &quot;HIGHSCORES&quot;);</span>
<span class="nc" id="L224"> highscoreScreen();</span>
<span class="nc" id="L225"> });</span>
<span class="nc" id="L226"> mainPanel.add(b, BorderLayout.SOUTH);</span>
<span class="nc" id="L227"> } else {</span>
<span class="nc" id="L228"> JLabel text = new JLabel(&quot;Noch kein Highscore eingetragen.&quot;);</span>
<span class="nc" id="L229"> highscorePanel.add(text);</span>
<span class="nc" id="L230"> JButton b = new JButton(&quot;Zurück&quot;);</span>
<span class="nc" id="L231"> b.addActionListener(e -&gt; {</span>
<span class="nc" id="L232"> cl.show(main, &quot;HIGHSCORES&quot;);</span>
<span class="nc" id="L233"> highscoreScreen();</span>
<span class="nc" id="L234"> });</span>
<span class="nc" id="L235"> mainPanel.add(b, BorderLayout.SOUTH);</span>
}
<span class="nc" id="L237"> mainPanel.add(highscorePanel, BorderLayout.CENTER);</span>
<span class="nc" id="L238"> mainPanel.setVisible(true);</span>
<span class="nc" id="L239"> mainPanel.setSize(600,600);</span>
<span class="nc" id="L240"> main.add(mainPanel, &quot;HIGHSCORES&quot;);</span>
<span class="nc" id="L241"> cl.show(main, &quot;HIGHSCORES&quot;);</span>
<span class="nc" id="L242"> }</span>
private static ArrayList&lt;String&gt; readFromFile(String path){
<span class="nc" id="L245"> ArrayList&lt;String&gt; lines = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L246"> try (BufferedReader reader = new BufferedReader(new FileReader(path))) {</span>
String line;
<span class="nc bnc" id="L248" title="All 2 branches missed."> while ((line = reader.readLine()) != null) {</span>
<span class="nc" id="L249"> lines.add(line);</span>
}
<span class="nc" id="L251"> } catch (IOException e) {</span>
<span class="nc" id="L252"> e.printStackTrace();</span>
<span class="nc" id="L253"> }</span>
<span class="nc" id="L254"> return lines;</span>
}
private static String[][] getData(String filepath, int rows) throws FileNotFoundException{
<span class="nc" id="L258"> String filepath0 = filepath;</span>
<span class="nc" id="L259"> Scanner sc = new Scanner(new File(filepath0));</span>
<span class="nc" id="L260"> String[][] data = new String[rows][rows];</span>
<span class="nc" id="L261"> int rowInt = 0;</span>
<span class="nc bnc" id="L262" title="All 4 branches missed."> while (sc.hasNextLine() &amp;&amp; rowInt &lt; rows) {</span>
<span class="nc" id="L263"> String line = sc.nextLine();</span>
<span class="nc" id="L264"> data[rowInt] = line.split(&quot;,&quot;);</span>
<span class="nc" id="L265"> rowInt++;</span>
<span class="nc" id="L266"> }</span>
<span class="nc" id="L267"> sc.close();</span>
<span class="nc" id="L268"> return data;</span>
}
public static String[][] makeColorArray(int size){
<span class="fc" id="L272"> String[][] colors = new String[size][size];</span>
<span class="fc bfc" id="L273" title="All 2 branches covered."> for(int i=0;i&lt;size;i++){</span>
<span class="fc bfc" id="L274" title="All 2 branches covered."> for(int j=0;j&lt;size;j++){</span>
<span class="fc" id="L275"> colors[i][j] = &quot;W&quot;;</span>
}
}
<span class="fc" id="L278"> return colors;</span>
}
private static JButton[][] makeButtonArray(String[][] data){
<span class="nc" id="L282"> JButton[][] buttons = new JButton[data.length][data.length];</span>
<span class="nc bnc" id="L283" title="All 2 branches missed."> for(int i=0;i&lt;data.length;i++){</span>
<span class="nc bnc" id="L284" title="All 2 branches missed."> for(int j=0;j&lt;data.length;j++){</span>
<span class="nc" id="L285"> String number = data[i][j];</span>
<span class="nc" id="L286"> JButton b0 = new JButton(number);</span>
<span class="nc" id="L287"> buttons[i][j] = b0;</span>
}
}
<span class="nc" id="L290"> return buttons;</span>
}
public static void paintGame(String[] filepath, JButton[][] buttons, String[][] colors, Stack&lt;String&gt; madeMoves, String[][] data) throws FileNotFoundException{
<span class="nc" id="L294"> elapsedSeconds = 0;</span>
<span class="nc" id="L295"> System.out.println(&quot;BB: &quot; + filepath[0]);</span>
<span class="nc" id="L296"> System.out.println(&quot;BB: &quot; + filepath[1]);</span>
<span class="nc" id="L297"> int num = buttons.length;</span>
<span class="nc" id="L298"> JPanel gameGrid = new JPanel(new GridLayout(num,num,0,0));</span>
<span class="nc bnc" id="L299" title="All 2 branches missed."> for(int i=0;i&lt;num;i++){</span>
<span class="nc bnc" id="L300" title="All 2 branches missed."> for(int j=0;j&lt;num;j++){</span>
<span class="nc" id="L301"> JButton b = buttons[i][j];</span>
<span class="nc" id="L302"> b.setOpaque(true);</span>
<span class="nc" id="L303"> b.setContentAreaFilled(true);</span>
<span class="nc" id="L304"> b.setBorderPainted(false);</span>
<span class="nc" id="L305"> b.setFocusPainted(false);</span>
<span class="nc" id="L306"> b.setBackground(Color.WHITE);</span>
<span class="nc" id="L307"> gameGrid.add(b);</span>
<span class="nc" id="L308"> String[] pos = getCords(i,j); </span>
<span class="nc" id="L309"> b.addActionListener(e -&gt; {paintButton(b, pos, colors , madeMoves);});</span>
}
}
<span class="nc" id="L312"> JPanel mainPanel = new JPanel(new BorderLayout());</span>
<span class="nc" id="L313"> mainPanel.add(gameGrid, BorderLayout.CENTER);</span>
<span class="nc" id="L314"> JPanel buttonGrid = new JPanel(new GridLayout(1,4,10,10));</span>
<span class="nc" id="L315"> JButton b0 = new JButton(&quot;Aufgeben&quot;);</span>
<span class="nc" id="L316"> b0.addActionListener(e -&gt; {</span>
<span class="nc" id="L317"> filepath[0] = &quot;&quot;;</span>
<span class="nc" id="L318"> filepath[1] = &quot;&quot;; </span>
<span class="nc" id="L319"> cl.show(main, &quot;HAUPT&quot;);</span>
<span class="nc" id="L320"> });</span>
<span class="nc" id="L321"> JButton b1 = new JButton(&quot;Zurück&quot;);</span>
<span class="nc" id="L322"> b1.addActionListener(e -&gt; {backOneStep(madeMoves, buttons, colors, gameGrid);});</span>
<span class="nc" id="L323"> JButton b2 = new JButton(&quot;Zurücksetzen&quot;);</span>
<span class="nc" id="L324"> b2.addActionListener(e -&gt; {try {</span>
<span class="nc" id="L325"> totalResetButton(buttons, colors, madeMoves, data);</span>
<span class="nc" id="L326"> } catch (FileNotFoundException e1) {</span>
<span class="nc" id="L327"> e1.printStackTrace();</span>
<span class="nc" id="L328"> }});</span>
<span class="nc" id="L329"> JButton b3 = new JButton(&quot;Abgeben&quot;);</span>
<span class="nc" id="L330"> String path = filepath[0];</span>
<span class="nc" id="L332"> boolean[] levelFinished = {false};</span>
<span class="nc" id="L333"> JPanel topGrid = new JPanel(new GridLayout(1,2,10,10));</span>
<span class="nc" id="L334"> mainPanel.add(topGrid, BorderLayout.NORTH);</span>
<span class="nc" id="L335"> JLabel timeLabel = new JLabel(&quot;Zeit: 00:00&quot;);</span>
<span class="nc" id="L336"> topGrid.add(timeLabel);</span>
<span class="nc" id="L337"> topGrid.revalidate();</span>
<span class="nc" id="L338"> topGrid.repaint();</span>
<span class="nc" id="L339"> boolean isOkay = abgabeMöglich(path, data, colors);</span>
<span class="nc" id="L340"> levelFinished[0] = isOkay; </span>
<span class="nc" id="L341"> Timer timer = new Timer();</span>
<span class="nc" id="L342"> startTimer();</span>
<span class="nc" id="L343"> timer.scheduleAtFixedRate(new TimerTask() {</span>
@Override
public void run() {
<span class="nc" id="L346"> elapsedSeconds = (System.currentTimeMillis() - startTime) / 1000;</span>
<span class="nc" id="L347"> long minutes = (elapsedSeconds % 3600) / 60;</span>
<span class="nc" id="L348"> long seconds = elapsedSeconds % 60;</span>
<span class="nc" id="L349"> String newTime = String.format(&quot;Zeit: %02d:%02d&quot;, minutes, seconds);</span>
<span class="nc" id="L351"> SwingUtilities.invokeLater(() -&gt; {</span>
<span class="nc" id="L352"> timeLabel.setText(newTime);</span>
<span class="nc" id="L353"> timeLabel.revalidate();</span>
<span class="nc" id="L354"> timeLabel.repaint();</span>
<span class="nc" id="L355"> });</span>
<span class="nc" id="L356"> }</span>
}, 0, 1000);
<span class="nc" id="L359"> JLabel luecke = new JLabel(&quot;&quot;);</span>
<span class="nc" id="L360"> topGrid.add(luecke);</span>
<span class="nc" id="L362"> b3.addActionListener(e -&gt; {</span>
try {
<span class="nc" id="L364"> levelFinished[0] = abgabeMöglich(path, data, colors);</span>
<span class="nc" id="L365"> System.out.println(&quot;0000: &quot; + levelFinished[0]);</span>
<span class="nc" id="L366"> } catch (FileNotFoundException e1) {</span>
<span class="nc" id="L367"> e1.printStackTrace();</span>
<span class="nc" id="L368"> }</span>
<span class="nc bnc" id="L369" title="All 2 branches missed."> if (levelFinished[0] == true) {</span>
<span class="nc" id="L370"> System.out.println(&quot;1111: &quot; + levelFinished[0]);</span>
<span class="nc" id="L371"> String endtime = stopTimer();</span>
<span class="nc" id="L372"> timer.cancel();</span>
try {
<span class="nc" id="L374"> finish(endtime, filepath, path, data, colors);</span>
<span class="nc" id="L375"> } catch (FileNotFoundException e1) {</span>
<span class="nc" id="L376"> e1.printStackTrace();</span>
<span class="nc" id="L377"> }</span>
<span class="nc" id="L378"> } else {</span>
<span class="nc" id="L379"> System.out.println(&quot;2222: &quot; + levelFinished[0]);</span>
<span class="nc" id="L380"> luecke.setText(&quot;Abgabe nicht richtig!&quot;);</span>
<span class="nc" id="L381"> mainPanel.revalidate();</span>
<span class="nc" id="L382"> mainPanel.repaint();</span>
}
<span class="nc" id="L384"> });</span>
<span class="nc" id="L386"> buttonGrid.add(b0);buttonGrid.add(b1);buttonGrid.add(b2);buttonGrid.add(b3);</span>
<span class="nc" id="L387"> mainPanel.add(buttonGrid, BorderLayout.SOUTH);</span>
<span class="nc" id="L388"> mainPanel.setVisible(true);</span>
<span class="nc" id="L389"> main.add(mainPanel, &quot;GAME&quot;);</span>
<span class="nc" id="L390"> cl.show(main, &quot;GAME&quot;);</span>
<span class="nc" id="L391"> }</span>
private static String startTimer() {
<span class="nc" id="L394"> startTime = System.currentTimeMillis();</span>
<span class="nc" id="L395"> elapsedSeconds = 0; </span>
<span class="nc" id="L396"> return &quot;Zeit: 00:00&quot;;</span>
}
private static String stopTimer() {
<span class="nc" id="L400"> timer.cancel();</span>
<span class="nc" id="L401"> long minutes = (elapsedSeconds % 3600) / 60;</span>
<span class="nc" id="L402"> long seconds = elapsedSeconds % 60;</span>
<span class="nc" id="L403"> String endtime = String.format(&quot;Zeit: %02d:%02d&quot;, minutes, seconds);</span>
<span class="nc" id="L404"> elapsedSeconds = 0; // Reset so the next start begins at zero</span>
<span class="nc" id="L405"> return endtime;</span>
}
private static boolean abgabeMöglich(String path, String[][] data, String[][] colors) throws FileNotFoundException{
<span class="nc" id="L409"> boolean abgabeMöglich = false;</span>
<span class="nc" id="L410"> String[][] result = getResult(data, colors);</span>
<span class="nc" id="L411"> System.out.println(&quot;RESULT&quot;);</span>
<span class="nc" id="L412"> printWholeCSV(path);</span>
<span class="nc" id="L413"> ArrayList&lt;String&gt; filteredData = getSolution(path, result);</span>
<span class="nc" id="L414"> String[][] ergebnis1 = getErgebnisArray(result, filteredData);</span>
<span class="nc" id="L415"> System.out.println(&quot;ERGEBNIS&quot;);</span>
<span class="nc" id="L416"> int count = 0;</span>
<span class="nc bnc" id="L417" title="All 2 branches missed."> for(int i=0;i&lt;result.length;i++){</span>
<span class="nc bnc" id="L418" title="All 2 branches missed."> for(int j=0;j&lt;result.length;j++){</span>
<span class="nc bnc" id="L419" title="All 2 branches missed."> if(result[i][j].equals(ergebnis1[i][j])){</span>
} else {
<span class="nc" id="L421"> count--;</span>
}
}
}
<span class="nc bnc" id="L425" title="All 2 branches missed."> if(count &lt;0){</span>
<span class="nc" id="L426"> abgabeMöglich = false;</span>
} else {
<span class="nc" id="L428"> abgabeMöglich = true;</span>
}
<span class="nc" id="L430"> return abgabeMöglich;</span>
}
public static void printWholeCSV(String path) {
<span class="nc" id="L434"> System.out.println(&quot;NOW PRINTING ALL&quot;);</span>
<span class="nc" id="L435"> try (BufferedReader br = new BufferedReader(new FileReader(path))) {</span>
String line;
<span class="nc bnc" id="L437" title="All 2 branches missed."> while ((line = br.readLine()) != null) {</span>
<span class="nc" id="L438"> System.out.println(line);</span>
}
<span class="nc" id="L440"> } catch (IOException e) {</span>
<span class="nc" id="L441"> System.out.println(&quot;An error occurred while reading the file: &quot; + e.getMessage());</span>
<span class="nc" id="L442"> }</span>
<span class="nc" id="L443"> System.out.println(&quot;DONE PRINTING ALL&quot;);</span>
<span class="nc" id="L444"> }</span>
private static void paintButton(JButton b, String[] pos, String[][] colors, Stack&lt;String&gt; madeMoves){
<span class="nc" id="L447"> int i = Integer.parseInt(pos[0]);</span>
<span class="nc" id="L448"> int j = Integer.parseInt(pos[1]);</span>
<span class="nc" id="L449"> String col = colors[Integer.parseInt(pos[0])][Integer.parseInt(pos[1])];</span>
<span class="nc bnc" id="L450" title="All 2 branches missed."> if(col.endsWith(&quot;W&quot;)){</span>
<span class="nc" id="L451"> b.setOpaque(true);</span>
<span class="nc" id="L452"> b.setForeground(Color.BLACK);</span>
<span class="nc" id="L453"> b.setContentAreaFilled(true);</span>
<span class="nc" id="L454"> b.setBorderPainted(false);</span>
<span class="nc" id="L455"> b.setFocusPainted(false);</span>
<span class="nc" id="L456"> b.setBackground(Color.lightGray);</span>
<span class="nc" id="L457"> colors[i][j] += &quot;G&quot;;</span>
<span class="nc" id="L458"> String logEntrance = i+&quot;.&quot;+j+&quot;.&quot;+&quot;G&quot;;</span>
<span class="nc" id="L459"> madeMoves.push(logEntrance);</span>
}
<span class="nc bnc" id="L461" title="All 2 branches missed."> if(col.endsWith(&quot;G&quot;)){</span>
<span class="nc" id="L462"> b.setOpaque(true);</span>
<span class="nc" id="L463"> b.setForeground(Color.WHITE);</span>
<span class="nc" id="L464"> b.setContentAreaFilled(true);</span>
<span class="nc" id="L465"> b.setBorderPainted(false);</span>
<span class="nc" id="L466"> b.setFocusPainted(false);</span>
<span class="nc" id="L467"> b.setBackground(Color.BLACK);</span>
<span class="nc" id="L468"> colors[i][j] += &quot;B&quot;;</span>
<span class="nc" id="L469"> String logEntrance = i+&quot;.&quot;+j+&quot;.&quot;+&quot;B&quot;;</span>
<span class="nc" id="L470"> madeMoves.push(logEntrance);</span>
}
<span class="nc bnc" id="L472" title="All 2 branches missed."> if(col.endsWith(&quot;B&quot;)){</span>
<span class="nc" id="L473"> b.setOpaque(true);</span>
<span class="nc" id="L474"> b.setForeground(Color.BLACK);</span>
<span class="nc" id="L475"> b.setContentAreaFilled(true);</span>
<span class="nc" id="L476"> b.setBorderPainted(false);</span>
<span class="nc" id="L477"> b.setFocusPainted(false);</span>
<span class="nc" id="L478"> b.setBackground(Color.WHITE);</span>
<span class="nc" id="L479"> colors[i][j] += &quot;W&quot;;</span>
<span class="nc" id="L480"> String logEntrance = i+&quot;.&quot;+j+&quot;.&quot;+&quot;W&quot;;</span>
<span class="nc" id="L481"> madeMoves.push(logEntrance);</span>
}
<span class="nc" id="L483"> }</span>
private static String[] getCords(int i, int j){
<span class="nc" id="L486"> String yKoordinate = String.valueOf(i);</span>
<span class="nc" id="L487"> String xKoordinate = String.valueOf(j);</span>
<span class="nc" id="L488"> String[] pos = {yKoordinate, xKoordinate};</span>
<span class="nc" id="L489"> return pos;</span>
}
public static void backOneStep(Stack&lt;String&gt; movesMade, JButton[][] buttons, String[][] colors, JPanel grid){
try {
<span class="nc" id="L494"> String move = movesMade.pop();</span>
<span class="nc" id="L495"> String[] line = move.split(&quot;\\.&quot;);</span>
<span class="nc" id="L496"> String y = line[0];</span>
<span class="nc" id="L497"> String x = line[1];</span>
<span class="nc" id="L498"> String color = line[2];</span>
<span class="nc bnc" id="L499" title="All 2 branches missed."> if(color.equals(&quot;W&quot;)){</span>
<span class="nc" id="L500"> int i = Integer.parseInt(y);</span>
<span class="nc" id="L501"> int j = Integer.parseInt(x);</span>
<span class="nc" id="L502"> JButton b0 = buttons[i][j];</span>
<span class="nc" id="L503"> b0.setOpaque(true);</span>
<span class="nc" id="L504"> b0.setForeground(Color.WHITE);</span>
<span class="nc" id="L505"> b0.setContentAreaFilled(true);</span>
<span class="nc" id="L506"> b0.setBorderPainted(false);</span>
<span class="nc" id="L507"> b0.setFocusPainted(false);</span>
<span class="nc" id="L508"> b0.setBackground(Color.BLACK);</span>
<span class="nc" id="L509"> buttons[i][j] = b0;</span>
<span class="nc" id="L510"> String str = colors[i][j]; </span>
<span class="nc" id="L511"> String str0 = str.substring(0, str.length() - 1);</span>
<span class="nc" id="L512"> colors[i][j] = str0;</span>
<span class="nc" id="L513"> grid.repaint();</span>
<span class="nc" id="L514"> gridUpdate(grid, buttons);</span>
<span class="nc bnc" id="L515" title="All 2 branches missed."> }else if(color.equals(&quot;G&quot;)){</span>
<span class="nc" id="L516"> int i = Integer.parseInt(y);</span>
<span class="nc" id="L517"> int j = Integer.parseInt(x);</span>
<span class="nc" id="L518"> JButton b0 = buttons[i][j];</span>
<span class="nc" id="L519"> b0.setOpaque(true);</span>
<span class="nc" id="L520"> b0.setForeground(Color.BLACK);</span>
<span class="nc" id="L521"> b0.setContentAreaFilled(true);</span>
<span class="nc" id="L522"> b0.setBorderPainted(false);</span>
<span class="nc" id="L523"> b0.setFocusPainted(false);</span>
<span class="nc" id="L524"> b0.setBackground(Color.WHITE);</span>
<span class="nc" id="L525"> buttons[i][j] = b0;</span>
<span class="nc" id="L526"> String str = colors[i][j]; </span>
<span class="nc" id="L527"> String str0 = str.substring(0, str.length() - 1);</span>
<span class="nc" id="L528"> colors[i][j] = str0;</span>
<span class="nc" id="L529"> gridUpdate(grid, buttons);</span>
<span class="nc bnc" id="L530" title="All 2 branches missed."> } else if(color.equals(&quot;B&quot;)){</span>
<span class="nc" id="L531"> int i = Integer.parseInt(y);</span>
<span class="nc" id="L532"> int j = Integer.parseInt(x);</span>
<span class="nc" id="L533"> JButton b0 = buttons[i][j];</span>
<span class="nc" id="L534"> b0.setOpaque(true);</span>
<span class="nc" id="L535"> b0.setForeground(Color.BLACK);</span>
<span class="nc" id="L536"> b0.setContentAreaFilled(true);</span>
<span class="nc" id="L537"> b0.setBorderPainted(false);</span>
<span class="nc" id="L538"> b0.setFocusPainted(false);</span>
<span class="nc" id="L539"> b0.setBackground(Color.lightGray);</span>
<span class="nc" id="L540"> buttons[i][j] = b0;</span>
<span class="nc" id="L541"> String str = colors[i][j]; </span>
<span class="nc" id="L542"> String str0 = str.substring(0, str.length() - 1);</span>
<span class="nc" id="L543"> colors[i][j] = str0;</span>
<span class="nc" id="L544"> gridUpdate(grid, buttons);</span>
}
<span class="nc" id="L546"> } catch(EmptyStackException e) {} </span>
<span class="nc" id="L547"> }</span>
private static void gridUpdate(JPanel grid, JButton[][] buttons){
<span class="nc" id="L550"> grid.removeAll();</span>
<span class="nc" id="L551"> grid.repaint();</span>
<span class="nc bnc" id="L552" title="All 2 branches missed."> for(int i = 0; i&lt;buttons.length; i++){</span>
<span class="nc bnc" id="L553" title="All 2 branches missed."> for(int j = 0; j&lt;buttons.length;j++){</span>
<span class="nc" id="L554"> JButton b0 = buttons[i][j];</span>
<span class="nc" id="L555"> grid.add(b0);</span>
<span class="nc" id="L556"> grid.repaint();</span>
}
}
<span class="nc" id="L559"> }</span>
private static void totalResetButton(JButton[][] buttons, String[][] colors,Stack&lt;String&gt; madeMoves,String[][] data) throws FileNotFoundException{
<span class="nc" id="L562"> madeMoves.clear();</span>
<span class="nc bnc" id="L563" title="All 2 branches missed."> for(int i = 0; i&lt;data.length;i++){</span>
<span class="nc bnc" id="L564" title="All 2 branches missed."> for(int j = 0; j&lt;data.length;j++){</span>
<span class="nc" id="L565"> colors[i][j] = &quot;W&quot;;</span>
<span class="nc" id="L566"> buttons[i][j] = null;</span>
}
<span class="nc" id="L568"> JButton[][] buttons0 = makeButtonArray(data);</span>
<span class="nc" id="L569"> paintGame(filepath, buttons0, colors, madeMoves, data); </span>
}
<span class="nc" id="L571"> }</span>
private static String[][] getResult(String[][] data, String[][] colors){
<span class="nc" id="L574"> String[][] result = new String[data.length][data.length];</span>
<span class="nc bnc" id="L575" title="All 2 branches missed."> for(int i=0;i&lt;data.length;i++){</span>
<span class="nc bnc" id="L576" title="All 2 branches missed."> for(int j=0;j&lt;data.length;j++){</span>
<span class="nc" id="L577"> String farben = colors[i][j];</span>
<span class="nc" id="L578"> String lastColor = String.valueOf(farben.charAt(farben.length() - 1));</span>
<span class="nc bnc" id="L579" title="All 2 branches missed."> if(lastColor.equals(&quot;G&quot;)){</span>
<span class="nc" id="L580"> lastColor = &quot;W&quot;;</span>
}
<span class="nc" id="L582"> result[i][j] = lastColor;</span>
}
}
<span class="nc" id="L585"> System.out.println(&quot;AAAAAAAA&quot;);</span>
<span class="nc" id="L586"> return result;</span>
}
private static ArrayList&lt;String&gt; getSolution(String path, String[][] result) throws FileNotFoundException {
<span class="nc" id="L590"> Scanner sc = new Scanner(new File(path)); </span>
<span class="nc" id="L591"> ArrayList&lt;String&gt; filteredData = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L592"> boolean isUnderComment = false;</span>
<span class="nc bnc" id="L593" title="All 2 branches missed."> while (sc.hasNextLine()) {</span>
<span class="nc" id="L594"> String line = sc.nextLine().trim();</span>
<span class="nc bnc" id="L595" title="All 2 branches missed."> if (line.equals(&quot;//Lösung (schwarze Felder)&quot;)) {</span>
<span class="nc" id="L596"> isUnderComment = true; </span>
<span class="nc" id="L597"> continue;</span>
}
<span class="nc bnc" id="L599" title="All 4 branches missed."> if (isUnderComment &amp;&amp; !line.isEmpty()) {</span>
<span class="nc" id="L600"> String[] lineArray = line.split(&quot;,&quot;);</span>
<span class="nc" id="L601"> int num1 = Integer.parseInt(lineArray[0]);</span>
<span class="nc" id="L602"> int num2 = Integer.parseInt(lineArray[1]);</span>
<span class="nc" id="L603"> String newNum1 = String.valueOf(num1-1);</span>
<span class="nc" id="L604"> String newNum2 = String.valueOf(num2-1);</span>
<span class="nc" id="L605"> String newLine = newNum1+&quot;,&quot;+newNum2;</span>
<span class="nc" id="L606"> filteredData.add(newLine);</span>
}
<span class="nc" id="L608"> }</span>
<span class="nc" id="L609"> sc.close();</span>
<span class="nc" id="L610"> System.out.println(&quot;BBBBBB&quot;);</span>
<span class="nc" id="L611"> System.out.println(filteredData);</span>
<span class="nc" id="L612"> return filteredData;</span>
}
private static String[][] getErgebnisArray(String[][] result, ArrayList&lt;String&gt; filteredData){
<span class="nc" id="L616"> String[][] ergebnis = new String[result.length][result.length];</span>
<span class="nc bnc" id="L617" title="All 2 branches missed."> for(int i=0;i&lt;result.length;i++){</span>
<span class="nc bnc" id="L618" title="All 2 branches missed."> for(int j=0;j&lt;result.length;j++){</span>
<span class="nc" id="L619"> ergebnis[i][j] = &quot;W&quot;;</span>
}
}
<span class="nc bnc" id="L622" title="All 2 branches missed."> for (String index : filteredData) { </span>
<span class="nc" id="L623"> String[] parts = index.split(&quot;,&quot;);</span>
<span class="nc" id="L624"> int row = Integer.parseInt(parts[0].trim());</span>
<span class="nc" id="L625"> int col = Integer.parseInt(parts[1].trim());</span>
<span class="nc bnc" id="L626" title="All 8 branches missed."> if (row &gt;= 0 &amp;&amp; row &lt; ergebnis.length &amp;&amp; col &gt;= 0 &amp;&amp; col &lt; ergebnis[row].length) {</span>
<span class="nc" id="L627"> ergebnis[row][col] = &quot;B&quot;; </span>
}
<span class="nc" id="L629"> }</span>
<span class="nc" id="L630"> System.out.println(&quot;ERGEBNISMETHODE&quot;);</span>
<span class="nc" id="L631"> return ergebnis;</span>
}
public static void finish(String endtime, String[] filepath, String path, String[][] data, String[][] colors) throws FileNotFoundException{
<span class="nc" id="L635"> JPanel mainPanel = new JPanel(new BorderLayout());</span>
<span class="nc" id="L636"> JLabel text = new JLabel(&quot;Geben Sie unten Ihren Namen an um sich in der Highscore Liste einzutragen.&quot;);</span>
<span class="nc" id="L637"> mainPanel.add(text, BorderLayout.NORTH);</span>
<span class="nc" id="L638"> JTextField field = new JTextField(20);</span>
<span class="nc" id="L639"> mainPanel.add(field, BorderLayout.CENTER);</span>
<span class="nc" id="L640"> JButton b = new JButton(&quot;In der Highscore Liste eintragen&quot;);</span>
<span class="nc" id="L641"> mainPanel.add(b, BorderLayout.SOUTH);</span>
<span class="nc" id="L642"> mainPanel.setVisible(true);</span>
<span class="nc" id="L643"> mainPanel.setSize(600,600);</span>
<span class="nc" id="L644"> b.addActionListener(e -&gt; {</span>
<span class="nc" id="L645"> String username = field.getText();</span>
try {
<span class="nc" id="L647"> LogTest.newRecord(path, username, endtime);</span>
<span class="nc" id="L648"> filepath[0] = &quot;&quot;;</span>
<span class="nc" id="L649"> filepath[1] = &quot;&quot;;</span>
<span class="nc" id="L650"> cl.show(main, &quot;HAUPT&quot;);</span>
<span class="nc" id="L651"> } catch (FileNotFoundException e1) {</span>
<span class="nc" id="L652"> e1.printStackTrace();</span>
<span class="nc" id="L653"> } </span>
<span class="nc" id="L654"> });</span>
<span class="nc" id="L655"> main.add(mainPanel, &quot;HIGHSCORENEU&quot;);</span>
<span class="nc" id="L656"> cl.show(main, &quot;HIGHSCORENEU&quot;);</span>
<span class="nc" id="L657"> }</span>
// private static void newRecord(String path, String username, String time) throws FileNotFoundException{
// String timePart = time.substring(&quot;Zeit: &quot;.length()).trim();
// String[] parts = path.split(&quot;/&quot;);
// String filename = parts[parts.length - 1];
// int dotIndex = filename.lastIndexOf(&quot;.&quot;);
// String result = filename.substring(0, dotIndex);
// String ordner = &quot;Hitori_Highscores/&quot;;
// String filetype = &quot;.txt&quot;;
// String filepath = ordner+result+filetype;
// Scanner sc = new Scanner(filepath);
// while (sc.hasNextLine()) {
// sc.nextLine();
// }
// sc.close();
// try (BufferedWriter writer = new BufferedWriter(new FileWriter(filepath, true))) {
// String eintrag = timePart + &quot; &quot; + username;
// System.out.println(eintrag);
// writer.write(timePart + &quot; &quot; + username);
// writer.newLine();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
@Override
public void actionPerformed(ActionEvent e){
<span class="nc" id="L685"> }</span>
}
class Entry {
LocalTime time;
String name;
<span class="nc" id="L692"> Entry(LocalTime time, String name) {</span>
<span class="nc" id="L693"> this.time = time;</span>
<span class="nc" id="L694"> this.name = name;</span>
<span class="nc" id="L695"> }</span>
}
<span class="nc" id="L699">class LogDemo{</span>
<span class="nc" id="L700"> private static final Logger log = Logger.getLogger(LogDemo.class.getName());</span>
public static void main(String[] args){
<span class="nc" id="L703"> log.info(&quot;LOS GEHTS!&quot;);</span>
try {
<span class="nc" id="L706"> ((Object) null).toString();</span>
<span class="nc" id="L707"> } catch (Exception e) {</span>
<span class="nc" id="L708"> log.log(Level.SEVERE, &quot;oh oh&quot;, e);</span>
<span class="nc" id="L709"> }</span>
<span class="nc" id="L711"> log.info(&quot;GING GUT.&quot;);</span>
<span class="nc" id="L712"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html>