C#基础:飞行棋游戏

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

namespace Pachee
{
    class Program
    {
        #region 静态字段
        // 关卡数量
        public static int[] Maps = new int[100];
        // 玩家坐标
        public static int[] PlayerPos = new int[2];
        // 玩家名称
        public static string[] PlayerNames = new string[2];
        // 判断玩家是否暂停
        public static bool[] Flags = new bool[2];
        #endregion

        /// <summary>
        /// 输出游戏头
        /// </summary>
        public static void ShowGame()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("****************************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("****************************");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("***C#基础练习:飞行棋项目***");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("****************************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("****************************");
        }
        /// <summary>
        /// 接受用户输入的游戏名称,判断是否合法
        /// </summary>
        /// <returns>游戏名称</returns>
        public static string[] InputPlayerNames()
        {
            PlayerNames[0] = "";
            PlayerNames[1] = "";
            Console.ForegroundColor = ConsoleColor.White;
            while (PlayerNames[0] == "")
            {
                Console.Write("Please enter the name of game A player: ");
                PlayerNames[0] = Console.ReadLine().Trim();
                if (PlayerNames[0] == "")
                {
                    Console.WriteLine("A player name cannot be empty, please enter again.");
                    continue;
                }
                break;
            }
            while (PlayerNames[1] == "" || PlayerNames[0] == PlayerNames[1])
            {
                Console.Write("Please enter the name of game B player: ");
                PlayerNames[1] = Console.ReadLine().Trim();
                if (PlayerNames[1] == "")
                {
                    Console.WriteLine("B player name cannot be empty, please enter again.");
                    continue;
                }
                else if (PlayerNames[1] == PlayerNames[0])
                {
                    Console.WriteLine("The player name cannot be the same as the player A B, please enter again.");
                    continue;
                }
                break;
            }
            return PlayerNames;
        }
        /// <summary>
        /// 初始化地图,改变默认的地图坐标类型
        /// 0:方块
        /// 1:轮盘
        /// 2:地雷
        /// 3:暂停
        /// 4:隧道
        /// </summary>
        public static void InitailMap()
        {
            #region 轮盘
            int[] luckTrun = { 6, 23, 40, 55, 69, 83 };
            for (int i = 0; i < luckTrun.Length; i++)
            {
                Maps[luckTrun[i]] = 1;
            }
            #endregion

            #region 地雷
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };
            for (int i = 0; i < landMine.Length; i++)
            {
                Maps[landMine[i]] = 2;
            }
            #endregion

            #region 暂停
            int[] pause = { 9, 27, 60, 93 };
            for (int i = 0; i < pause.Length; i++)
            {
                Maps[pause[i]] = 3;
            }
            #endregion

