diff --git a/BoardPanel.java b/BoardPanel.java new file mode 100644 index 0000000..735c402 --- /dev/null +++ b/BoardPanel.java @@ -0,0 +1,170 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.net.URL; + +public class BoardPanel extends JPanel implements MouseListener { + private static final int[] HORIZONTAL_POSITIONS = { 44, 223, 405 }; + private static final int[] VERTICAL_POSITION = { 40, 224, 403 }; + private static final int FIGURE_WIDTH = 110; + private static final int FIGURE_HEIGHT = 110; + + private Image boardImage; + private Image[] boardFigures = new Image[9]; + private Image[] catImages = new Image[9]; + private Image[] dogImages = new Image[9]; + private TIC_TAC_TOE game; + + public BoardPanel() { + initGame(); + initComponents(); + } + + private void initGame() { + game = new TIC_TAC_TOE(); + game.resetBoard(); + } + + private void initComponents() { + // load images + boardImage = getImage("board.png"); + loadPieceImages("cat", catImages); + loadPieceImages("dog", dogImages); + + // Listen to the mouse + addMouseListener(this); + + // set panel size + setPreferredSize(new Dimension(boardImage.getWidth(null), boardImage.getHeight(null))); + } + + private void loadPieceImages(String name, Image[] images) { + for (int i = 0; i < 9; ++i) { + images[i] = getImage(String.format("%s-%02d.png", name, i + 1)); + } + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + + // draw board + g.drawImage(boardImage, 0, 0, this); + + paintFigure(g, 0, 0); + paintFigure(g, 1, 0); + paintFigure(g, 2, 0); + + paintFigure(g, 0, 1); + paintFigure(g, 1, 1); + paintFigure(g, 2, 1); + + paintFigure(g, 0, 2); + paintFigure(g, 1, 2); + paintFigure(g, 2, 2); + + // make sure we update the graphics + Toolkit.getDefaultToolkit().sync(); + } + + void paintFigure(Graphics g, int x, int y) { + char figure = game.ch[y][x]; + if (figure != 'X' && figure != '0') { + return; + } + g.drawImage(boardFigures[y * 3 + x], HORIZONTAL_POSITIONS[x], VERTICAL_POSITION[y], this); + } + + private Point clickToSquare(int x, int y) { + Point square = new Point(-1, -1); + if (x >= HORIZONTAL_POSITIONS[0] && x <= HORIZONTAL_POSITIONS[0] + FIGURE_WIDTH) { + square.x = 0; + } else if (x >= HORIZONTAL_POSITIONS[1] && x <= HORIZONTAL_POSITIONS[1] + FIGURE_WIDTH) { + square.x = 1; + } else if (x >= HORIZONTAL_POSITIONS[2] && x <= HORIZONTAL_POSITIONS[2] + FIGURE_WIDTH) { + square.x = 2; + } + + if (y >= VERTICAL_POSITION[0] && y <= VERTICAL_POSITION[0] + FIGURE_HEIGHT) { + square.y = 0; + } else if (y >= VERTICAL_POSITION[1] && y <= VERTICAL_POSITION[1] + FIGURE_HEIGHT) { + square.y = 1; + } else if (y >= VERTICAL_POSITION[2] && y <= VERTICAL_POSITION[2] + FIGURE_HEIGHT) { + square.y = 2; + } + return square; + } + + private char nextFigure = 'X'; + + private boolean takeSquare(Point square) { + // already something there skip + if (game.ch[square.y][square.x] == 'X' || game.ch[square.y][square.x] == '0') { + System.out.printf("Square (%d, %d) already taken!%n", + square.x, square.y); + return false; + } + + // update the game + game.ch[square.y][square.x] = nextFigure; + + // pick a random index for an image + Image symbolImage; + int randomImageIndex = (int) (Math.random() * 9); + + // Assign images and swap the figure for next turn + if (nextFigure == 'X') { + symbolImage = catImages[randomImageIndex]; + nextFigure = '0'; + + } else { + symbolImage = dogImages[randomImageIndex]; + nextFigure = 'X'; + } + + boardFigures[square.y * 3 + square.x] = symbolImage; + + repaint(); + return true; + } + + @Override + public void mouseClicked(MouseEvent mouseEvent) { + Point square = clickToSquare(mouseEvent.getX(), mouseEvent.getY()); + if (square.x != -1 && square.y != -1 && takeSquare(square)) { + int check = game.check(); + if (check == 1) { + JOptionPane.showMessageDialog(this, "Player wins!", + "Game Over", JOptionPane.PLAIN_MESSAGE); + + } else if (check == 2) { + JOptionPane.showMessageDialog(this, "No more moves!", + "Game Over", JOptionPane.PLAIN_MESSAGE); + } + } + } + + @Override + public void mousePressed(MouseEvent mouseEvent) { + /* NO need to handle mouse presses */ + } + + @Override + public void mouseReleased(MouseEvent mouseEvent) { + /* No need to handle button release */ + } + + @Override + public void mouseEntered(MouseEvent mouseEvent) { + /* No need to test for enter */ + } + + @Override + public void mouseExited(MouseEvent mouseEvent) { + /* No need to test for exit */ + } + + public static Image getImage(String filename) { + return new ImageIcon("images/" + filename).getImage(); + } +} diff --git a/TIC_TAC_TOE.java b/TIC_TAC_TOE.java index 59b7ba9..bf612d1 100644 --- a/TIC_TAC_TOE.java +++ b/TIC_TAC_TOE.java @@ -36,6 +36,18 @@ void initialize(int i,char sym ) } } + void resetBoard() { + char i1 = '1'; + for(int i = 0;i<3;i++) + { + for(int j = 0;j<3;j++) + { + ch[i][j] = (char)i1; + i1++; + } + } + } + void input() { int c1=0,c2=0; @@ -67,15 +79,7 @@ void input() } while(true) { - char i1 = '1'; - for(int i = 0;i<3;i++) - { - for(int j = 0;j<3;j++) - { - ch[i][j] = (char)i1; - i1++; - } - } + resetBoard(); display(); diff --git a/TicTacToeGui.java b/TicTacToeGui.java new file mode 100644 index 0000000..faef86b --- /dev/null +++ b/TicTacToeGui.java @@ -0,0 +1,28 @@ +import javax.swing.*; +import java.awt.*; + +public class TicTacToeGui extends JFrame { + public TicTacToeGui() { + initUI(); + } + + private void initUI() { + BoardPanel boardPanel = new BoardPanel(); + + add(boardPanel); + + pack(); + setResizable(false); + + setTitle("Tic-Tac-Toe"); + setLocationRelativeTo(null); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } + + public static void main(String[] args) { + EventQueue.invokeLater(() -> { + TicTacToeGui app = new TicTacToeGui(); + app.setVisible(true); + }); + } +} diff --git a/images/board.png b/images/board.png new file mode 100644 index 0000000..133cdaf Binary files /dev/null and b/images/board.png differ diff --git a/images/cat-01.png b/images/cat-01.png new file mode 100644 index 0000000..f7d6386 Binary files /dev/null and b/images/cat-01.png differ diff --git a/images/cat-02.png b/images/cat-02.png new file mode 100644 index 0000000..2656d22 Binary files /dev/null and b/images/cat-02.png differ diff --git a/images/cat-03.png b/images/cat-03.png new file mode 100644 index 0000000..2f359b3 Binary files /dev/null and b/images/cat-03.png differ diff --git a/images/cat-04.png b/images/cat-04.png new file mode 100644 index 0000000..c6607bb Binary files /dev/null and b/images/cat-04.png differ diff --git a/images/cat-05.png b/images/cat-05.png new file mode 100644 index 0000000..2c12e43 Binary files /dev/null and b/images/cat-05.png differ diff --git a/images/cat-06.png b/images/cat-06.png new file mode 100644 index 0000000..7fdc4b3 Binary files /dev/null and b/images/cat-06.png differ diff --git a/images/cat-07.png b/images/cat-07.png new file mode 100644 index 0000000..1efa7ae Binary files /dev/null and b/images/cat-07.png differ diff --git a/images/cat-08.png b/images/cat-08.png new file mode 100644 index 0000000..ee3caa6 Binary files /dev/null and b/images/cat-08.png differ diff --git a/images/cat-09.png b/images/cat-09.png new file mode 100644 index 0000000..fb89d30 Binary files /dev/null and b/images/cat-09.png differ diff --git a/images/dog-01.png b/images/dog-01.png new file mode 100644 index 0000000..9460e4c Binary files /dev/null and b/images/dog-01.png differ diff --git a/images/dog-02.png b/images/dog-02.png new file mode 100644 index 0000000..9ba8eb4 Binary files /dev/null and b/images/dog-02.png differ diff --git a/images/dog-03.png b/images/dog-03.png new file mode 100644 index 0000000..2ddef48 Binary files /dev/null and b/images/dog-03.png differ diff --git a/images/dog-04.png b/images/dog-04.png new file mode 100644 index 0000000..0d91c46 Binary files /dev/null and b/images/dog-04.png differ diff --git a/images/dog-05.png b/images/dog-05.png new file mode 100644 index 0000000..7f05cfc Binary files /dev/null and b/images/dog-05.png differ diff --git a/images/dog-06.png b/images/dog-06.png new file mode 100644 index 0000000..bcf1431 Binary files /dev/null and b/images/dog-06.png differ diff --git a/images/dog-07.png b/images/dog-07.png new file mode 100644 index 0000000..08b40a2 Binary files /dev/null and b/images/dog-07.png differ diff --git a/images/dog-08.png b/images/dog-08.png new file mode 100644 index 0000000..ae7b8cd Binary files /dev/null and b/images/dog-08.png differ diff --git a/images/dog-09.png b/images/dog-09.png new file mode 100644 index 0000000..ea38039 Binary files /dev/null and b/images/dog-09.png differ