Esempio n. 1
0
        //Get the average score for current player rolling all possible combinations
        private double PropabilityScore(Player.Color myColor)
        {
            var allDiceRoll = AllRolls();
            var scores      = new List <double>();

            foreach (var roll in allDiceRoll)
            {
                Game.FakeRoll(roll.dice1, roll.dice2);
                var bestScore = double.MinValue;
                var seqs      = GenerateMovesSequence();
                foreach (var s in seqs)
                {
                    var hits  = DoSequence(s);
                    var score = EvaluatePoints(myColor) + EvaluateCheckers(myColor);
                    if (score > bestScore)
                    {
                        bestScore = score;
                    }
                    UndoSequence(s, hits);
                }
                int m = roll.dice1 == roll.dice2 ? 1 : 2; // dice roll with not same value on dices are twice as probable. 2 / 36, vs 1 / 36
                if (seqs.Any())
                {
                    scores.Add(bestScore * m);
                }
                // Get best score of each roll, and make an average.
                // some rolls are more probable, multiply them
                // some rolls will be blocked or partially blocked
            }
            if (!scores.Any())
            {
                return(-100000);
            }
            return(scores.Average());
        }
Esempio n. 2
0
        public Disk(int row, int col, Player.Color color)
        {
            Pair pair = new Pair(row, col);

            this.pos   = pair;
            this.color = color;
        }
Esempio n. 3
0
        public bool isValid(Pair to, Pair from, PegBoard pegBoard, Player.Color currentPlayerColor)
        {
            int moveUpDown    = from.row - to.row;
            int moveLeftRight = from.col - to.col;

            if (pegBoard.checkColor(from) != currentPlayerColor || // correct color
                pegBoard.isEmpty(from) ||  // selected empty peg slot
                !pegBoard.isEmpty(to))     // dropped on occupied slot
            {
                // conditions for immediate failure
                return(false);
            }

            else if (Math.Abs(moveUpDown) == 1 ||
                     Math.Abs(moveLeftRight) == 1)
            {
                // jump OR move 1
                return(true);
            }

            else if (Math.Abs(moveUpDown) == 2
                     ^ Math.Abs(moveLeftRight) == 2) // xor jump (2)
            {
                // Jumps A color and Jumps OTHER color
                return(!pegBoard.isEmpty(from.row - moveUpDown / 2, from.col - moveLeftRight / 2) &&
                       pegBoard.checkColor(from.row - moveUpDown / 2, from.col - moveLeftRight / 2) != currentPlayerColor);
            }

            else
            {
                // Don't jump the rainbow!
                return(false);
            }
        }
Esempio n. 4
0
    public void ChangeColor(Player.Color color)
    {
        switch (color)
        {
        case Player.Color.White:
            colorMeshes[0].SetActive(true);
            break;

        case Player.Color.Blue:
            colorMeshes[1].SetActive(true);
            AudioSource wind = GetComponent <AudioSource>();
            wind.Play();
            //AudioClip.PlayOneShot (wind, 0.0F);
            break;

        case Player.Color.Red:
            colorMeshes[2].SetActive(true);
            break;

        case Player.Color.Yellow:
            colorMeshes[3].SetActive(true);
            break;

        case Player.Color.Green:
            colorMeshes[4].SetActive(true);
            break;
        }
    }
Esempio n. 5
0
    public void TurnOffMesh(Player.Color color)
    {
        switch (color)
        {
        case Player.Color.White:
            colorMeshes[0].SetActive(false);
            break;

        case Player.Color.Blue:
            colorMeshes[1].SetActive(false);
            break;

        case Player.Color.Red:
            colorMeshes[2].SetActive(false);
            break;

        case Player.Color.Yellow:
            colorMeshes[3].SetActive(false);
            break;

        case Player.Color.Green:
            colorMeshes[4].SetActive(false);
            break;
        }
    }
Esempio n. 6
0
 public void AddCheckers(int count, Player.Color color, int point)
 {
     for (int i = 0; i < count; i++)
     {
         Points.Single(p => p.GetNumber(color) == point).Checkers.Add(new Checker {
             Color = color
         });
     }
 }
