Пример #1
0
        // Both players use same strategies when choosing a cell to put their stones
        // and execute them in the following order
        // 1. Can I win in this move (make 5 in a row) (offensive move)
        // 2. Can opponent win in the next move (defensive move)
        // 3. Can I align 4 my stones in a row in this move (offensive)
        // 4. Can opponent align 4 of his stones in a row in the next move (defensive)
        // 5. Can I align 3 my stones in a row in this move (offensive)
        // 6. Can I align 2 my stones in a row in this move (offensive)
        // 7. I make a move on a random empty cell (neutral?)


        // If a player wants to make offensive move he checks possible moves with list of his own moves
        // If a player wants to make defensive move he checks possible moves with list of his opponent's moves
        // Check on each condition is performed step by step until a matching move is found
        // since operations are performed with relatively small list of integers
        // and deep nesting of loops and statements is avoided, players make moves very quickly

        public static void MakeMove()
        {
            if (MoveCount % 2 == 0) // Player 1 chooses next move
            {
                if ((CurrentMove = CheckForFiveInARow(Player1Moves)) == NoSuchMove &&
                    (CurrentMove = CheckForFiveInARow(Player2Moves)) == NoSuchMove &&
                    (CurrentMove = CheckForFourInARow(Player1Moves)) == NoSuchMove &&
                    (CurrentMove = CheckForFourInARow(Player2Moves)) == NoSuchMove &&
                    (CurrentMove = CheckForThreeInARow(Player1Moves)) == NoSuchMove &&
                    (CurrentMove = CheckForTwoInARow(Player1Moves)) == NoSuchMove)
                {
                    CurrentMove = GenerateRandomMove();
                }
                Board[CurrentMove / 100, CurrentMove % 100] = 'x';
                Player1Moves.Add(CurrentMove);
            }
            else // Player 2 chooses next move
            {
                if ((CurrentMove = CheckForFiveInARow(Player2Moves)) == NoSuchMove &&
                    (CurrentMove = CheckForFiveInARow(Player1Moves)) == NoSuchMove &&
                    (CurrentMove = CheckForFourInARow(Player2Moves)) == NoSuchMove &&
                    (CurrentMove = CheckForFourInARow(Player1Moves)) == NoSuchMove &&
                    (CurrentMove = CheckForThreeInARow(Player2Moves)) == NoSuchMove &&
                    (CurrentMove = CheckForTwoInARow(Player2Moves)) == NoSuchMove)
                {
                    CurrentMove = GenerateRandomMove();
                }
                Board[CurrentMove / 100, CurrentMove % 100] = 'o';
                Player2Moves.Add(CurrentMove);
            }

            PossibleMoves.Remove(CurrentMove);
        }
Пример #2
0
        public static void DisplayRoundResult(Move move1, Move move2, IPlayer p1, IPlayer p2)
        {
            Player1Moves.Add(move1);
            Player2Moves.Add(move2);

            int result = move1 - move2;

            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write(move1.ToString());
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(" vs ");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine(move2.ToString());
            Console.ForegroundColor = ConsoleColor.White;

            if (result == 0)
            {
                Console.WriteLine("Tie");
            }
            else if (result == 1 || result == -2)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine($"Player 1 ({p1.Name}) wins!");
                Console.ForegroundColor = ConsoleColor.White;

                p1.Score++;
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine($"Player 2 ({p2.Name}) wins!");
                Console.ForegroundColor = ConsoleColor.White;

                p2.Score++;
            }
        }