finished the game
This commit is contained in:
+8
@@ -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>
|
||||
@@ -17,5 +17,15 @@
|
||||
<SOURCES />
|
||||
</library>
|
||||
</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>
|
||||
</module>
|
||||
@@ -14,6 +14,8 @@ public class Board extends JPanel implements ActionListener {
|
||||
private final int TILE_Y = 300;
|
||||
private final int DELAY = 50;
|
||||
|
||||
private boolean ended = false;
|
||||
|
||||
private boolean gameWon = false;
|
||||
private boolean initialized = false;
|
||||
|
||||
@@ -37,7 +39,6 @@ public class Board extends JPanel implements ActionListener {
|
||||
int column = e.getX()/TILE_X;
|
||||
int row = e.getY()/TILE_Y;
|
||||
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++;
|
||||
}
|
||||
}
|
||||
if (ended) {
|
||||
painter.paintWinnerLine(g);
|
||||
}
|
||||
}
|
||||
|
||||
public void resetBoard(){
|
||||
for (int i = 0; i < game.getPlayfield().length; i++){
|
||||
game.setPlayfield(i, 0);
|
||||
}
|
||||
timer.start();
|
||||
oldPlayfield = game.getPlayfield().clone();
|
||||
game.setTurnTaken(false);
|
||||
}
|
||||
ended = false;
|
||||
repaint();
|
||||
}
|
||||
|
||||
@@ -110,10 +114,10 @@ public class Board extends JPanel implements ActionListener {
|
||||
}
|
||||
//stop timer if game won
|
||||
else if (gameWon || game.emptyTiles() == 0) {
|
||||
ended = true;
|
||||
setWinningLine();
|
||||
repaint();
|
||||
timer.stop();
|
||||
System.out.println("Game ended");
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
int n = JOptionPane.showConfirmDialog(null, "Do you want to play again?");
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class Game {
|
||||
|
||||
@@ -30,8 +29,7 @@ public class Game {
|
||||
e.printStackTrace();
|
||||
}
|
||||
while(!isPlaced){
|
||||
int random = (int) (Math.random() * (8 - 0 + 1) + 0);
|
||||
System.out.println(random);
|
||||
int random = (int) (Math.random() * 9);
|
||||
// if field is free
|
||||
if (playfield[random] == 0) {
|
||||
place(random, -1);
|
||||
@@ -46,22 +44,29 @@ public class Game {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
//horizontal
|
||||
if ((playfield[i] == playfield[i + 3] && playfield[i] != 0) && (playfield[i] == playfield[i + 6])) {
|
||||
winningX1 = 0;
|
||||
winningX2 = 3;
|
||||
winningY1 = winningY2 = i;
|
||||
winningX1 = 75;
|
||||
winningX2 = 825;
|
||||
winningY1 = winningY2 = i * 300 + 150;
|
||||
return true;
|
||||
}
|
||||
//vertical
|
||||
else if ((playfield[i * 3] == playfield[i * 3 + 1] && playfield[i * 3] != 0) && (playfield[i * 3] == playfield[i * 3 + 2])) {
|
||||
winningX1 = winningX2 = i;
|
||||
winningY1 = 0;
|
||||
winningY2 = 3;
|
||||
winningY1 = 75;
|
||||
winningY2 = 825;
|
||||
winningX1 = winningX2 = i * 300 + 150;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//diagonal
|
||||
return (playfield[2] == playfield[4] && playfield[2] != 0) && (playfield[2] == playfield[6]) ||
|
||||
(playfield[0] == playfield[4] && playfield[0] != 0) && (playfield[0] == playfield[8]);
|
||||
if ((playfield[2] == playfield[4] && playfield[2] != 0) && (playfield[2] == playfield[6])){
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: Executor
|
||||
|
||||
@@ -52,8 +52,7 @@ public class Painter {
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.setColor(Color.RED);
|
||||
g2d.setStroke(new BasicStroke(40));
|
||||
System.out.println(winningX1 + ", " + winningY1 + ", " + winningX2 + ", " + winningY2);
|
||||
g2d.drawLine(winningX1 * 300 + 150, winningY1 * 300 + 150, winningX2 * 300 + 150, winningY2 * 300 + 150);
|
||||
g2d.drawLine(winningX1, winningY1, winningX2, winningY2);
|
||||
}
|
||||
|
||||
public void setWinningX1(int winningX1) {
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,3 +0,0 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: Operator
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: Executor
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user