Esempio n. 7
0
        internal async Task ConnectAndListen(WebSocket webSocket, Player.Color color, Db.User dbUser, bool playAi)
        {
            if (color == Player.Color.Black)
            {
                Client1                = webSocket;
                Game.BlackPlayer.Id    = dbUser != null ? dbUser.Id : Guid.Empty;
                Game.BlackPlayer.Name  = dbUser != null ? dbUser.Name : "Guest";
                Game.BlackPlayer.Photo = dbUser != null && dbUser.ShowPhoto ? dbUser.PhotoUrl : "";
                Game.BlackPlayer.Elo   = dbUser != null ? dbUser.Elo : 0;
                if (Game.IsGoldGame)
                {
                    Game.BlackPlayer.Gold = dbUser != null ? dbUser.Gold - firstBet : 0;
                    Game.Stake            = firstBet * 2;
                }

                if (playAi)
                {
                    var aiUser = Db.BgDbContext.GetDbUser(Db.User.AiUser);
                    Game.WhitePlayer.Id   = aiUser.Id;
                    Game.WhitePlayer.Name = aiUser.Name;
                    // TODO: AI image
                    Game.WhitePlayer.Photo = aiUser.PhotoUrl;
                    Game.WhitePlayer.Elo   = aiUser.Elo;
                    if (Game.IsGoldGame)
                    {
                        Game.WhitePlayer.Gold = aiUser.Gold;
                    }
                    Engine = new Ai.Engine(Game);
                    CreateDbGame();
                    StartGame();
                    if (Game.CurrentPlayer == Player.Color.White)
                    {
                        await EnginMoves(Client1);
                    }
                }
                await ListenOn(webSocket);
            }
            else
            {
                if (playAi)
                {
                    throw new ApplicationException("Ai always plays as white. This is not expected");
                }
                Client2                = webSocket;
                Game.WhitePlayer.Id    = dbUser != null ? dbUser.Id : Guid.Empty;
                Game.WhitePlayer.Name  = dbUser != null ? dbUser.Name : "Guest";
                Game.WhitePlayer.Photo = dbUser != null && dbUser.ShowPhoto ? dbUser.PhotoUrl : "";
                Game.WhitePlayer.Elo   = dbUser != null ? dbUser.Elo : 0;
                if (Game.IsGoldGame)
                {
                    Game.WhitePlayer.Gold = dbUser != null ? dbUser.Gold - firstBet : 0;
                }
                CreateDbGame();
                StartGame();
                await ListenOn(webSocket);
            }
        }
Esempio n. 8
0
 /// <summary>Sets the piece up.</summary>
 /// <param name="type">The piece's type.</param>
 /// <param name="color">The piece's color.</param>
 /// <param name="x">The x-value.</param>
 /// <param name="y">The y-value.</param>
 /// <param name="automaticallyUpdateTransform">Whether the transform's position should be updated. Defaults to true.</param>
 public void SetUp(BoardPiece.Type type, Player.Color color, int x, int y, bool automaticallyUpdateTransform = true)
 {
     //set the type, color, (x, y) and name
     this.type  = type;
     this.color = color;
     SetXY(x, y, automaticallyUpdateTransform);
     name = string.Format("{0} {1}", type.ToString(), color.ToString());
     //update the sprite
     UpdateSprite();
 }
Esempio n. 9
0
 private static double EvaluatePoints(Player.Color myColor, Game game)
 {
     if (myColor == Player.Color.White) // Higher score for white when few checkers and black has many checkers left
     {
         return(game.BlackPlayer.PointsLeft - game.WhitePlayer.PointsLeft);
     }
     else
     {
         return(game.WhitePlayer.PointsLeft - game.BlackPlayer.PointsLeft);
     }
 }
Esempio n. 10
0
 private Player.Color oppositeColor(Player.Color color)
 {
     if (color == Player.Color.Black)
     {
         return(Player.Color.White);
     }
     else
     {
         return(Player.Color.Black);
     }
 }
