-
Simple tic tac toe applet
I want to create a simple tic tac toe applet. Below is my code
Questions:
1. For my actionPerformed method. Instead of labelling every button with numbers (1~9) and using getActionCommand to identify which button is clicked, is there other simple way to detect which button has been clicked? So that i need not copy my codes in actionPerformed() 9 times..
2. How do i detect a win/lose/tie? How do i code it?
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Simple tic-tac-toe applet. Win/tie status is shown at the top of the
* applet.
*/
public class TicTacToe extends JApplet implements ActionListener {
JLabel messagesLabel=new JLabel("X goes first");
JPanel displayPanel=new JPanel(new GridLayout(3,3));
private static int count=0;
private static JButton b1=new JButton("1");
private static JButton b2=new JButton("2");
private static JButton b3=new JButton("3");
private static JButton b4=new JButton("4");
private static JButton b5=new JButton("5");
private static JButton b6=new JButton("6");
private static JButton b7=new JButton("7");
private static JButton b8=new JButton("8");
private static JButton b9=new JButton("9");
public void init(){
getContentPane().setLayout(new BorderLayout());
getContentPane().add(messagesLabel, BorderLayout.NORTH);
add(displayPanel, BorderLayout.CENTER);
b1.addActionListener(this);
displayPanel.add(b1);
b2.addActionListener(this);
displayPanel.add(b2);
b3.addActionListener(this);
displayPanel.add(b3);
b4.addActionListener(this);
displayPanel.add(b4);
b5.addActionListener(this);
displayPanel.add(b5);
b6.addActionListener(this);
displayPanel.add(b6);
b7.addActionListener(this);
displayPanel.add(b7);
b8.addActionListener(this);
displayPanel.add(b8);
b9.addActionListener(this);
displayPanel.add(b9);
getContentPane().add(displayPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
String actionCommand=e.getActionCommand();
if(actionCommand=="1"){
if(count==0 ||count%2==0){
b1.setText("X");
count++;
}else{
b1.setText("O");
count++;
}
}
if(actionCommand=="2"){
if(count==0 ||count%2==0){
b2.setText("X");
count++;
}else{
b2.setText("O");
count++;
}
}
if(actionCommand=="3"){
if(count==0 ||count%2==0){
b3.setText("X");
count++;
}else{
b3.setText("O");
count++;
}
}
if(actionCommand=="4"){
if(count==0 ||count%2==0){
b4.setText("X");
count++;
}else{
b4.setText("O");
count++;
}
}
if(actionCommand=="5"){
if(count==0 ||count%2==0){
b5.setText("X");
count++;
}else{
b5.setText("O");
count++;
}
}
if(actionCommand=="6"){
if(count==0 ||count%2==0){
b6.setText("X");
count++;
}else{
b6.setText("O");
count++;
}
}
if(actionCommand=="7"){
if(count==0 ||count%2==0){
b7.setText("X");
count++;
}else{
b7.setText("O");
count++;
}
}
if(actionCommand=="8"){
if(count==0 ||count%2==0){
b8.setText("X");
count++;
}else{
b8.setText("O");
count++;
}
}
if(actionCommand=="9"){
if(count==0 ||count%2==0){
b9.setText("X");
count++;
}else{
b9.setText("O");
count++;
}
}
}
}
-
Check this code.
Code:
/**
* A tictactoe button
*/
class TTTBtn extends JButton {
private int btnNo=-1;
private boolean hit=false;
private int value=-1;
public TTTBtn (int btnNo) {
super();
this.btnNo=btnNo;
}
public void setHit (boolean p1p2) {
super.setText(p1p2 ? "X" : "O");
value= p1p2 ? 1 : 4;
}
public int getValue() {
return value;
}
public boolean isHit () {
return value > 0;
}
public void reset() {
setText("");
value=-1;
}
public int getButtonNo () {
return btnNo;
}
}
/**
* A tictactoe applet
*/
public class TicTacToe
extends JApplet
implements ActionListener {
JLabel messagesLabel = new JLabel("X goes first");
JPanel displayPanel = new JPanel(new GridLayout(3, 3));
private TTTBtn[] bb = new TTTBtn[9];
private int [][] winLine={
{0,1,2}, // horizontal
{3,4,5},
{6,7,8},
{0,3,6}, // vertical
{1,4,7},
{2,5,8},
{0,4,8}, // diagonal
{6,4,2}
};
private JButton resetBtn=new JButton("Reset");
private boolean player1 = true;
public void init() {
messagesLabel.setOpaque(true);
messagesLabel.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(messagesLabel, BorderLayout.NORTH);
getContentPane().add(resetBtn, BorderLayout.SOUTH);
getContentPane().add(displayPanel, BorderLayout.CENTER);
resetBtn.addActionListener(this);
for (int i = 0; i < 9; i++) {
bb[i] = new TTTBtn(i);
bb[i].addActionListener(this);
displayPanel.add(bb[i]);
}
}
public int getWinner() {
for (int i=0; i<winLine.length; i++) {
int ret = lineWin(winLine[i]);
if (ret > 0) return ret;
}
return -1;
}
private int lineWin(int [] pos) {
int sum=
bb[pos[0]].getValue()+
bb[pos[1]].getValue()+
bb[pos[2]].getValue();
if (sum==3) return 1;
if (sum==12) return 2;
return -1;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == resetBtn) {
resetGame();
}
else if (e.getSource()instanceof TTTBtn) {
TTTBtn btn = (TTTBtn) e.getSource();
if (!btn.isHit()) {
if (haveWinner(btn)) {
enableButtons(false);
}
}
}
}
private void enableButtons(boolean enabled) {
for (int i=0; i<bb.length; i++) {
bb[i].setEnabled(enabled);
}
}
private boolean haveWinner(TTTBtn btn) {
bb[btn.getButtonNo()].setHit(player1);
int winner = getWinner();
if (winner > 0) {
messagesLabel.setBackground(Color.green);
messagesLabel.setText("Player " + winner + " wins");
return true;
}
else {
player1 = !player1;
return false;
}
}
private void resetGame() {
for (int i=0; i<bb.length; i++) {
bb[i].reset();
}
player1=true;
messagesLabel.setBackground(Color.lightGray);
messagesLabel.setText("X goes first");
enableButtons(true);
}
/**
* Test driver main
*/
public static void main(String[] args) {
JFrame f=new JFrame("Tic Tac Toe");
f.getContentPane().setLayout(new GridLayout());
TicTacToe ttt=new TicTacToe();
f.getContentPane().add(ttt);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
ttt.init();
f.setBounds(100,100,300,270);
f.setVisible(true);
}
}
eschew obfuscation
-
Can you explain this method?
Code:
public void setHit (boolean p1p2) {
super.setText(p1p2 ? "X" : "O");
value= p1p2 ? 1 : 4;
}
What is the ? and "X" : "O" also p1p2 ? 1 : 4
Thanks
-
 Originally Posted by Ant_Magma
Can you explain this method?
What is the ? and "X" : "O" also p1p2 ? 1 : 4
Thanks
It follows the following structure:
boolean condition ? true : false
So, if p1p2 is true, it sets the text to "X", and if it is false, to "O"
-
So its like if-else....
And how about the line
Code:
value= p1p2 ? 1 : 4;
why set it to 1 or 4?
-
Sorry for the late reply, been down w. the flu.
The reason for the value is the sum; A winner row will have the sum of 3 or 12,
nothing else.
Check the lineWin method in my code above
Last edited by sjalle; 09-26-2005 at 03:43 AM.
eschew obfuscation
Similar Threads
-
Replies: 2
Last Post: 04-27-2002, 12:09 AM
-
By Charlie Flynn in forum Java
Replies: 3
Last Post: 08-23-2001, 11:01 AM
-
Replies: 1
Last Post: 08-20-2001, 07:17 AM
-
Replies: 1
Last Post: 02-10-2001, 12:20 AM
-
By Benjamin Post in forum Java
Replies: 0
Last Post: 06-21-2000, 10:48 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks