Example #1
0
 public void createGame()
 {
     if (seatsOpen == 0) {
         //then start a new game!
         curGame = new CheckersGame(black, red, this, getNewGameId());
     }
 }
        protected override int CalculateStrength(CheckersGame initGame, CheckersGame curGame)
        {
            int player = ((curGame.Turn != 1) ? (2) : (1));
            int opponent = ((curGame.Turn != 1) ? (1) : (2));
            int strength = 0;

            // Strength Heuristics
            // --------------------

            // Heuristic: Stronger for each player pawn and king the player still has on the board
            strength += 3 * curGame.GetPawnCount(player);
            strength += 10 * curGame.GetKingCount(player);

            // Heuristic: Stronger if opponent was jumped
            strength += 2 * ((initGame.GetPawnCount(opponent) - curGame.GetPawnCount(opponent)));
            strength += 5 * (initGame.GetKingCount(opponent) - curGame.GetKingCount(opponent));

            // Weakness Heuristics
            // --------------------

            // Heuristic: Weaker for each opponent pawn and king the player still has on the board
            strength -= 3 * curGame.GetPawnCount(opponent);
            strength -= 10 * curGame.GetKingCount(opponent);

            // Heuristic: Weaker if player was jumped
            strength -= 2 * ((initGame.GetPawnCount(player) - curGame.GetPawnCount(player)));
            strength -= 5 * (initGame.GetKingCount(player) - curGame.GetKingCount(player));

            // Return the difference of strengths
            return strength;
        }
Example #3
0
 public GameTable(int tableId)
 {
     gameHistroy = new Dictionary<int, CheckersGame>();
     observers = new Dictionary<string, GameClient>();
     tid = tableId;
     seatsOpen = 2;
     black = null;
     red = null;
     curGame = null;
 }
Example #4
0
 public void alertGameEnd()
 {
     /* check if there is a winner before saving this game.
      * If a client left the table, not the game never started, there is no
      * winner, cause the game never even started.
      */
     if(curGame.getWinner() > 0)
         gameHistroy.Add(curGame.getGameId(), curGame);
     curGame = null;
 }
Example #5
0
 public override CheckersMove NextMove(CheckersGame game)
 {
     CheckersPiece[] movable = game.EnumMovablePieces();
     CheckersMove move = game.BeginMove(movable[rand.Next(movable.Length)]);
     while(move.MustMove)
     {
         Point[] moves = move.EnumMoves();
         move.Move(moves[rand.Next(moves.Length)]);
     }
     return move;
 }
        protected override int CalculateStrength(CheckersGame initGame, CheckersGame curGame)
        {
            int player = ((curGame.Turn != 1) ? (2) : (1));
            int opponent = ((curGame.Turn != 1) ? (1) : (2));
            int strength = base.CalculateStrength(initGame, curGame);

            // Sum all player's piece heuristic values
            foreach(CheckersPiece piece in curGame.Pieces)
            {
                if(piece.Player == player)
                    strength += CalculatePieceStrength(piece);
                else
                    strength -= CalculatePieceStrength(piece);
            }

            return strength;
        }
Example #7
0
        public GameForm(int i_BoardSize, bool i_IsPlayer2Mode, string i_Player1Name, string i_Player2Name)
        {
            r_BoardSize            = i_BoardSize;
            r_CheckersLogic        = new CheckersGame(r_BoardSize);
            r_IsPlayer2Mode        = i_IsPlayer2Mode;
            r_Player1Name          = i_Player1Name;
            r_Player2Name          = i_Player2Name;
            r_CheckersBoard        = new List <BoardSquare>(r_BoardSize * r_BoardSize);
            this.BackColor         = Color.FromArgb(40, 79, 79);
            this.boardSquareActive = null;
            r_CheckersLogic.StartGame(r_Player1Name, r_Player2Name);
            createGame();

            r_Timer = new Timer();
            initializeTimer();

            InitializeComponent();
        }
        public void CannotjumpAgainIfNotAllowedAfterFirstJump()
        {
            // arrange
            CheckersGame target = new CheckersGame();

            int[,] board = target.GetBoard();
            board[6, 1]  = 0;
            board[7, 2]  = 0;
            // act
            target.Move(2, 1, 3, 2);
            target.Move(5, 0, 4, 1);
            var result = target.Move(3, 2, 5, 0);

            // assert
            Assert.IsTrue(result.ValidMove);
            Assert.AreEqual(target.GetBoard()[5, 0], 1);
            Assert.AreEqual(target.GetBoard()[4, 1], 0);
        }
Example #9
0
        protected virtual int doCalculateStrength(CheckersGame initGame, CheckersGame curGame)
        {
            int player   = ((curGame.Turn != 1) ? (2) : (1));
            int opponent = ((curGame.Turn != 1) ? (1) : (2));

            // Heuristic: Check for player won state
            if (curGame.Winner == player)
            {
                return(int.MaxValue);
            }

            // Heuristic: Check for player lost state
            if (curGame.Winner == opponent)
            {
                return(int.MinValue);
            }

            return(CalculateStrength(initGame, curGame));
        }
