Format code

This commit is contained in:
Victor 2014-01-17 18:47:48 +02:00
parent c70ec31da6
commit bfb89267b7
6 changed files with 243 additions and 246 deletions

View File

@ -1,31 +1,31 @@
package jtictactoe; package jtictactoe;
/** /**
* *
* @author aNNiMON * @author aNNiMON
*/ */
public class Figure { public class Figure {
public static final char public static final char
EMPTY = ' ', EMPTY = ' ',
X = 'X', X = 'X',
O = 'O'; O = 'O';
protected char figure; protected char figure;
public Figure() { public Figure() {
this.figure = EMPTY; this.figure = EMPTY;
} }
public boolean isEmpty() { public boolean isEmpty() {
return (figure == EMPTY); return (figure == EMPTY);
} }
public char getFigure() { public char getFigure() {
return figure; return figure;
} }
public void setFigure(char figure) { public void setFigure(char figure) {
this.figure = figure; this.figure = figure;
} }
} }

View File

@ -13,6 +13,7 @@ import javax.swing.JPanel;
* @author aNNiMON * @author aNNiMON
*/ */
public class GameBoard extends JPanel { public class GameBoard extends JPanel {
private static final int CELL_SIZE = 150; private static final int CELL_SIZE = 150;
private static final int OFFSET = 15; private static final int OFFSET = 15;
@ -114,4 +115,4 @@ public class GameBoard extends JPanel {
return point; return point;
} }
} }

View File

@ -11,7 +11,6 @@ public class Main extends JFrame {
public static void main(String[] args) { public static void main(String[] args) {
new Main().setVisible(true); new Main().setVisible(true);
} }
public Main() { public Main() {
@ -29,4 +28,4 @@ public class Main extends JFrame {
pack(); pack();
} }
} }

View File

@ -44,7 +44,7 @@ public class Table {
} }
public void setFigure(int x, int y) { public void setFigure(int x, int y) {
if( (checkWinner() != Figure.EMPTY) || (!hasMoreMoves()) ) { if ( (checkWinner() != Figure.EMPTY) || (!hasMoreMoves()) ) {
resetTable(); resetTable();
return; return;
} }
@ -59,7 +59,7 @@ public class Table {
} }
public void computerMove() { public void computerMove() {
if( (checkWinner() != Figure.EMPTY) || (!hasMoreMoves()) ) { if ( (checkWinner() != Figure.EMPTY) || (!hasMoreMoves()) ) {
//resetTable(); //resetTable();
return; return;
} }
@ -105,14 +105,14 @@ public class Table {
// Trying to put figure to the center. // Trying to put figure to the center.
int x = 1, y = 1; int x = 1, y = 1;
if(!table[y][x].isEmpty() || (random.nextInt(5) <= 3) ) { if (!table[y][x].isEmpty() || (random.nextInt(5) <= 3) ) {
// If it's busy, put to the corners. // If it's busy, put to the corners.
int count = 0; int count = 0;
do { do {
x = random.nextBoolean() ? 0 : 2; x = random.nextBoolean() ? 0 : 2;
y = random.nextBoolean() ? 0 : 2; y = random.nextBoolean() ? 0 : 2;
count++; count++;
} while ( (!table[y][x].isEmpty()) && (count < 10)); } while ( (!table[y][x].isEmpty()) && (count < 10) );
if (count > 9) { if (count > 9) {
// Put to the remaining cells. // Put to the remaining cells.
@ -122,7 +122,7 @@ public class Table {
x = pair[random.nextInt(pair.length)][0]; x = pair[random.nextInt(pair.length)][0];
y = pair[random.nextInt(pair.length)][1]; y = pair[random.nextInt(pair.length)][1];
count++; count++;
} while ( (!table[y][x].isEmpty()) && (count < 10)); } while ( (!table[y][x].isEmpty()) && (count < 10) );
} }
} }
table[y][x].setFigure(nextMoveIsX ? Figure.X : Figure.O); table[y][x].setFigure(nextMoveIsX ? Figure.X : Figure.O);
@ -198,4 +198,4 @@ public class Table {
} }
return false; return false;
} }
} }

View File