            #region 隧道
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                Maps[timeTunnel[i]] = 4;
            }
            #endregion
        }
        /// <summary>
        /// 设定当前坐标的类型
        /// </summary>
        /// <param name="i">坐标</param>
        /// <returns>坐标类型</returns>
        public static string DrawStringMap(int i)
        {
            string str = null;
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)
            {
                str = "<>";
            }
            else if (PlayerPos[0] == i)
            {
                str = "A";
            }
            else if (PlayerPos[1] == i)
            {
                str = "B";
            }
            else
            {
                switch (Maps[i])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        str = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        str = "卐";
                        break;
                }
            }
            return str;
        }
        /// <summary>
        /// 生成所有坐标
        /// </summary>
        public static void DrawMap()
        {
            Console.WriteLine("Legend: LuckTrun<◎> landMine<☆> Pause<▲> timeTunnel<卐>");

            #region 第一橫行
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            Console.WriteLine();
            #endregion

            #region 第一竖行
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j <= 28; j++)
                {
                    Console.Write("  ");
                }
                Console.Write(DrawStringMap(i));
                Console.WriteLine();
            }
            #endregion

            #region 第二橫行
            for (int i = 64; i >= 35; i--)
            {
                Console.Write(DrawStringMap(i));
            }
            Console.WriteLine();
            #endregion

            #region 第二竖行
            for (int i = 65; i < 70; i++)
            {
                Console.WriteLine(DrawStringMap(i));
            }
            #endregion

            #region 第三橫行
            for (int i = 70; i <= 99; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            Console.WriteLine();
            #endregion
        }
        /// <summary>
        /// 判断坐标是否超出范围
        /// </summary>
        public static void ChangePos()
        {
            #region Player A
            if (PlayerPos[0] < 0)
            {
                PlayerPos[0] = 0;
            }
            if (PlayerPos[0] > 99)
            {
                PlayerPos[0] = 99;
            }
            #endregion

            #region Player B
            if (PlayerPos[1] < 0)
            {
                PlayerPos[1] = 0;
            }
            if (PlayerPos[1] > 99)
            {
                PlayerPos[1] = 99;
            }
            #endregion
        }
        /// <summary>
        /// 踩到轮盘时,选择功能
        /// </summary>
        /// <param name="input">玩家的选择</param>
        /// <param name="player">玩家标示</param>
        public static void PlayerSelect(string input, int player)
        {
            while (true)
            {
                if (input == "1")
                {
                    Console.WriteLine("Player {0} select and {1} swap places.", PlayerNames[player], PlayerNames[1 - player]);
                    int temp = PlayerPos[player];
                    PlayerPos[player] = PlayerPos[1 - player];
                    PlayerPos[1 - player] = temp;
                    Console.WriteLine("Swap complete, press any key continue.");
                    Console.ReadKey(true);
                    break;
                }
                else if (input == "2")
                {
                    Console.WriteLine("Player {0} select bombing {1}, Player {2} back to 6.", PlayerNames[player], PlayerNames[1 - player], PlayerNames[1 - player]);
                    PlayerPos[1 - player] -= 6;
                    Console.ReadKey(true);
                    break;
                }
                else
                {
                    Console.WriteLine("Can only select: 1--Swap places 2--bombing: ");
                    input = Console.ReadLine();
                }
            }
        }
        /// <summary>
        /// 进行游戏
        /// </summary>
        /// <param name="player">玩家标示位</param>
        public static void PlayGame(int player)
        {
            Random r = new Random();
            int next = r.Next(1, 7);
            Console.WriteLine("{0} press any key to start rolling the dice.", PlayerNames[player]);
            Console.ReadKey(true);
            Console.WriteLine("{0} Throw out of {1}", PlayerNames[player], next);
            PlayerPos[player] += next;
            ChangePos();
            Console.ReadKey(true);
            Console.WriteLine("{0} press any key to start action.", PlayerNames[player]);
            Console.ReadKey(true);
            Console.WriteLine("{0} action complete.", PlayerNames[player]);
            Console.ReadKey(true);
            // Player A 有可能踩到: Player B、方块、轮盘、地雷、暂停、隧道
            if (PlayerPos[player] == PlayerPos[1 - player])
            {
                Console.WriteLine("Player {0} step on {1}, {2} back to 6.", PlayerNames[player], PlayerNames[1 - player], PlayerNames[1 - player]);
                PlayerPos[1 - player] -= 6;
                Console.ReadKey(true);
            }
            else
            {
                switch (Maps[PlayerPos[player]])
                {
                    case 0:
                        Console.WriteLine("Player {0} step on Square, safe.", PlayerNames[player]);
                        Console.ReadKey(true);
                        break;
                    case 1:
                        Console.WriteLine("Player {0} step on a LuckTrun, please select: 1--Swap places 2--bombing: ", PlayerNames[player]);
                        string input = Console.ReadLine().Trim();
                        PlayerSelect(input, player);
                        Console.ReadKey(true);
                        break;
                    case 2:
                        Console.WriteLine("Player {0} step on a LandMine, back to 6", PlayerNames[player]);
                        PlayerPos[player] -= 6;
                        Console.ReadKey(true);
                        break;
                    case 3:
                        Console.WriteLine("Player {0} step on a Pause, to suspend a round.", PlayerNames[player]);
                        Console.ReadKey(true);
                        Flags[player] = true;
                        break;
                    case 4:
                        Console.WriteLine("Player {0} step on a TimeTunnel, forward 10.", PlayerNames[player]);
                        PlayerPos[player] += 10;
                        Console.ReadKey();
                        break;
                }
            }
            ChangePos();
            Console.Clear();
            DrawMap();
        }
        static void Main(string[] args)
        {
            ShowGame();
            InputPlayerNames();
            Console.WriteLine("Player {0} is A.", PlayerNames[0]);
            Console.WriteLine("Player {0} is B.", PlayerNames[1]);
            InitailMap();
            DrawMap();

            while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
            {
                #region A
                if (Flags[0] == false)
                {
                    PlayGame(0);
                }
                else
                {
                    Flags[0] = false;
                }
                #endregion
                #region B
                if (Flags[1] == false)
                {
                    PlayGame(1);
                }
                else
                {
                    Flags[1] = false;
                }
                #endregion
            }
            #region 判断玩家胜利

            if (PlayerPos[0] == 99)
            {
                Console.Clear();
                Console.WriteLine("Player {0} Win.", PlayerNames[0]);
            }
            if (PlayerPos[1] == 99)
            {
                Console.Clear();
                Console.WriteLine("Player {0} Win.", PlayerNames[1]);
            }
            #endregion

            Console.ReadKey();
        }
    }
}
时间: 2024-10-09 20:23:53

