JAVA中国象棋游戏设计_第1页
JAVA中国象棋游戏设计_第2页
JAVA中国象棋游戏设计_第3页
JAVA中国象棋游戏设计_第4页
JAVA中国象棋游戏设计_第5页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

佛山科学技术学院佛山科学技术学院 可视化编程技术可视化编程技术课程设计报告课程设计报告 中国象棋游戏设计中国象棋游戏设计 学生姓名:学生姓名: 柯柯 智智 学学 号:号: 年级专业:年级专业:12 级教育技术学级教育技术学 2 指导老师:指导老师: 容汝佳容汝佳 学学 院:院: 教育科学学院教育科学学院 广东佛山 提交日期:2014 年 6 月 1 目目 录录 1.1. 前言前言22 2 2概要设计概要设计22 2.12.1 开发环境开发环境 22 2.22.2 象棋功能象棋功能 22 2.32.3 界面设计界面设计 33 3.3. 详细设计详细设计 44 3.13.1 象棋面板的象棋面板的 JAVAJAVA 类的说明类的说明 44 4.4. 运行结果运行结果88 6.6. 源程序源程序2424 设计总结设计总结3232 2 1 前言前言 随着科学技术的不断发展,计算机已经成为我们工作学习和生活中不可缺 少的工具。文本编辑器是个人计算机最司空见惯的应用程序了,在学习了 Java 语言之后,我决定使用 Java 语言编写一个简单有趣的中国象棋游戏,可以实 现简单的中国象棋游戏功能,满足日常益智娱乐需要。 Java 是由 Sun 公司开发的新一代纯面向对象的网络编程语言。其目标是建 立一种在任意种机器、任一种操作系统的网络环境中运行的软件,实行所谓的 “程序写一次,到处运行”的目标。正因为如此,Java 已成为当今 Internet 上最流行、最受欢迎的一种程序开发语言。 Java 开发小组把 Java 按特性分为基本版、移动版、企业版,每个版本有 一个软件开发包。Java 基本版本叫 Java 2 标准版(Java 2 Standard Edition,J2SE),它包含建立 Java 应用程序或者是 Applet 所需的应用程序编程 接口(API) 。Java 2 移动版(The Java 2 Mobile Edition,J2ME)包含创建 无线 Java 应用程序的 API。还有 Java 2 企业版(The Java 2 Enterprise,J2EE)是 J2SE 的增强版本,包含建立多层架构应用程序 API。 Java 语言是由 C+语言发展起而来的,是一种彻底的面向对象的程序设计 语言。作为一种纯面向对象的程序设计语言,它非常适合大型软件的开发。 Java 语言去掉了 C+语言的一些容易引起错误的特性。Java 语言的特点有:面 向对象、跨平台、安全性、多线程和图形功能强。 关键字:Java 象棋游戏,游戏类,中国象棋游戏 2 概要设计概要设计 2.1 开发环境开发环境 开发平台:Microsoft Windows XP Professional Service Pack 2 开发工具:JBuilder 2007 + JDK 1.6.0_02 2.2 象棋功能象棋功能 3 功能 1:下棋,游戏开始后通过鼠标点击对棋子进行移动。 功能 2:悔棋,通过 back 键可以后退一次最近的操作 功能 3:存档,对当前的棋局进行保存 功能 4:读档,可以继承之前保存的记录,继续进行游戏 功能 5:可以设置进行不同模式的对战,可进行双人单机,网络对战,人机对战 功能 6:翻转棋盘,可以使红黑双方对调 2.32.3 界面设计界面设计 图 2.3 象棋面板 4 3 详细设计详细设计 3.1 象棋面板使用的象棋面板使用的 Java 类的说明类的说明 package org.acerge.engine; import java.io.Serializable; import java.util.ArrayList; public class ActiveBoard implements Serializable /Rankx,Filex,Bottomx 比 x % 10,x / 10,x * 10 运算快 public static final int RANK = / File19=1,Rank19=9; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ; public static final int FILE = / File12=1,Rank12=2; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 5 ; public static final int BOTTOM = 0, 10, 20, 30, 40, 50, 60, 70, 80 ; public static final int HORSE_LEG_TABLE = /int -10, 0,-10, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,10, 0, 10 /Move.Dst - Move.Src=-21,-19,-12,-8,8,12,19,21 /HorseLegDst-Src+21=-10,-10,-1,1,-1,1,10,10:蹩马腿的增量 /Legal Move: return SquaresMove.Src + HorseLegTabMove.Dst - Move.Src + 21=0 ; public static final int PIECE_TYPES = -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 13, 13, 13 ; public final static int MAX_MOVE_NUM = 256; public final static int LOOP_HASH_MASK = 0 x3ff; public final static int MAX_CONSECUTIVE_MOVES = 200; private int player; / 0 = Red(White) and 1 = Black private int evalue;/int2Total Value of Red(0)/Black(1) Pieces private int squares;/int90Piece Indexes of each square, Squaresi = 0:Unoccupied, 16-31:Red, 32-47 = Black private int pieces;/int48Square Indexes of each piece, -1 = Captured/被 吃了 / Squarex=y(y: index of PieceTypes in Squarex), / Piecesy=x(y is the pieceType, x represent pieceType Y in Square 5), / x:index of location in board(089) private int bitFiles;/9方便按行位纵线查询 BitFiles1表示第 1 列(b 纵 线)上的棋子 6 private int bitRanks;/10方便按列位横线查询 private BitBoard pieceBits;/14分兵种及红黑的棋子位棋盘 private BitBoard allPieces;/整个棋盘 / Zobrist Key and Lock private long zobristKey, zobristLock; / History MoveNodes,用来作为循环重复检测 private int moveNum; MoveNode moveList;/ChessStruct.MaxMoveNum; char loopHash;/LoopHashMask + 1; public ActiveBoard() int i; player = 0; evalue = new int2; evalue0 = evalue1 = 0; squares = new int90; for (i = 0; i 90; i +) squaresi = 0; pieces = new int48; for (i = 16; i 48; i +) piecesi = -1; bitFiles = new int9; for (i = 0; i 9; i +) bitFilesi = 0; bitRanks = new int10; for (i = 0; i 10; i +) bitRanksi = 0; pieceBits = new BitBoard14; for (i = 0; i 14; i +) pieceBitsi = new BitBoard(0); 7 allPieces = new BitBoard(0); zobristKey = zobristLock = 0; moveNum = 1; moveList = new MoveNodeMAX_MOVE_NUM; for (i = 0;i:其他棋子 Captured = squaresMove.dst if (Captured 10) Captured = RANKMove.dst; return (player!=0) ? (Captured = 5) : (Captured 4; public MoveNode lastMove() return moveListmoveNum - 1; public int evaluation() return evalueplayer - evalue1 - player; private void changePiece(int Square, int Piece) changePiece(Square,Piece,false); 9 private void changePiece(int Square, int Piece, boolean IsAdd) int x, y, PieceType, Side, Value; allPieces.assignXor(PreMoveNodesGen.BitMaskSquare); x = FILESquare; y = RANKSquare; bitFilesx = 1 y; bitRanksy = 1 x; PieceType = PIECE_TYPESPiece; pieceBitsPieceType.assignXor(PreMoveNodesGen.BitMaskSquare); zobristKey = PreMoveNodesGen.ZobristKeyTablePieceTypeSquare; zobristLock = PreMoveNodesGen.ZobristLockTablePieceTypeSquare; if (PieceType 7) Side = 0; Value = CCEvalue.BasicValuesPieceType + CCEvalue.PosValuesPieceTypeSquare; else Side = 1; Value = CCEvalue.BasicValuesPieceType - 7 + CCEvalue.PosValuesPieceType - 789 - Square; if (IsAdd) evalueSide += Value; else evalueSide -= Value; public boolean movePiece(MoveNode Move) int Moved, Captured; MoveNode ThisMove; long OldZobristKey; if (Move.src0 | Move.dst0) return false;/add for search function OldZobristKey = zobristKey; Moved = squaresMove.src; Captured = squaresMove.dst; if (Captured!=0) 10 clearSquare(Move.dst); clearSquare(Move.src); setPiece(Move.dst, Moved); if (checked(player) Moved = squaresMove.dst; clearSquare(Move.dst); setPiece(Move.src, Moved); if (Captured!=0) setPiece(Move.dst, Captured); return false; else if (loopHash(int) (OldZobristKey changeSide(); ThisMove = Move; ThisMove.cap = Captured; ThisMove.chk = checked(player); moveListmoveNum = ThisMove; moveNum +; return true; public void undoMove() int Moved; MoveNode ThisMove; moveNum -; ThisMove = moveListmoveNum; Moved = squaresThisMove.dst; clearSquare(ThisMove.dst); setPiece(ThisMove.src, Moved); if (ThisMove.cap!=0) setPiece(ThisMove.dst, ThisMove.cap); 11 changeSide(); if (loopHash(int) (zobristKey /Leagal Move Detection Procedures public boolean leagalMove(MoveNode Move) int Piece, Attack, x, y, BitWord; Piece = squaresMove.src; if (Piece /所选的棋子是否是当前 Player 的 Attack = squaresMove.dst; if (Attack /所吃的棋子是否是对方的 switch (PIECE_TYPESPiece - (player!=0 ? 7 : 0) case 5:/炮,吃子时中间要有炮架 x = FILEMove.src; y = RANKMove.src; if (x = FILEMove.dst) /进退 BitWord = bitFilesx; if (Move.src = PreMoveNodesGen.FileNonCapMinyBitWord + BOTTOMx; else /平 BitWord = bitRanksy; if (Move.src Move.dst) if (Attack else return Move.dst = PreMoveNodesGen.RankNonCapMinxBitWord + y; case 4:/车,吃子时,中间不能有间隔 x = FILEMove.src; y = RANKMove.src; if (x = FILEMove.dst) BitWord = bitFilesx; if (Move.src Move.dst) if (Attack else return Move.dst = PreMoveNodesGen.FileNonCapMinyBitWord + BOTTOMx; else BitWord = bitRanksy; if (Move.src 1=0; default:/仕将卒,无特别规则 return true; 14 /将状态检测 public boolean checked(int Side) int SrcSq, Side7, BitFileWord, BitRankWord; SrcSq = pieces(Side + 1) 4; if (SrcSq = -1) return true; / 被将 BitFileWord = bitFilesFILESrcSq; BitRankWord = bitRanksRANKSrcSq; SrcSq = PreMoveNodesGen.KingIndexSrcSq; if (SrcSq = -1) return true; / 将出九宫? Side7 = Side!=0 ? 7 : 0; if (PreMoveNodesGen.FileRookCheckSrcSqBitFileWord.opXor(PreMoveNod esGen.RankRookCheckSrcSqBitRankWord).opAnd(pieceBits7 - Side7.opXor(pieceBits11 - Side7).notZero() return true; / 老将会面或被车将 if (PreMoveNodesGen.FileCannonCheckSrcSqBitFileWord.opXor(PreMoveN odesGen.RankCannonCheckSrcSqBitRankWord).opAnd(pieceBits12 - Side7).notZero() return true; / 炮 BitBoard tmpBit00 = PreMoveNodesGen.CheckLegsSrcSq;/别马腿的 位置 BitBoard tmpBit0 = tmpBit00.opAnd(allPieces);/别马腿的位置有子的 位置 int tmpI = BitBoard.CheckSum(PreMoveNodesGen.CheckLegsSrcSq.opAnd(allPieces); int tmpI2 = BitBoard.CheckSum(tmpBit0);/tmpI=tmpI2? BitBoard tmpBit1 = PreMoveNodesGen.KnightPinCheckSrcSqtmpI;/马将军不蹩腿的位置(实际) 15 BitBoard tmpBit2 = pieceBits10 - Side7; BitBoard tmpBit3 = tmpBit1.opAnd(tmpBit2); if (PreMoveNodesGen.KnightPinCheckSrcSqBitBoard.CheckSum(PreMoveNo desGen.CheckLegsSrcSq.opAnd(allPieces).opAnd(pieceBits10 - Side7).notZero() return true; / 马将 if (PreMoveNodesGen.PawnCheckSrcSq.opAnd(pieceBits13 - Side7).notZero() return true; / 兵将 return false; /重复检测 private final int loopMove = 1; private final int perpCheckMove = 2; private final int oppPerpCheckMove = 4; public int loopValue(int Arg, int MoveNum) return (Arg public int isLoop(int Recur) int Index, i, j, MovedPieces, ThisRecur; boolean OppSide,PerpCheck, OppPerpCheck; int DstLst=new intMAX_MOVE_NUM, SrcLst=new intMAX_MOVE_NUM; MoveNode ThisMove; if (loopHash(int) (zobristKey ThisRecur = 0; OppSide = true; 16 MovedPieces = 0; PerpCheck = OppPerpCheck = true; Index = moveNum; while (true) Index -; ThisMove = moveListIndex; if (ThisMove.cap!=0) break; if (OppSide) if (OppPerpCheck else if (PerpCheck for (i = 0; i MovedPieces; i +) if (SrcLsti = ThisMove.dst) SrcLsti = ThisMove.src; if (!OppSide) for (j = 0; j MovedPieces; j +) if (SrcLstj != DstLstj) break; if (j = MovedPieces) ThisRecur +; if (ThisRecur = Recur) return loopMove + (PerpCheck ? perpCheckMove : 0) + (OppPerpCheck ? oppPerpCheckMove : 0); break; 17 if (i = MovedPieces) DstLstMovedPieces = ThisMove.dst; SrcLstMovedPieces = ThisMove.src; MovedPieces +; OppSide = !OppSide; return 0; /Piece Types and Values public static int fenPiece(char Arg) switch (Arg) case K:/将,帅 return 0; case A:/仕 return 1; case B:/相 case E:/象 return 2; case N:/knight case H:/horse return 3; case R:/rook return 4; case C:/cannon return 5; default:/卒,兵 return 6; /- public void loadFen(final String FenStr) int i, j, k; int RedPiece=new int7; int BlackPiece=new int7; final char CharPtr; 18 int index; / Init: RedPiece0 = 16; for (i = 1; i 7; i +) RedPiecei = (i 1) + 15; for (i = 0; i 7; i +) BlackPiecei = RedPiecei + 16; for (i = 16; i = A if (i 9) if (RedPiecek = a if (i 9) if (BlackPiecek 48) setPiece(BOTTOMi + j, BlackPiecek); BlackPiecek +; i +; index+; if (CharPtrindex = 0) return; index +; / Read Player: if (player = (CharPtrindex = b ? 0 : 1) changeSide(); / Set Check Status moveListmoveNum - 1.chk = checked(player); /catch (ArrayIndexOutOfBoundsException e) /e.printStackTrace(); /System.out.println(this.AllPieces); / 20 public String getFenStr()/current state FenStr ArrayList pieceList = new ArrayList(); int i,j,k,pieceType; int FilePieces; int low,high; int bitMask = 1=0;j-) if (FilePieces else int tmpPiece = squares(8-j)*10+i; FileStr=FileStr+getFenName(PIECE_TYPEStmpPiece); if (i!=0) FileStr=FileStr+/; if(player=0) FileStr=FileStr + w - - 0 + (moveNum+1)/2; else FileStr=FileStr + b - - 0 + (moveNum+1)/2; i=0; String retVal=; if (FileStr.charAt(i)!=1) retVal = retVal+FileStr.charAt(i); i+; int maxId = FileStr.indexOf(-); while(imaxId) int m = 0; while(FileStr.charAt(i)=1) m+;i+; if(m!=0) 21 retVal=retVal+m; if(imaxId) retVal=retVal+FileStr.charAt(i); i+; retVal = retVal + FileStr.substring(FileStr.indexOf(-); return retVal; public char getFenName(int pieceTypeNum) /rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1 switch (pieceTypeNum%7) case 0 :/将,帅 return (pieceTypeNum 7) ? K:k; case 1:/仕 return (pieceTypeNum 7) ? A:a; case 2:/相 return (pieceTypeNum 7) ? B:b; case 3:/knight return (pieceTypeNum 7) ? N:n; case 4:/rook return (pieceTypeNum 7) ? R:r; case 5:/cannon return (pieceTypeNum Java 代码生成 代码和注释 25 */ package org.acerge.rule; /* * author Administrator * * 更改所生成类型注释的模板为 * 窗口 首选项 Java 代码生成 代码和注释 */ public class Rule public static final int PLAYER_RED = 0, PLAYER_BLACK = 1;/redOrBlack,红方,黑方 public static final int DIR_JIN = 1, DIR_TUI = 2 , DIR_PING = 3;/direction,进,退,平 public static final int FRONT = 1, BACK = 2;/两个同样的兵种在一 条纵线上 public static final int TIMERULE_QUICK = 1, TIMERULE_MULTI = 2;/ 时间规则的模式,快棋和多时间规则 /public static final int /* * 更改所生成文件模板为 * 窗口 首选项 Java 代码生成 代码和注释 */ package org.acerge.rule; /* * author Administrator * * 更改所生成类型注释的模板为 * 窗口 首选项 Java 代码生成 代码和注释 */ 26 public class Rule public static final int PLAYER_RED = 0, PLAYER_BLACK = 1;/redOrBlack,红方,黑方 public static final int DIR_JIN = 1, DIR_TUI = 2 , DIR_PING = 3;/direction,进,退,平 public static final int FRONT = 1, BACK = 2;/两个同样的兵种在一 条纵线上 public static final int TIMERULE_QUICK = 1, TIMERULE_MULTI = 2;/ 时间规则的模式,快棋和多时间规则 /public static final int package org.acerge.pieces; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.border.*; import org.acerge.main.*; import java.util.*; public class Qizi extends JButton protected int coordinateX;/coordinate protected int coordinateY; protected int pieceType; protected int pieceSize; protected char pieceName; protected boolean isDead;/状态 27 protected Image image; /copy public Qizi(Qizi qz) this(qz.pieceName,qz.pieceType, qz.coordinateX, qz.coordinateY, qz.image, qz.pieceSize); public String toString() return +getPieceName() + coordinateX +, + coordinateY; public char getPieceName() return pieceName; public int getPieceType() return pieceType; public void setGridSize(int gsize) pieceSize = (int)(gsize*0.9); this.setSize(pieceSize,pieceSize); public int getCoordinateX() return coordinateX; public int getCoordinateY() return coordinateY; public void setCoordinate(int x,int y) coordinateX = x; coordinateY = y; public void setCoordinate(int square) coordinateX = square/10; 28 coordinateY = square%10; public int getCoordinate() return coordinateX*10+coordinateY; public void setCoordinateX(int x) coordinateX = x; public void setCoordinateY(int y) coordinateY = y; public void setToDead() isDead = true; public void setToAlive() isDead = false; private Qizi() isDead = false; this.setBorder(BorderFactory.createRaisedBevelBorder(); this.setBorderPainted(false); this.setOpaque(false); this.setVisible(false); public Qizi(char name, int type) this(); pieceName = name;pieceType = type; public Qizi(char name, int type, Image img,int gsize) this(); pieceName = name;pieceType = type;setImage(img,gsize); 29 public Qizi(char name,int type, int x, int y, Image img, int gsize) this(); coordinateX = x; coordinateY = y; pieceType = type; pieceName = name; setImage(img,gsize); public void setPieceSize(int newSize) setImage(image, newSize); public void setImage(Image img) setImage(img, pieceSize); public void setImage(Image img,int gsize) pieceSize = gsize; image = img.getScaledInstance(pieceSize,pieceSize,Image.SCALE_SMOOTH); this.setSize(pieceSi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论