Ejemplo n.º 1
0
 public void AddTurn(Turn turn)
 {
     turns.Add(turn);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Detects open "2 in a rows"
        /// </summary>
        /// <param name="game"></param>
        /// <returns>Amount of times 2 in a row with open ends is on board for current player</returns>
        private int Detect2(Game game)
        {
            int  count = 0;
            Turn turn  = game.Turn;

            // Check horizontal
            for (int y = 0; y < Game.NROWS; y++)
            {
                // Detect 3 with open right
                for (int x = 0; x < Game.NCOLS - 2; x++)
                {
                    if (game.Board[y, x] != (int)turn)
                    {
                        continue;                                                       // Don't check opponent or empty squares
                    }
                    // Find 3 in a row
                    if (game.Board[y, x] == game.Board[y, x + 1] &&
                        game.Board[y, x + 2] == -1)                              // Find open right
                    {
                        count++;
                    }
                }

                // Detect 3 with open left
                for (int x = 1; x < Game.NCOLS - 1; x++)
                {
                    if (game.Board[y, x] != (int)turn)
                    {
                        continue;                                                       // Don't check opponent or empty squares
                    }
                    // Find 3 in a row
                    if (game.Board[y, x] == game.Board[y, x + 1] &&
                        game.Board[y, x - 1] == -1)                              // Find open left
                    {
                        count++;
                    }
                }
            }

            // Check vertical (open bottom cannot exist)
            for (int y = 1; y < Game.NROWS - 1; y++)
            {
                for (int x = 0; x < Game.NCOLS; x++)
                {
                    if (game.Board[y, x] != (int)turn)
                    {
                        continue;                                                    // Don't check opponent or empty squares
                    }
                    if (game.Board[y, x] == game.Board[y + 1, x] &&
                        game.Board[y - 1, x] == -1)                              // Open top
                    {
                        count++;
                    }
                }
            }

            // Check diag 1 [\]
            for (int y = 0; y < Game.NROWS - 2; y++)
            {
                for (int x = 0; x < Game.NCOLS - 2; x++)
                {
                    if (game.Board[y, x] != (int)turn)
                    {
                        continue;                                                    // Don't check opponent or empty squares
                    }
                    if (game.Board[y, x] == game.Board[y + 1, x + 1] &&
                        game.Board[y + 2, x + 2] == -1)
                    {
                        count++;
                    }
                }
            }
            for (int y = 1; y < Game.NROWS - 1; y++)
            {
                for (int x = 1; x < Game.NCOLS - 1; x++)
                {
                    if (game.Board[y, x] != (int)turn)
                    {
                        continue;                                                    // Don't check opponent or empty squares
                    }
                    if (game.Board[y, x] == game.Board[y + 1, x + 1] &&
                        game.Board[y - 1, x - 1] == -1)
                    {
                        count++;
                    }
                }
            }


            // Check diag 2 [/]
            for (int y = 2; y < Game.NROWS; y++)
            {
                for (int x = 0; x < Game.NCOLS - 2; x++)
                {
                    if (game.Board[y, x] != (int)turn)
                    {
                        continue;                                                    // Don't check opponent or empty squares
                    }
                    if (game.Board[y, x] == game.Board[y - 1, x + 1] &&
                        game.Board[y - 2, x + 2] == -1)
                    {
                        count++;
                    }
                }
            }
            for (int y = 1; y < Game.NROWS - 1; y++)
            {
                for (int x = 1; x < Game.NCOLS - 1; x++)
                {
                    if (game.Board[y, x] != (int)turn)
                    {
                        continue;                                                    // Don't check opponent or empty squares
                    }
                    if (game.Board[y, x] == game.Board[y - 1, x + 1] &&
                        game.Board[y + 1, x - 1] == -1)
                    {
                        count++;
                    }
                }
            }

            return(count);
        }