Exemple #1
0
        static void Main(string[] args)
        {
            var boardModel = new BoardModel();

            BoardView.ShowBoard(boardModel);

            foreach (var c in boardModel.BoxContents)
            {
                Console.WriteLine(c);
            }
        }
Exemple #2
0
        public static void ShowBoard(BoardModel boardModel)
        {
            var board = boardModel.BoxContents;

            Console.WriteLine("   a b c");
            Console.WriteLine(" ┌───────┐");
            var startIndex = 0;

            ShowOneLine(0, board);
            ShowOneLine(3, board);
            ShowOneLine(6, board);
            Console.WriteLine(" └───────┘");
        }
Exemple #3
0
        public static Tuple <bool, char> IsGameOver(BoardModel board)
        {
            var result = false;
            var player = ' ';

            Tuple <int, int>[][] indexes =
            {
                new Tuple <int,     int>[] {
                    new Tuple <int, int>(0, 0),
                    new Tuple <int, int>(0, 1),
                    new Tuple <int, int>(0, 2)
                },
                new Tuple <int,     int>[] {
                    new Tuple <int, int>(1, 0),
                    new Tuple <int, int>(1, 1),
                    new Tuple <int, int>(1, 2)
                },
                new Tuple <int,     int>[] {
                    new Tuple <int, int>(2, 0),
                    new Tuple <int, int>(2, 1),
                    new Tuple <int, int>(2, 2)
                },
                new Tuple <int,     int>[] {
                    new Tuple <int, int>(0, 0),
                    new Tuple <int, int>(1, 0),
                    new Tuple <int, int>(2, 0)
                },
                new Tuple <int,     int>[] {
                    new Tuple <int, int>(0, 1),
                    new Tuple <int, int>(1, 1),
                    new Tuple <int, int>(2, 1)
                },
                new Tuple <int,     int>[] {
                    new Tuple <int, int>(0, 2),
                    new Tuple <int, int>(1, 2),
                    new Tuple <int, int>(2, 2)
                },
                new Tuple <int,     int>[] {
                    new Tuple <int, int>(0, 0),
                    new Tuple <int, int>(1, 1),
                    new Tuple <int, int>(2, 2)
                },
                new Tuple <int,     int>[] {
                    new Tuple <int, int>(0, 2),
                    new Tuple <int, int>(1, 1),
                    new Tuple <int, int>(2, 0)
                }
            };

            foreach (var sequence in indexes)
            {
                var line = sequence.Aggregate("", (current, step) => current + _brett.board[step.Item1][step.Item2]);

                if (line != "xxx" && line != "ooo")
                {
                    continue;
                }
                result = true;
                player = Convert.ToChar(line.Substring(0, 1));
                break;
            }

            return(new Tuple <bool, char>(result, player));
        }
Exemple #4
0
        static void Main(string[] args)
        {
            _brett   = new BoardModel();
            _visning = new BoardView(_brett);
            var  finished = false;
            bool ai;
            Tuple <bool, char> result = new Tuple <bool, char>(false, ' ');

            Console.WriteLine("Play against the computer? (Y/N):");
            while (true)
            {
                var userSelect = Console.ReadLine();
                if (userSelect == "y" || userSelect == "Y")
                {
                    ai = true;
                    break;
                }
                if (userSelect == "n" || userSelect == "N")
                {
                    ai = false;
                    break;
                }
            }

            _visning.Show();
            while (!finished)
            {
                var selectX = false;
                var selectY = false;
                while (!selectX)
                {
                    if (!AddChar('x'))
                    {
                        continue;
                    }
                    result   = IsGameOver(_brett);
                    finished = result.Item1;
                    _visning.Show();
                    selectX = true;
                }

                while (!selectY)
                {
                    if (!ai)
                    {
                        if (!AddChar('o'))
                        {
                            continue;
                        }
                        result   = IsGameOver(_brett);
                        finished = result.Item1;
                        _visning.Show();
                        selectY = true;
                    }
                    else
                    {
                        selectY  = SetRandomO();
                        result   = IsGameOver(_brett);
                        finished = result.Item1;
                        _visning.Show();
                    }
                }
            }
            Console.WriteLine($"Game over! Winner is: {result.Item2}");
        }