import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class TictacToe extends JApplet {
// sets first as X player
private char whoseTurn = 'X';
// Create cells
private Cell[][] cells = new Cell[3][3];
// Create and initialize a status label
private JLabel jlblStatus = new JLabel("X's turn to play");
public TictacToe() {
// sets up grid
JPanel p = new JPanel(new GridLayout(3, 3, 0, 0));
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
p.add(cells[i][j] = new Cell());
//sets borders
p.setBorder(new LineBorder(Color.red, 1));
jlblStatus.setBorder(new LineBorder(Color.yellow, 1));
add(p, BorderLayout.CENTER);
add(jlblStatus, BorderLayout.SOUTH);
}
public class Cell extends JPanel {
// Token used for this cell
private char token = ' ';
public Cell() {
setBorder(new LineBorder(Color.black, 1)); // border of cell
addMouseListener(new MouseListener()); // alllows mouse to be used
}
/** Return token */
public char getToken() {
return token;
}
/** Set a new token */
public void setToken(char c) {
token = c;
repaint();
}
/** colors cell */
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (token == 'X') {
g.drawLine(10, 10, getWidth() - 10, getHeight() - 20);
g.drawLine(getWidth() - 10, 10, 10, getHeight() - 20);
}
else if (token == 'O') {
g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
}
}
private class MouseListener extends MouseAdapter {
/** tells if mouse clicked on a cell */
public void mouseClicked(MouseEvent e) {
// If empty game is not over
if (token == ' ' && whoseTurn != ' ') {
setToken(whoseTurn); // Set token in the cell
// game status
if (isWon(whoseTurn)) {
jlblStatus.setText(whoseTurn + " won! The game is over");
whoseTurn = ' '; // Game is over
}
else if (isFull()) {
jlblStatus.setText("Draw! The game is over");
whoseTurn = ' '; // Game is over
}
else {
whoseTurn = (whoseTurn == 'X') ? 'O': 'X'; // switches the turn
jlblStatus.setText(whoseTurn + "'s turn"); // shows whose turn
}
}
}
}
}
/** checks to see if cell is empty */
public boolean isFull() {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (cells[i][j].getToken() == ' ')
return false;
return true;
}
/** Determine who wins */
public boolean isWon(char token) {
for (int i = 0; i < 3; i++)
if ((cells[i][0].getToken() == token)
&& (cells[i][1].getToken() == token)
&& (cells[i][2].getToken() == token)) {
return true;
}
for (int j = 0; j < 3; j++)
if ((cells[0][j].getToken() == token)
&& (cells[1][j].getToken() == token)
&& (cells[2][j].getToken() == token)) {
return true;
}
if ((cells[0][0].getToken() == token)
&& (cells[1][1].getToken() == token)
&& (cells[2][2].getToken() == token)) {
return true;
}
if ((cells[0][2].getToken() == token)
&& (cells[1][1].getToken() == token)
&& (cells[2][0].getToken() == token)) {
return true;
}
return false;
}
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("TicTacToe");
// Create new applet
TictacToe applet = new TictacToe();
// Add the new instance to the frame
frame.add(applet, BorderLayout.CENTER);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
*edit: Please use code tags in the future, thanks!
This post has been edited by Martyr2: 22 February 2009 - 02:25 PM

New Topic/Question
Reply




MultiQuote




|