java五子棋(二)

今天我们继续来完善五子棋的功能。

上次将棋盘的大体框架弄好了,按钮添加进去(没有功能时效),画笔传递以及棋盘的重绘问题。

今天主要完成立体棋子的绘制以及重绘问题。

建立Chess类,储存棋子对象。

import java.awt.Color;

public class Chess {
	public int x,y;
	public Color color;
	public Chess(int x,int y,Color color) {
		this.x=x;
		this.y=y;
		this.color =color;
	}
}

 在ChassMouse类里添加相关的属性

private Graphics gr;
	private int x0 = 50, y0 = 50, Size = 50, Chess = 50,Line=15;
	private int chessX, chessY;
	private int chessCount = 0;
	private int[][] chessArr = new int[Line][Line];
	private boolean start = false;
	private Color color;
	
	private Chess[] objArr =new Chess[15*15];
	
	public void setGr(Graphics g) {
		gr = g;
	}

	public int[][] getChessArr() {
		return chessArr;
	}

设置棋子交点值,使得点击后,棋子落在棋盘的交点上。

public void mouseClicked(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		// 计算交点值
		if ((x - x0) % Size > Size / 2) {
			chessX = (x - x0) / Size + 1;
		} else {
			chessX = (x - x0) / Size;
		}
		if ((y - y0) % Size > Size / 2) {
			chessY = (y - y0) / Size + 1;
		} else {
			chessY = (y - y0) / Size;
		}
	}

 接着判断是否有棋子,不能下重复棋。

// 判断当前位置是否有棋子
		if (chessArr[chessY][chessX] == 0) {
			//判断计数器的奇偶数
			if (chessCount % 2 == 0) {
				color =Color.BLACK;
				chessArr[chessY][chessX] = 1;
			} else {
				color =Color.WHITE;
				chessArr[chessY][chessX] = -1;
			}
			
			Chess chess =new Chess(chessX,chessY,color);
			objArr[chessCount] =chess;
			
			drawChess();

			chessCount++;
}

在ChassMouse类里绘制立体棋子

public void drawChess() {
		if(chessCount % 2 == 0) {
			for(int i=0;i<100;i++) {
				gr.setColor(new Color(i*2,i*2,i*2));
				gr.fillOval(chessX * Size + x0 - Chess / 2+i/2, chessY * Size + y0 - Chess / 2, Chess-i, Chess-i);
			}
		}else {
			for(int i=0;i<Chess;i++) {
				gr.setColor(new Color(200+i,200+i,200+i));
				gr.fillOval(chessX * Size + x0 - Chess / 2+i/2, chessY * Size + y0 - Chess / 2, Chess-i, Chess-i);
			}
		}
		
	}

判断输赢

public boolean isWin(int x, int y) {
		int count1 = 1, count2 = 1, count3 = 0, count4 = 1;
		// 横
		for (int i = x + 1; i < chessArr.length; i++) {
			if (chessArr[y][x] == chessArr[y][i]) {
				count1++;
			} else {
				break;
			}
		}
		for (int i = x - 1; i >= 0; i--) {
			if (chessArr[y][x] == chessArr[y][i]) {
				count1++;
			} else {
				break;
			}
		}
		// 竖
		for (int i = y + 1; i < chessArr.length; i++) {
			if (chessArr[y][x] == chessArr[i][x]) {
				count2++;
			} else {
				break;
			}
		}
		for (int i = y - 1; i >= 0; i--) {
			if (chessArr[y][x] == chessArr[i][x]) {
				count2++;
			} else {
				break;
			}
		}
		// 斜
		for (int i = x, j = y; i >= 0 && j >= 0; i--, j--) {
			if (chessArr[y][x] == chessArr[j][i]) {
				count3++;
			} else {
				break;
			}
		}
		for (int i = x + 1, j = y + 1; i < chessArr.length && j < chessArr.length; i++, j++) {
			if (chessArr[y][x] == chessArr[j][i]) {
				count3++;
			} else {
				break;
			}
		}
		for (int i = x - 1, j = y + 1; i >= 0 && j < chessArr.length; i--, j++) {
			if (chessArr[y][x] == chessArr[j][i]) {
				count4++;
			} else {
				break;
			}
		}
		for (int i = x + 1, j = y - 1; i < chessArr.length && j >= 0; i++, j--) {
			if (chessArr[y][x] == chessArr[j][i]) {
				count4++;
			} else {
				break;
			}
		}
		if (count1 >= 5 || count2 >= 5 || count3 >= 5 || count4 >= 5) {
			return true;
		}
		return false;
	}

写完判断输赢的方法以后,在mouseClicked方法中实现

         //判断输赢
			if (isWin(chessX, chessY) == true) {
				start =false;//后面讲到的按钮功能会用,这里先提一下
				gr.setColor(Color.RED);
				if (chessArr[chessY][chessX] == 1) {
					System.out.println("黑棋赢!");
					JOptionPane.showMessageDialog(null, "黑棋获胜!");
				}
				if (chessArr[chessY][chessX] == -1) {
					System.out.println("白棋赢!");
					JOptionPane.showMessageDialog(null, "白棋获胜!");
				}
			}

接下来进行棋子的重绘,需要在ChessJPanel里面进行


import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;


public class ChessJPanel extends JPanel {
	private int [][] chessArr;
	private int x0=50,y0=50,Size=50,Chess=50,Line=15;
	public void setChessArr(int [][] chessArr) {
		this.chessArr=chessArr;
	}
	public void paint(Graphics g) {
		super.paint(g);
		//重绘棋盘
		for(int i=0;i<Line;i++) {
			g.drawLine(x0, y0+Size*i, (Line-1)*Size+x0,y0+Size*i);
			g.drawLine(x0+Size*i, y0, x0+Size*i, (Line-1)*Size+y0);
		}
		
		for(int i=0;i<chessArr.length;i++) {
			for(int j=0;j<chessArr.length;j++) {
				int chess=chessArr[i][j];
				if(chess==1) {
					drawChessB(g,j,i);
				}else if(chess==-1) {
					drawChessW(g,j,i);
				}
			}
			
		}
		
	}
	public void drawChessB(Graphics gr,int chessX, int chessY) {
		for (int i = 0; i < 100; i++) {
			gr.setColor(new Color(i * 2, i * 2, i * 2));
			gr.fillOval(chessX * Size + x0 - Chess / 2 + i / 2, chessY * Size + y0 - Chess / 2, Chess - i, Chess - i);
		}
	}

	public void drawChessW(Graphics gr,int chessX, int chessY) {
		for (int i = 0; i < Chess; i++) {
			gr.setColor(new Color(200 + i, 200 + i, 200 + i));
			gr.fillOval(chessX * Size + x0 - Chess / 2 + i / 2, chessY * Size + y0 - Chess / 2, Chess - i, Chess - i);
		}
	}
	
}

今天实现了棋子的重绘以及棋子判断问题,下篇将解决按钮实现的问题,我们下篇见

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值