// Start is called before the first frame update void Start() { int row = ChessHelper.GetRow(gameObject.transform.parent.gameObject); int column = transform.GetSiblingIndex() + 1; position = new Vector2(column, row); }
public void DoubleMove() { IBoard board = new ChessBoardBulider() .SetPawn(PawChess.King, 8, 4, PawColors.Black) .SetPawn(PawChess.King, 1, 5, PawColors.White) .SetPawn(PawChess.Pawn, 2, 4, PawColors.White) .Bulid(); IChessGame game = ChessHelper.SetChessGame(board); IField whitePawn = game.Board.FieldList.First(f => f.Heigh == 2 && f.Width == 4); //Act var firstWherCanMove = game.PawnWherCanMove(whitePawn); var secendField = firstWherCanMove.First(f => f.Heigh == 4); game.PawnMove(whitePawn, secendField); IEnumerable <IField> secendWherCanMove = game.PawnWherCanMove(secendField); //Czy to wyjdzie? //Assert /* * Assert.AreEqual(moveList.Count(), 2); * Assert.IsTrue(moveList.Any(a => a.Heigh == 3 & a.Width == 4)); * Assert.IsTrue(moveList.Any(a => a.Heigh == 4 & a.Width == 4)); */ }
public static void AttackPiece(Piece attacker, Piece attacked) { ChessBoard board = ChessTracker.board; if (attacked.isWhite) { board.WhitePieces.Remove(board.WhitePieces.Find(x => x.Controller.name == attacked.Controller.name)); } else { board.BlackPieces.Remove(board.BlackPieces.Find(x => x.Controller.name == attacked.Controller.name)); } Destroy(ChessHelper.GetPiece(attacked.position).Controller.gameObject); attacker.position = attacked.position; attacker.Controller.transform.position = ChessHelper.CalcPosition(attacked.position, attacked); ChessHistory.selectedSquare.GetComponent <MeshRenderer>().material = ChessHistory.selectedSquareMat; ChessHelper.ResetOldSquares(); ChessHelper.ResetHistory(); ChessHelper.ResetOldAttackSquares(); ChessTracker.isWhiteTurn = !ChessTracker.isWhiteTurn; ChessHelper.HandleKingsInCheck(); }
public static void ClearMovesWithPieces(ref List <Vector2> moves) { for (int i = moves.Count - 1; i >= 0; i--) { if (ChessHelper.HasPiece(moves[i])) { moves.Remove(moves[i]); } } }
public static string CheckEndGame(this Engine engine) { var reason = ChessHelper.checkEndGame(engine); if (reason == FSharpOption <Tuple <Command.SessionResult, string> > .None) { return(null); } else { return(reason.Value.Item2); } }
private static Bitboard[] InitializeKnightAttacks() { var result = new Bitboard[ChessConstants.SquareCount]; for (var squareIndex = 0; squareIndex < ChessConstants.SquareCount; squareIndex++) { var square = new Square(squareIndex); var moveSquares = ChessHelper.GetKnightMoveSquares(square); var bitboard = new Bitboard(moveSquares); result[squareIndex] = bitboard; } return(result); }
public void AutoOutCard() { OneSide oneSide = Battle.CurrentSide; bool isWin = IsWin(Battle.CurrentSide, true, null); if (isWin) { FlipCard(); } else { Card card = ChessHelper.GetRecommendOutCard(oneSide.Cards); OutCard(oneSide, card.Type, card.Num, card.ID); } }
public static GameObject PlaceSquare(Vector2 position, GameObject prefab) { GameObject squarePossibleMove = ChessHelper.GetSquare(position); if (squarePossibleMove == null) { return(null); } Vector3 squarePosition = squarePossibleMove.transform.position; Vector3 moveableSquarePos = new Vector3(squarePosition.x, squarePosition.y + 0.1f, squarePosition.z); GameObject GOmoveableSquare = Instantiate(prefab, moveableSquarePos, Quaternion.identity); GOmoveableSquare.transform.SetParent(squarePossibleMove.transform); GOmoveableSquare.transform.position = moveableSquarePos; return(GOmoveableSquare); }
public void FirstMovChose() { IBoard board = new ChessBoardBulider() .SetPawn(PawChess.King, 8, 4, PawColors.Black) .SetPawn(PawChess.King, 1, 5, PawColors.White) .SetPawn(PawChess.Pawn, 2, 4, PawColors.White) .Bulid(); IChessGame game = ChessHelper.SetChessGame(board); IField whitePawn = game.Board.FieldList.First(f => f.Heigh == 2 && f.Width == 4); //Act IEnumerable <IField> moveList = game.PawnWherCanMove(whitePawn); //Assert Assert.AreEqual(moveList.Count(), 2); Assert.IsTrue(moveList.Any(a => a.Heigh == 3 & a.Width == 4)); Assert.IsTrue(moveList.Any(a => a.Heigh == 4 & a.Width == 4)); }
public void HandOutCard(Battle battle) { SynchronousData(battle); if (BattleAIClient.SendServerCommand.UserID != battle.CurrentSide.AccountID) { return; } LogHelper.DebugLog("随机时间回调:" + BattleAIClient.SendServerCommand.UserID); Task.Factory.StartNew(() => { Sleep(); OneSide oneSide = battle.Sides.FirstOrDefault(c => c.AccountID == BattleAIClient.SendServerCommand.UserID); List <WinCardModel> winCardModels = WinALG.Win(oneSide.Cards, true); if (winCardModels.Count > 0) { BattleAIClient.SendServerCommand.HandOutCardBack(false, CardType.Tiao, 0, 0); } else { Card card = ChessHelper.GetRecommendOutCard(oneSide.Cards); BattleAIClient.SendServerCommand.HandOutCardBack(true, card.Type, card.Num, card.ID); } }); }
private void OnMouseDown() { if (ChessHistory.selectedSquare != null && ChessHistory.selectedPiece.PossibleMoves.Contains(position)) { // Move the pawn to chosen square ChessHistory.selectedPiece.position = position; ChessHistory.selectedPiece.Controller.transform.position = ChessHelper.CalcPosition(position, ChessHistory.selectedPiece); if (ChessHistory.selectedPiece is Pawn) { Pawn pawn = (Pawn)ChessHistory.selectedPiece; pawn.isFirstMove = false; } ChessTracker.isWhiteTurn = !ChessTracker.isWhiteTurn; ChessHistory.selectedSquare.GetComponent <MeshRenderer>().material = ChessHistory.selectedSquareMat; ChessHelper.ResetOldSquares(); ChessHelper.ResetOldAttackSquares(); ChessHelper.ResetHistory(); // Calculate if any king is in check or checkmate ChessHelper.HandleKingsInCheck(); } }
public ChessService(AssetService assetService, ChessHelper chessHelper) { _chessHelper = chessHelper; _assetService = assetService; }
/// <summary> /// 同步数据,玩家在网络不好,或是断线重连时会直接显示当前数据 /// </summary> /// <param name="battle"></param> public void SynchronousData(Battle battle) { SendCommand.BattleCode = battle.Code; //if (battle.CratorID == SendCommand.UserID) //{ // BattleUI.BtnJSRoom.gameObject.SetActive(true); //} //else //{ // BattleUI.BtnJSRoom.gameObject.SetActive(false); //} if (battle.Step != BattleCommand.CreateBattleBack && (int)battle.Step > (int)BattleCommand.RollDice) { IsBattleing = true; } BattleUI.MatchLoading.gameObject.SetActive(false); BattleUI.EnableOperation(); //同步玩家信息 OneSide mySide = battle.Sides.FirstOrDefault(c => c.AccountID == SendCommand.UserID); if (mySide == null) { return; } if (GlobalVariable.IsBattleRecordPlay && battle.CurrentSide != null) { mySide = battle.CurrentSide; } SetCurrentPlayer(mySide.Order); int orderOffset = mySide.Order - 1; BattleUI.BattleCode.text = battle.Code; BattleUI.LeaveCardNum.text = battle.LibraryCardNum.ToString(); if (ZhuoXinRenderer != null && battle.CurrentSide != null) { if (battle.CurrentSide.Order == 1) { ZhuoXinRenderer.sharedMaterial.SetTexture("_MainTex", MJZhuoTextureN); } else if (battle.CurrentSide.Order == 2) { ZhuoXinRenderer.sharedMaterial.SetTexture("_MainTex", MJZhuoTextureX); } else if (battle.CurrentSide.Order == 3) { ZhuoXinRenderer.sharedMaterial.SetTexture("_MainTex", MJZhuoTextureB); } else if (battle.CurrentSide.Order == 4) { ZhuoXinRenderer.sharedMaterial.SetTexture("_MainTex", MJZhuoTextureD); } } foreach (var p in BattleUI.PlayerUIList) { int newOrder = p.Order + orderOffset; if (newOrder > 4) { newOrder = newOrder % 4; } OneSide side = battle.Sides.FirstOrDefault(c => c.Order == newOrder); if (side != null) { p.Show(side.NickName, side.Face, side.TotalScore, side.Vip, System.Convert.ToInt32(side.AccountID), battle); } else { p.gameObject.SetActive(false); } if (IsBattleing) { p.GoOut.gameObject.SetActive(false); } } //同步卡牌信息 foreach (var p in PlayerList) { if (battle.Sides == null) { continue; } OneSide oneSide = battle.Sides.FirstOrDefault(c => c.Order == p.Order); if (oneSide == null) { continue; } p.UserID = oneSide.AccountID; //同步手牌区 foreach (var c in oneSide.Cards) { CardCtr cardCtr = p.HandCards.GetCard(c.ID); if (cardCtr == null) { CardCtr newCard = CardLibrary.GetOrCreateCard(c.ID, c.Type, c.Num); newCard.IsFront = c.IsFront; newCard.PlayerCtr = p; p.AddCardToHandCards(newCard, newCard.IsNew); } else { cardCtr.IsFront = c.IsFront; } } List <CardCtr> handCards = new List <CardCtr>(p.HandCards.ListCards()); foreach (var cc in handCards) { if (!oneSide.Cards.Any(c => c.ID == cc.ID)) { p.HandCards.RemoveCard(cc); } } //同步出牌区 foreach (var c in oneSide.OutCards) { CardCtr cardCtr = p.OutCards1.GetCard(c.ID); if (cardCtr == null) { cardCtr = p.OutCards2.GetCard(c.ID); } if (cardCtr == null) { cardCtr = p.OutCards3.GetCard(c.ID); } if (cardCtr == null) { CardCtr newCard = CardLibrary.GetOrCreateCard(c.ID, c.Type, c.Num); p.AddCardToOutCards(newCard); } else { cardCtr.IsFront = c.IsFront; } } List <CardCtr> outCards = new List <CardCtr>(p.OutCards1.ListCards()); outCards.AddRange(p.OutCards2.ListCards()); outCards.AddRange(p.OutCards3.ListCards()); foreach (var cc in outCards) { if (!oneSide.OutCards.Any(c => c.ID == cc.ID)) { p.OutCards1.RemoveCard(cc); p.OutCards2.RemoveCard(cc); } } } //获取听牌列表 List <Card> listenCards = ChessHelper.CanListenCardList(mySide.Cards); PlayerCtr myPlayerCtr = PlayerList.FirstOrDefault(c => c.UserID == mySide.AccountID); foreach (var c in myPlayerCtr.HandCards.CardList) { if (IsCanOutCard && listenCards.Any(b => b.ID == c.ID)) { ShowTing(c); } else { HideTing(c); } } //重新计算位置 if ((battle.Step != BattleCommand.TakeCard && battle.Step != BattleCommand.HandOutCard) || GlobalVariable.IsBattleRecordPlay) { foreach (var p in PlayerList) { p.HandCards.AutoSettleCards(); } } //排除可能要播动画的,其它的直接设置位置 if (battle.Step == BattleCommand.Licensing) { } else if (battle.Step == BattleCommand.TakeCard) { foreach (var c in CardLibrary.ListAllShowCards()) { if (c.ID == battle.CurrentSide.GetACard.ID) { continue; } c.transform.localPosition = c.TargetLocalPosition; } } else if (battle.Step == BattleCommand.HandOutCard) { foreach (var c in CardLibrary.ListAllShowCards()) { if (battle.CurrentSide.GetACard != null && c.ID == battle.CurrentSide.GetACard.ID) { continue; } c.transform.localPosition = c.TargetLocalPosition; } } else if (battle.Step == BattleCommand.NoticeOutCard) { foreach (var c in CardLibrary.ListAllShowCards()) { if (c.ID == battle.CurrentSide.TakeOutCard.ID) { continue; } c.transform.localPosition = c.TargetLocalPosition; } } else if (battle.Step == BattleCommand.NoticeTouchCard) { foreach (var c in CardLibrary.ListAllShowCards()) { if (battle.CurrentSide.TakeOutCard == null) { continue; } if (c.IsFront && c.CardType == battle.CurrentSide.TakeOutCard.Type && c.Num == battle.CurrentSide.TakeOutCard.Num) { continue; } c.transform.localPosition = c.TargetLocalPosition; } } else { foreach (var c in CardLibrary.ListAllShowCards()) { c.transform.localPosition = c.TargetLocalPosition; } } }
public Square[] GetPotentialMoveSquares( CastlingOptions castlingOptions, [CanBeNull] EnPassantCaptureInfo enPassantCaptureInfo, Square sourceSquare) { var piece = PiecePosition[sourceSquare]; var pieceType = piece.GetPieceType(); if (pieceType == PieceType.None) { throw new ArgumentException( $@"No piece at the source square '{sourceSquare}'.", nameof(sourceSquare)); } var pieceSide = piece.GetSide().EnsureNotNull(); switch (pieceType) { case PieceType.Knight: //// TODO [HarinezumiSama] Use bitboard instead of squares var result = ChessHelper.GetKnightMoveSquares(sourceSquare) .Where(square => PiecePosition[square].GetSide() != pieceSide) .ToArray(); return(result); case PieceType.King: case PieceType.Pawn: throw new InvalidOperationException("MUST NOT go into this branch anymore."); case PieceType.None: throw new ArgumentException( $@"No piece at the source square '{sourceSquare}'.", nameof(sourceSquare)); case PieceType.Bishop: case PieceType.Rook: case PieceType.Queen: // Just go ahead break; default: throw pieceType.CreateEnumValueNotImplementedException(); } var resultList = new List <Square>(); if (pieceType.IsSlidingStraight()) { GetPotentialMoveSquaresByRays( sourceSquare, pieceSide, ChessHelper.StraightRays, ChessHelper.MaxSlidingPieceDistance, true, resultList); } if (pieceType.IsSlidingDiagonally()) { GetPotentialMoveSquaresByRays( sourceSquare, pieceSide, ChessHelper.DiagonalRays, ChessHelper.MaxSlidingPieceDistance, true, resultList); } return(resultList.ToArray()); }
public ChessModule(ChessService chessService, ChessHelper chessHelper) { _chessHelper = chessHelper; _chessService = chessService; }
private void OnMouseDown() { bool setMatOnCurrentSquare = true; bool displayMoveableSquares = true; if (piece.isWhite && !ChessTracker.isWhiteTurn) { if (ChessHistory.selectedPiece != null && ChessHistory.selectedPiece.PossibleAttacks.Contains(piece.position)) { ChessHelper.AttackPiece(ChessHistory.selectedPiece, piece); piece.attacks = new List <Vector2>(); } return; } else if (!piece.isWhite && ChessTracker.isWhiteTurn) { if (ChessHistory.selectedPiece != null && ChessHistory.selectedPiece.PossibleAttacks.Contains(piece.position)) { ChessHelper.AttackPiece(ChessHistory.selectedPiece, piece); piece.attacks = new List <Vector2>(); } return; } GameObject square = ChessHelper.GetSquare(piece.position); if (ChessHistory.selectedSquare != null) { // If clicks on same piece if (square.GetInstanceID() == ChessHistory.selectedSquare.GetInstanceID()) { square.GetComponent <MeshRenderer>().material = ChessHistory.selectedSquareMat; setMatOnCurrentSquare = false; ChessHelper.ResetHistory(); displayMoveableSquares = false; } else { ChessHistory.selectedSquare.GetComponent <MeshRenderer>().material = ChessHistory.selectedSquareMat; } } // Removing old moveable squares ChessHelper.ResetOldSquares(); ChessHelper.ResetOldAttackSquares(); piece.attacks = new List <Vector2>(); Debug.Log(displayMoveableSquares); // Displaying moveable squares if (displayMoveableSquares) { foreach (Vector2 possibleMove in piece.PossibleMoves) { GameObject go = ChessHelper.PlaceSquare(possibleMove, piece.moveableSquare); ChessHistory.moveableSquares.Add(go); } } if (setMatOnCurrentSquare) { ChessHistory.selectedSquareMat = square.GetComponent <MeshRenderer>().material; square.GetComponent <MeshRenderer>().material = piece.selectedMat; ChessHistory.selectedSquare = square; ChessHistory.selectedPiece = piece; } // Display possible attacks if (displayMoveableSquares) { foreach (Vector2 possibleAttack in piece.PossibleAttacks) { if (ChessHelper.HasPiece(possibleAttack) && ChessHelper.GetPiece(possibleAttack).isWhite != piece.isWhite) { GameObject go = ChessHelper.PlaceSquare(possibleAttack, piece.attackSquare); ChessHistory.attackSquares.Add(go); } } } }
public static Vector3 CalcPosition(Vector2 pos, Piece piece) { Vector3 SquarePos = ChessHelper.GetSquare(pos).transform.position; return(new Vector3(SquarePos.x, piece.Controller.transform.position.y, SquarePos.z)); }
public void TestIsValidFenFormat(string fen, bool expectedResult) { var actualResult = ChessHelper.IsValidFenFormat(fen); Assert.That(actualResult, Is.EqualTo(expectedResult)); }
public static void InitializePieces(bool isWhite) { Transform pieces; if (isWhite) { pieces = GameObject.Find("Pieces").transform.Find("White"); } else { pieces = GameObject.Find("Pieces").transform.Find("Black"); } int counter = 1; foreach (Transform Piece in pieces) { GameObject piece = Piece.gameObject; Piece pieceComponent = ChessHelper.GetCorrectPieceType(piece); pieceComponent.isWhite = isWhite; ChessTracker.PieceConnections.Add(piece, pieceComponent); if (isWhite) { ChessTracker.board.WhitePieces.Add(pieceComponent); } else { ChessTracker.board.BlackPieces.Add(pieceComponent); } if (piece.name.StartsWith("Pawn")) { int pawnIndex = Int32.Parse(piece.name.Split(new[] { "Pawn" }, StringSplitOptions.None)[1]); int yPosition; if (isWhite) { yPosition = 2; } else { yPosition = 7; } pieceComponent.position = new Vector2(pawnIndex + 1, yPosition); } else { int yPosition; if (isWhite) { yPosition = 1; } else { yPosition = 8; } pieceComponent.position = new Vector2(counter, yPosition); counter++; } } }