C# 飞行棋(人机对战)

本文介绍了如何使用C#编程语言,在Microsoft Visual Studio 2022环境下开发一款飞行棋游戏,包括人机对战的实现细节。
摘要由CSDN通过智能技术生成

2022.02.13 / Microsoft Visual Studio 2022 / C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 飞行棋_v2._0
{
    internal class Program
    {
        // 用静态字段模拟全局变量
        public static int[] GameMap = new int[100];
        // 用静态数组存储玩家坐标
        public static int[] PlayerPos = new int[2];
        // 用字符串数组存储玩家姓名
        public static string[] PlayerNames = new string[2];
        // 用布尔数组标记玩家是否处于暂停状态
        public static bool[] flags = new bool[2];

        static void Main(string[] args)
        {
            // 游戏开始时,显示游戏头
            GameShow();

            // 输入玩家A的姓名
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("玩家A的姓名:");
            PlayerNames[0] = Console.ReadLine();
            while (PlayerNames[0] == "")
            {
                Console.Write("玩家A的姓名不能为空,请重新输入:");
                PlayerNames[0] = Console.ReadLine();
            }
            Console.WriteLine();
            // 玩家B为电脑
            Console.WriteLine("玩家B的姓名:电脑");
            PlayerNames[1] = "电脑";
            Console.WriteLine();
            Console.WriteLine("请按任意键开始游戏……");
            Console.ReadKey(true);

            // 玩家的姓名输入完成后,清屏
            Console.Clear();

            // 初始化地图
            InitailGameMap();

            // 绘制地图
            DrawGameMap();

            // 游戏开始。没有玩家到达终点前,游戏会一直进行
            while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
            {
                // 没有踩到暂停
                if (flags[0] == false)
                {
                    PlayGame(0);
                }
                // 上回合踩到了暂停,这回合不能玩,但须变更flags标记,确保下回合可以玩
                else
                {
                    flags[0] = false;
                }
                if (PlayerPos[0] >= 99)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("    恭喜! 玩家 {0} 战胜了 电脑 !", PlayerNames[0]);
                    break;
                }

                // 没有踩到暂停
                if (flags[1] == false)
                {
                    AutoGame(1);
                }
                // 上回合踩到了暂停,这回合不能玩,但须变更flags标记,确保下回合可以玩
                else
                {
                    flags[1] = false;
                }
                if (PlayerPos[1] >= 99)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("    很遗憾! 玩家 {0} 输给了 电脑 !", PlayerNames[0]);
                    break;
                }



            }  // while结尾
            Console.WriteLine("     请按任意键退出游戏……");
            Console.ReadKey();

        }  // Main结尾

        /// <summary>
        /// 游戏头
        /// </summary>
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("------------------------------------------------------------");
            Console.WriteLine("|                 飞行棋 (人机对战) v2.0                 |");
            Console.WriteLine("------------------------------------------------------------");
            Console.WriteLine("                                                            ");
        }

        /// <summary>
        /// 初始化地图
        /// </summary>
        public static void InitailGameMap()
        {
            int[] landMine = { 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96 };  // 地雷点
            for (int i = 0; i < landMine.Length; i++)
            {
                GameMap[landMine[i]] = 1;
            }

            int[] pause = { 2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 54, 58, 62, 66, 70, 74, 78, 82, 86, 90, 94, 98 };  // 暂停点
            for (int i = 0; i < pause.Length; i++)
            {
                GameMap[pause[i]] = 2;
            }

            int[] lucky = { 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 55, 59, 63, 67, 71, 75, 79, 83, 87, 91, 95 };  // 幸运点
            for (int i = 0; i < lucky.Length; i++)
            {
                GameMap[lucky[i]] = 3;
            }
        }

        /
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值