Ejemplo n.º 1
0
        static Task Task4()
        {
            Console.Clear();
            var   numberOfColumns = 9;
            var   numberOfRows    = 9;
            Board board           = new Board(numberOfRows, numberOfColumns);

            Player p1 = new Player("Clive", 'O');
            Player p2 = new Player("Sharon", 'X');

            Random rnd = new Random();
            int    i   = 0;

            var winnerProvider = new WinnerProvider(board);

            while (true)
            {
                while (!board.IsFull())
                {
                    i++;
                    var randomColumn = rnd.Next(0, numberOfColumns);

                    var position = board.Add(i % 2 == 0 ? new Counter(p1) : new Counter(p2), randomColumn);
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(25));
                    }
                    Console.WriteLine(string.Empty);
                    Console.Clear();
                    board.Display();
                    var winner = winnerProvider.Provide();
                    if (winner.IsWinner)
                    {
                        Console.WriteLine($"We have a winner!!! Congrats {winner.Player.Name}, '{winner.Player.Counter}' !!!");
                        break;
                    }
                }

                Console.WriteLine("Press enter to close...");
                Console.ReadLine();
                board.Clear();
            }
            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        static void TestTask3()
        {
            Console.WriteLine("task 3d");
            Board  board = new Board(6, 7);
            Player p1    = new Player("Clive", 'o');
            Player p2    = new Player("Sharon", 'x');

            board.Add(new Counter(p2), 6);
            board.Add(new Counter(p1), 3);
            board.Add(new Counter(p2), 4);
            board.Add(new Counter(p1), 4);
            board.Add(new Counter(p2), 5);
            board.Add(new Counter(p1), 5);
            board.Add(new Counter(p2), 6);
            board.Add(new Counter(p1), 5);
            board.Add(new Counter(p2), 6);
            board.Add(new Counter(p1), 6);

            board.Display();
            Console.WriteLine(board.IsFull());
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            bool       turn = false;
            int        column;
            TokenState state;

            do
            {
                turn = turn ? false : true;
                Console.Clear();
                Board.Draw();

                Console.WriteLine("Player " + (turn ? 1 : 2));

                do
                {
                    column = Prompt.GetMove(1, 7);
                    state  = turn ? TokenState.X : TokenState.O;
                } while (!Board.DropToken(state, column) && !Board.IsFull());
            } while (!Board.IsWin() && !Board.IsFull());

            Console.Clear();
            Board.Draw();

            if (Board.IsFull())
            {
                Console.WriteLine("Tie!");
            }
            else
            {
                Console.WriteLine("Player " + (turn ? 1 : 2) + " wins!");
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
Ejemplo n.º 4
0
        public int SwitchPosition()
        {
            while (!WinChecker.HasWon(TokenType.Red) && !WinChecker.HasWon(TokenType.Black) && !Board.IsFull())
            {
                Console.Clear();
                Renderer.DrawHeader(_currentColumn, _currentType);
                Renderer.DrawBoard();
                var key = Console.ReadKey();
                if (key.Key != ConsoleKey.Enter)
                {
                    _currentColumn = key switch
                    {
                        { KeyChar : var k } when             k >= '1' && k <= '7' => k - '0' - 1,
                        { Key : ConsoleKey.LeftArrow } when  _currentColumn > 0 => _currentColumn - 1,
                        { Key : ConsoleKey.RightArrow } when _currentColumn < 6 => _currentColumn + 1,
                        var _ => _currentColumn,
                    };
                }
                else
                {
                    if (Board.CanAdd(_currentColumn))
                    {
                        Board.Add(_currentColumn, _currentType);
                        _currentType = _currentType == TokenType.Red ? TokenType.Black : TokenType.Red;
                    }
                }
            }

            return(_currentColumn);
        }