Java三子棋程序是怎么实现的,高手帮忙看一上三子棋的java程序运行不了的原因...

当前位置:我的异常网» Eclipse » 高手帮忙看一上三子棋的java程序运行不了的原因

高手帮忙看一上三子棋的java程序运行不了的原因

www.myexceptions.net  网友分享于:2013-09-17  浏览:19次

高手帮忙看一下三子棋的java程序运行不了的原因

//实现三子棋游戏

import javax.swing.*;

import java.awt.*;

import java.awt.Event.*;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

public class threeChess extends JFrame {

private char whoseTurn='x';

private Cell[][] cell=new Cell[3][3];

private JLabel label=new JLabel("'x' is turn to play");

//实现图形界面

public threeChess(){

setTitle("ThreeChess!!");

JPanel p=new JPanel(new GridLayout(3,3,5,5));

for(int i=0;i<9;i++)

for(int j=0;j<3;j++)

p.add(cell[i][j]);

getContentPane().setLayout(new BorderLayout());

getContentPane().add(p);

getContentPane().add(label,BorderLayout.SOUTH);

}

//判断是否填满

public boolean isFull(){

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

if(cell[i][j].getToken()==' ')

return false;

return true;

}

//判断是否获胜

public boolean isWon(char token){

for(int i=0;i<3;i++)

if(cell[i][0].getToken()==token&&

cell[i][1].getToken()==token&&

cell[i][2].getToken()==token)

return true;

for(int j=0;j<3;j++)

if(cell[0][j].getToken()==token&&

cell[1][j].getToken()==token&&

cell[2][j].getToken()==token)

return true;

if(cell[0][0].getToken()==token&&

cell[1][1].getToken()==token&&

cell[2][2].getToken()==token)

return true;

if(cell[2][0].getToken()==token&&

cell[1][1].getToken()==token&&

cell[0][2].getToken()==token)

return true;

return false;

}

//单元格类

public class Cell extends JPanel implements MouseListener{

private char token=' ';

public Cell(){

addMouseListener(this);

}

public char getToken(){

return token;

}

public void setToken(char token){

this.token=token;

repaint();

}

protected void paintComponent(Graphics g){

super.paintComponent(g);

if(token=='o'){

setForeground(Color.red);

g.fillOval(getWidth()/2,getHeight()/2,(int)(getWidth()*0.4),(int)(getHeight()*0.4));

}

else if(token=='x'){

setForeground(Color.BLUE);

int height=getHeight();

int width=getWidth();

g.drawLine((int)(width/2-width*0.4),(int)(height/2-height*0.4),(int)(width/2+width*0.4),(int)(height/2+height*0.4));

g.drawLine((int)(width/2+width*0.4),(int)(height/2-height*0.4),(int)(width/2-width*0.4),(int)(height/2-height*0.4));

}

}

public void mouseClicked(MouseEvent e) {

// TODO Auto-generated method stub

if(token==' '&&whoseTurn!=' '){

setToken(whoseTurn);

if(isWon(whoseTurn)){

label.setText(whoseTurn+" is won!The game is over.");

whoseTurn=' ';

}

if(isFull()){

label.setText("The game is over");

whoseTurn=' ';

}

else{

whoseTurn=(whoseTurn=='x')?'o':'x';

label.setText(whoseTurn+"'s turn!");

}

}

}

@Override

public void mouseEntered(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseExited(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mousePressed(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseReleased(MouseEvent arg0) {

// TODO Auto-generated method stub

}

}

public static void main(String[] args){

threeChess frame=new threeChess();

frame.setSize(1000,1000);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocation(200,200);

文章评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值