Example #10
0
 public override CheckersMove NextMove(CheckersGame game)
 {
     int maxJumps = 0;
     CheckersPiece[] movables = game.EnumMovablePieces();
     ArrayList possibleMoves = new ArrayList(movables.Length);
     foreach(CheckersPiece movable in movables)
         possibleMoves.Add(game.BeginMove(movable));
     // Get all possible jump combos
     ArrayList finishedMoves = new ArrayList();
     while(possibleMoves.Count > 0)
     {
         CheckersMove move = (CheckersMove)possibleMoves[0];
         possibleMoves.RemoveAt(0);
         Point[] points = move.EnumMoves();
         if(points.Length == 0)
         {
             // Move is complete; add to finished moves and test for current max jumps
             finishedMoves.Add(move);
             if(maxJumps < move.Jumped.Length)
                 maxJumps = move.Jumped.Length;
             continue;
         }
         // Enumerate all moves from this point and append them to the possible moves array
         foreach(Point p in points)
         {
             CheckersMove next = move.Clone();
             next.Move(p);
             possibleMoves.Add(next);
         }
     }
     // Get list of max jumps
     ArrayList moveList = new ArrayList();
     foreach(CheckersMove move in finishedMoves)
     {
         if(move.Jumped.Length != maxJumps)
             continue;
         moveList.Add(move);
     }
     // Choose at random between any path with same number of jumps
     if(moveList.Count == 0)
         return null;
     return (CheckersMove)moveList[rand.Next(moveList.Count)];
 }
Example #11
0
        protected override int CalculateStrength(CheckersGame initGame, CheckersGame curGame)
        {
            int player   = ((curGame.Turn != 1) ? (2) : (1));
            int opponent = ((curGame.Turn != 1) ? (1) : (2));
            int strength = base.CalculateStrength(initGame, curGame);

            // Sum all player's piece heuristic values
            foreach (CheckersPiece piece in curGame.Pieces)
            {
                if (piece.Player == player)
                {
                    strength += CalculatePieceStrength(piece);
                }
                else
                {
                    strength -= CalculatePieceStrength(piece);
                }
            }

            return(strength);
        }
Example #12
0
        //--------------------------------Private funcations------------------------------------

        /*
         * initialization is a method that initialize CheckersGameUI component.
         */
        private void initialization()
        {
            if (m_FormGameSettings.ShowDialog() == DialogResult.OK)
            {
                // Initialize CheckersGame
                byte   boardSize   = (byte)m_FormGameSettings.BoardSize;
                string player1Name = m_FormGameSettings.Player1Name;
                string player2Name = m_FormGameSettings.Player2Name;
                bool   againstAI   = !m_FormGameSettings.CheckBoxPlayer2Checked;
                m_CheckersGame = new CheckersGame(boardSize, player1Name, player2Name, againstAI);
                m_CheckersGame.InitializeGame();
                m_CheckersGame.CellChanged     += new CellChangeEventHandler(cell_Changed);
                m_CheckersGame.PieceBecameKing += new PieceBecameKingEventHandler(piece_BecameKing);
                m_CheckersGame.PieceEaten      += new PieceEatenEventHandler(piece_Eaten);

                // Initialize FormCheckersGame
                m_FormCheckersGame             = new FormCheckersGame(boardSize);
                m_FormCheckersGame.Player1Name = player1Name;
                m_FormCheckersGame.Player2Name = player2Name;
                m_FormCheckersGame.SignDelegateToAllSquareEvent(buttonSquare_Clicked);
            }
        }
Example #13
0
        public void test_scoreGame()
        {
            CheckersGame game = new CheckersGame();

            //Start with a blank board
            for (int i = 0; i < 32; i++)
            {
                game.squares[i].squareType = CheckerType.empty;
            }
            game.squares[4].squareType  = CheckerType.whiteKing;
            game.squares[0].squareType  = CheckerType.whiteChecker;
            game.squares[9].squareType  = CheckerType.whiteChecker;
            game.squares[14].squareType = CheckerType.whiteChecker;
            game.squares[10].squareType = CheckerType.redChecker;
            game.squares[29].squareType = CheckerType.redChecker;
            game.squares[17].squareType = CheckerType.redKing;
            game.squares[16].squareType = CheckerType.redKing;
            double score   = game.scoreGame(game, false);
            double epsilon = 0.001;

            Assert.IsTrue(epsilon > Math.Abs(score - 0.583333));
        }
Example #14
0
 public override CheckersMove NextMove(CheckersGame game)
 {
     const int minValue = int.MaxValue;
     int maxValue = int.MinValue;
     CheckersMove bestMove = null;
     // Enumerate all moves
     foreach(CheckersMove move in game.EnumLegalMoves())
     {
         if(!game.IsPlaying)
             break;
         CheckersGame nextGameState = game.Clone();
         nextGameState.MovePiece(move.Clone(nextGameState));
         int curValue = minMove(game, nextGameState, 1, maxValue, minValue);
         if((curValue > maxValue) || (bestMove == null))
         {
             maxValue = curValue;
             bestMove = move;
         }
         OnTick(game);
     }
     return bestMove;
 }