Esempio n. 11
0
 private double EvaluatePoints(Player.Color myColor)
 {
     if (myColor == Player.Color.Black)
     {
         return(Game.BlackPlayer.PointsLeft - Game.WhitePlayer.PointsLeft);
     }
     else
     {
         return(Game.WhitePlayer.PointsLeft - Game.BlackPlayer.PointsLeft);
     }
 }
Esempio n. 12
0
 public int myPegCount(Player.Color color)
 {
     if (color == Player.Color.White)
     {
         return(_whitePegCount);
     }
     else
     {
         return(_blackPegCount);
     }
 }
Esempio n. 13
0
 private void incDiskCount(Player.Color color)
 {
     // add new disk
     if (color == Player.Color.Black)
     {
         _blackDiskCount++;
     }
     else
     {
         _whiteDiskCount++;
     }
 }
Esempio n. 14
0
 public MoveHeuristic(Player.Color Me)
 {
     if (Me.Equals(Player.Color.Red))
     {
         MyStatus       = Cell.CellStatus.OccupiedRed;
         OpponentStatus = Cell.CellStatus.OccupiedBlack;
     }
     else
     {
         MyStatus       = Cell.CellStatus.OccupiedBlack;
         OpponentStatus = Cell.CellStatus.OccupiedRed;
     }
 }
Esempio n. 15
0
        public bool sameColorDiskAt(int row, int col, Player.Color currentColor)
        {
            bool sameColor = false;

            if (this.board[row, col] != null)
            {
                if (this.board[row, col].color == currentColor)
                {
                    sameColor = true;
                }
            }

            return(sameColor);
        }
Esempio n. 16
0
 private void adjDiskCount(Player.Color color)
 {
     // flip opposite disk
     if (color == Player.Color.Black)
     {
         _blackDiskCount++;
         _whiteDiskCount--;
     }
     else
     {
         _whiteDiskCount++;
         _blackDiskCount--;
     }
 }
Esempio n. 17
0
        public int movesFromWin(Player.Color color)
        {
            int count;

            if (color == Player.Color.White)
            {
                count = movesFromWhiteWin(0, 0);
            }
            else
            {
                count = movesFromBlackWin(5, 0);
            }

            return(count);
        }
Esempio n. 18
0
        /// <summary>
        /// Private Constructor used to initialize child node
        /// </summary>
        /// <param name="currentColor"></param> ---> will switch color
        /// <param name="pegBoard"></param>
        /// <param name="diskBoard"></param>
        /// <param name="minimaxValue"></param> ---> will switch min/max
        /// <param name="to"></param>
        /// <param name="from"></param>
        /// <param name="levelInTree"></param>
        /// <param name="aiColor"></param>
        private TreeNode(Player.Color currentColor, PegBoard pegBoard, DiskBoard diskBoard, MiniMax minimaxValue, Pair to, Pair from, int levelInTree, Player.Color aiColor, int movesFromWin, int maxTreeDepth, double middlePegHeuristic)
        {
            this._pegBoard           = pegBoard;
            this._diskBoard          = diskBoard;
            this._moveMade           = new PieceMove();
            this._moveMade.to        = to;
            this._moveMade.from      = from;
            this._levelInTree        = levelInTree;
            this._aiColor            = aiColor;
            this._movesFromWin       = movesFromWin - 1;
            this._maxTreeDepth       = maxTreeDepth;
            this._middlePegHeuristic = middlePegHeuristic;

            if (maxTreeDepth == 1)
            {
                this._difficultyLevel = Difficulty.Easy;
            }
            else if (maxTreeDepth == 3)
            {
                this._difficultyLevel = Difficulty.Medium;
            }
            else
            {
                this._difficultyLevel = Difficulty.Hard;
            }

            // Switch colors
            if (currentColor == Player.Color.White)
            {
                this._currentColor = Player.Color.Black;
            }
            else
            {
                this._currentColor = Player.Color.White;
            }

            // Switch minimaxValue
            if (minimaxValue == MiniMax.Max)
            {
                this._minimaxValue = MiniMax.Min;
            }
            else
            {
                this._minimaxValue = MiniMax.Max;
            }
        }
