ช่วยดู โค้ดนี้ให้ทีครับ เขียนเกม OX ครับ
คือผมรันแล้ว ก็ไม่ขึ้นอะไรเลยครับ ช่วยตรวจสอบด้วยนะครับ ขอบคุณมากครับ
package tictac2; import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Tictac extends Applet implements MouseListener , ActionListener { public static void main(String[] args) { new Tictac();
}
JLabel[][] tb = new JLabel [6][6] ;
Icon imo = new ImageIcon("C:\\Documents and Settings\\Admin\\Desktop\\O.jpg");
Icon imx = new ImageIcon("C:\\Documents and Settings\\Admin\\Desktop\\X.jpg");
boolean ST=false;
boolean end=false;
JLabel p1 = new JLabel("Player 1 is O ");
JLabel p2 = new JLabel("Player 2 is X ");
JLabel win =new JLabel();
JButton b1=new JButton("New Game");
int CTB=0;
public void init()
{
setLayout(null);
add(p1);
p1.setBounds(400,20,100,100); p1.setForeground(Color.red);
add(p2);
p2.setBounds(400,40,100,100);
p2.setForeground(Color.blue);
add(b1);
b1.addActionListener(this);
b1.setBounds(400,150,95,30);
for(int i=0;i<6;i++){
for(int j =0 ;j<6;j++){
tb[i][j] = new JLabel("");
add(tb[i][j]);
tb[i][j].addMouseListener(this);
tb[i][j].setBounds((j*50)+20,(50*i)+20,50,50);
tb[i][j].setBorder(BorderFactory.createLineBorder(Color.black,2));
}
}
}
public void newGame() {
ST=false;
for(int i=0;i<6;i++){
for(int j =0 ;j<6;j++){
tb[i][j].setIcon(null);
tb[i][j].setText("");
tb[i][j].removeMouseListener(this); tb[i][j].addMouseListener(this); }
}
win.setText("");
end=false;
}
public void mouseClicked(MouseEvent e){
if (!end)
{
if(ST==true){
((JLabel)(e.getComponent())).setIcon(imx);
((JLabel)(e.getComponent())).setText("X");
if (chkAll("X")==true)
{
WIN("Player2");
}
ST=false;
}
else{
((JLabel)(e.getComponent())).setIcon(imo);
((JLabel)(e.getComponent())).setText("O"); if (chkAll("O")==true)
{
WIN("Player1"); }
ST=true;
}
((JLabel)(e.getComponent())).removeMouseListener(this);
}
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public boolean chkAll(String a)
{
int c1,c2;
c1=c2=0;
boolean r=false;
for(int i=0;i<6;i++) { for(int j =0 ;j<6;j++){
if( tb[i][j].getText()==a)
{
c1++;
}
else
c1 = 0;
if (c1== 4)
{
r=true; break;
}
if( tb[j][i].getText()==a)
{
c2++;
}
else
c2 = 0;
if (c2== 4)
{
r=true;
break;
}
}
}
if( tb[0][0].getText()==a && tb[1][1].getText()==a && tb[2][2].getText()==a &&tb[3][3].getText()==a)
{
r=true;
}
if( tb[1][1].getText()==a && tb[2][2].getText()==a && tb[3][3].getText()==a &&tb[4][4].getText()==a)
{
r=true;
}
if( tb[2][2].getText()==a && tb[3][3].getText()==a && tb[4][4].getText()==a &&tb[5][5].getText()==a)
{
r=true;
}
if( tb[1][0].getText()==a && tb[2][1].getText()==a && tb[3][2].getText()==a &&tb[4][3].getText()==a)
{
r=true;
}
if( tb[2][1].getText()==a && tb[3][2].getText()==a && tb[4][3].getText()==a &&tb[5][4].getText()==a)
{
r=true;
}
if( tb[2][0].getText()==a && tb[3][1].getText()==a && tb[4][2].getText()==a &&tb[5][3].getText()==a)
{
r=true;
}
if( tb[0][1].getText()==a && tb[1][2].getText()==a && tb[2][3].getText()==a &&tb[3][4].getText()==a)
{
r=true;
}
if( tb[1][2].getText()==a && tb[2][3].getText()==a && tb[3][4].getText()==a &&tb[4][5].getText()==a)
{
r=true;
}
if( tb[0][2].getText()==a && tb[1][3].getText()==a && tb[2][4].getText()==a &&tb[3][5].getText()==a)
{
r=true;
}
if( tb[0][5].getText()==a && tb[1][4].getText()==a && tb[2][3].getText()==a &&tb[3][2].getText()==a)
{
r=true;
}
if( tb[1][4].getText()==a && tb[2][3].getText()==a && tb[3][2].getText()==a &&tb[4][1].getText()==a)
{
r=true;
}
if( tb[2][3].getText()==a && tb[3][2].getText()==a && tb[4][1].getText()==a &&tb[5][0].getText()==a)
{
r=true;
}
if( tb[0][4].getText()==a && tb[1][3].getText()==a && tb[2][2].getText()==a &&tb[3][1].getText()==a)
{
r=true;
}
if( tb[1][3].getText()==a && tb[2][2].getText()==a && tb[3][1].getText()==a &&tb[4][0].getText()==a)
{
r=true;
}
if( tb[0][3].getText()==a && tb[1][2].getText()==a && tb[2][1].getText()==a &&tb[3][0].getText()==a)
{
r=true;
}
if( tb[1][5].getText()==a && tb[2][4].getText()==a && tb[3][3].getText()==a &&tb[4][2].getText()==a)
{
r=true;
}
if( tb[2][4].getText()==a && tb[3][3].getText()==a && tb[4][2].getText()==a &&tb[5][1].getText()==a)
{
r=true;
}
if( tb[2][5].getText()==a && tb[3][4].getText()==a && tb[4][3].getText()==a &&tb[5][2].getText()==a)
{
r=true;
}
CTB=0;
for(int i = 0;i<6;i++){
for(int j=0;j<6;j++){
if(tb[i][j].getText()==""){
CTB++;
}
}
}
if(CTB==0){
r=true;
}
end=r;
return r;
}
public void WIN(String c){
if(CTB==0)
{
win.setText("Draw");
}
else
{
win.setText(c + " WIN");
}
add(win);
win.setBounds(400,65,100,100);
win.setFont(new Font("Tahoma",1,15));
win.setForeground(Color.black);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==b1)
{
newGame();
}
}
}
|