| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3199 人关注过本帖
标题:五子棋
取消只看楼主 加入收藏
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
五子棋
程序代码:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ChessGame extends JPanel {
    private static final long serialVersionUID = 1L;
    // size, area, rows, cols
    static final int CELL_SIZE = 32;
    static final int DEFAULT_WIDTH = CELL_SIZE * 19;
    static final int DEFAULT_HEIGHT = CELL_SIZE * 19;
    static final int rows = 19;
    static final int cols = 19;
    static BufferedImage  blackImg;
    static BufferedImage whiteImg;
    static List<Cell> cells = new ArrayList<Cell>();
    static JFrame frame=new JFrame();
    static ChessGame game = new ChessGame();
    static int state;
    // state value
    static final int START=0,RUNNING=1,OVER=2;
    static class Cell{
        int x;
        int y;
        boolean mark;
        public Cell(int x, int y) {
            this.x = x;
            this.y = y;
        }
        @Override
        public boolean equals(Object obj) {
            if(obj instanceof Cell){
                return this.x==((Cell)obj).x&&
                        this.y==((Cell)obj).y;
            }
            return false;
        }
    }
    void init() throws IOException{        
        blackImg = ImageIO.read(ChessGame.class.getResource("black.gif"));
        whiteImg = ImageIO.read(ChessGame.class.getResource("white.gif"));
        frame.setSize(700,700);
        frame.setAlwaysOnTop(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(game);
        frame.setVisible(true);
        MouseAdapter mouseAdapter = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(state==START){
                    state=RUNNING;
                }else if(state==OVER){
                    state=START;
                    cells.clear();
                } else if(state==RUNNING) {
                    int s = CELL_SIZE;
                    int x=e.getX(),y=e.getY();
                    int sp_x=x%s,sp_y=y%s;
                    int x1=x-sp_x,y1=y-sp_y;
                    x1+=+sp_x>s/2?s:0;
                    y1+=sp_y>s/2?s:0;
                    Cell newCell = new Cell(x1, y1);
                    if(!cells.contains(newCell)){
                        newCell.mark=cells.size()%2==0;
                        cells.add(newCell);                    
                        for(int i=1; i<=4; i++) check(newCell, i);
                    }
                }
                game.repaint();
            }
        };
        game.addMouseListener(mouseAdapter);
        game.addMouseMotionListener(mouseAdapter);
    }
    public static void main(String []args) throws IOException {
        new ChessGame().init();
    }
    void paintArea(Graphics g){
        g.setColor(Color.WHITE);
        int s = CELL_SIZE, t = DEFAULT_WIDTH - CELL_SIZE;
        g.fillRect(s, s, t, t);
        g.setColor(Color.BLACK);
        for (int i = 0; i < rows; i++) {
            g.drawLine(s*(1+i), s, s*(1+i), s*rows);    
            g.drawLine(s, s*(1+i), s*rows, s*(i+1));                            
        }
        for (int i = 3; i <= 15;i += 6) {
            for (int j = 3; j <= 15; j += 6) {
                g.fillArc((i+1)*CELL_SIZE-6, (j+1)*CELL_SIZE-6, 12, 12, 0, 360);
            }
        }
    }
    void paintCell(Graphics g){
        for(Cell c: cells){
            g.drawImage(c.mark?whiteImg:blackImg, c.x-CELL_SIZE/2, c.y-CELL_SIZE/2, null);
        }
    }
    void paintState(Graphics g){
        g.setColor(Color.RED);
        g.setFont(new Font("Arial", Font.BOLD, 32));
        switch(state){
        case OVER:
            String res=cells.get(cells.size()-1).mark?"white":"black";
            g.drawString("game over  @1 win!".replace("@1", res), DEFAULT_WIDTH/3, DEFAULT_HEIGHT/2);
            break;
        case START:
            g.drawString("start", DEFAULT_WIDTH/3, DEFAULT_HEIGHT/2);
        }
    }
    // paint task
    public void paint(Graphics g) {
        paintArea(g);
        paintCell(g);
        paintState(g);
    }
    boolean subCheck(Cell c, Cell cell, int type) {
        if(type == 1) {
            return c.x==cell.x;
        } else if(type == 2) {
            return c.y==cell.y;
        } else if(type == 3) {
            return Math.abs(c.y-cell.y-c.x+cell.x) == 0;
        } else if(type == 4) {
            return Math.abs(c.y-cell.y+c.x-cell.x) == 0;
        }
        return false;
    }
    // check game is over
    void check(Cell cell, int type) {
        if(cells.stream().filter(c->subCheck(c, cell, type)&&c.mark==cell.mark&&
                Math.abs(c.y-cell.y)<=CELL_SIZE*4&&Math.abs(c.y-cell.y)>0).count()>=4) {
            state = OVER;
            return;
        }
    }
}

图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: import java static int Cell 
2019-05-11 23:31
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
附棋子图片
图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册


剑栈风樯各苦辛,别时冰雪到时春
2019-05-11 23:36
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
1、原横向判定无效。
 横向坐标、纵向坐标均需判定。

好像写在subCheck里面了,不确定对不对啊

2、封堵一边后,连线数量为4同样结束。
计数>=5

这个提的好,得加钱

3、重新开局不会擦出边界上的图片的超出范围的部分。

恩,说得对,改起来应该不难,留给其他同学们练手吧

希望大家多写多想不怕犯错,有这么多大神给你们指出错误,一定能有所收获


剑栈风樯各苦辛,别时冰雪到时春
2019-05-15 13:19
快速回复:五子棋
数据加载中...
 
   



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

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