Esempio n. 19
0
        private bool isValid(Pair to, Pair from, PegBoard pegBoard, Player.Color currentPlayerColor)
        {
            int moveUpDown    = from.row - to.row;
            int moveLeftRight = from.col - to.col;

            if (to.row < 0 || to.col < 0 || from.row < 0 || from.col < 0 ||
                to.row > boardSize || to.col > boardSize || from.row > boardSize || from.col > boardSize)
            {
                return(false);
            }

            else if (pegBoard.checkColor(from) != currentPlayerColor || // correct color
                     pegBoard.isEmpty(from) || // selected empty peg slot
                     !pegBoard.isEmpty(to))    // dropped on occupied slot
            {
                // Conditions for immediate failure
                return(false);
            }

            else if (Math.Abs(moveUpDown) == 1 &&
                     Math.Abs(moveLeftRight) == 1)
            {
                // Jump
                return(true);
            }

            else if (Math.Abs(moveUpDown) + Math.Abs(moveLeftRight) == 1)
            {
                // Move peg over 1
                return(true);
            }

            else if (Math.Abs(moveUpDown) + Math.Abs(moveLeftRight) == 2)
            {
                // jump (2,0) or (0,2) --> the (1,1) case has already been tested
                return(!pegBoard.isEmpty(from.row - moveUpDown / 2, from.col - moveLeftRight / 2) &&
                       pegBoard.checkColor(from.row - moveUpDown / 2, from.col - moveLeftRight / 2) != currentPlayerColor);
                // Jumps A color and Jumps OTHER color
            }

            else
            {
                // Don't jump the rainbow!
                return(false);
            }
        }
Esempio n. 20
0
 private void HandleColorSwap()
 {
     Player.Color playerColor = player.currentColor;
     cc.TurnOffMesh(playerColor);
     cc.ChangeColor(fairy.GetColor());
     player.currentColor = fairy.GetColor();
     fairycc.TurnOffMesh(fairy.GetColor());
     fairycc.ChangeColor(playerColor);
     fairy.SetColor(playerColor);
     if (player.currentColor == Player.Color.Blue)
     {
         player.TurnDoubleJumpOn();
     }
     else
     {
         player.TurnDoubleJumpOff();
     }
 }
Esempio n. 21
0
        public void movePeg(Player.Color color, Pair to, Pair from)
        {
            if (Math.Abs(to.row - from.row) == 2
                ^ Math.Abs(to.col - from.col) == 2)
            {
                // Capture then move
                capturePeg(color, to, from);
            }

            foreach (Peg peg in this.pegList)
            {
                if (peg.isLocated(from))
                {
                    this.pegList.Remove(peg);
                    this.pegList.Add(new Peg(to, peg.color));
                    return;
                }
            }
        }
Esempio n. 22
0
        private int ApplyHeuristic(Player.Color aiColor)
        {
            int heuristicValue = 0;

            if (this._difficultyLevel == Difficulty.Easy)
            {
                heuristicValue = EasyHeuristic(aiColor);
            }
            else if (this._difficultyLevel == Difficulty.Medium)
            {
                heuristicValue = MediumHeuristic(aiColor);
            }
            else
            {
                heuristicValue = HardHeuristic(aiColor);
            }

            this._heuristicValue = heuristicValue;

            return(heuristicValue);
        }
Esempio n. 23
0
        private double EvaluateCheckers(Player.Color myColor)
        {
            double score   = 0;
            var    inBlock = false;
            var    counter = 0;

            for (int i = 1; i < 25; i++)
            {
                var point = Game.Points[i];
                if (point.MyBlock(myColor))
                {
                    if (inBlock)
                    {
                        counter++;
                    }
                    else
                    {
                        counter = 1;
                    }
                    inBlock = true;
                }
                else
                {
                    if (inBlock)
                    {
                        score  += Math.Pow(counter, 2);
                        counter = 0;
                    }
                    inBlock = false;
                }
            }

            if (inBlock)
            {
                score += Math.Pow(counter, 2);
            }

            score += Game.GetHome(myColor).Checkers.Count * 10;
            return(score);
        }
