C# 骑士飞行棋的源码(分享)

代码如下所示:


代码如下:

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

namespace 骑士飞行棋
{
    class Program
    {
        //在下面的数组存储我们游戏地图各各关卡
        //数组的下标为0的元素对应地图上的第1格    下标为1的元素对应元素第2格...下标为n的元素对应n+1格
        //在数组中   1:表示幸运轮盘  ◎
        //           2: 表示地雷 ☆
        //           3: 表示暂停 ▲
        //           4: 表示时空隧道 卍
        //           0: 表示普通  □
        static int[] map = new int[100];

static string[] names = new string[2]; //names[0]存储玩家A的姓名    name[1]存玩家B的姓名

static int[] playerPos = { 0, 0 };//playPos[0]存玩家A的位置,playPos[1]存玩家B的位置

static int step = 0; //用于存放产生的随机数

static string input = ""; //用户存储用户的输入

static string msg = ""; //用于存储用户踩到某个关卡,输出的话

static bool[] isStop = { false, false };//isStop[0]表示玩家A是否上次一走到暂停,似的话为true,不是为false

static Random r = new Random();//r是产生的随机数

static void Main( string[] args)
        {

ShowUI(); //显示游戏
            InitialName();
            Console.Clear();
            ShowUI();
            Console.WriteLine("对战开始......");
            Console.WriteLine("{0}用A来表示", names[0]);
            Console.WriteLine("{0}用B来表示", names[1]);
            Console.WriteLine("如果AB在同一位置,用<>表示");
            InitialMap();//初始化地图
            drawMap();//绘制地图
            Console.WriteLine("开始游戏......");

//这个循环中让玩家A和玩家B轮流掷骰子  当玩家A或者玩家B的坐标>=99时,则循环结束
            while (playerPos[0] < 99 && playerPos[1] < 99)
            {
                Action(0);//A掷筛子
                Action(1);//B掷筛子 
            }
            Console.ReadKey();
        }

/// <summary>
        /// 用于绘制飞行棋的名称
        /// </summary>
        static void ShowUI()
        {
            Console.WriteLine("*******************************************************");
            Console.WriteLine("*                                                     *");
            Console.WriteLine("*         骑     士     飞     行      棋             *");
            Console.WriteLine("*                                                     *");
            Console.WriteLine("*******************************************************");
        }

static void InitialName()
        {
            Console.WriteLine("请输入玩家A的姓名");
            names[0] = Console.ReadLine();
            //判断用书输入的内容是否为空,如果为空,则让用户重新输入
            while (names[0] == "")
            {
                Console.WriteLine("玩家A的姓名不能为空,请重新输入!");
                names[0] = Console.ReadLine();
            }
            Console.WriteLine("请输入玩家B的姓名");
            names[1] = Console.ReadLine();
            //判断用户输入的内容是否为空,如果为空,则让用户重新输入
            while (names[1] == "" || names[1] == names[0])
            {
                if (names[1] == "")
                {
                    Console.WriteLine("玩家B的姓名不能为空,请重新输入!");
                    names[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("你输入的姓名与玩家A的姓名{0}相同,请重新输入", names[0]);
                    names[1] = Console.ReadLine();
                }

}
        }

static void InitialMap()
        {
            //用于存储在地图中为地雷的下标
            int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘 1
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷 2
            int[] pause = { 9, 27, 60, 93 };//暂停的坐标 3
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道  4

for (int i = 0; i < 100; i++) // 初始化map数组的数据
                map[i] = 0;

//把幸运轮盘位置填入map中
            for (int i = 0; i < luckyTurn.Length; i++)
                map[luckyTurn[i]] = 1;
            //把地雷填入map中
            for (int i = 0; i < landMine.Length; i++)
                map[landMine[i]] = 2;
            //把暂停填入map中
            for (int i = 0; i < pause.Length; i++)
                map[pause[i]] = 3;
            //把时空隧道填入map中
            for (int i = 0; i < timeTunnel.Length; i++)
                map[timeTunnel[i]] = 4;
        }

/// <summary>
        ///  获得第pos坐标上应该绘制的图案
        /// </summary>
        /// <param name="pos">要绘制的坐标</param>
        /// <returns></returns>
        static string getMapString(int pos)
        {
            string result = "";
            if (playerPos[0] == pos && playerPos[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = "<>";
            }
            else if (playerPos[0] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = "A";
            }
            else if (playerPos[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = "B";
            }
            else
            {
                switch (map[pos])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.White;
                        result = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Red;
                        result = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Green;
                        result = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        result = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.DarkBlue;
                        result = "卍";
                        break;
                }
            }
            return result;
        }

static void drawMap()
        {
            Console.WriteLine("图例:幸运轮盘:◎   地雷:☆   暂停:▲     时空隧道:卍  ");
            //画第一行
            for (int i = 0; i < 30; i++)
                Console.Write(getMapString(i));
            Console.WriteLine();

//左边的第一列
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j < 29; j++)
                    Console.Write(" ");
                Console.Write(getMapString(i));
                Console.WriteLine();
            }

//第二行
            for (int i = 64; i >= 35; i--)
                Console.Write(getMapString(i));
            Console.WriteLine();

//右边的列
            for (int i = 65; i < 70; i++)
            {
                Console.Write(getMapString(i));
                Console.WriteLine();
            }

//第三行
            for (int i = 70; i < 100; i++)
                Console.Write(getMapString(i));
            Console.ResetColor();
            Console.WriteLine();
        }

static void checkPos()
        {
            for (int i = 0; i <= 1; i++)
            {
                if (playerPos[i] > 99)
                {
                    playerPos[i] = 99;
                }
                if (playerPos[i] < 0)
                {
                    playerPos[i] = 0;
                }
            }
        }

static int ReadInt()//产生一个整数
        {
            int i = ReadInt(int.MaxValue, int.MinValue);
            return i;
        }

static int ReadInt(int min, int max)//产生min--max 之间的数
        {
            while (true)
            {
                try
                {
                    int number = Convert.ToInt32(Console.ReadLine());
                    if (number < min || number > max)
                    {
                        Console.WriteLine("只能输入{0}--{1}之间的数字,请重新输入", min, max);
                        continue;
                    }
                    return number;
                }
                catch
                {
                    Console.WriteLine("只能输入数字,请重新输入!");
                }

}

}

/// <summary>
        /// A或者B掷筛子的方法
        /// </summary>
        /// <param name="playerNumber">A掷筛子传0过来   B掷筛子传1过来</param>
        static void Action(int playerNumber)
        {
            if (isStop[playerNumber] == false)
            {
                Console.WriteLine("{0}按任意键开始掷筛子......", names[playerNumber]);
                ConsoleKeyInfo sec = Console.ReadKey(true);
                step = r.Next(1, 7);//产生一个1到6之间的随机数
                if (sec.Key == ConsoleKey.Tab)
                {
                    ConsoleKeyInfo sec1 = Console.ReadKey(true);
                    if (sec1.Key == ConsoleKey.F1)
                    {
                        step = ReadInt(1, 100);
                    }
                }

Console.WriteLine("{0}掷出了{1}", names[playerNumber], step);
                Console.WriteLine("{0}按任意键开始行动......", names[playerNumber]);
                Console.ReadKey(true);
                playerPos[playerNumber] += step; //注意,一旦坐标发生改变,就要判断坐标值是否>99||<0
                checkPos();//检查坐标是否越界

if (playerPos[playerNumber] == playerPos[1 - playerNumber]) //玩家A采到玩家B
                {
                    playerPos[1 - playerNumber] = 0;
                    msg = string.Format("{0}踩到了{1},{1}退回了原点", names[playerNumber], names[1 - playerNumber]);
                }
                else
                {//没踩到,要判断玩家A现在所在的位置是否有其他关卡
                    switch (map[playerPos[playerNumber]])
                    {
                        case 0:
                            //普通,没有效果
                            msg = "";
                            break;
                        case 1:
                            //走到了 幸运轮盘关卡
                            Console.Clear();
                            Console.WriteLine("你走到了幸运轮盘,请选择运气?");
                            Console.WriteLine("1 ---交换位置  2---轰炸对方");
                            int userSelect = ReadInt(1, 2);
                            if (userSelect == 1)
                            {//要与对方交换位置
                                int temp = playerPos[playerNumber];
                                playerPos[playerNumber] = playerPos[1 - playerNumber];
                                playerPos[1 - playerNumber] = temp;
                                msg = string.Format("{0}选了与对方交换位置", names[playerNumber]);
                            }
                            else
                            {//轰炸对方
                                playerPos[1 - playerNumber] -= 6;
                                msg = string.Format("{0}轰炸了{1},{1}退回了6格", names[playerNumber], names[1 - playerNumber]);
                                checkPos();
                            }
                            break;
                        case 2:
                            //踩到了地雷
                            playerPos[playerNumber] -= 6;
                            checkPos();
                            msg = string.Format("{0}踩到了地雷,{0}退了6格", names[playerNumber]);
                            break;
                        case 3:
                            //暂停一次
                            isStop[playerNumber] = true;
                            msg = string.Format("{0}走到了红灯,下次暂停一次啊", names[playerNumber]);
                            break;
                        case 4:
                            //踩到时空隧道
                            playerPos[playerNumber] += 10;
                            msg = string.Format("{0}进入了时空隧道,爽死了,进了10格", names[playerNumber]);
                            break;
                    }

}
            }
            else
            {
                isStop[playerNumber] = false;
            }

if (playerPos[playerNumber] >= 99)
            {
                //判断谁胜利,谁失败
                Console.Clear();
                if (playerPos[0] >= 99)
                {
                    Console.WriteLine("{0}胜利了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[0]);
                }
                else
                {
                    Console.WriteLine("{0}胜利了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[1]);
                }
            }

Console.Clear();
            drawMap();
            if (msg != "")
            {
                Console.WriteLine(msg);
            }
            Console.WriteLine("{0}掷出了{1},行动完成!", names[playerNumber], step);
            Console.WriteLine("*************玩家A和玩家B的位置*********");
            Console.WriteLine("{0}的位置为:{1}", names[0], playerPos[0] + 1);
            Console.WriteLine("{0}的位置为:{1}", names[1], playerPos[1] + 1);

}
    }
}

(0)

相关推荐

