当前位置:我的异常网» 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);
文章评论