Example #1
0
        static void Main(string[] args)
        {
            var    grid = new Grid();
            string input;

            const string TRYAGAIN = "Try again.";

            int x, y;

            // Game loop
            while (grid.playing)
            {
                // Show grid to players
                Console.WriteLine(grid.ToString());

                // Get player move and check the input; see regex for current format
                input = Console.ReadLine().ToUpper();
                if (!(input.Length == 3 && Regex.IsMatch(input, "^([0-2]{2})(X|O)$")))
                {
                    Console.WriteLine(TRYAGAIN);
                    continue;
                }

                // Plugin input
                x = int.Parse(input[0].ToString());
                y = int.Parse(input[1].ToString());

                // Set the spot, returns false if spot is already set or given wrong input
                if (!grid.setSpot(new Spot(x, y), (Pieces)input[2]))
                {
                    Console.WriteLine(TRYAGAIN);
                    continue;
                }
            }

            // Game is over when execution gets here
            Console.WriteLine("Game is over!");
            Console.WriteLine("Result: " + grid.result);
            Console.WriteLine(grid.ToString());

            Console.ReadLine();
        }
Example #2
0
        static void Main(string[] args)
        {
            var grid = new Grid();
            string input;

            const string TRYAGAIN = "Try again.";

            int x, y;

            // Game loop
            while (grid.playing)
            {
                // Show grid to players
                Console.WriteLine(grid.ToString());

                // Get player move and check the input; see regex for current format
                input = Console.ReadLine().ToUpper();
                if (!(input.Length == 3 && Regex.IsMatch(input, "^([0-2]{2})(X|O)$")))
                {
                    Console.WriteLine(TRYAGAIN);
                    continue;
                }

                // Plugin input
                x = int.Parse(input[0].ToString());
                y = int.Parse(input[1].ToString());

                // Set the spot, returns false if spot is already set or given wrong input
                if (!grid.setSpot(new Spot(x, y), (Pieces)input[2]))
                {
                    Console.WriteLine(TRYAGAIN);
                    continue;
                }
            }

            // Game is over when execution gets here
            Console.WriteLine("Game is over!");
            Console.WriteLine("Result: " + grid.result);
            Console.WriteLine(grid.ToString());

            Console.ReadLine();
        }
Example #3
0
        public void Run()
        {
            for (var i = 0; ; i++)
            {
                _output("Grid layout:");
                _output(_grid.ToString());
                var activePlayer = _players[i % 2];
                GetPositionForActivePlayer(activePlayer);
                if (!_victoryPossibilities.IsWinning(activePlayer))
                {
                    continue;
                }

                _output($"Player {activePlayer} won!!!");
                break;
            }

            _output("Grid layout:");
            _output(_grid.ToString());
            _input();
        }
Example #4
0
 public override string ToString()
 {
     return(_grid.ToString());
 }