C#基础:飞行棋游戏的相关文章

传智播客的飞行棋游戏代码

这个视频教程是非常值得一看的教程,课程体系很完整. 与视频中老师讲的有一点区别,就是我在最开始的cmd命令行中,游戏头使用了随机颜色,但也只显示2次…… using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { //使用静态字段,模拟全局变量 static int[] Maps = new

hdu4405--Aeroplane chess(概率dp第七弹:飞行棋游戏--2012年网络赛)

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1628    Accepted Submission(s): 1103 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids la

0505.Net基础班第八天(飞行棋)

1.画游戏头 2.初始化地图(加载地图所需要的资源) 将整数数组中的数字编程控制台中显示的特殊字符串的这个过程 就是初始化地图 3.画地图 4.玩游戏 游戏规则: 如果玩家A踩到了玩家B  玩家B退6格  踩到了地雷 退6格 踩到了时空隧道 进10格 踩到了幸运轮盘 1交换位置  2 轰炸对方 使对方退6格 踩到了暂停  暂停一回合  踩到了方块  神马都不干 Map[50] if(map[40]==1) {  Console.WriteLine("◎"); } //初始化地图   M

007 飞行棋小项目

2016-01-16 1.画游戏头2.初始化地图(加载地图所需要的资源)将整数数组中的数字编程控制台中显示的特殊字符串的这个过程 就是初始化地图3.画地图 4.玩游戏 游戏规则:如果玩家A踩到了玩家B  玩家B退6格  踩到了地雷 退6格踩到了时空隧道 进10格踩到了幸运轮盘 1交换位置  2 轰炸对方 使对方退6格踩到了暂停  暂停一回合  踩到了方块  神马都不干 主要引用用的方法 1 Map[50] 2 if(map[40]==1) 3 { 4 Console.WriteLine("◎&q

C# 基础之飞行棋

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 飞行棋 { class Program { //地图 //0代表普通方块□ //1代表选择盘◎ //2代表地雷☆ //3代表暂停▲ //4代表通道卍 static int[] map = new int[100]; static int[] playerPos = {0,0 }; static int st

传智的光辉岁月-C#基础篇六飞行棋

飞行棋业务:我们要能够让2个玩家 在地图上 按照游戏规则 进行游戏 玩家类 变量:玩家位置,玩家名称,玩家标识,玩家是否在陷阱中 方法:投骰子,移动 地图类 变量:地图数据数组 方法:初始化地图数据,绘制地图,显示Logo 游戏规则类 变量: 方法:第一次游戏说明,判断玩家位置是否有障碍物,根据不同障碍物执行不同操作,判断游戏是否结束 using System; using System.Collections.Generic; using System.Linq; using System.T

飞行棋2.0--输完姓名后全自动,可修改为人机交互模式

1 // 2 // main.m 3 4 #import <Foundation/Foundation.h> 5 #import "GameController.h" 6 int main(int argc, const char * argv[]) 7 { 8 GameController *ctl = [GameController new]; 9 [ctl startGame]; 10 11 // GameMap *map = [GameMap new]; 12 //

纠正飞行棋的错误

错误1:在运行暂停功能时,这个暂停功能可以实现,但无法显示提示信息. 改正如下: case 3: Console.Clear(); Program.drawmap(); isstop[0] = true; Console .WriteLine ("{0}走到了暂停,暂停一次!", name[0]); Console.WriteLine("按任意键继续...");  Console.ReadKey(true); 原因:缺少Console.WriteLine("

骑士飞行棋第三版(上色)

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 骑士飞行棋 8 { 9 class Program 10 { 11 12 //在下面的数组存储我们游戏地图各个关卡 13 //数组的小标为0的元素对应地图上的第一格 下标为1的元素对应第二格...下标为n的元素对应n+1