Welcome

首页 / 软件开发 / .NET编程技术 / 用WPF实现兽棋游戏

用WPF实现兽棋游戏2015-01-304x4的方格作为棋盘

16张牌打乱之后背着放在棋盘的格子里

牌分为黑红两队

规则是象吃狮  狮吃虎 吃豹狼吃狗吃猫 吃鼠 最后鼠吃象 同级相吃两个都消失

先手的那个人先翻一张牌,翻到什么颜色就用什么颜色.后翻的只能用另一种颜色

第一个翻开第一张牌确定了双方各用什么颜色的牌

然后第二个也翻一张,然后第一个人开始走第二步

他可以选择继续翻牌,或者用已经翻开的牌去撞他周围的4张牌

撞完哪张就翻开哪张,如果是自己的的牌,就完成本回合,如果是对方的牌,按照规则,谁小谁被杀死.被撞得小的话就吃了它,然后占领他的格子

玩到后期的时候,棋盘就有空格了,这时候可以选择上下左右走一步或者继续撞牌

在中间没有其他子的情况下,豹可以自由横向竖向移动,其他棋子只能上、下、左、右移动一格

根据他所提供的说明,运用WPF,做了一个小软件,其中翻牌效果应用了提供的效果。界面风格用了高手的动画效果

游戏主界面如下:

主要代码实现:

1.初始化棋子:

ChessPiece rElephant = new ChessPiece("象",Colors.Red,7); listChess.Add(rElephant); ChessPiece rLion = new ChessPiece("狮",Colors.Red,6); listChess.Add(rLion); ChessPiece rTiger = new ChessPiece("虎",Colors.Red,5); listChess.Add(rTiger);
2.吃子规则:

/// <summary> /// 棋子互吃规则 /// </summary> /// <param name="movePiece"></param> /// <param name="targetPiece"></param> /// <returns></returns> private bool CanEat(ChessPiece movePiece,ChessPiece targetPiece) { //互吃的子必须颜色不同 if (movePiece.Color != targetPiece.Color) { if (movePiece.Name == "鼠" && targetPiece.Name == "象") { return true; } else if (movePiece.Name == "象" && targetPiece.Name == "鼠") { return false; } else if (movePiece.Index >= targetPiece.Index) { return true; } return false; } return false; }
3.

/// <summary>/// 判断能否移动/// </summary>/// <param name="chessPiece"></param>/// <param name="row"></param>/// <param name="col"></param>/// <returns></returns>private bool CanMove(ChessPiece chessPiece, int row, int col){if (chessPiece.PieceRow != row && chessPiece.PieceCol != col){return false;}else{if (chessPiece.Name == "豹"){foreach (ChessPiece c in listChess){//向上移动了if (selectedPiece.PieceRow == row && selectedPiece.PieceCol > col&& c.PieceRow == row && c.PieceCol > col && c.PieceCol < selectedPiece.PieceCol){return false;}//向下移动了else if (selectedPiece.PieceRow == row && selectedPiece.PieceCol < col&& c.PieceRow == row && c.PieceCol < col && c.PieceCol > selectedPiece.PieceCol){return false;}//向左移动了else if (selectedPiece.PieceCol == col && selectedPiece.PieceRow > row&& c.PieceCol == col && c.PieceRow > row && c.PieceRow < selectedPiece.PieceRow){return false;}//向右移动了else if (selectedPiece.PieceCol == col && selectedPiece.PieceRow < row&& c.PieceCol == col && c.PieceRow < row && c.PieceRow > selectedPiece.PieceRow){return false;}}return true;}else{if ((Math.Abs(col - selectedPiece.PieceCol) == 1 && row == selectedPiece.PieceRow) || (Math.Abs(row - selectedPiece.PieceRow) == 1 && col == selectedPiece.PieceCol)){return true;}return false;} }}
4.随机排列棋子:

/// <summary> /// 随机排列数组元素 /// </summary> /// <param name="listChess"></param> /// <returns></returns> private List<ChessPiece> ListRandom(List<ChessPiece> listChess) { Random ran = new Random(); List newList = new List(); int k = 0; ChessPiece c = new ChessPiece(); for (int i = 0; i < listChess.Count; i++) {k = ran.Next(0, 15); if (k != i) { c = listChess[i]; listChess[i] = listChess[k]; listChess[k] = c; } } return listChess; }
由于水平有限,代码还有很多不足之处,代码有些乱,面向对象封装、继承、多态的特性没有体现出来,欢迎一起探讨进步

源代码:

From:cnblogs Infly