五子棋
程序代码:
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; } } }