perfected the tic tac toe game
This commit is contained in:
@@ -15,9 +15,7 @@ public class Board extends JPanel implements ActionListener {
|
|||||||
private final int DELAY = 50;
|
private final int DELAY = 50;
|
||||||
|
|
||||||
private boolean ended = false;
|
private boolean ended = false;
|
||||||
|
|
||||||
private boolean gameWon = false;
|
private boolean gameWon = false;
|
||||||
private boolean initialized = false;
|
|
||||||
|
|
||||||
int[] oldPlayfield;
|
int[] oldPlayfield;
|
||||||
|
|
||||||
@@ -75,7 +73,7 @@ public class Board extends JPanel implements ActionListener {
|
|||||||
actions++;
|
actions++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ended) {
|
if (gameWon) {
|
||||||
painter.paintWinnerLine(g);
|
painter.paintWinnerLine(g);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,7 +85,7 @@ public class Board extends JPanel implements ActionListener {
|
|||||||
timer.start();
|
timer.start();
|
||||||
oldPlayfield = game.getPlayfield().clone();
|
oldPlayfield = game.getPlayfield().clone();
|
||||||
game.setTurnTaken(false);
|
game.setTurnTaken(false);
|
||||||
ended = false;
|
gameWon = false;
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,9 +111,10 @@ public class Board extends JPanel implements ActionListener {
|
|||||||
oldPlayfield = game.getPlayfield().clone();
|
oldPlayfield = game.getPlayfield().clone();
|
||||||
}
|
}
|
||||||
//stop timer if game won
|
//stop timer if game won
|
||||||
else if (gameWon || game.emptyTiles() == 0) {
|
if (gameWon || game.emptyTiles() == 0) {
|
||||||
ended = true;
|
if (gameWon) {
|
||||||
setWinningLine();
|
setWinningLine();
|
||||||
|
}
|
||||||
repaint();
|
repaint();
|
||||||
timer.stop();
|
timer.stop();
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -3,11 +3,13 @@ import javax.swing.*;
|
|||||||
public class Game {
|
public class Game {
|
||||||
|
|
||||||
private int[] playfield;
|
private int[] playfield;
|
||||||
|
private int boardValue;
|
||||||
private boolean turnTaken = false;
|
private boolean turnTaken = false;
|
||||||
private int winningX1,winningY1,winningX2,winningY2;
|
private int winningX1,winningY1,winningX2,winningY2;
|
||||||
|
|
||||||
public Game(){
|
public Game(){
|
||||||
playfield = new int[9];
|
playfield = new int[9];
|
||||||
|
boardValue = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void place(int position, int player){
|
public void place(int position, int player){
|
||||||
@@ -22,21 +24,19 @@ public class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void computersTurn(){
|
public void computersTurn(){
|
||||||
boolean isPlaced = false;
|
for (int i = 0; i < playfield.length; i++){
|
||||||
try {
|
if (playfield[i] == 0){
|
||||||
Thread.sleep(750);
|
if (minmax(playfield, 0, true) == 1){
|
||||||
} catch (InterruptedException e) {
|
place(i, -1);
|
||||||
e.printStackTrace();
|
return;
|
||||||
}
|
|
||||||
while(!isPlaced){
|
|
||||||
int random = (int) (Math.random() * 9);
|
|
||||||
// if field is free
|
|
||||||
if (playfield[random] == 0) {
|
|
||||||
place(random, -1);
|
|
||||||
isPlaced = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int minmax(int[] board, int depth, boolean isMaximizing){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean checkWin() {
|
public boolean checkWin() {
|
||||||
//only check if winning is possible
|
//only check if winning is possible
|
||||||
@@ -47,6 +47,11 @@ public class Game {
|
|||||||
winningX1 = 75;
|
winningX1 = 75;
|
||||||
winningX2 = 825;
|
winningX2 = 825;
|
||||||
winningY1 = winningY2 = i * 300 + 150;
|
winningY1 = winningY2 = i * 300 + 150;
|
||||||
|
if (playfield[i] == 1){
|
||||||
|
boardValue = 10;
|
||||||
|
} else {
|
||||||
|
boardValue = -10;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//vertical
|
//vertical
|
||||||
@@ -54,6 +59,11 @@ public class Game {
|
|||||||
winningY1 = 75;
|
winningY1 = 75;
|
||||||
winningY2 = 825;
|
winningY2 = 825;
|
||||||
winningX1 = winningX2 = i * 300 + 150;
|
winningX1 = winningX2 = i * 300 + 150;
|
||||||
|
if (playfield[i * 3] == 1){
|
||||||
|
boardValue = 10;
|
||||||
|
} else {
|
||||||
|
boardValue = -10;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,13 +71,24 @@ public class Game {
|
|||||||
if ((playfield[2] == playfield[4] && playfield[2] != 0) && (playfield[2] == playfield[6])){
|
if ((playfield[2] == playfield[4] && playfield[2] != 0) && (playfield[2] == playfield[6])){
|
||||||
winningX2 = winningY1 = 75;
|
winningX2 = winningY1 = 75;
|
||||||
winningX1 = winningY2 = 825;
|
winningX1 = winningY2 = 825;
|
||||||
|
if (playfield[2] == 1){
|
||||||
|
boardValue = 10;
|
||||||
|
} else {
|
||||||
|
boardValue = -10;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
} else if ((playfield[0] == playfield[4] && playfield[0] != 0) && (playfield[0] == playfield[8])){
|
} else if ((playfield[0] == playfield[4] && playfield[0] != 0) && (playfield[0] == playfield[8])){
|
||||||
winningX1 = winningY1 = 75;
|
winningX1 = winningY1 = 75;
|
||||||
winningX2 = winningY2 = 825;
|
winningX2 = winningY2 = 825;
|
||||||
|
if (playfield[0] == 1){
|
||||||
|
boardValue = 10;
|
||||||
|
} else {
|
||||||
|
boardValue = -10;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
boardValue = 0;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +99,6 @@ public class Game {
|
|||||||
n -= 1;
|
n -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println(n);
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,4 +133,12 @@ public class Game {
|
|||||||
public int getWinningY2() {
|
public int getWinningY2() {
|
||||||
return winningY2;
|
return winningY2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getBoardValue() {
|
||||||
|
return boardValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBoardValue(int boardValue) {
|
||||||
|
this.boardValue = boardValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user