// detects whenever checker move was back which is not allowed in game
 private static Boolean IsCheckerMovedBack(CheckerPB checker, Point newCheckerPosition)
 {
     if (checker.checker.checkerType == Constants.blackChecker)
     {
         if ((newCheckerPosition.X - checker.checker.oldPositionOnBord.X) > 0)
         {
             return(false);
         }
         else
         {
             return(true);
         }
     }
     else
     {
         if ((newCheckerPosition.X - checker.checker.oldPositionOnBord.X) < 0)
         {
             return(false);
         }
         else
         {
             return(true);
         }
     }
 }
コード例 #2
0
        private void Animation(object Sender, EventArgs e)
        {
            GameForm     form1Ref = (GameForm)Application.OpenForms["GameForm"];
            DTO_MOVEMENT movement = restoredGame.movements[animationProgress];
            CheckerPB    checker  = GameController.checkers[movement.Checker_ID];

            form1Ref.animationHolder.initAnimation(new Point(movement.Col * form1Ref.getCellSize().X, (movement.Row * form1Ref.getCellSize().Y) + form1Ref.getCellSize().Y));
            form1Ref.animationHolder.startAnimation();

            GameController.OnFinishCheckerMove(ref checker, new Point(movement.Row, movement.Col));
            GameController.UpdateOpponentsVisabilityOnGameRestore(checker.checker.checkerType);
            animationProgress++;

            if (animationProgress == restoredGame.movements.Length)
            {
                StopAnimation();
                form1Ref.animationHolder.stopAnimation();

                // if (restoredGame.gameStatus == Constants.GAME_STATUS_END) {

                if (checker.checker.checkerType == Constants.whiteChecker)
                {
                    form1Ref.OnGameWon(restoredGame.players[0]);
                }
                else
                {
                    form1Ref.OnGameWon(restoredGame.players[1]);
                }
                //   }
            }
        }
        public static void UpdateCheckerPosition(CheckerPB ch, Point updatedPosition)
        {
            ch.checker.oldPositionOnBord = updatedPosition;
            ch.FixPosition(updatedPosition);

            GameForm form1Ref = (GameForm)Application.OpenForms["GameForm"];

            form1Ref.updateMainPanelScreenShoot(ch);
        }
        // This method generates game movement when user plays vs computer
        // The generation of movement is random but smart whenever random movement can eat user checker it dose it .
        public static void PlayAsComputer()
        {
            // check if there is option to eat
            foreach (KeyValuePair <string, CheckerPB> pair in checkers)
            {
                if (pair.Value.checker.checkerType == Constants.blackChecker)
                {
                    CheckerPB optionalCheckerForMove = pair.Value;
                    Point     moveToForEet           = isCheckerCanEat(optionalCheckerForMove, Constants.blackChecker);

                    if (!moveToForEet.Equals(new Point(-1, -1)))
                    {
                        OnFinishCheckerMove(ref optionalCheckerForMove, moveToForEet);
                        isThisPlayerTurn = true;
                        return;
                    }
                }
            }


            List <CheckerPB> optionalCheckersMoves = new List <CheckerPB>();
            List <Point>     optionalMoves         = new List <Point>();

            // find options for simple move
            foreach (KeyValuePair <string, CheckerPB> pair in checkers)
            {
                if (pair.Value.checker.checkerType == Constants.blackChecker)
                {
                    CheckerPB optionalCheckerForMove = pair.Value;

                    Point moveOption1 = new Point(optionalCheckerForMove.checker.oldPositionOnBord.X + 1, optionalCheckerForMove.checker.oldPositionOnBord.Y - 1);
                    Point moveOption2 = new Point(optionalCheckerForMove.checker.oldPositionOnBord.X + 1, optionalCheckerForMove.checker.oldPositionOnBord.Y + 1);

                    if (isCheckerCanMove(optionalCheckerForMove, moveOption1))
                    {
                        optionalCheckersMoves.Add(optionalCheckerForMove);
                        optionalMoves.Add(moveOption1);
                    }

                    if (isCheckerCanMove(optionalCheckerForMove, moveOption2))
                    {
                        optionalCheckersMoves.Add(optionalCheckerForMove);
                        optionalMoves.Add(moveOption2);
                    }
                }
            }

            //chose random move
            int    numberOfOptions   = optionalMoves.Count;
            Random rnd               = new Random();
            int    randomOptionIndex = rnd.Next(0, numberOfOptions - 1);

            CheckerPB checkerForMove = optionalCheckersMoves[randomOptionIndex];

            OnFinishCheckerMove(ref checkerForMove, optionalMoves[randomOptionIndex]);
            isThisPlayerTurn = true;
        }
        // detects whenever checker eaten other checker
        public static CheckerPB FindEetenChecker(CheckerPB checker, Point newCheckerPosition)
        {
            int       moveAngel     = CalculateMoveAngel(checker.checker.oldPositionOnBord, newCheckerPosition);
            CheckerPB eatenChecker  = null;
            Point     moveDirection = new Point(Math.Sign((float)(Math.Cos(ToRadians(moveAngel)))), Math.Sign((float)Math.Sin(ToRadians(moveAngel))));

            Point possiblePosition;

            if (checker.checker.checkerType == Constants.blackChecker)
            {
                if (moveDirection.X > 0)
                {
                    possiblePosition = new Point((newCheckerPosition.X - 1), (newCheckerPosition.Y - 1));
                }
                else
                {
                    possiblePosition = new Point((newCheckerPosition.X - 1), (newCheckerPosition.Y + 1));
                }

                foreach (KeyValuePair <string, CheckerPB> pair in checkers)
                {
                    if ((pair.Value.checker.oldPositionOnBord == possiblePosition) && (pair.Value.checker.checkerType != checker.checker.checkerType))
                    {
                        CheckerPB ch = pair.Value;
                        return(ch);
                    }
                }
            }
            else
            {
                if (moveDirection.X > 0)
                {
                    possiblePosition = new Point((newCheckerPosition.X + 1), (newCheckerPosition.Y - 1));
                }
                else
                {
                    possiblePosition = new Point((newCheckerPosition.X + 1), (newCheckerPosition.Y + 1));
                }

                foreach (KeyValuePair <string, CheckerPB> pair in checkers)
                {
                    if ((pair.Value.checker.oldPositionOnBord == possiblePosition) && (pair.Value.checker.checkerType != checker.checker.checkerType))
                    {
                        CheckerPB   ch = pair.Value;
                        DTO_Checker c  = ch.checker;
                        return(ch);
                    }
                }
            }

            return(eatenChecker);
        }
        private static bool isCheckerCanMove(CheckerPB checkerPB, Point newCheckerPosition)
        {
            int     moveAngel          = CalculateMoveAngel(checkerPB.checker.oldPositionOnBord, newCheckerPosition);
            Boolean isCheckerMovedBack = IsCheckerMovedBack(checkerPB, newCheckerPosition);
            Boolean isCellEmpty        = IsCellEmpty(checkerPB, newCheckerPosition);
            int     jumpSize           = Math.Abs(newCheckerPosition.Y - checkerPB.checker.oldPositionOnBord.Y);

            //critical conditions for not moving
            if (!isCellEmpty || isCheckerMovedBack || (Math.Abs(moveAngel) != 135 && Math.Abs(moveAngel) != 45) || jumpSize >= Constants.maxJumpSize)
            {
                return(false);
            }


            return(true);
        }
        private static Boolean IsCellEmpty(CheckerPB checker, Point newCheckerPosition)
        {
            // if the cell for check is out of board bounds , do not check
            // act like !IsCellEmpty ,we wont jump out of board bounds

            if (newCheckerPosition.Y == -1 || newCheckerPosition.Y == Constants.NumberOfGameBoardCols || newCheckerPosition.X == -1 || newCheckerPosition.X == Constants.NumberOfGameBoardRows)
            {
                return(false);
            }

            foreach (KeyValuePair <string, CheckerPB> pair in checkers)
            {
                if (pair.Value.checker.oldPositionOnBord.Equals(newCheckerPosition))
                {
                    return(false);
                }
            }

            return(true);
        }
        // This function is called each mouse up event when checker is selected
        // Used to deside and detect game moves such as game over(win/lose)/checker normal moves etc .
        public static bool OnFinishCheckerMove(ref CheckerPB checkerPB, Point newCheckerPosition)
        {
            // this special case when playing vs computer and player won pc , we dont take last computer move
            if (gameStatus == Constants.GAME_STATUS_END && gameType == Constants.GAME_TYPE_GAME_VS_COMPUTER)
            {
                return(false);
            }

            bool isCheckerPositionChanged = true;

            int jumpSize = Math.Abs(newCheckerPosition.Y - checkerPB.checker.oldPositionOnBord.Y);

            if (!isCheckerCanMove(checkerPB, newCheckerPosition))
            {
                checkerPB.moveToOldPosition();
                isCheckerPositionChanged = false;
            }
            else if (jumpSize == Constants.eatOtherCheckerJumpSize)
            {
                CheckerPB eatenChecker = FindEetenChecker(checkerPB, newCheckerPosition);
                if (eatenChecker == null || eatenChecker.checker.checkerType == checkerPB.checker.checkerType)
                {
                    checkerPB.moveToOldPosition();
                    isCheckerPositionChanged = false;
                }
                else
                {
                    //EATE CHECKER
                    RemoveEatenChecker(eatenChecker.checker.ID);
                    UpdateCheckerPosition(checkerPB, newCheckerPosition);
                    DataAcsess.UpdateServerWithCheckerMove(checkerPB.checker, eatenChecker.checker.ID);
                    updateThisClientOppenentVisibility();
                    if (isThisPlayerTurn)
                    {
                        numberOfeatenCheckers++;
                    }
                    else
                    {
                        numberOfeatenCheckersByOpponent++;
                    }
                }
            }
            else
            {
                UpdateCheckerPosition(checkerPB, newCheckerPosition);
                DataAcsess.UpdateServerWithCheckerMove(checkerPB.checker, null);
                updateThisClientOppenentVisibility();
            }

            if (gameType != Constants.GAME_TYPE_GAME_RESTORE && IsWonGame(newCheckerPosition))
            {
                gameStatus = Constants.GAME_STATUS_END;
                GameForm gameForm = (GameForm)Application.OpenForms["GameForm"];

                if (gameType == Constants.GAME_TYPE_PLAY_ONLINE)
                {
                    gameForm.OnGameWon(loggedInAccountPlayerPlaying);
                }
                else
                {
                    if (isThisPlayerTurn)
                    {
                        gameForm.OnGameWon(new DTO_Player {
                            TEAM_NAME = loggedInAccount.NAME
                        });
                    }
                    else
                    {
                        gameForm.OnGameWon(new DTO_Player {
                            TEAM_NAME = Constants.COMPUTER_TEAM_NAME
                        });
                    }
                }

                DataAcsess.UpdateServerPlayerWon(loggedInAccountPlayerPlaying);
            }

            if ((gameType == Constants.GAME_TYPE_PLAY_ONLINE || gameType == Constants.GAME_TYPE_GAME_VS_COMPUTER))
            {
                DataAcsess.AddGameMovement(currentPlayingGameID, new DTO_MOVEMENT {
                    Checker_ID = checkerPB.checker.ID, Row = newCheckerPosition.X, Col = newCheckerPosition.Y
                });
            }

            return(isCheckerPositionChanged);
        }
        // for current player checks if there is option to eat opponent checker
        // if there is, returns the position player must move else returns -1,-1
        public static Point isCheckerCanEat(CheckerPB optionalCheckerForMove, Nullable <int> playerType)
        {
            Point currentCheckerPos = optionalCheckerForMove.checker.oldPositionOnBord;

            if (playerType == Constants.whiteChecker)
            {
                // for white checker check 2 directions
                Point positionForCheck1 = new Point(currentCheckerPos.X - 2, currentCheckerPos.Y - 2);
                Point positionForCheck2 = new Point(currentCheckerPos.X - 2, currentCheckerPos.Y + 2);

                CheckerPB eatenCheckerOp1 = null;
                CheckerPB eatenCheckerOp2 = null;

                if (IsCellEmpty(optionalCheckerForMove, positionForCheck1))
                {
                    eatenCheckerOp1 = FindEetenChecker(optionalCheckerForMove, positionForCheck1);
                }

                if (eatenCheckerOp1 == null && IsCellEmpty(optionalCheckerForMove, positionForCheck2))
                {
                    eatenCheckerOp2 = FindEetenChecker(optionalCheckerForMove, positionForCheck2);
                }

                if (eatenCheckerOp1 != null)
                {
                    return(new Point(currentCheckerPos.X - 2, currentCheckerPos.Y - 2));
                }
                else if (eatenCheckerOp2 != null)
                {
                    return(new Point(currentCheckerPos.X - 2, currentCheckerPos.Y + 2));
                }
            }
            else
            {
                Point positionForCheck1 = new Point(currentCheckerPos.X + 2, currentCheckerPos.Y - 2);
                Point positionForCheck2 = new Point(currentCheckerPos.X + 2, currentCheckerPos.Y + 2);

                CheckerPB eatenCheckerOp1 = null;
                CheckerPB eatenCheckerOp2 = null;

                if (IsCellEmpty(optionalCheckerForMove, positionForCheck1))
                {
                    eatenCheckerOp1 = FindEetenChecker(optionalCheckerForMove, positionForCheck1);
                }

                if (eatenCheckerOp1 == null && IsCellEmpty(optionalCheckerForMove, positionForCheck2))
                {
                    eatenCheckerOp2 = FindEetenChecker(optionalCheckerForMove, positionForCheck2);
                }

                if (eatenCheckerOp1 != null)
                {
                    return(new Point(currentCheckerPos.X + 2, currentCheckerPos.Y - 2));
                }
                else if (eatenCheckerOp2 != null)
                {
                    return(new Point(currentCheckerPos.X + 2, currentCheckerPos.Y + 2));
                }
            }

            return(new Point(-1, -1));
        }