  • C#绘制飞行棋地图小程序

    1. 初始化地图,在绘制时可先将地图进行初始化,用数组来存储关卡的位置,然后利用循环给地图中 关卡所在处赋予代表关卡的值. 关键代码如下 /// <summary> /// 初始化游戏地图 /// </summary> static void InitialMap() { for (int i=0;i<Map.Length;i++) { Map[i] =0; } //用于存储关卡位置 int[] luckyTurn = { 6, 23, 40, 55, 69, 83,98 }

  • 中国象棋js代码,仅演示,未能真下

    www.jb51.net 中国象棋 我们 var st=""; var qipuid=new Array(); var qipux=new Array(); var qipuy=new Array(); var qipuc=0; qipust="27,8,5,2,3,3,"; qipust+="24,8,7,1,1,2,"; qipust+="29,6,3,15,5,7,"; qipust+="18,8,3,8,3,

  • C#飞行棋小程序设计代码

    飞行棋游戏大家应该都玩过吧,如何使用C#语言进行编写,本文实例就为大家分享了飞行棋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

  • JQuery 国际象棋棋盘 实现代码

    当时没觉得有什么,今天回头看看,觉得还有点意思,拿来给大家鉴定一下. 首先要实现棋子的排布,这毫无疑问得用div+css了. 具体代码可以看Jquery做的国际象棋棋盘(查看源代码) . 一开始我给每种棋子都写了类,实际上这是比较愚蠢的行为.首先,这样子css代码会有冗余.其次,在棋子div上绑定侦听器时会比较麻烦,要判断是否棋子div时,我不得不靠类名中的-b和-w后缀来判断是否棋子.但是如果我们将棋子的共同属性提取出来专设一个类qizi,那么这两个问题都可以迎刃而解. 在代码中我特意没有将之