Example #15
0
        int maxMove(CheckersGame initGame, CheckersGame curGame, int depth, int alpha, int beta)
        {
            // Check algorithm limits..end prematurely, but with an educated approximation
            if (doCutOff(initGame, curGame, depth))
            {
                return(doCalculateStrength(initGame, curGame));
            }

            // Make move with all possibilities
            foreach (CheckersMove move in curGame.EnumLegalMoves())
            {
                // Create next move
                CheckersGame nextGameState = move.Game.Clone();
                CheckersMove nextMoveState = move.Clone(nextGameState);

                // Make next move and search move space
                if (!nextGameState.MovePiece(nextMoveState))
                {
                    continue;
                }
                int value = minMove(initGame, nextGameState, depth + 1, alpha, beta);

                if (value > alpha)
                {
                    // Get new max value
                    alpha = value;
                }

                if (alpha > beta)
                {
                    // Return max value with pruning
                    return(beta);
                }
            }
            // Return alpha (max value)
            return(alpha);
        }
Example #16
0
 /// <summary>
 /// Calculates current player's strength at current point in game.
 /// This gives us a reasonable approximation of a particular player's chances of winning.
 /// This heuristic given is used once the min-max algorithm has reached its max search depth.
 /// </summary>
 protected abstract int CalculateStrength(CheckersGame initGame, CheckersGame curGame);
Example #17
0
 /// <summary>
 /// Calculates the cut-off point in the game.
 /// </summary>
 protected virtual bool CutOff(CheckersGame initGame, CheckersGame curGame, int depth)
 {
     return false;
 }
Example #18
0
        public void test_initGame()
        {
            CheckersGame game = new CheckersGame();

            Assert.IsTrue(game.squares[0].squareType == CheckerType.whiteChecker);
        }
Example #19
0
        protected virtual int doCalculateStrength(CheckersGame initGame, CheckersGame curGame)
        {
            int player = ((curGame.Turn != 1) ? (2) : (1));
            int opponent = ((curGame.Turn != 1) ? (1) : (2));

            // Heuristic: Check for player won state
            if(curGame.Winner == player)
                return int.MaxValue;

            // Heuristic: Check for player lost state
            if(curGame.Winner == opponent)
                return int.MinValue;

            return CalculateStrength(initGame, curGame);
        }
Example #20
0
 /// <summary>
 /// Calculates the cut-off point in the game.
 /// </summary>
 protected virtual bool CutOff(CheckersGame initGame, CheckersGame curGame, int depth)
 {
     return(false);
 }
Example #21
0
 /// <summary>
 /// Calculates current player's strength at current point in game.
 /// This gives us a reasonable approximation of a particular player's chances of winning.
 /// This heuristic given is used once the min-max algorithm has reached its max search depth.
 /// </summary>
 protected abstract int CalculateStrength(CheckersGame initGame, CheckersGame curGame);
 public void OnGet()
 {
     CheckersGame = new CheckersGame();
 }
Example #23
0
 bool doCutOff(CheckersGame initGame, CheckersGame curGame, int depth)
 {
     OnTick(initGame);
     // Test the game-oriented cut-offs
     if((!curGame.IsPlaying) || (!initGame.IsPlaying))
         return true;
     // Test the depth cut-off
     int curSearchDepth = searchDepth;
     if(increasingSearchDepth)
     {
         int totalPieces = CheckersGame.PiecesPerPlayer * CheckersGame.PlayerCount;
         //int removed = (CheckersGame.PiecesPerPlayer*CheckersGame.PlayerCount) - curGame.GetRemainingCount();
         //int factor = (int)Math.Log(removed, 3);
         int factor = (int)Math.Log(curGame.GetRemainingCount(), 3), mfactor = (int)Math.Log(totalPieces, 3);
         curSearchDepth += (mfactor - factor);
     }
     if((depth >= 0) && (depth > curSearchDepth))
         return true;
     // Test the extended cut-off
     return CutOff(initGame, curGame, depth);
 }
Example #24
0
        int minMove(CheckersGame initGame, CheckersGame curGame, int depth, int alpha, int beta)
        {
            // Check algorithm limits..end prematurely, but with an educated approximation
            if(doCutOff(initGame, curGame, depth))
                return -doCalculateStrength(initGame, curGame);

            // Make move with all possibilities
            foreach(CheckersMove move in curGame.EnumLegalMoves())
            {
                // Create next move
                CheckersGame nextGameState = move.Game.Clone();
                CheckersMove nextMoveState = move.Clone(nextGameState);

                // Make next move and search move space
                if(!nextGameState.MovePiece(nextMoveState))
                    continue;
                int value = maxMove(initGame, nextGameState, depth + 1, alpha, beta);

                if(value < beta)
                {
                    // Get new min value
                    beta = value;
                }

                if(beta < alpha)
                {
                    // Return min value with pruning
                    return alpha;
                }
            }
            // Return alpha (max value)
            return beta;
        }
Example #25
0
    static void Main(string[] args)
    {
        CheckersGame game = new CheckersGame();

        game.Play();
    }
Example #26
0
        // $G$ SFN-999 (-5) Player name should not be empty

        public static void Main()
        {
            CheckersGame.Run();
        }