怎么把五子棋的单机版做成网络版?

来源:互联网 发布:java 带文件表单提交 编辑:程序博客网 时间:2024/04/30 09:11
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();     }}

0 0