package com.cn.fiveinarow;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ChessBoard extends JPanel implements MouseListener {
public static final int MARGIN = 30;
public static final int GRID_SPAN = 35;
public static final int ROWS = 10;
public static final int COLS = 10;
Point[] chessList = new Point[(ROWS + 1) * (COLS + 1)];
boolean isBack = true;
boolean gameOver = false;
int chessCount;
int xIndex, yIndex;
public ChessBoard() {
setBackground(Color.ORANGE);
addMouseListener(this);
addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
int x1 = (e.getX() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
int y1 = (e.getY() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
if (x1 < 0 || x1 > ROWS || y1 < 0 || y1 > COLS || gameOver
|| findChess(x1, y1))
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
else
setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i <= ROWS; i++) {
g.drawLine(MARGIN, MARGIN + i * GRID_SPAN, MARGIN + COLS
* GRID_SPAN, MARGIN + i * GRID_SPAN);
}
for (int i = 0; i <= COLS; i++) {
g.drawLine(MARGIN + i * GRID_SPAN, MARGIN, MARGIN + i * GRID_SPAN,
MARGIN + ROWS * GRID_SPAN);
}
for (int i = 0; i < chessCount; i++) {
int xPos = chessList[i].getX() * GRID_SPAN + MARGIN;
int yPos = chessList[i].getY() * GRID_SPAN + MARGIN;
g.setColor(chessList[i].getColor());
g.fillOval(xPos - Point.DIAMETER / 2, yPos - Point.DIAMETER / 2,
Point.DIAMETER, Point.DIAMETER);
if (i == chessCount - 1) {
g.setColor(Color.red);
g.drawRect(xPos - Point.DIAMETER / 2,
yPos - Point.DIAMETER / 2, Point.DIAMETER,
Point.DIAMETER);
}
}
}
public void mousePressed(MouseEvent e) {
if (gameOver) {
return;
}
String colorName = isBack ? "黑棋" : "白棋";
xIndex = (e.getX() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
yIndex = (e.getY() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
if (xIndex < 0 || xIndex > ROWS || yIndex < 0 || yIndex > COLS)
return;
if (findChess(xIndex, yIndex))
return;
Point ch = new Point(xIndex, yIndex, isBack ? Color.black : Color.white);
chessList[chessCount++] = ch;
repaint();
if (isWin()) {
String msg = String.format("恭喜,%s赢了!", colorName);
JOptionPane.showMessageDialog(this, msg);
gameOver = true;
}
isBack = !isBack;
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
private boolean findChess(int x, int y) {
for (Point c : chessList) {
if (c != null && c.getX() == x && c.getY() == y) {
return true;
}
}
return false;
}
private boolean isWin() {
int continueCount = 1;
for (int x = xIndex - 1; x >= 0; x--) {
Color c = isBack ? Color.black : Color.white;
if (getChess(x, yIndex, c) != null) {
continueCount++;
} else
break;
}
for (int x = xIndex + 1; x <= ROWS; x++) {
Color c = isBack ? Color.black : Color.white;
if (getChess(x, yIndex, c) != null) {
continueCount++;
} else
break;
}
if (continueCount >= 5) {
return true;
} else {
continueCount = 1;
}
for (int y = yIndex - 1; y >= 0; y--) {
Color c = isBack ? Color.black : Color.white;
if (getChess(xIndex, y, c) != null) {
continueCount++;
} else
break;
}
for (int y = yIndex + 1; y <= COLS; y++) {
Color c = isBack ? Color.black : Color.white;
if (getChess(xIndex, y, c) != null) {
continueCount++;
} else
break;
}
if (continueCount >= 5) {
return true;
} else {
continueCount = 1;
}
for (int x = xIndex + 1, y = yIndex - 1; y >= 0 && x <= COLS; x++, y--) {
Color c = isBack ? Color.black : Color.white;
if (getChess(x, y, c) != null) {
continueCount++;
} else
break;
}
for (int x = xIndex - 1, y = yIndex + 1; y <= ROWS && x >= 0; x--, y++) {
Color c = isBack ? Color.black : Color.white;
if (getChess(x, y, c) != null) {
continueCount++;
} else
break;
}
if (continueCount >= 5) {
return true;
} else {
continueCount = 1;
}
for (int x = xIndex - 1, y = yIndex - 1; y >= 0 && x >= 0; x--, y--) {
Color c = isBack ? Color.black : Color.white;
if (getChess(x, y, c) != null) {
continueCount++;
} else
break;
}
for (int x = xIndex + 1, y = yIndex + 1; y <= ROWS && x <= COLS; x++, y++) {
Color c = isBack ? Color.black : Color.white;
if (getChess(x, y, c) != null) {
continueCount++;
} else
break;
}
if (continueCount >= 5) {
return true;
} else {
continueCount = 1;
}
return false;
}
private Point getChess(int xIndex, int yIndex, Color color) {
for (Point c : chessList) {
if (c != null && c.getX() == xIndex && c.getY() == yIndex
&& c.getColor() == color) {
return c;
}
}
return null;
}
public void restartGame() {
for (int i = 0; i < chessList.length; i++) {
chessList[i] = null;
}
isBack = true;
gameOver = false;
chessCount = 0;
repaint();
}
public void goback() {
if (chessCount == 0)
return;
chessList[chessCount - 1] = null;
chessCount--;
if (chessCount > 0) {
xIndex = chessList[chessCount - 1].getX();
yIndex = chessList[chessCount - 1].getY();
}
isBack = !isBack;
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(MARGIN * 2 + GRID_SPAN * COLS, MARGIN * 2
+ GRID_SPAN * ROWS);
}
}
Java单机版五子棋源码
需积分: 10 198 浏览量
2015-09-17
15:25:40
上传
评论
收藏 13KB ZIP 举报
阿信vvvv
- 粉丝: 0
- 资源: 2
最新资源
- 探索tecreate:软件开发的未来之星.zip
- 打标机项目C#源码连接扫码
- 基于SSM的房屋租赁系统的设计与实现
- xyctf:从入门到精通的实用指南.zip
- mmqrcode1714153659780.png
- Screenshot_2024-04-27-06-08-58-486_com.baidu.xin.aiqicha.jpg
- 基于Javaweb+Tomcat+MySQL的大学生公寓管理系统+sql文件.zip
- 实训作业基于javaweb的订单管理系统源码+数据库+实训报告.zip
- 多机调度问题贪心算法基于最小堆和贪心算法求解多机调度问题.zip
- 基于同态加密技术的匿名电子投票系统源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