コード例 #1
0
        static bool addship(Battleship ship, string[,] map)
        {
            /*  add a ship into the map
             *  Return:
             *      ture - added successfully
             *      false - invalid position
             */
            if (ship.xpos >= 10 || ship.ypos >= 10 || ship.xpos < 0 || ship.ypos < 0)
            {
                return(false);
            }
            switch (ship.align)
            {
            case "h":
                if (ship.xpos + ship.length < 10)
                {
                    for (int x = ship.xpos; x < ship.xpos + ship.length; x++)
                    {
                        map[x, ship.ypos] = "A";
                    }
                }
                return(true);

            //break;

            case "v":
                if (ship.ypos + ship.length < 10)
                {
                    for (int y = ship.ypos; y < ship.ypos + ship.length; y++)
                    {
                        map[ship.xpos, y] = "A";
                    }
                }
                return(true);
                //break;
            }

            return(false);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Battleship myship = new Battleship()
            {
                xpos   = 3,
                ypos   = 3,
                length = 5
            };
            Battleship enermyship = new Battleship()
            {
                xpos   = 4,
                ypos   = 4,
                length = 5,
                align  = "v"
            };
            string useraction;

            Console.WriteLine("Battleship");
            // initial board setup
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    mymap[i, j]     = "~";
                    enermymap[i, j] = "~";
                    mymap2[i, j]    = "O";
                }
            }
            addship(myship, mymap);
            addship(enermyship, enermymap);

            // game starts
            do
            {
                PrintMap(mymap, "My Map");
                PrintMap(mymap2, "Attacking History");
                //PrintMap(enermymap, "Enermy's Map");
                Console.WriteLine("Your Turn! Press (1) to attack, press (2) to quit the game...");
                useraction = Console.ReadLine();
                if (useraction == "1")
                {
                    Console.WriteLine("Attacking!");
                    if (attacking() || pcattacking())
                    {
                        // hit a ship
                        break;
                    }
                }
                else if (useraction == "2")
                {
                    Console.WriteLine("Quit!");
                }
                else
                {
                    Console.WriteLine("Sorry, unknown action. Please input the correct action.");
                }
            } while (useraction != "2");

            // print the outcome
            if (winner != "")
            {
                Console.WriteLine("Game Over! The winner is {0}!", winner);
                PrintMap(mymap, "My Map");
                PrintMap(enermymap, "Enermy's Map");
            }
        }