  • JavaScript 井字棋人工智能实现代码

    /* Code Written by Cory Fogliani (Email: cory@ijustdontcare.com) Testers: Cory Fogliani, Chris Gordon Featured on JavaScript Kit (http://javascriptkit.com) For this and over 400+ free scripts, visit http://javascriptkit.com */ //if IE4/NS6, apply sty

  • VC实现五子棋游戏的一个算法示例

    本文讲述了VC实现五子棋游戏的一个算法示例,该算法采用极大极小剪枝博弈算法,感兴趣的读者可以对程序中不完善的部分进行修改与完善. 该设计主要包括:数据结构.估值函数.胜负判断.搜索算法 程序运行界面如下: 具体实现步骤如下: 1.数据结构 //记录每步棋,可以建立链表用来进行悔棋.后退(本程序没有实现) struct Step { int x,y; //棋子坐标 int ball; //表示下子方{BLACK,WHITE} }; //记录棋盘情况,用于搜索过程 class CBoardSitua

  • javascript 7行代码画出一个围棋棋盘

    function qi(q){ var t=["┏","┯","┓","┠","┼","┨","┗","┷","┛","╋"],text=[]; for(y=0;y●":"●"; } } return text.join(""); } var q=[{x:5,

  • C#飞行棋小程序设计分析

    C#小程序飞行棋,程序效果图 1.设计分析 这个程序界面大致分为四部分: ① 最上面游戏名字界面 ②信息提示区 ③游戏界面区 ④游戏操作提示区 2.分区设计实现 一.游戏界面显示区,由于只需要显示出图形即可,因此直接用Console.Writeline()输出即可. 二.信息提示区,此处用于显示 游戏对战双方的姓名等信息,在游戏开始时需要由用户录入对战双方的姓名信息,因此可用Console.Readline()来读取 用户键入的值,<注:需要检查验证对战双方的姓名不可相同!> 三.游戏界面区,

  • C# L型棋牌覆盖实现代码与效果

    //Main 复制代码 代码如下: using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ChessBoard{    class Program    {        //谁能教教我英语啊,英语语法什么的错误之处还望海涵,        static void Main(string[] args)        {            Function ob

  • java基于swing实现的五子棋游戏代码

    本文实例讲述了java基于swing实现的五子棋游戏代码.分享给大家供大家参考. 主要功能代码如下: 复制代码 代码如下: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Main extends JFrame implements ActionListener{         private static final long serialVersionUID = 1L;      

随机推荐