cleaned the repo

This commit is contained in:
2020-08-26 22:51:28 +02:00
parent 8a0afc37ba
commit 55c28fad29
19 changed files with 5 additions and 166 deletions

12
TicTacToe/TicTacToe.iml Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="TOOLS" exported="" />
</component>
</module>

146
TicTacToe/src/Board.java Normal file
View File

@@ -0,0 +1,146 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
public class Board extends JPanel implements ActionListener {
private final int B_WIDTH = 900;
private final int B_HEIGHT = 900;
private final int TILE_X = 300;
private final int TILE_Y = 300;
private final int DELAY = 50;
private boolean ended = false;
private boolean gameWon = false;
int[] oldPlayfield;
private Timer timer;
private Game game;
private Painter painter;
public Board(){
initBoard();
}
private void initBoard(){
painter = new Painter(B_WIDTH,B_HEIGHT);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
int column = e.getX()/TILE_X;
int row = e.getY()/TILE_Y;
game.place(column * 3 + row, 1);
}
});
setBackground(Color.BLACK);
setFocusable(true);
setPreferredSize(new Dimension(B_WIDTH,B_HEIGHT));
initGame();
}
private void initGame(){
game = new Game();
oldPlayfield = game.getPlayfield().clone();
timer = new Timer(DELAY, this);
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
painter.paintGrid(g);
updateBoard(g);
}
private void updateBoard(Graphics g){
int actions = 0;
for (int column = 0; column < 3; column++) {
for (int row = 0; row < 3; row++) {
if (game.getPlayfield()[actions] == 1) {
painter.drawX(g, column, row);
} else if (game.getPlayfield()[actions] == -1) {
painter.drawO(g, column, row);
}
actions++;
}
}
if (gameWon) {
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);
gameWon = false;
repaint();
}
public void setWinningLine(){
painter.setWinningX1(game.getWinningX1());
painter.setWinningY1(game.getWinningY1());
painter.setWinningX2(game.getWinningX2());
painter.setWinningY2(game.getWinningY2());
}
//game controlling method
@Override
public void actionPerformed(ActionEvent e) {
Thread actionThread = new Thread(){
@Override
public void run() {
//check if game state evaluation needs to be done
if (isChanged(oldPlayfield)) {
gameWon = game.checkWin();
//repaint board if not won
if (!gameWon) {
repaint();
oldPlayfield = game.getPlayfield().clone();
}
//stop timer if game won
if (gameWon || game.emptyTiles() == 0) {
if (gameWon) {
setWinningLine();
}
repaint();
timer.stop();
try {
Thread.sleep(1000);
int n = JOptionPane.showConfirmDialog(null, "Do you want to play again?");
if (n == 0){
resetBoard();
} else {
System.exit(0);
}
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
}
//check if computer needs to take a turn
if (game.isTurnTaken()){
game.setTurnTaken(false);
game.computersTurn();
}
}
};
actionThread.start();
}
private boolean isChanged(int[] playfield){
return !Arrays.equals(game.getPlayfield(), playfield);
}
}

View File

@@ -0,0 +1,26 @@
import javax.swing.*;
public class Executor extends JFrame {
public Executor(){
initUI();
}
private void initUI(){
Board board = new Board();
setTitle("TicTacToe - MinMax");
add(board);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Executor exc = new Executor();
exc.setVisible(true);
}
});
}
}

116
TicTacToe/src/Game.java Normal file
View File

@@ -0,0 +1,116 @@
import javax.swing.*;
public class Game {
private int[] playfield;
private boolean turnTaken = false;
private int winningX1,winningY1,winningX2,winningY2;
public Game(){
playfield = new int[9];
}
public void place(int position, int player){
if (playfield[position] == 0){
playfield[position] = player;
if (player == 1) {
turnTaken = true;
}
} else {
JOptionPane.showInternalMessageDialog(null,"Tile is already taken");
}
}
public void computersTurn(){
boolean isPlaced = false;
try {
Thread.sleep(750);
} catch (InterruptedException e) {
e.printStackTrace();
}
while(!isPlaced){
int random = (int) (Math.random() * 9);
// if field is free
if (playfield[random] == 0) {
place(random, -1);
isPlaced = true;
}
}
}
public boolean checkWin() {
//only check if winning is possible
if (emptyTiles() < 5) {
for (int i = 0; i < 3; i++) {
//horizontal
if ((playfield[i] == playfield[i + 3] && playfield[i] != 0) && (playfield[i] == playfield[i + 6])) {
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])) {
winningY1 = 75;
winningY2 = 825;
winningX1 = winningX2 = i * 300 + 150;
return true;
}
}
//diagonal
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;
}
public int emptyTiles(){
int n = 9;
for (int i = 0; i < playfield.length; i++){
if (playfield[i] != 0){
n -= 1;
}
}
return n;
}
public boolean isTurnTaken() {
return turnTaken;
}
public void setTurnTaken(boolean turnTaken) {
this.turnTaken = turnTaken;
}
public void setPlayfield(int position, int value) {
playfield[position] = value;
}
public int[] getPlayfield() {
return playfield;
}
public int getWinningX1() {
return winningX1;
}
public int getWinningX2() {
return winningX2;
}
public int getWinningY1() {
return winningY1;
}
public int getWinningY2() {
return winningY2;
}
}

View File

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

View File

@@ -0,0 +1,73 @@
import java.awt.*;
public class Painter {
private final int TILE_X;
private final int TILE_Y;
private int winningX1, winningY1, winningX2, winningY2;
public Painter(int boardWidth, int boardHeight){
TILE_X = boardWidth/3;
TILE_Y = boardHeight/3;
}
public void drawX(Graphics g, int column, int row) {
Graphics2D g2d = (Graphics2D) g;
int nextColumn = column + 1;
int nextRow = row + 1;
int x1 = column * TILE_X + 25;
int x2 = nextColumn * TILE_Y - 25;
int y1 = row * TILE_X + 25;
int y2 = nextRow * TILE_Y - 25;
g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(5));
g2d.drawLine(x1, y1, x2, y2);
g2d.drawLine(x1, y2, x2, y1);
}
public void drawO(Graphics g, int column, int row){
int x = column * TILE_X + 25;
int y = row * TILE_Y + 25;
g.drawOval(x,y,250,250);
}
public void paintGrid(Graphics g){
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(10));
//horizontal
for (int i = 1; i < 3; i++) {
g2d.drawLine(0, TILE_Y*i, TILE_X*3, TILE_Y*i);
}
//vertical
for (int i = 1; i < 3; i++){
g2d.drawLine(TILE_X*i, 0, TILE_X*i, TILE_Y*3);
}
}
public void paintWinnerLine(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.setStroke(new BasicStroke(40));
g2d.drawLine(winningX1, winningY1, winningX2, winningY2);
}
public void setWinningX1(int winningX1) {
this.winningX1 = winningX1;
}
public void setWinningX2(int winningX2) {
this.winningX2 = winningX2;
}
public void setWinningY1(int winningY1) {
this.winningY1 = winningY1;
}
public void setWinningY2(int winningY2) {
this.winningY2 = winningY2;
}
}

View File

@@ -0,0 +1,9 @@
Variables:
player, board, search_depth
Data_structure:
BinaryTree
Loops:
recursion
for-loop