| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 665 人关注过本帖
标题:关于井字游戏的问题
只看楼主 加入收藏
川云漠漠
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2014-10-8
结帖率:57.89%
收藏
已结贴  问题点数:4 回复次数:1 
关于井字游戏的问题
package com.imooc;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class TicTacToe extends JApplet {
   private char whoseTurn='X';
   private  Cell[][]  cells= new Cell[3][3];
   private  JLabel jlblStatus=new JLabel("X's turn to play");
   public TicTacToe(){
       JPanel p=new JPanel(new GridLayout(3,3,0,0));
       for(int i=0;i<3;i++)
           for(int j=0;j<3;j++)
               p.add(cells[i][j]=new Cell());
       p.setBorder(new LineBorder(Color.red,1));
      JlblStatus.setBorder(new LineBorder(Color.yellow,1));
       add(p,BorderLayout.CENTER);
       add(jlblStatus,BorderLayout.SOUTH);
   }
   public boolean isFull(){
       for(int i=0;i<3;i++)
           for(int j=0;j<3;j++)
               if(cells[i][j].getToken()==' ')
                   return false;
      
       return true;
   }
   public boolean isWon(char token){
       for(int i=0;i<3;i++)
          if((cells[i][0].getToken()==token)&&(cells[i][2].getToken()==token)&&(cells[i][2].getToken()==token)){
               return true;
           }
       for(int j=0;j<3;j++)
       if((cells[0][j].getToken()==token)&&(cells[1][j].getToken()==token)&&(cells[2][j].getToken()==token)){
           return true;
       }
       if((cells[0][2].getToken()==token)&&(cells[1][1].getToken()==token)&&(cells[2][0].getToken()==token)){
           return true;
       }
       return false;
   }
   public class Cell extends Jpanel{
       private char token=' ';
       public Cell(){
        setBorder(new LineBorder(Color.black,1));
           addMouseListener(new MyMouseListener());
       }
       public char getToken(){
           return token;
       }
       public void setToken(char c){
           token=c;
           repaint();
       }
       protected void paintComponnet(Graphics g){
           if(token=='X'){
               g.drawLine(10,10,getWidth()-10,getHeight()-10);
               g.drawLine(getWidth()-10,10,10,getHeight()-10);
           }
           else if(token=='0'){
               g.drawOval(10,10,getWidth()-20,getHeight()-20);
           }
       }
       private class MyMouseListener extends MouseAdapter{
           public void mouseClicked(MouseEvent e){
               if(token==' '&&whoseTurn!=' '){
                   setToken(whoseTurn);
                   if(isWon(whoseTurn)){
                       jlblStatus.setText(whoseTurn+"won! The game is over");
                       whoseTurn=' ';
                   }
                   else if(isFull()){
                       jlblStatus.setText("Draw! The game is over");
                       whoseTurn=' ';
                   }
                   else {
                       whoseTurn=(whoseTurn=='X')?'O':'X';
                       jlblStatus.setText(whoseTurn+"'s turn");
                   }
               }
           }
       }
   }
   }
红色标注的部分是有错误的地方,请问有什么错误?
搜索更多相关主题的帖子: private package public import 游戏 
2014-11-28 17:53
编号1016
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:46
专家分:188
注 册:2014-5-8
收藏
得分:4 
JlblStatus这个跟你上面写的不一样,是这个吧:jlblStatus;Jpanel这个也是一样的错误,应该是这个JPanel;不过你这游戏还不是很完整,好好努力把它完善得更好一点

相互学习,让自己变得更强大!
2014-12-01 09:59
快速回复:关于井字游戏的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017161 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved