| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 623 人关注过本帖, 1 人收藏
标题:求助,怎么把五子棋的单机版做成网络版?
只看楼主 加入收藏
z66261123
Rank: 1
等 级:新手上路
帖 子:2
专家分:7
注 册:2014-4-23
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:1 
求助,怎么把五子棋的单机版做成网络版?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class FiveChessPanel extends Panel implements MouseListener
{
    int chess[][] = new int[11][11];    //定义11行11列的棋盘
    boolean BlackChess ;
    /**
     * 构造方法
     * 初始化棋盘数据
     * 设置Panel面板属性
     */
    public FiveChessPanel()      
    {
        BlackChess  = true;
        for(int i = 0;i < 11;i++)
        {
            for(int j = 0;j < 11;j++)
            {
                chess[i][j] = 0;
            }
        }
        addMouseListener(this);     //给鼠标注册监听器
        setBackground(Color.gray);
        setVisible(true);
    }
     
    /**
     * 鼠标按下方法
     */
    public void mousePressed(MouseEvent e)
    {
        //获取鼠标坐标
        int x = e.getX();
        int y = e.getY();
        
//计算是否超出棋盘边界
 if(x < 30 || x > 360 ||y < 30 || y > 360)
        {
     JOptionPane.showMessageDialog(this, "超出棋盘边界");
  return;
        }
 //计算此处是否有棋子,0为未下棋子
 if(chess[x/30-1][y/30-1] != 0)
        {
     JOptionPane.showMessageDialog(this, "已有棋子");
  return;  
      }
 //////////////////////////////开始下棋,黑棋先下
        if(BlackChess == true)
        {
            chess[x/30-1][y/30-1] = 1; //1为黑棋,2为白棋
            BlackChess  = false;
            repaint();   //调用paint重绘方法
            Justisewiner();
            return;
        }
        if(BlackChess  == false)
        {
            chess[x/30-1][y/30-1] = 2;  //1为黑棋,2为白棋
            BlackChess  = true;
            repaint();   
            Justisewiner();
           return;
        }
    }
  /**
   * 画棋盘
   * @param g
   */
   public void Drawline(Graphics g)
    {
       for(int i = 30;i <= 330;i += 30)
    {
            for(int j = 30;j <= 330; j+= 30)
            {
                g.setColor(Color.WHITE);
                g.drawLine(i, j, i, 330);      //画垂直线,设置每个点坐标
            }
 }
      
 for(int j = 30;j <= 330;j += 30)
 {
            g.setColor(Color.WHITE);
            g.drawLine(30, j, 330, j);   //画横线
 }
        
    }
/**
 * 画棋子
 * @param g
 */
   public void Drawchess(Graphics g)
    {
        for(int i = 0;i < 11;i++)
    {
            for(int j = 0;j < 11;j++)
            {
                if(chess[i][j] == 1)
                {
                    g.setColor(Color.BLACK);
                    g.fillOval((i + 1) * 30-8, (j + 1) * 30 - 8, 18, 18);
                }
                if(chess[i][j] == 2)
                {
                    g.setColor(Color.WHITE);
                    g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 18, 18);
                }
            }
        }
   }
   
   
   /**
    * 判断输赢的方法
    */
   public void Justisewiner()
    {
        int black_count = 0;
        int white_count = 0;
        
//横向判断输赢     
 for(int i=0 ;i < 11;i++)      
 {
      for(int j = 0;j < 11;j++)
            {
                if(chess[i][j] == 1)     // 判断1为黑棋
                {
                    black_count++;
                    if(black_count == 5)  //黑棋横向判断
                    {
                        JOptionPane.showMessageDialog(this, "黑棋胜利");
                        Clear_Chess();
                    
                    }                                    
                }
                else
                {
                    black_count = 0;
                }
                if(chess[i][j] == 2)    //判断2为白棋
                {
                    white_count++;
                    if(white_count == 5)  //白旗横向判断
                    {
                        JOptionPane.showMessageDialog(this, "白棋胜利");
                        Clear_Chess();
                    
                    }
                }
                else
                {
                    white_count = 0;
                }
            }
 }
 
 //纵向判断输赢
 for(int i = 0;i < 11;i++)
 {
            for(int j = 0;j < 11;j++)
            {
                if(chess[j][i] == 1)      //判断1为黑棋
                {
                    black_count++;
                    if(black_count == 5)  //黑棋纵向判断
                    {
                        JOptionPane.showMessageDialog(this, "黑棋胜利");
                        Clear_Chess();
                     
                    }
                }
                else
                {
                    black_count = 0;
                }
                if(chess[j][i] == 2)           //判断2为白棋
                {
                    white_count++;
                    if(white_count == 5)     //白棋纵向判断
                    {
                        JOptionPane.showMessageDialog(this, "白棋胜利");
                        Clear_Chess();
                       
                    }
                }
                else
                {
                    white_count = 0;
                }
            }
 }
 
//左斜判断
 for(int i = 0;i < 7;i++)
 {
            for(int j = 0;j < 7;j++)
            {
                for(int k = 0;k < 5;k++)
                {
                    if(chess[i + k][j + k] == 1)   //判断1为黑棋
                    {
                        black_count++;
                        if(black_count == 5)      //黑棋左斜判断
                        {
                            JOptionPane.showMessageDialog(this, "黑棋胜利");
                            Clear_Chess();
                           
                        }
                    }
                    else
                    {
                        black_count = 0;
                    }
                    if(chess[i + k][j + k] == 2)     //判断2为白棋
                    {
                        white_count++;
                        if(white_count == 5)        //白棋左斜判断
                        {
                            JOptionPane.showMessageDialog(this, "白棋胜利");
                            Clear_Chess();
                        
                        }
                    }
                    else
                    {
                        white_count = 0;
                    }
                }
            }
 }
 
//右斜判断
 for(int i = 4;i < 11;i++)
 {
            for(int j = 6;j >= 0;j--)
            {
                for(int k = 0;k < 5;k++)
                {
                    if(chess[i - k][j + k] == 1)       //判断1为黑棋
                    {
                        black_count++;
                        if(black_count == 5)      //黑棋右斜判断
                        {
                            JOptionPane.showMessageDialog(this, "黑棋胜利");
                            Clear_Chess();
                        
                        }
                    }
                    else
                    {
                        black_count = 0;
                    }
                    if(chess[i - k][j + k] == 2)       //判断2为白棋
                    {
                        white_count++;
                        if(white_count == 5)      //白棋右斜判断
                        {
                            JOptionPane.showMessageDialog(this, "白棋胜利");
                            Clear_Chess();
                           
                        }
                    }
                    else
                    {
                        white_count = 0;
                    }
                }
            }
 }
      
    }
   
   
   /**
    * 清除棋盘上的所有棋子坐标对应数据
    * 清除棋子
    * 重绘棋盘
    */
    void Clear_Chess()
    {
 for(int i=0;i<11;i++)
 {
  for(int j=0;j<11;j++)
  {
   chess[i][j]=0;
  }
 }
 repaint();//调用paint重绘方法
    }
  
   
    /**
     * 调用画棋盘和画棋子的方法
     */
    public void paint(Graphics g)
    {
        Drawline(g);
        Drawchess(g);
    }
    public void mouseExited(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    public void mouseClicked(MouseEvent e){}
}

 class myframe
{
   
