沐之潼 2017-06-22 05:41 采纳率: 66.7%
浏览 5682
已结题

添加悔棋功能,整体代码

package wuziqi1;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class wuziqi1 extends JFrame{
CheseGame CG;
wuziqi1 ()
{
this.CG= new CheseGame();
addMouseListener(CG);
add(CG);
}
public static void main(String[] args) {
wuziqi1 fivechese= new wuziqi1 ();
fivechese.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fivechese.setSize(900, 700);
fivechese.setTitle("五子棋");
fivechese.setVisible(true);
fivechese.setResizable(true);
}
}
class CheseGame extends JPanel implements MouseListener,ItemListener
{
int [][]map = new int[20][20];
Color []color = {Color.BLACK,Color.WHITE,Color.GREEN};
int x = -1;
int y = -1;
int flag = 1;
int winner = 3;
JButton btn1 = new JButton("开始");
JButton btn2 = new JButton("再来一局");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox chb1 = new Checkbox("黑棋先开始",cbg,true);
Checkbox chb2 = new Checkbox("白棋先开始",cbg,false);
public void itemStateChanged(ItemEvent ie) {
if(chb1.getState())
{
flag = 1;
}
else
{
flag = 2;
}
}
class GameStart implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{

         if(ae.getSource() == btn1)
         {
             start();
             repaint();
             btn1.setEnabled(false);
             btn2.setEnabled(true);
             chb1.setEnabled(false);
             chb2.setEnabled(false);
         }
         if(ae.getSource() == btn2)
         {
             start();
             repaint();
             chb1.setEnabled(true);
             chb2.setEnabled(true);
         }
    }
}
CheseGame()
{  

    this.setLayout(null);
    chb1.setBounds(650, 50, 100, 30);
    chb2.setBounds(650, 100, 100, 30);
    chb1.addItemListener(this);
    chb2.addItemListener(this);
    btn1.setBounds(650,150,100,30);
    btn1.addActionListener(new GameStart()); 
    btn2.setBounds(650, 200, 100, 30);
    btn2.addActionListener(new GameStart());
    btn2.setEnabled(false);
    this.add(btn1);
    this.add(btn2);
    this.add(chb1);
    this.add(chb2);
}
void start()
{
    for(int i = 0;i < 20;i++)
        for(int j = 0;j < 20;j++)
            map[i][j] = 0;
    winner = 0;
}
Boolean win(int x,int y)
{
    int score = 1;
         for(int i = x;i < x+4;)
         {
             if(i == 19)
                 break;
             if(map[i][y] != map[++i][y])
                break;
             score++;
         }
         for(int i = x;i > x-4;)
         {
             if(i == 0)
                 break;
             if(map[i][y] != map[--i][y])
                break;
             score++;
         } 
        if(score >= 5)
             return true;
        else
             score = 1;
        for(int j = y; j < y+4;)
        {
            if(j == 19)
                break;
            if(map[x][j] != map[x][++j])
                break;
            score++;
        }
        for(int j = y; j > y-4;)
        {
            if(j == 0)
                break;
            if(map[x][j] != map[x][--j])
                break;
            score++;
        } 
        if(score >= 5)
             return true;
        else
             score = 1;
        for(int i = x;i < x+4;)
            for(int j = y;j < y+4;)
            {
                if(i == 19 || y == 19)
                {i = x+4; break;}
                if(map[i][j] != map[++i][++j])
                {
                    i = x+4;
                    break;
                }
                score++;
            }
          for(int i = x;i > x-4;)
            for(int j = y;j > y-4;)
            {
                if(i == 0||j==0)
                {i = x-4; break;}
                if(map[i][j] != map[--i][--j])
                {
                    i = x-4;
                    break;
                }
                score++;
            } 
          if(score >= 5)
             return true;
         else
             score = 1;
        for(int i = x;i > x-4;)
            for(int j = y;j < y+4;)
            {
                if(i==0 || j==19)
                { i = x-4;break;}
                if(map[i][j] != map[--i][++j])
                {
                    i = x-4;
                    break;
                }
                score++;
            }  
        for(int i = x;i < x+4;)
            for(int j = y;j > y-4;)
            {
                if(i==19||j==0)
                { i = x+4;break;}
                if(map[i][j] != map[++i][--j])
                {
                    i = x + 4;
                    break;
                }
                score++; 
            } 
        if(score >= 5)
             return true;
        else
             return false;
}
public void paintComponent(Graphics g)
 {
       super.paintComponent(g);
       setBackground(color[2]);
       for(int i = 1;i <= 20;i++)
       {
           g.setColor(color[0]);
           g.drawLine(30, i*30 ,600 , i*30);
           g.drawLine(i*30,30, i*30, 600);
       }
       for(int i = 0;i < 20;i++)
           for(int j = 0;j < 20;j++)
           {
               if(map[i][j] == 1)
               { 
                   g.setColor(color[0]);
                   g.fillOval((i+1)*30-15, (j+1)*30-15, 30, 30);
               }
               if(map[i][j] == 2)
               {
                   g.setColor(color[1]);
                   g.fillOval((i+1)*30-15, (j+1)*30-15, 30, 30);
               }
           }
       Font font = new Font("",30,50);
       g.setFont(font);
       if(winner == 1)
       {
           g.setColor(color[0]);
           g.drawString("黑棋胜", 650, 300);
       }
       if(winner == 2)
       { 
           g.setColor(color[1]);
           g.drawString("白棋胜", 650, 300);
       }
 }
public void mouseClicked(MouseEvent me) {
}
public void mousePressed(MouseEvent me) {
    if(winner == 0)
    {
        x = me.getX();
        y = me.getY();
        chb1.setEnabled(false);
        chb2.setEnabled(false);
    }
    x = (int)(x/30)-1;
    y = (int)((y-15)/30)-1;
    if(x>=0&&x<=19&&y>=0&&y<=19)
    {
        if(flag == 1&&map[x][y] == 0)
        {
            map[x][y] = 1;
            if(win(x,y))
                winner = 1;
            flag = 2;
        }
        else
            if(flag == 2&&map[x][y] == 0)
        {
            map[x][y] = 2;
             if(win(x,y))
                 winner = 2;
            flag = 1;
        }
    }
    repaint();
}
@Override
public void mouseReleased(MouseEvent me) {
   // throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseEntered(MouseEvent me) {
   // throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseExited(MouseEvent me) {
   // throw new UnsupportedOperationException("Not supported yet.");
}

}

  • 写回答

2条回答

  • 三木来啦 2017-06-22 06:16
    关注

    没有注释,不想看,头疼,大概思路就是保存所有步数,然后回到上一步

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题