basic game tic tac toe
This commit is contained in:
@@ -1,2 +1,93 @@
|
|||||||
public class Board {
|
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 gameWon = false;
|
||||||
|
private boolean initialized = 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 MAdapter());
|
||||||
|
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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (!game.checkWin() && isChanged(oldPlayfield)){
|
||||||
|
repaint();
|
||||||
|
oldPlayfield = game.getPlayfield().clone();
|
||||||
|
System.out.println(2);
|
||||||
|
} else if (game.checkWin()){
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isChanged(int[] playfield){
|
||||||
|
return !Arrays.equals(game.getPlayfield(), playfield);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class MAdapter extends MouseAdapter{
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
super.mouseClicked(e);
|
||||||
|
int column = e.getX()/TILE_X;
|
||||||
|
int row = e.getY()/TILE_Y;
|
||||||
|
game.setPlayfield(column * 3 + row, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +1,21 @@
|
|||||||
public class Executor {
|
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) {
|
||||||
|
Executor exc = new Executor();
|
||||||
|
exc.setVisible(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
53
TicTacToe - MinMax/src/Game.java
Normal file
53
TicTacToe - MinMax/src/Game.java
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class Game {
|
||||||
|
|
||||||
|
int[] playfield;
|
||||||
|
|
||||||
|
public Game(){
|
||||||
|
playfield = new int[9];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void place(int player, int position){
|
||||||
|
if (playfield[position] == -1){
|
||||||
|
playfield[position] = player;
|
||||||
|
} else {
|
||||||
|
JOptionPane.showConfirmDialog(null,"Tile is already taken");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean checkWin() {
|
||||||
|
if (emptyTiles() < 5) {
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
if ((playfield[i] == playfield[i + 3]) && (playfield[i] == playfield[i + 6])) {
|
||||||
|
System.out.println("vertical");
|
||||||
|
return true;
|
||||||
|
} else if ((playfield[i * 3] == playfield[i * 3 + 1]) && (playfield[i * 3] == playfield[i * 3 + 2])) {
|
||||||
|
System.out.println("horizontal");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (playfield[2] == playfield[4]) && (playfield[2] == playfield[6]) || (playfield[0] == playfield[4]) && (playfield[0] == playfield[8]);
|
||||||
|
} else {
|
||||||
|
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 void setPlayfield(int position, int value) {
|
||||||
|
playfield[position] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] getPlayfield() {
|
||||||
|
return playfield;
|
||||||
|
}
|
||||||
|
}
|
||||||
48
TicTacToe - MinMax/src/Painter.java
Normal file
48
TicTacToe - MinMax/src/Painter.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class Painter {
|
||||||
|
|
||||||
|
private final int TILE_X;
|
||||||
|
private final int TILE_Y;
|
||||||
|
|
||||||
|
public Painter(int boardWidth, int boardHeight){
|
||||||
|
TILE_X = boardWidth/3;
|
||||||
|
TILE_Y = boardWidth/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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
out/production/TicTacToe - MinMax/Board$MAdapter.class
Normal file
BIN
out/production/TicTacToe - MinMax/Board$MAdapter.class
Normal file
Binary file not shown.
BIN
out/production/TicTacToe - MinMax/Board.class
Normal file
BIN
out/production/TicTacToe - MinMax/Board.class
Normal file
Binary file not shown.
BIN
out/production/TicTacToe - MinMax/Executor.class
Normal file
BIN
out/production/TicTacToe - MinMax/Executor.class
Normal file
Binary file not shown.
BIN
out/production/TicTacToe - MinMax/Game.class
Normal file
BIN
out/production/TicTacToe - MinMax/Game.class
Normal file
Binary file not shown.
BIN
out/production/TicTacToe - MinMax/Painter.class
Normal file
BIN
out/production/TicTacToe - MinMax/Painter.class
Normal file
Binary file not shown.
Reference in New Issue
Block a user