コード例 #1
0
 public static void PrintHelp(int size)
 {
     Board.DrawMap(size);
     Console.WriteLine("\nA legal move is one placed in the same axis as an existing square of the same character while also being adjacent to an oppositions character. Diagonal placing relations are not supported. This placing will change all of the squares in between to the players character.");
     Console.Write("The object of the game is to have the most squares covered with your character (X or O). When there are no more empty squares or valid moves the game is ended.");
     Console.Write("The state of the board can be saved and loaded.");
 }
コード例 #2
0
        public static void TurnLoop(string mode, int size, char[,] boardMatrix)
        {
            // set player char arrays for looping though
            char[] activeplayer = new char[] { 'X', 'O' };
            char[] idleplayer   = new char[] { 'O', 'X' };
            int[]  playerid     = new int[] { 1, 2 };
            bool   gameover     = false;

            // loop for 2 human players
            if (mode == "h1")
            {
                while (gameover == false)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        // render board map
                        Board.DrawMap(size);
                        // render in board state
                        Console.WriteLine("\n");
                        for (int y = 0; y < size; y++)
                        {
                            for (int x = 0; x < size; x++)
                            {
                                Console.Write(string.Format("|{0}", boardMatrix[x, y]));
                                if (x == size - 1)
                                {
                                    Console.Write("|\n");
                                }
                            }
                        }
                        // prompt and handle user input
                        Console.WriteLine("\nPlayer {0}, your pieces are denoted by '{1}'. Enter a 2 digit co-ordinate to place, 'help' to view rules again, 'save' to save the current board state and 'end' to view the current score and finish:", playerid[i], activeplayer[i]);
                        string req   = Console.ReadLine();
                        bool   input = Int32.TryParse(req, out int placing);
                        // print game rules
                        if (req == "help")
                        {
                            Help.PrintHelp(size);
                        }
                        // save board matrix to txt file
                        else if (req == "save")
                        {
                            using (StreamWriter sr = new StreamWriter("save.txt"))
                            {
                                foreach (var item in boardMatrix)
                                {
                                    sr.WriteLine(item);
                                }
                            }
                            Console.WriteLine("The game has been saved to save.txt.");
                        }
                        // end game - display score
                        else if (req == "end")
                        {
                            EndOfGame.Score(boardMatrix);
                        }
                        // action move request if valid number input
                        else if (input)
                        {
                            int xcoord = placing / 10;
                            int ycoord = placing % 10;
                            boardMatrix = HumanMove.LookAndPlace(xcoord, ycoord, boardMatrix, activeplayer[i], idleplayer[i]);
                            // end of game test
                            int count = 0;
                            foreach (char square in boardMatrix)
                            {
                                if (square == ' ')
                                {
                                    count++;
                                }
                            }
                            if (count == 0)
                            {
                                EndOfGame.Score(boardMatrix);
                            }
                        }
                        else
                        {
                            Console.Write("\nInput not recognised.");
                        }
                    }
                }
            }
            // loop for 1 computer player, 1 human
            else if (mode == "c1")
            {
                while (gameover == false)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        // render in board state console
                        Console.WriteLine("\n");
                        for (int y = 0; y < size; y++)
                        {
                            for (int x = 0; x < size; x++)
                            {
                                Console.Write(string.Format("|{0}", boardMatrix[x, y]));
                                if (x == size - 1)
                                {
                                    Console.Write("|\n");
                                }
                            }
                        }
                        // prompt and handle human input
                        Console.WriteLine("\nPlayer 1, your pieces are denoted by 'X'. Enter a 2 digit co-ordinate to place, 'help' to view rules again, 'save' to save the current board state and 'end' to view the current score and finish:");
                        string req   = Console.ReadLine();
                        bool   input = Int32.TryParse(req, out int placing);
                        // print game rules
                        if (req == "help")
                        {
                            Help.PrintHelp(size);
                        }
                        // save board state to txt file
                        else if (req == "save")
                        {
                            using (StreamWriter sr = new StreamWriter("save.txt"))
                            {
                                foreach (var item in boardMatrix)
                                {
                                    sr.WriteLine(item);
                                }
                            }
                            Console.WriteLine("The game has been saved to save.txt.");
                        }
                        // end game - show scores
                        else if (req == "end")
                        {
                            EndOfGame.Score(boardMatrix);
                        }
                        // action move request if valid number input
                        else if (input)
                        {
                            int xcoord = placing / 10;
                            int ycoord = placing % 10;
                            boardMatrix = HumanMove.LookAndPlace(xcoord, ycoord, boardMatrix, 'X', 'O');
                        }
                        else
                        {
                            Console.Write("\nInput not recognised.");
                        }
                        // computer move
                        boardMatrix = EasyAI.MakeMove(boardMatrix, size);
                        // test for full board, if so end game
                        foreach (char square in boardMatrix)
                        {
                            if (square == ' ')
                            {
                                break;
                            }
                            else
                            {
                                EndOfGame.Score(boardMatrix);
                            }
                        }
                    }
                }
            }
        }