//Nếu chơi với máy hì máy sẽ chon quân Hậu và không hieern thị form chọn //Nếu đóng Form thì mặc định sẽ chọn quân hậu public static bool Promotion(ChessBase UcPawn, ChessPieceType PromoteTo) { if ((UcPawn.Position.y == 7) || (UcPawn.Position.y == 0)) { // Bitmap queen = clsImageProcess.GetChessPieceBitMap(UcPawn.Side, ChessPieceType.Queen, UcPawn.Style); // Bitmap root = clsImageProcess.GetChessPieceBitMap(UcPawn.Side, ChessPieceType.Rook, UcPawn.Style); // Bitmap knight = clsImageProcess.GetChessPieceBitMap(UcPawn.Side, ChessPieceType.Knight, UcPawn.Style); // Bitmap bishop = clsImageProcess.GetChessPieceBitMap(UcPawn.Side, ChessPieceType.Bishop, UcPawn.Style); // if (PromoteTo == ChessPieceType.Null) // { // Form f = new frmPromotion(queen, root, knight, bishop); // f.ShowDialog(); // UcPawn.Type = frmPromotion.Type; // } // else // { // UcPawn.Type = PromoteTo; // } // Bitmap image = clsImageProcess.GetChessPieceBitMap(UcPawn.Side, UcPawn.Type, UcPawn.Style); // UcPawn.Image = image; return(true); } return(false); }
/// <summary> /// 获取棋子可移动方向 /// </summary> /// <param name="chess">棋子对象</param> /// <param name="moveX">移动横向距离</param> /// <param name="moveY">移动纵向距离</param> /// <returns>棋子可移动方向</returns> private Direction GetChessMoveDirection(ChessBase chess, double moveX, double moveY) { double absX = Math.Abs(moveX); double absY = Math.Abs(moveY); Direction moveDiretion = Direction.Hold; if (absX > 0 || absY > 0) { if (absX >= absY) { if (moveX > 0 && chess.CanMoveRight(this.BlankPosition, Common.GridRows, Common.GridColumns)) { moveDiretion = Direction.Right; } if (moveX < 0 && chess.CanMoveLeft(this.BlankPosition, Common.GridRows, Common.GridColumns)) { moveDiretion = Direction.Left; } } else { if (moveY > 0 && chess.CanMoveDown(this.BlankPosition, Common.GridRows, Common.GridColumns)) { moveDiretion = Direction.Down; } if (moveY < 0 && chess.CanMoveUp(this.BlankPosition, Common.GridRows, Common.GridColumns)) { moveDiretion = Direction.Up; } } } return(moveDiretion); }
/// <summary> /// 移动棋子到新的位置 /// </summary> /// <param name="chess">待移动的棋子</param> /// <param name="moveDirection">移动方向</param> /// <param name="newBlankPosition">新的空白格子</param> private void MoveChessToNext(ChessBase chess, Direction moveDirection, BlankPosition newBlankPosition) { int oldPosition = chess.Position; int newPosition = chess.NextPosition; this.BlankPosition = chess.ChessMove(newBlankPosition, moveDirection, Common.GridColumns); chess.Position = chess.NextPosition; this.MoveAnimation(chess.Element, newPosition, oldPosition, moveDirection); }
private int GetChess(ChessBase chess) { int index = (int)chess.Type - 1; if (chess.IsWhite == ChessSide.White) { index += 6; } return(index); }
public void ClearView() { image.color = new Color(0, 0, 0, 0); CurrentType = null; CellStatus = CellType.None; firstTime = true; if (PreChess != null) { SetView(PreChess); } }
public void TempView(ChessBase chess) { if (firstTime) { firstTime = false; PreChess = CurrentType; } CurrentType = chess; image.color = new Color(1, 1, 1, 0.5f); image.sprite = MainController.ListChess [GetChess(chess)]; CellStatus = CellType.Temp; image.SetNativeSize(); }
public void SetView(ChessBase chess) { image.color = new Color(1, 1, 1, 1); CurrentType = chess; chess.Position = new Vector2(PosX, PosY); image.sprite = MainController.ListChess [GetChess(chess)]; image.SetNativeSize(); int temp = (int)chess.Type - 1; if (temp < MainController.AddPosY.Count) { rect.localPosition = new Vector2(0, MainController.AddPosY[temp]); } }
/// <summary> /// 尝试将棋子移动到下一个指定的位置 /// </summary> /// <param name="chess">待移动的棋子</param> /// <param name="moveDirection">移动方向</param> /// <param name="newBlankPosition">新的空白格子</param> private void TryMoveToNext(ChessBase chess, Direction moveDirection, BlankPosition newBlankPosition) { List <ChessBase> chessList = this.CurrentNode.CurrentCode.GetChessList(); chessList[this.ChessIndex].Position = chess.NextPosition; long nodeCode = chessList.GetLayoutCode(); if (nodeCode != this.CurrentNode.ParentCode && !this.StepCodeDict.Keys.Contains(nodeCode)) { PathNode node = new PathNode { CurrentCode = nodeCode, ParentCode = this.CurrentNode.CurrentCode, //BlankPosition = chess.ChessMove(this.CurrentNode.BlankPosition, moveDirection, Common.GridColumns), BlankPosition = chess.ChessMove(newBlankPosition, moveDirection, Common.GridColumns), IsLast = chessList.LayoutFinished() }; this.NextNodeList.Add(node); } }
/// <summary> /// 画布局 /// </summary> /// <param name="layoutCode"></param> /// <param name="container"></param> public static void Draw(this long layoutCode, Panel container) { ChessType chessType; long tempCode = 0; for (int r = 0; r < Common.GridRows; r++) { for (int c = 0; c < Common.GridColumns; c++) { int idx = r * Common.GridColumns + c; tempCode = Common.ChessBit << (idx * 3); chessType = (ChessType)((int)((tempCode & layoutCode) >> (idx * 3))); ChessBase currentChess = chessType.CreateChess(); if (chessType != ChessType.Blank) { currentChess.CreateElement(GridSize, 0, (double)c * GridSize, (double)r * GridSize); container.Children.Add(currentChess.Element); } } } }
/// <summary> /// 初始化布局 /// </summary> /// <param name="container"></param> public void Draw(Panel container) { IDictionary <ChessType, int> chessCountDict = new Dictionary <ChessType, int>(4) { { ChessType.Square, 0 }, { ChessType.HRect, 0 }, { ChessType.VRect, 0 }, { ChessType.Block, 0 } }; ChessType chessType; long tempCode = 0; for (int r = 0; r < Common.GridRows; r++) { for (int c = 0; c < Common.GridColumns; c++) { int idx = r * Common.GridColumns + c; tempCode = Common.ChessBit << (idx * 3); chessType = (ChessType)((int)((tempCode & this.LayoutCode) >> (idx * 3))); if (chessType != ChessType.Blank) { ChessBase currentChess = chessType.CreateChess(); currentChess.Position = idx; currentChess.NextPosition = idx; currentChess.CreateElement(GridSize, chessCountDict[chessType], (double)(c * GridSize), (double)(r * GridSize)); currentChess.Element.DragCompleted += new DragCompletedEventHandler((sender, e) => { Direction moveDirection = this.GetChessMoveDirection(currentChess, e.HorizontalChange, e.VerticalChange); if (moveDirection != Direction.Hold) { currentChess.SetNewPosition(moveDirection, Common.GridColumns, this.BlankPosition, this.MoveChessToNext); } }); container.Children.Add(currentChess.Element); this.ChessList.Add(currentChess); chessCountDict[chessType]++; } } } }
/// <summary> /// 将布局UINT64数值转换为棋子布局集合 /// </summary> /// <param name="layoutCode">布局UINT64数值</param> /// <returns>棋子布局集合</returns> public static List <ChessBase> GetChessList(this long layoutCode) { List <ChessBase> chessList = new List <ChessBase>(0); ChessType chessType; long tempCode = 0; for (int r = 0; r < GridRows; r++) { for (int c = 0; c < GridColumns; c++) { int idx = r * GridColumns + c; tempCode = ChessBit << (idx * 3); chessType = (ChessType)((int)((tempCode & layoutCode) >> (idx * 3))); if (chessType != ChessType.Blank) { ChessBase currentChess = chessType.CreateChess(); currentChess.Position = idx; currentChess.NextPosition = idx; chessList.Add(currentChess); } } } return(chessList); }
/// <summary> /// 设置网格背景颜色并且判断类型 /// </summary> /// <param name="idx"></param> private void SetRectangleColor(int idx) { string rectName = "Rect_" + idx; Rectangle tmpRect = this.GridRect_Dict[rectName]; ChessBase chess = null; if (this.radioButton1.IsChecked.Value) { // 2*2 棋子 (田) int p1 = idx, p2 = p1 + 1, p3 = p1 + 4, p4 = p2 + 4; if (p1 % 4 > 2 || p1 > 15) { MessageBox.Show("该网格不能放置2*2棋子!"); return; } if (this.HasSetRectIdxList.Contains(p1) && this.HasSetRectIdxList.Contains(p2) && this.HasSetRectIdxList.Contains(p3) && this.HasSetRectIdxList.Contains(p4)) { tmpRect.Stroke = new SolidColorBrush(Colors.Gray); tmpRect.Fill = new SolidColorBrush(Colors.Red); tmpRect = this.GridRect_Dict["Rect_" + p2]; tmpRect.Stroke = new SolidColorBrush(Colors.Gray); tmpRect.Fill = new SolidColorBrush(Colors.Red); tmpRect = this.GridRect_Dict["Rect_" + p3]; tmpRect.Stroke = new SolidColorBrush(Colors.Gray); tmpRect.Fill = new SolidColorBrush(Colors.Red); tmpRect = this.GridRect_Dict["Rect_" + p4]; tmpRect.Stroke = new SolidColorBrush(Colors.Gray); tmpRect.Fill = new SolidColorBrush(Colors.Red); this.HasSetRectIdxList.Remove(idx); this.HasSetRectIdxList.Remove(p2); this.HasSetRectIdxList.Remove(p3); this.HasSetRectIdxList.Remove(p4); chess = ChessType.Block.CreateChess(); chess.Position = idx; this.InitChessList.Add(chess); return; } else { MessageBox.Show("该网格不能放置2*2棋子!"); return; } } if (this.radioButton2.IsChecked.Value) { // 2*1 棋子 (日) int p1 = idx, p2 = p1 + 4; if (p1 > 15) { MessageBox.Show("该网格不能放置2*1棋子!"); return; } if (this.HasSetRectIdxList.Contains(p1) && this.HasSetRectIdxList.Contains(p2)) { tmpRect.Stroke = new SolidColorBrush(Colors.Gray); tmpRect.Fill = new SolidColorBrush(Colors.Blue); tmpRect = this.GridRect_Dict["Rect_" + p2]; tmpRect.Stroke = new SolidColorBrush(Colors.Gray); tmpRect.Fill = new SolidColorBrush(Colors.Blue); this.HasSetRectIdxList.Remove(idx); this.HasSetRectIdxList.Remove(p2); chess = ChessType.VRect.CreateChess(); chess.Position = idx; this.InitChessList.Add(chess); return; } else { MessageBox.Show("该网格不能放置2*1棋子!"); return; } } if (this.radioButton3.IsChecked.Value) { // 1*2 棋子 (口口) int p1 = idx, p2 = p1 + 1; if (p1 % 4 > 2) { MessageBox.Show("该网格不能放置1*2棋子!"); return; } if (this.HasSetRectIdxList.Contains(p1) && this.HasSetRectIdxList.Contains(p2)) { tmpRect.Stroke = new SolidColorBrush(Colors.Gray); tmpRect.Fill = new SolidColorBrush(Colors.Orange); tmpRect = this.GridRect_Dict["Rect_" + p2]; tmpRect.Stroke = new SolidColorBrush(Colors.Gray); tmpRect.Fill = new SolidColorBrush(Colors.Orange); this.HasSetRectIdxList.Remove(idx); this.HasSetRectIdxList.Remove(p2); chess = ChessType.HRect.CreateChess(); chess.Position = idx; this.InitChessList.Add(chess); return; } else { MessageBox.Show("该网格不能放置1*2棋子!"); return; } } if (this.radioButton4.IsChecked.Value) { // 1*1 棋子 (口) if (this.HasSetRectIdxList.Contains(idx)) { tmpRect.Stroke = new SolidColorBrush(Colors.Gray); tmpRect.Fill = new SolidColorBrush(Colors.Yellow); this.HasSetRectIdxList.Remove(idx); chess = ChessType.Square.CreateChess(); chess.Position = idx; this.InitChessList.Add(chess); return; } else { MessageBox.Show("该网格已放置其他棋子!"); return; } } }
public void ClearPreCell() { PreChess = null; }
private void NewGame() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { _BoardState [i, j] = 0; } } // Pawn = 0,//Tốt // Bishop = 1,//Tượng // Knight = 2,//Mã // Rook = 3,//Xe // Queen = 4,//Hậu // King = 5,//Vua string mapData = "1-0-0-3;1-7-0-3;1-1-0-2;1-6-0-2;1-2-0-1;1-5-0-1;1-3-0-5;1-4-0-4;" + "1-0-1-0;1-1-1-0;1-2-1-0;1-3-1-0;1-4-1-0;1-5-1-0;1-6-1-0;1-7-1-0;" + "0-0-6-0;0-1-6-0;0-2-6-0;0-3-6-0;0-4-6-0;0-5-6-0;0-6-6-0;0-7-6-0;" + "0-0-7-3;0-7-7-3;0-1-7-2;0-6-7-2;0-2-7-1;0-5-7-1;0-3-7-5;0-4-7-4;"; var temp = mapData.Split(';'); foreach (var item in temp) { if (item.Length > 0) { ChessBase newChess = new ChessBase(); var parts = item.Split('-'); newChess.IsWhite = parts [0] == "0" ? ChessSide.White : ChessSide.Black; newChess.Position = new Vector2(int.Parse(parts[1]), int.Parse(parts[2])); newChess.Type = (ChessPieceType)(int.Parse(parts [3]) + 1); int index = newChess.IsWhite == ChessSide.White ? 1 : 2; _BoardState[(int)newChess.Position.x + 1, (int)newChess.Position.y + 1] = (int)newChess.Type * 10 + index; AllChess.Add(newChess); } } currentMap = new MapData(); currentMap.IsWhiteFirst = true; currentMap.CurrentTime = 0; currentMap.ListAllChess.AddRange(AllChess); foreach (var cell in AllCell) { cell.ClearView(); } foreach (var item in currentMap.ListAllChess) { foreach (var cell in AllCell) { if ((int)item.Position.x == cell.PosX && (int)item.Position.y == cell.PosY) { cell.SetView(item); } } } arrFEN = new ArrayList(); arrFEN.Add(clsFEN.GetPiecePlacementString(this._BoardState)); }
public void SelectingThisCell(CellBase cell) { if (CurrentStatus != GameStatus.NowPlaying || isThinking) { return; } if (CurrentGameMode == GameMode.TwoPlayer || isWhite == ChessSide.White) { if (cell.CurrentType != null && cell.CellStatus == CellBase.CellType.None && cell.CurrentType.IsWhite == isWhite) { if (preCell != null) { foreach (var item in TempCell) { item.ClearView(); } } TempCell.Clear(); Selecting.color = new Color(1, 1, 1, 1); Selecting.transform.position = cell.gameObject.transform.position; isSelecting = true; var temp = FindAllPossibleMove(cell); ShowTempMove(cell, temp); preCell = cell; } else if (cell.CurrentType != null && cell.CellStatus == CellBase.CellType.Temp && cell.CurrentType.IsWhite == isWhite) { ChessBase chess = cell.CurrentType; foreach (var item in TempCell) { item.ClearView(); } TempCell.Clear(); if (preCell != null && preCell.CurrentType != null) { LastMove = new clsMove(new Vector2(preCell.PosX + 1, preCell.PosY + 1), new Vector2(cell.PosX + 1, cell.PosY + 1)); int index2 = preCell.CurrentType.IsWhite == ChessSide.White ? 1 : 2; _BoardState [(int)preCell.CurrentType.Position.x + 1, (int)preCell.CurrentType.Position.y + 1] = 0; preCell.ClearPreCell(); preCell.ClearView(); preCell = null; } cell.SetView(chess); Selecting.color = new Color(1, 1, 1, 0); arrMove = clsChessEngine.FindAllLegalMove(_BoardState, MyMove.CurPos, cell.CurrentType.Type); if (_BoardState [(int)cell.CurrentType.Position.x + 1, (int)cell.CurrentType.Position.y + 1] / 10 == 6) { EndGame(); } else { int index = cell.CurrentType.IsWhite == ChessSide.White ? 1 : 2; _BoardState [(int)cell.CurrentType.Position.x + 1, (int)cell.CurrentType.Position.y + 1] = (int)cell.CurrentType.Type * 10 + index; isWhite = isWhite == ChessSide.Black ? ChessSide.White : ChessSide.Black; } arrFEN.Add(clsFEN.GetPiecePlacementString(this._BoardState)); } } else { ComputerMove(); } }