|
||||||
| Java Tutorials Tutorials and Code for Java |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Sidewinder | Please use [php], [code] and [quote] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted.
Still regarding the image instead of text, it still does not appear to be working. If you could be so kind, can you please show me how to setIcon using the code. Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public cl*** TicTacToeV2 implements ActionListener {
/*Instance Variables*/
private int[][] winCombinations = new int[][] {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
{0, 4, 8}, {2, 4, 6} //diagonal wins
};
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton buttons[] = new JButton[9];
private int count = 0;
private String letter = "";
private boolean win = false;
public TicTacToeV2(){
/*Create Window*/
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
/*Add Buttons To The Window*/
for(int i=0; i<=8; i++){
buttons[i] = new JButton();
window.add(buttons[i]);
buttons[i].addActionListener(this);
}
/*Make The Window Visible*/
window.setVisible(true);
}
/**
When an object is clicked, perform an action.
@param a action event object
*/
public void actionPerformed(ActionEvent a) {
count++;
/*Calculate whose turn it is*/
if(count % 2 == 0){
letter = "O";
} else {
letter = "X";
}
/*Write the letter to the button and deactivate it*/
JButton pressedButton = (JButton)a.getSource();
pressedButton.setText(letter);
pressedButton.setEnabled(false);
/*Determine who won*/
for(int i=0; i<=7; i++){
if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) &&
buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &&
buttons[winCombinations[i][0]].getText() != ""){
win = true;
}
}
/*Show a dialog when game is over*/
if(win == true){
JOptionPane.showMessageDialog(null, letter + " wins the game!");
System.exit(0);
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "The game was tie!");
System.exit(0);
}
}
public static void main(String[] args){
TicTacToeV2 starter = new TicTacToeV2();
}
}
Last edited by John; 06-02-2007 at 10:57 AM. |
|
|||||
|
From the looks of it, you've made no changes to my code. Show me what you did and I will help.
Have you looked at the Javadocs? Java Platform 1.2 Beta 4 API Specification: Cl*** JButton |
|
|||
|
I'm having trouble creating a working win function, so I googled it.. and here I am.
I know this thread is meretriciously old, but I really need this help lol. Here's my current code: Code:
package tictactoe;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public cl*** TicTacToeForm extends javax.swing.JFrame implements ActionListener
{
private int[][] winCombinations = new int[][] {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
{0, 4, 8}, {2, 4, 6} //diagonal wins
};
private String turn = "";
private String config[] = new String[10];
private boolean win = false;
private int count = 0;
public TicTacToeForm()
{
initComponents();
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
jButton4.addActionListener(this);
jButton5.addActionListener(this);
jButton6.addActionListener(this);
jButton7.addActionListener(this);
jButton8.addActionListener(this);
jButton9.addActionListener(this);
}
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents()
{
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("TicTacToe");
setName("jFrame");
jButton2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 10, 75, 70));
jButton3.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 10, 75, 70));
jButton4.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton4ActionPerformed(evt);
}
});
getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, 75, 70));
jButton5.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton5ActionPerformed(evt);
}
});
getContentPane().add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 90, 75, 70));
jButton6.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton6ActionPerformed(evt);
}
});
getContentPane().add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 90, 75, 70));
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 75, 70));
jButton7.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton7ActionPerformed(evt);
}
});
getContentPane().add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 170, 75, 70));
jButton8.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton8ActionPerformed(evt);
}
});
getContentPane().add(jButton8, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 170, 75, 70));
jButton9.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton9ActionPerformed(evt);
}
});
getContentPane().add(jButton9, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 170, 75, 70));
pack();
}// </editor-fold>
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt)
{
jButton9.setText(turn);
jButton9.setEnabled(false);
config[9] = turn;
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt)
{
jButton8.setText(turn);
jButton8.setEnabled(false);
config[8] = turn;
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt)
{
jButton7.setText(turn);
jButton7.setEnabled(false);
config[7] = turn;
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt)
{
jButton6.setText(turn);
jButton6.setEnabled(false);
config[6] = turn;
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)
{
jButton5.setText(turn);
jButton5.setEnabled(false);
config[5] = turn;
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
{
jButton4.setText(turn);
jButton4.setEnabled(false);
config[4] = turn;
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
jButton3.setText(turn);
jButton3.setEnabled(false);
config[3] = turn;
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
jButton2.setText(turn);
jButton2.setEnabled(false);
config[2] = turn;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
jButton1.setText(turn);
jButton1.setEnabled(false);
config[1] = turn;
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new TicTacToeForm().setVisible(true);
}
});
}
public boolean checkWinner()
{
for(int i=0; i<=7; i++)
{
if( config[winCombinations[i][0]].equals(config[winCombinations[i][1]]) &&
config[winCombinations[i][1]].equals(config[winCombinations[i][2]]) &&
config[winCombinations[i][0]] != "")
{
win = true;
}
}
return win;
}
public void actionPerformed(ActionEvent a)
{
count++;
if(count %2 == 0)
{
turn = "O";
}
else
{
turn = "X";
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
// End of variables declaration
}
Any help is appreciated. |
|
|||||
|
Well there is 3 things I noticed.
1) You used a crappy IDE to generate your code, use a descent IDE like eclipse and write every line of code yourself and it wont look like fecal matter. 2) Your config array starts at 1 and goes to 9, yet winCombinations[][] only only checks an array from 0 to 8. 3) checkWinner() is never called...
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||
|
Sidewinder | Please use [php], [code] and [highlight...] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted.
I know its been a year for the java tutotrial,but I need ur help! can u help me add: 1. A new Game button that starts a new game 2.A running score showing the number of games won by each player(X or O). in my tic tac toe program? Java Code:
Sidewinder | Please use [php], [code] and [highlight...] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Last edited by John; 11-07-2007 at 01:40 PM. |