finished the game

This commit is contained in:
2020-07-16 02:20:20 +02:00
parent 68fbb6531c
commit 794fd33a2e
100 changed files with 51 additions and 22 deletions

View File

@@ -0,0 +1,8 @@
<component name="ArtifactManager">
<artifact type="jar" name="TicTacToe - MinMax:jar">
<output-path>$PROJECT_DIR$/out/artifacts/TicTacToe___MinMax_jar</output-path>
<root id="archive" name="TicTacToe - MinMax.jar">
<element id="module-output" name="TicTacToe - MinMax" />
</root>
</artifact>
</component>

View File

@@ -17,5 +17,15 @@
<SOURCES /> <SOURCES />
</library> </library>
</orderEntry> </orderEntry>
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component> </component>
</module> </module>

View File

@@ -14,6 +14,8 @@ public class Board extends JPanel implements ActionListener {
private final int TILE_Y = 300; private final int TILE_Y = 300;
private final int DELAY = 50; private final int DELAY = 50;
private boolean ended = false;
private boolean gameWon = false; private boolean gameWon = false;
private boolean initialized = false; private boolean initialized = false;
@@ -37,7 +39,6 @@ public class Board extends JPanel implements ActionListener {
int column = e.getX()/TILE_X; int column = e.getX()/TILE_X;
int row = e.getY()/TILE_Y; int row = e.getY()/TILE_Y;
game.place(column * 3 + row, 1); game.place(column * 3 + row, 1);
System.out.println(Arrays.toString(game.getPlayfield()));
} }
}); });
@@ -74,16 +75,19 @@ public class Board extends JPanel implements ActionListener {
actions++; actions++;
} }
} }
painter.paintWinnerLine(g); if (ended) {
painter.paintWinnerLine(g);
}
} }
public void resetBoard(){ public void resetBoard(){
for (int i = 0; i < game.getPlayfield().length; i++){ for (int i = 0; i < game.getPlayfield().length; i++){
game.setPlayfield(i, 0); game.setPlayfield(i, 0);
timer.start();
oldPlayfield = game.getPlayfield().clone();
game.setTurnTaken(false);
} }
timer.start();
oldPlayfield = game.getPlayfield().clone();
game.setTurnTaken(false);
ended = false;
repaint(); repaint();
} }
@@ -110,10 +114,10 @@ public class Board extends JPanel implements ActionListener {
} }
//stop timer if game won //stop timer if game won
else if (gameWon || game.emptyTiles() == 0) { else if (gameWon || game.emptyTiles() == 0) {
ended = true;
setWinningLine(); setWinningLine();
repaint(); repaint();
timer.stop(); timer.stop();
System.out.println("Game ended");
try { try {
Thread.sleep(1000); Thread.sleep(1000);
int n = JOptionPane.showConfirmDialog(null, "Do you want to play again?"); int n = JOptionPane.showConfirmDialog(null, "Do you want to play again?");

View File

@@ -1,5 +1,4 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*;
public class Game { public class Game {
@@ -30,8 +29,7 @@ public class Game {
e.printStackTrace(); e.printStackTrace();
} }
while(!isPlaced){ while(!isPlaced){
int random = (int) (Math.random() * (8 - 0 + 1) + 0); int random = (int) (Math.random() * 9);
System.out.println(random);
// if field is free // if field is free
if (playfield[random] == 0) { if (playfield[random] == 0) {
place(random, -1); place(random, -1);
@@ -46,22 +44,29 @@ public class Game {
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
//horizontal //horizontal
if ((playfield[i] == playfield[i + 3] && playfield[i] != 0) && (playfield[i] == playfield[i + 6])) { if ((playfield[i] == playfield[i + 3] && playfield[i] != 0) && (playfield[i] == playfield[i + 6])) {
winningX1 = 0; winningX1 = 75;
winningX2 = 3; winningX2 = 825;
winningY1 = winningY2 = i; winningY1 = winningY2 = i * 300 + 150;
return true; return true;
} }
//vertical //vertical
else if ((playfield[i * 3] == playfield[i * 3 + 1] && playfield[i * 3] != 0) && (playfield[i * 3] == playfield[i * 3 + 2])) { else if ((playfield[i * 3] == playfield[i * 3 + 1] && playfield[i * 3] != 0) && (playfield[i * 3] == playfield[i * 3 + 2])) {
winningX1 = winningX2 = i; winningY1 = 75;
winningY1 = 0; winningY2 = 825;
winningY2 = 3; winningX1 = winningX2 = i * 300 + 150;
return true; return true;
} }
} }
//diagonal //diagonal
return (playfield[2] == playfield[4] && playfield[2] != 0) && (playfield[2] == playfield[6]) || if ((playfield[2] == playfield[4] && playfield[2] != 0) && (playfield[2] == playfield[6])){
(playfield[0] == playfield[4] && playfield[0] != 0) && (playfield[0] == playfield[8]); winningX2 = winningY1 = 75;
winningX1 = winningY2 = 825;
return true;
} else if ((playfield[0] == playfield[4] && playfield[0] != 0) && (playfield[0] == playfield[8])){
winningX1 = winningY1 = 75;
winningX2 = winningY2 = 825;
return true;
}
} }
return false; return false;
} }

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Executor

View File

@@ -52,8 +52,7 @@ public class Painter {
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED); g2d.setColor(Color.RED);
g2d.setStroke(new BasicStroke(40)); g2d.setStroke(new BasicStroke(40));
System.out.println(winningX1 + ", " + winningY1 + ", " + winningX2 + ", " + winningY2); g2d.drawLine(winningX1, winningY1, winningX2, winningY2);
g2d.drawLine(winningX1 * 300 + 150, winningY1 * 300 + 150, winningX2 * 300 + 150, winningY2 * 300 + 150);
} }
public void setWinningX1(int winningX1) { public void setWinningX1(int winningX1) {

Binary file not shown.

View File

@@ -1,3 +0,0 @@
Manifest-Version: 1.0
Main-Class: Operator

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Executor