代码的结构挺好,mvc模式的,给大家分享出来
原创不易,转载请注明出处:分享网上找到的一个中国象棋源码
部分代码
Controller.java
package com.zuidaima.chess.controller;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import com.yaoyun.chess.entity.ChessBoard;
import com.yaoyun.chess.entity.ChessMan;
import com.yaoyun.chess.panel.GamePanel;
import com.yaoyun.chess.util.MusicUtil;
public class Controller extends MouseAdapter {
private static ChessBoard chessBoard;
private static ChessMan chessMan;
private static GamePanel gamePanel;
private static boolean gameover = false;
/*
* 重新显示游戏界面
*/
public Controller(GamePanel gamePanel, ChessBoard chess, ChessMan chessMan) {
Controller.chessBoard = chess;
Controller.chessMan = chessMan;
Controller.gamePanel = gamePanel;
}
/*
* 功能:响应鼠标点击事件
*/
public void mouseClicked(MouseEvent e) {
// 获取鼠标的位置,20代表的是菜单栏的高度
int x = e.getX();
int y = e.getY()-20;
// 如果鼠标点击的坐标有效
if ((x >= 40 && x <= 570) && (y >= 40 && y <= 630) && !gameover) {
// 把鼠标的坐标转换成棋子数组的下标,25代表棋子的半径
int newi = (y + 25) / 60 - 1;
int newj = (x + 25) / 60 - 1;
// 获取鼠标上一步点击位置数组的下标
int oldi = chessMan.getOldX();
int oldj = chessMan.getOldY();
// 获取保存棋子位置的数组
int[][] chessFlag = chessMan.getChessFlag();
// 判断选中的地方是否有棋子
if (chessFlag[newi][newj] == 0) {
// 判断上一步是否有选中棋子
if (oldi != -1 && oldj != -1) {
// 判断棋子是否能移动
if (isChessManMove(chessFlag, newi, newj)) {
//播放下棋声音
if(MusicUtil.isGameMusic()){
MusicUtil.playMusic("go");
}
// 移动棋子
chessMan.setChessFlag(newi, newj, chessFlag[oldi][oldj]);
chessMan.setChessFlag(oldi, oldj, 0);
// 移动成功后,清除上一步选中的棋子
chessMan.setOldX(-1);
chessMan.setOldY(-1);
gamePanel.dispaly(chessBoard, chessMan);
// 轮到对方下棋
chessMan.setRedMove(!chessMan.isRedMove());
}
}
} else {
// 判断上一步是否有棋子
if (oldi == -1 && oldj == -1) { // 上一步没有棋子
// 如果有棋子,并且是自己
if ((oldi == newi && oldj == newj)) {
} else {
// 把选中的棋子设为选中状态
chessMan.setSelect(newi, newj);
// 并把其保存为上一颗棋子
chessMan.setOldX(newi);
chessMan.setOldY(newj);
showGame();
}
} else { // 如果上一步有棋子
// 判断是否吃的到下一步的棋子
if (isChessManMove(chessFlag, newi, newj)) {
// 判断是否是自己一方的棋子
if ((chessFlag[oldi][oldj] >= 8 && chessFlag[newi][newj] < 8) || (chessFlag[oldi][oldj] < 8 && chessFlag[newi][newj] >= 8)) {
if (chessFlag[newi][newj] == 5) {
gamePanel.setMessage(1);
gameover = true;
} else if (chessFlag[newi][newj] == 14) {
gamePanel.setMessage(2);
gameover = true;
}
//播放吃棋子的声音
if(MusicUtil.isGameMusic()){
MusicUtil.playMusic("eat");
}
// 吃棋子
chessMan.setChessFlag(newi, newj,chessFlag[oldi][oldj]);
chessMan.setChessFlag(oldi, oldj, 0);
// 轮到对方下棋
chessMan.setRedMove(!chessMan.isRedMove());
}
}
// 并清除上一步棋子
chessMan.setOldX(-1);
chessMan.setOldY(-1);
// 重新显示界面
showGame();
}
}
}
}
/*
* 功能:重新显示界面
*/
public static void showGame(){
gamePanel.dispaly(chessBoard, chessMan);
}
/*
* 判断棋子是否能移动
* chessFlag:棋盘棋子坐标
* newi、newj:棋子下一步的坐标
*/
public boolean isChessManMove(int[][] chessFlag, int newi, int newj) {
// 默认棋子不能移动
boolean b = false;
// 获取上一步选中的棋子坐标
int oldi = chessMan.getOldX();
int oldj = chessMan.getOldY();
// 默认该棋子是红方的
boolean redFlag = true;
// 棋子不是为红方的
if (chessFlag[oldi][oldj] >= 8) {
redFlag = false;
}
// 如果棋子是红方且不是红方下棋,则返回
if(redFlag != chessMan.isRedMove()){
System.out.println("还没轮到你下呢!");
return false;
}
// 获取棋盘中所有的棋子
String[] chessStr = chessMan.getChessStr();
// 获取上一步选中的是什么棋子
String name = chessStr[chessFlag[oldi][oldj] - 1];
// 根据棋子名称选择棋子的走法
if(name.equals("兵")) {
if (oldi >= 5) { // 没有过河
if ((oldi - newi == 1) && (newj == oldj)) {
b = true;
}
} else { // 已过河
if ((oldi - newi == 1) && (newj == oldj)) {
b = true;
} else if ((Math.abs(newj - oldj) == 1) && (newi == oldi)) {
b = true;
} else {
return false;
}
}
}
else if(name.equals("卒")) {
if (oldi < 5) { // 没有过河
if ((newi - oldi == 1) && (newj == oldj)) {
b = true;
}
} else { // 已过河
if ((newi - oldi == 1) && (newj == oldj)) {
b = true;
} else if ((Math.abs(newj - oldj) == 1) && (newi == oldi)) {
b = true;
} else {
return false;
}
}
}
else if(name.equals("车")) {
if (newi == oldi) {
int k = 0;
if (newj > oldj) { // 红车水平向右方向移动
for (int i = oldj; i <= newj; i++) {
if (chessFlag[oldi][i] > 0) {
k++;
}
}
} else {
for (int i = oldj; i >= newj; i--) { // 红车水平向左方向移动
if (chessFlag[oldi][i] > 0) {
k++;
}
}
}
if (k <= 2) {
b = true;
}
} else if (newj == oldj) { // 红车垂直下方向移动
int k = 0;
if (newi > oldi) {
for (int i = oldi; i <= newi; i++) {
if (chessFlag[i][oldj] > 0) {
k++;
}
}
} else {
for (int i = oldi; i >= newi; i--) { // 红车垂直上方向移动
if (chessFlag[i][oldj] > 0) {
k++;
}
}
}
if (k <= 2) {
b = true;
}
}
}
else if(name.equals("炮")) {
int s = 0;
if (newi == oldi) {
if (newj > oldj) {
for (int i = oldj; i <= newj; i++) {
if (chessFlag[oldi][i] > 0) {
s++;
}
}
} else {
for (int i = oldj; i >= newj; i--) {
if (chessFlag[oldi][i] > 0) {
s++;
}
}
}
} else if (newj == oldj) {
if (newi > oldi) {
for (int i = oldi; i <= newi; i++) {
if (chessFlag[i][oldj] > 0) {
s++;
}
}
} else {
for (int i = oldi; i >= newi; i--) {
if (chessFlag[i][oldj] > 0) {
s++;
}
}
}
}
if (s == 1 || (s == 3 && chessFlag[newi][newj] > 0)) {
b = true;
}
}
else if(name.equals("帅")||name.equals("将")) {
if (newj == oldj && (Math.abs(newi - oldi) >= 5)) {
int flag = 0;
if (newi > oldi) { // 将吃帅
for (int i = oldi; i <= newi; i++) {
if ((chessFlag[oldi][oldj] == 5)
&& (chessFlag[newi][newj] == 14)) {
if (chessFlag[i][oldj] > 0) {
flag++;
}
}
}
} else { // 帅吃将
for (int i = newi; i <= oldi; i++) {
if ((chessFlag[oldi][oldj] == 14)
&& (chessFlag[newi][newj] == 5)) {
if (chessFlag[i][oldj] > 0) {
flag++;
}
}
}
}
if (flag == 2) {
b = true;
}
} else if (((newi <= 2 && redFlag == true) || (newi >= 7 && redFlag == false))
&& (newj <= 5 && newj >= 3)) {
if (Math.abs(newi - oldi) == 1 && newj == oldj) {
b = true;
} else if (Math.abs(newj - oldj) == 1 && newi == oldi) {
b = true;
}
}
}
else if(name.equals("士")) {
if (redFlag) { // 红方棋子
if (newi <= 2 && newj <= 5 && newj >= 3) {
if (Math.abs(newi - oldi) == 1
&& Math.abs(newj - oldj) == 1) {
b = true;
}
}
} else { // 黑方棋子
if (newi >= 7 && newj <= 5 && newj >= 3) {
if (Math.abs(newi - oldi) == 1
&& Math.abs(newj - oldj) == 1) {
b = true;
}
}
}
}
else if(name.equals("象")||name.equals("相")) {
if ((newi >= 5 && redFlag == false)
|| (newi < 5 && redFlag == true)) {
if ((newi - oldi) == 2 && (newj - oldj) == 2) { // 移到右下方
if (chessFlag[oldi + 1][oldj + 1] == 0) {
b = true;
}
} else if ((newi - oldi) == -2 && (newj - oldj) == 2) { // 移到右上方
if (chessFlag[oldi - 1][oldj + 1] == 0) {
b = true;
}
} else if ((newi - oldi) == 2 && (newj - oldj) == -2) { // 移到左下方
if (chessFlag[oldi + 1][oldj - 1] == 0) {
b = true;
}
} else if ((newi - oldi) == -2 && (newj - oldj) == -2) { // 移到左上方
if (chessFlag[oldi - 1][oldj - 1] == 0) {
b = true;
}
}
}
}
else if(name.equals("马")) {
if ((newi - oldi) == -2 && (newj - oldj) == 1) { // 移到NE方
if (chessFlag[oldi - 1][oldj] == 0) {
b = true;
}
} else if ((newi - oldi) == -2 && (newj - oldj) == -1) { // 移到NW方
if (chessFlag[oldi - 1][oldj] == 0) {
b = true;
}
} else if ((newi - oldi) == 2 && (newj - oldj) == 1) { // 移到SE方
if (chessFlag[oldi + 1][oldj] == 0) {
b = true;
}
} else if ((newi - oldi) == 2 && (newj - oldj) == -1) { // 移到SW方
if (chessFlag[oldi + 1][oldj] == 0) {
b = true;
}
} else if ((newi - oldi) == -1 && (newj - oldj) == 2) { // 移到EN方
if (chessFlag[oldi][oldj + 1] == 0) {
b = true;
}
} else if ((newi - oldi) == 1 && (newj - oldj) == 2) { // 移到ES方
if (chessFlag[oldi][oldj + 1] == 0) {
b = true;
}
} else if ((newi - oldi) == -1 && (newj - oldj) == -2) { // 移到WN方
if (chessFlag[oldi][oldj - 1] == 0) {
b = true;
}
} else if ((newi - oldi) == 1 && (newj - oldj) == -2) { // 移到WS方
if (chessFlag[oldi][oldj - 1] == 0) {
b = true;
}
}
}
return b;
}
/*
* 功能:设置游戏状态
*/
public static void setGameover(boolean gameover) {
Controller.gameover = gameover;
}
}
package com.zuidaima.chess.entity;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.io.Serializable;
import com.yaoyun.chess.util.FontUtil;
@SuppressWarnings("serial")
public class ChessMan implements Serializable {
private int oldX = -1; // 棋子上一步所在位置的x坐标
private int oldY = -1; // 棋子上一步所在位置的y坐标
private int newX = -1; // 棋子现在所在位置的x坐标
private int newY = -1; // 棋子现在所在位置的y坐标
private boolean redMove = true; // 红棋先行
private String[] chessStr = { "车", "马", "象", "士", "将", "炮", "卒", "兵", "炮", "车", "马", "相", "士", "帅" };
private static int[][] chessFlag = new ChessFlag().getChessFlag();
private boolean[][] select = new boolean[10][9]; // 保存棋子被选中的坐标
/*
* 功能: 画出象棋棋盘中的棋子
*/
public void drawChessMan(Graphics g) {
// 转成2D模式
Graphics2D g2d = (Graphics2D) g;
// 获取当前的画刷
Stroke stroke = g2d.getStroke();
// 遍历棋盘中所有的位置
for (int i = 0; i < chessFlag.length; i++) {
for (int j = 0; j < chessFlag[i].length; j++) {
// 判断指定位置是否有棋子
if (chessFlag[i][j] != 0) {
// 把棋子数组的下标转换成棋子的坐标
int x = (j + 1) * 60;
int y = (i + 1) * 60;
// 判断指定位置的棋子是否被选中
if (select[i][j]) {
// 设置画刷的颜色
g2d.setColor(new Color(0, 200, 0));
// 设置画刷的大小
g2d.setStroke(new BasicStroke(3));
// 画选中棋子时的棋子边框
g2d.drawLine(x - 25, y - 20, x - 25, y - 25);
g2d.drawLine(x - 25, y + 20, x - 25, y + 25);
g2d.drawLine(x - 20, y - 25, x - 25, y - 25);
g2d.drawLine(x - 20, y + 25, x - 25, y + 25);
g2d.drawLine(x + 25, y - 20, x + 25, y - 25);
g2d.drawLine(x + 25, y + 20, x + 25, y + 25);
g2d.drawLine(x + 25, y + 25, x + 20, y + 25);
g2d.drawLine(x + 25, y - 25, x + 20, y - 25);
// 还原为默认画刷
g2d.setStroke(stroke);
// 把棋子设为没有选中的状态
select[i][j] = !select[i][j];
}
// 画内圆
g2d.setColor(new Color(219, 196, 154));
g2d.fillOval(x - 25, y - 25, 50, 50);
// 画大外圆
g2d.setColor(new Color(109, 98, 77));
g2d.drawOval(x - 25, y - 25, 50, 50);
// 根据棋子的所属一方,显示不同的棋子颜色
if (chessFlag[i][j] < 8) {
g2d.setColor(Color.RED);
} else {
g2d.setColor(Color.BLACK);
}
// 画小外圆
g2d.drawOval(x - 20, y - 20, 40, 40);
g2d.setFont(FontUtil.myFont2);
String newstring = new String(chessStr[chessFlag[i][j] - 1]);
g2d.drawString(newstring, x - 20, y + 10);
}
}
}
}
public int getOldX() {
return oldX;
}
public boolean isRedMove() {
return redMove;
}
public void setRedMove(boolean redMove) {
this.redMove = redMove;
}
public void setOldX(int oldX) {
this.oldX = oldX;
}
public int getOldY() {
return oldY;
}
public void setOldY(int oldY) {
this.oldY = oldY;
}
public int getNewX() {
return newX;
}
public void setNewX(int newX) {
this.newX = newX;
}
public int getNewY() {
return newY;
}
public void setNewY(int newY) {
this.newY = newY;
}
// 获取棋子对应的字符串
public String[] getChessStr() {
return chessStr;
}
// 获取棋盘中所有棋子的位置
public int[][] getChessFlag() {
return chessFlag;
}
// 设置棋子对应的字符串
public void setChessFlag(int[][] chessFlag) {
ChessMan.chessFlag = chessFlag;
}
// 设置棋子的坐标
public void setChessFlag(int i, int j, int z) {
chessFlag[i][j] = z;
}
// 获取选中状态
public boolean[][] getSelect() {
return select;
}
// 设置选中状态
public void setSelect(int i, int j) {
select[i][j] = !select[i][j];
}
}
代码下载地址:http://www.zuidaima.com/share/1550463398644736.htm