    /**
     * 主窗口构造方法
     */
     myframe()
    {
        Frame frame=new Frame("五子棋");
        frame.setBounds(200, 200, 360, 383);
        FiveChessPanel  panel = new FiveChessPanel();
        panel.setBounds(0,24,360, 360);   
        frame.setLayout(null);  //将Frame窗口的布局设置为null
        frame.add(panel);
           
        frame.setVisible(true);           
   
        frame.addWindowListener(new WindowListener(){    //给窗口注册监听器
       public void windowClosing(WindowEvent frame)
    {
           frame.getWindow().setVisible(false);   //设置为隐藏
           ((Frame)frame.getComponent()).dispose();  //释放资源
        System.exit(0);   //退出程序
    }
    public void windowDeactivated(WindowEvent e){}
    public void windowActivated(WindowEvent e){}
    public void windowOpened(WindowEvent e){}
    public void windowClosed(WindowEvent e){}
    public void windowIconified(WindowEvent e){}
    public void windowDeiconified(WindowEvent e){}
});
}
}
 public class FiveChess{
     public static void main(String[] args){
    myframe p=new myframe();
     }
 }
搜索更多相关主题的帖子: 五子棋 public import 网络版 单机 
2014-04-23 22:51
鸥翔鱼游
Rank: 5Rank: 5
等 级:职业侠客
帖 子:182
专家分:323
注 册:2014-4-19
收藏
得分:20 
观摩技术性回复
2014-04-24 14:45
快速回复:求助,怎么把五子棋的单机版做成网络版?
数据加载中...
 
   



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

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