代码功能和界面比较粗糙,还有更多的完善空间。
思路:生成窗口 -> 画棋盘 ->真值数组和颜色数组 ->搜索判赢
注:文件中的主类名为Board
import java.awt.*;
import java.awt.event.*;
//import javax.swing.Painter;
public class Board extends Frame implements MouseListener {
int win=0;
int color_=0;
boolean loc[][]=new boolean[15][15]; //记录此位置是否有棋子
int colors[][]=new int[15][15]; //记录此位置棋子颜色
public void tte(){ for(int i=0;i<15;i++) { for(int j=0;j<15;j++) { //清空颜色数组
colors[i][j]=-1; } } }
public void paint(Graphics g) { //画棋盘
// final Graphics g2 = null;
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
for (int i = 0; i < 15; i++) {
g2.drawLine(i * 30 + 100, 100, i * 30 + 100, 14 * 30 + 100);
g2.drawLine(100, i * 30 + 100, 14 * 30 + 100, i * 30 + 100);
}
g2.setStroke(new BasicStroke(3.5f)); //加粗棋盘边线
g2.drawLine(100, 100, 100, 14 * 30 + 100);
g2.drawLine(30 * 14 + 100, 100, 30 * 14 + 100, 14 * 30 + 100);
g2.drawLine(100, 100, 14 * 30 + 100, 100);
g2.drawLine(100, 30 * 14 + 100, 14 * 30 + 100, 14 * 30 + 100);
g2.fillOval(100+85,100+85,5,5);
g2.fillOval(185,95+11*30,5,5);
g2.fillOval(95+11*30,185,5,5);
g2.fillOval(95+11*30,95+11*30,5,5);
}
public Board() { //显示棋盘和实现关闭窗口指令和鼠标监听器
setSize(800, 800);
setBackground(Color.ORANGE);
setTitle("五子棋");
setVisible(true);
//paint(null);
addWindowListener(new closeWin());
tte();
addMouseListener(this);
}
public static void main(String[] args) { //新建窗口
new Board();
}
class closeWin extends WindowAdapter { //关闭窗口
@Override
public void windowClosing(WindowEvent e) {
Window w = e.getWindow();
w.dispose();
}
}
public void find(int xx,int yy,int color) { //判赢
if(1+find1(xx-1,yy-1,color)+find5(xx+1,yy+1,color)>=5) {
if(color==1)System.out.println("黑子胜!");
else System.out.println("白子胜!");
win=1;
}
else if(fi_2(xx,yy,color)>=5) {
if(color==1)System.out.println("黑子胜!");
else System.out.println("白子胜!");
win=1;
}
else if(1+find3(xx+1,yy-1,color)+find7(xx-1,yy+1,color)>=5) {
if(color==1)System.out.println("黑子胜!");
else System.out.println("白子胜!");
win=1;
}
else if(fi_4(xx,yy,color)>=5) {
if(color==1)System.out.println("黑子胜!");
else System.out.println("白子胜!");
win=1;
}
if(win==1) { //一方赢时输出信息,然后重新生成一个窗口进行下一场
for(int i=0;i<15;i++) {
for(int j=0;j<15;j++) {
colors[i][j]=-1;
loc[i][j]=false;
}
}
new Board();
}
}
//find和fi_都为搜索函数
public int fi_2(int xx,int yy,int color) { //遍历竖向
int k=0,max=-1;
for(int i=0;i<14;i++) {
if(colors[xx][i]==color) {
k++;
if(k>max) {
max=k;
}
}
else k=0;
}
//System.out.println("竖着:"+max);
return max;
}
public int fi_4(int xx,int yy,int color) { //遍历横向
int k=0,max=-1;
for(int i=0;i<14;i++) {
if(colors[i][yy]==color) {
k++;
if(k>max) {
max=k;
}
}
else k=0;
}
//System.out.println("横着:"+max);
return max;
}
public int find1(int xx,int yy,int color) { //递归
//System.out.println("Find1*");
if(xx>14||yy>14||xx<0||yy<0) {
return 0;
}
else {
if(loc[xx][yy]==false||colors[xx][yy]!=color)return 0;
else {
//System.out.println(1+find1(xx-1,yy-1,color));
return 1+find1(xx-1,yy-1,color);
}
}
}
public int find3(int xx,int yy,int color) { //递归
//System.out.println("Find3");
if(xx>14||yy>14||xx<0||yy<0) {
return 0;
}
else {
if(loc[xx][yy]==false||colors[xx][yy]!=color)return 0;
else return 1+find3(xx+1,yy-1,color);
}
}
public int find5(int xx,int yy,int color) { //递归
if(xx>14||yy>14||xx<0||yy<0) {
return 0;
}
else {
if(loc[xx][yy]==false||colors[xx][yy]!=color)return 0;
else return 1+find5(xx+1,yy+1,color);
}
}
public int find7(int xx,int yy,int color) { //递归
if(xx>14||yy>14||xx<0||yy<0) {
return 0;
}
else {
if(loc[xx][yy]==false||colors[xx][yy]!=color)return 0;
else return 1+find7(xx-1,yy+1,color);
}
}
@Override
public void mouseClicked(MouseEvent e) { //在棋盘上增加棋子
// TODO 自动生成的方法存根
Graphics g = this.getGraphics();
//g.drawOval(e.getX(), e.getY(), 5, 5);
int xx=e.getX();
int yy=e.getY();
xx=(xx-100+15)/30;
yy=(yy-100+15)/30;
if(xx>14||yy>14)return;
else if(loc[xx][yy])return;
else {
if(color_%2==0) {
g.setColor(Color.black);
colors[xx][yy]=1;
}
else {
g.setColor(Color.white);
colors[xx][yy]=0;
}
//System.out.println(xx+","+yy);
loc[xx][yy]=true;
g.fillOval(xx*30-7+100,yy*30-7+100, 12, 12);
color_++;
find(xx,yy,colors[xx][yy]);
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseExited(MouseEvent e) {
// TODO 自动生成的方法存根
}
}