Ejemplo n.º 1
0
        /// <summary>
        /// A method to setup a default game
        /// </summary>
        /// <param name="player1">Player one</param>
        /// <param name="player2">Player two</param>
        /// <param name="grid">Grid</param>
        public static void SetUpDefaultGame(out Player player1, out Player player2, out HexGrid grid)
        {
            //sets up needed variables, including defautl terrain (t), gridSize, the grid and the players
            List <string> t = new List <string>()
            {
                " ", "#", "#", " ", "~", "~", " ", " ", " ", "~", " ", "#", "#", " ", " ", " "
                , " ", " ", "#", "#", "#", "#", "~", "~", "~", "~", "~", " ", "#", " ", "#", " "
            };
            int gridSize = 8;

            grid    = new HexGrid(gridSize);
            player1 = new Player("Player One", 0, 10, 10, 5);
            player2 = new Player("Player Two", 1, 10, 10, 5);

            //setup the grid with the information above
            grid.SetUpGridTerrain(t);
            grid.AddPiece(true, "Baron", 0);
            grid.AddPiece(true, "Serf", 8);
            grid.AddPiece(false, "Baron", 31);
            grid.AddPiece(false, "Serf", 23);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads a game from a file
        /// </summary>
        /// <param name="player1">First player</param>
        /// <param name="player2">Second player</param>
        /// <param name="grid">Grid</param>
        /// <returns>Returns bool indicating success</returns>
        public static bool LoadGame(out Player player1, out Player player2, out HexGrid grid)
        {
            //gets file to load
            Console.Write("Enter the name of the file to load: ");
            string fileName = Console.ReadLine();

            //initialises data structures for loading file
            List <string> items;
            string        lineFromFile;

            //try and read from the given file
            try
            {
                using (StreamReader myStream = new StreamReader(fileName))
                {
                    //read the first line, get items and generate a player from it
                    lineFromFile = myStream.ReadLine();
                    items        = lineFromFile.Split(',').ToList();
                    player1      = new Player(items[0], Convert.ToInt32(items[1]), Convert.ToInt32(items[2]), Convert.ToInt32(items[3]), Convert.ToInt32(items[4]));

                    //do the same for the second line to generate the second player
                    lineFromFile = myStream.ReadLine();
                    items        = lineFromFile.Split(',').ToList();
                    player2      = new Player(items[0], Convert.ToInt32(items[1]), Convert.ToInt32(items[2]), Convert.ToInt32(items[3]), Convert.ToInt32(items[4]));

                    //read the third line, which gives info on the size of the grid
                    int gridSize = Convert.ToInt32(myStream.ReadLine());
                    grid = new HexGrid(gridSize);

                    //read the fourth line which gives info on terrain in grid
                    List <string> t = new List <string>(myStream.ReadLine().Split(','));
                    grid.SetUpGridTerrain(t);

                    //finally read all remaining lines to setup grid with info from each line
                    lineFromFile = myStream.ReadLine();
                    while (lineFromFile != null)
                    {
                        //get all the items from the line
                        items = lineFromFile.Split(',').ToList();

                        //if the first item is a "1", add a true piece to the grid at given coordinates
                        if (items[0] == "1")
                        {
                            grid.AddPiece(true, items[1], Convert.ToInt32(items[2]));
                        }
                        //otherwise add a false piece to the grid at given coordinates
                        else
                        {
                            grid.AddPiece(false, items[1], Convert.ToInt32(items[2]));
                        }

                        //repeat until file finished
                        lineFromFile = myStream.ReadLine();
                    }
                }
            }
            catch
            {
                //if file does not load, setup dummy variables and return false (indicating failure)
                Console.WriteLine("File not loaded");
                player1 = new Player("", 0, 0, 0, 0);
                player2 = new Player("", 0, 0, 0, 0);
                grid    = new HexGrid(0);

                return(false);
            }

            //return true (indicating success)
            return(true);
        }