@ -1,181 +1,179 @@
package jtictactoe; package jtictactoe;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Font; import java.awt.Font;
import java.awt.Frame; import java.awt.Frame;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Point; import java.awt.Point;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
/** /**
* *
* @author aNNiMON * @author aNNiMON
*/ */
public class TitlePanel extends JPanel { public class TitlePanel extends JPanel {
private final BufferedImage exitImage, exitImageBW; private final BufferedImage exitImage, exitImageBW;
private final BufferedImage minimizeImage, minimizeImageBW; private final BufferedImage minimizeImage, minimizeImageBW;
private final BufferedImage background; private final BufferedImage background;
private final JFrame mainFrame; private final JFrame mainFrame;
private final Font messageFont, infoFont; private final Font messageFont, infoFont;
private boolean moveForm; private boolean moveForm;
private Point clickedStart; private Point clickedStart;
private int winX, winO; private int winX, winO;
private String message; private String message;
public TitlePanel(JFrame mainFrame) { public TitlePanel(JFrame mainFrame) {
this.mainFrame = mainFrame; this.mainFrame = mainFrame;
background = Util.getImage("/res/title_bg.jpg"); background = Util.getImage("/res/title_bg.jpg");
minimizeImage = Util.getImage("/res/minimize_button.png"); minimizeImage = Util.getImage("/res/minimize_button.png");
minimizeImageBW = Util.getImage("/res/minimize_button_bw.png"); minimizeImageBW = Util.getImage("/res/minimize_button_bw.png");
exitImage = Util.getImage("/res/exit_button.png"); exitImage = Util.getImage("/res/exit_button.png");
exitImageBW = Util.getImage("/res/exit_button_bw.png"); exitImageBW = Util.getImage("/res/exit_button_bw.png");
moveForm = false; moveForm = false;
messageFont = new Font("Arial", Font.BOLD, 60); messageFont = new Font("Arial", Font.BOLD, 60);
infoFont = new Font("Arial", Font.PLAIN, 30); infoFont = new Font("Arial", Font.PLAIN, 30);
winX = winO = 0; winX = winO = 0;
message = ""; message = "";
initComponents(); initComponents();
} }
public void updateWin(int winX, int winO) { public void updateWin(int winX, int winO) {
this.winX += winX; this.winX += winX;
this.winO += winO; this.winO += winO;
} }
public void setMessage(String message) { public void setMessage(String message) {
this.message = message; this.message = message;
repaint(); repaint();
} }
@Override @Override
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
final int width = background.getWidth(); final int width = background.getWidth();
g.drawImage(background, 0, 0, null); g.drawImage(background, 0, 0, null);
g.setColor(Color.DARK_GRAY); g.setColor(Color.DARK_GRAY);
g.setFont(infoFont); g.setFont(infoFont);
g.drawString("X:"+winX, 5, 110); g.drawString("X:"+winX, 5, 110);
String win = "O:" + winO; String win = "O:" + winO;
g.drawString(win, width - getFontMetrics(infoFont).stringWidth(win) - 5, 110); g.drawString(win, width - getFontMetrics(infoFont).stringWidth(win) - 5, 110);
if (!message.isEmpty()) { if (!message.isEmpty()) {
g.setFont(messageFont); g.setFont(messageFont);
int x = getFontMetrics(messageFont).stringWidth(message) / 2; int x = getFontMetrics(messageFont).stringWidth(message) / 2;
x = width / 2 - x; x = width / 2 - x;
g.drawString(message, x, 110); g.drawString(message, x, 110);
} }
} }
private void initComponents() { private void initComponents() {
setBackground(new Color(51, 51, 51)); setBackground(new Color(51, 51, 51));
setPreferredSize(new Dimension(480, 150)); setPreferredSize(new Dimension(480, 150));
addMouseListener(new MouseAdapter() { addMouseListener(new MouseAdapter() {
@Override @Override
public void mousePressed(MouseEvent evt) { public void mousePressed(MouseEvent evt) {
formMousePressed(evt); formMousePressed(evt);
} }
@Override @Override
public void mouseReleased(MouseEvent evt) { public void mouseReleased(MouseEvent evt) {
formMouseReleased(evt); formMouseReleased(evt);
} }
}); });
addMouseMotionListener(new MouseMotionAdapter() { addMouseMotionListener(new MouseMotionAdapter() {
@Override @Override
public void mouseDragged(MouseEvent evt) { public void mouseDragged(MouseEvent evt) {
formMouseDragged(evt); formMouseDragged(evt);
} }
}); });
setLayout(null); setLayout(null);
final JButton exitButton = new JButton();
final JButton exitButton = new JButton(); exitButton.setIcon(new ImageIcon(exitImageBW));
exitButton.setIcon(new ImageIcon(exitImageBW)); exitButton.setBorder(null);
exitButton.setBorder(null); exitButton.setBorderPainted(false);
exitButton.setBorderPainted(false); exitButton.setContentAreaFilled(false);
exitButton.setContentAreaFilled(false); exitButton.addMouseListener(new MouseAdapter() {
exitButton.addMouseListener(new MouseAdapter() { @Override
@Override public void mouseEntered(MouseEvent evt) {
public void mouseEntered(MouseEvent evt) { exitButton.setIcon(new ImageIcon(exitImage));
exitButton.setIcon(new ImageIcon(exitImage)); }
} @Override
@Override public void mouseExited(MouseEvent evt) {
public void mouseExited(MouseEvent evt) { exitButton.setIcon(new ImageIcon(exitImageBW));
exitButton.setIcon(new ImageIcon(exitImageBW)); }
} });
}); exitButton.addActionListener(new ActionListener() {
exitButton.addActionListener(new ActionListener() { @Override
@Override public void actionPerformed(ActionEvent evt) {
public void actionPerformed(ActionEvent evt) { exitButtonActionPerformed(evt);
exitButtonActionPerformed(evt); }
} });
}); add(exitButton);
add(exitButton); exitButton.setBounds(378, 2, 100, 50);
exitButton.setBounds(378, 2, 100, 50);
final JButton minimizeButton = new JButton();
final JButton minimizeButton = new JButton(); minimizeButton.setIcon(new ImageIcon(minimizeImageBW));
minimizeButton.setIcon(new ImageIcon(minimizeImageBW)); minimizeButton.setBorder(null);
minimizeButton.setBorder(null); minimizeButton.setBorderPainted(false);
minimizeButton.setBorderPainted(false); minimizeButton.setContentAreaFilled(false);
minimizeButton.setContentAreaFilled(false); minimizeButton.addMouseListener(new MouseAdapter() {
minimizeButton.addMouseListener(new MouseAdapter() { @Override
@Override public void mouseEntered(MouseEvent evt) {
public void mouseEntered(MouseEvent evt) { minimizeButton.setIcon(new ImageIcon(minimizeImage));
minimizeButton.setIcon(new ImageIcon(minimizeImage)); }
} @Override
@Override public void mouseExited(MouseEvent evt) {
public void mouseExited(MouseEvent evt) { minimizeButton.setIcon(new ImageIcon(minimizeImageBW));
minimizeButton.setIcon(new ImageIcon(minimizeImageBW)); }
} });
}); minimizeButton.addActionListener(new ActionListener() {
minimizeButton.addActionListener(new ActionListener() { @Override
@Override public void actionPerformed(ActionEvent evt) {
public void actionPerformed(ActionEvent evt) { mainFrame.setState(Frame.ICONIFIED);
mainFrame.setState(Frame.ICONIFIED); }
} });
}); add(minimizeButton);
add(minimizeButton); minimizeButton.setBounds(2, 2, 100, 50);
minimizeButton.setBounds(2, 2, 100, 50); }
}
private void exitButtonActionPerformed(ActionEvent evt) {
private void exitButtonActionPerformed(ActionEvent evt) { mainFrame.setVisible(false);
mainFrame.setVisible(false); mainFrame.dispose();
mainFrame.dispose(); System.exit(0);
System.exit(0); }
}
private void formMousePressed(MouseEvent evt) {
private void formMousePressed(MouseEvent evt) { if (evt.getButton() == MouseEvent.BUTTON1) {
if (evt.getButton() == MouseEvent.BUTTON1) { moveForm = true;
moveForm = true; clickedStart = evt.getPoint();
clickedStart = evt.getPoint(); }
} }
}
private void formMouseReleased(MouseEvent evt) {
private void formMouseReleased(MouseEvent evt) { moveForm = false;
moveForm = false; }
}
private void formMouseDragged(MouseEvent evt) {
private void formMouseDragged(MouseEvent evt) { if (moveForm) {
if (moveForm) { Point moved = evt.getLocationOnScreen();
Point moved = evt.getLocationOnScreen(); moved.translate(-clickedStart.x, -clickedStart.y);
moved.translate(-clickedStart.x, -clickedStart.y); mainFrame.setLocation(moved);
mainFrame.setLocation(moved); }
} }
} }
}

View File

@ -1,25 +1,24 @@
package jtictactoe; package jtictactoe;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
/** /**
* *
* @author aNNiMON * @author aNNiMON
*/ */
public class Util { public class Util {
public static BufferedImage getImage(String path) { public static BufferedImage getImage(String path) {
BufferedImage image = null; BufferedImage image = null;
try { try {
InputStream is = Runtime.getRuntime().getClass().getResourceAsStream(path); InputStream is = Runtime.getRuntime().getClass().getResourceAsStream(path);
image = ImageIO.read(is); image = ImageIO.read(is);
is.close(); is.close();
} catch (IOException ex) { } catch (IOException ex) {
} }
return image; return image;
} }
}
}