Esempio n. 24
0
        /// <summary>
        /// Public Constructor
        /// </summary>
        /// <param name="player"></param>
        /// <param name="pegBoard"></param>
        /// <param name="diskBoard"></param>
        /// <param name="minimaxValue"></param>
        public TreeNode(Player player, PegBoard pegBoard, DiskBoard diskBoard, MiniMax minimaxValue, int treeDepth)
        {
            this._minimaxValue = minimaxValue;
            this._pegBoard     = pegBoard;
            this._currentColor = this._aiColor = player.color;
            this._diskBoard    = diskBoard;
            this._movesFromWin = diskBoard.movesFromWin();
            this._maxTreeDepth = treeDepth;

            if (treeDepth == 1)
            {
                this._difficultyLevel = Difficulty.Easy;
            }
            else if (treeDepth == 3)
            {
                this._difficultyLevel = Difficulty.Medium;
            }
            else
            {
                this._difficultyLevel = Difficulty.Hard;
            }
        }
Esempio n. 25
0
        public void capturePeg(Player.Color color, Pair to, Pair from)
        {
            // Find the peg
            foreach (Peg peg in this.pegList)
            {
                // Remove jumped peg
                if (peg.isLocated((from.row + to.row) / 2, (from.col + to.col) / 2))
                {
                    this.pegList.Remove(peg);

                    if (color == Player.Color.White)
                    {
                        this._blackPegCount--;
                    }
                    else
                    {
                        this._whitePegCount--;
                    }

                    return;
                }
            }
        }
Esempio n. 26
0
        public void placeDisk(Player.Color color, Pair to, Pair from)
        {
            int row = (from.row + to.row) / 2;
            int col = (from.col + to.col) / 2;

            // place onto empty
            if (this.board[row, col] == null)
            {
                this.board[row, col] = new Disk(row, col, color);
                incDiskCount(color);
            }

            else if ((row == 0 && col == 0) || (row == 5 && col == 0) ||
                     (row == 0 && col == 5) || (row == 5 && col == 5))
            {
            }

            // flip over occupied
            else if (this.board[row, col].color != color)
            {
                this.board[row, col] = new Disk(row, col, color);
                adjDiskCount(color);
            }
        }
Esempio n. 27
0
 internal async Task ConnectAndListen(WebSocket webSocket, Player.Color color, Db.User dbUser, bool playAi)
 {
     if (color == Player.Color.Black)
     {
         Client1               = webSocket;
         Game.BlackPlayer.Id   = dbUser != null ? dbUser.Id : Guid.Empty;
         Game.BlackPlayer.Name = dbUser != null ? dbUser.Name : "Guest";
         if (playAi)
         {
             var aiUser = Db.BgDbContext.GetDbUser(Db.User.AiUser);
             Game.WhitePlayer.Id   = aiUser.Id;
             Game.WhitePlayer.Name = aiUser.Name;
             Engine = new Ai.Engine(Game);
             CreateDbGame();
             StartGame();
             if (Game.CurrentPlayer == Player.Color.White)
             {
                 await EnginMoves(Client1);
             }
         }
         await ListenOn(webSocket);
     }
     else
     {
         if (playAi)
         {
             throw new ApplicationException("Ai always playes as white. This is not expected");
         }
         Client2               = webSocket;
         Game.WhitePlayer.Id   = dbUser != null ? dbUser.Id : Guid.Empty;
         Game.WhitePlayer.Name = dbUser != null ? dbUser.Name : "Guest";
         CreateDbGame();
         StartGame();
         await ListenOn(webSocket);
     }
 }
Esempio n. 28
0
 public void Fill(Player.Color color)
 {
     if (!filled && OnStoneFilled != null)
     {
         if (color == Player.Color.Red)
         {
             sr.sprite = redFilled;
         }
         else if (color == Player.Color.Blue)
         {
             sr.sprite = blueFilled;
         }
         else if (color == Player.Color.Green)
         {
             sr.sprite = greenFilled;
         }
         else
         {
             sr.sprite = yellowFilled;
         }
         filled = true;
         OnStoneFilled();
     }
 }
Esempio n. 29
0
 // Start is called before the first frame update
 void Start()
 {
     currColor = Player.Color.White;
 }
Esempio n. 30
0
 public void SetColor(Player.Color color)
 {
     currColor = color;
 }