日期:2014-05-17  浏览次数:20847 次

C#(经典)新手项目《骑士飞行棋 》

   如果把这个小游戏写转了,那么C#基础基础就不错了


点击更多惊喜


代码如下:

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

namespace 骑士飞行棋
{
    class Program
    {
        static string msg = "";
        static bool[] isStop = { false, false };
        static int[] Map = new int[100];
        static string[] playerName = new string[2] { " ", " " };//用来存储玩家姓名 playerName[0]用来存储玩家A的姓名 playerName[1]存储玩家B的姓名
        static int[] playerPos = new int[] { 0, 0 };// playerPos[0] playerPos[1]玩家AB的坐标
        static void Main(string[] args)
        {
            //数组的下标为0的元素对应地图上的第1格,下标为1的元素对应第二格...下标为n的元素对应n+1格。
            //在数组中用:          1.表示幸运轮盘◎1  白色
            //                     2.地雷☆2 红色
            //                     3.暂停▲3 黄色
            //                     4.时空隧道卐4 绿色
            //                     0.表示普通
            //                    <>:表示AB在同一个坐标
            Random r = new Random();
            int step = 0;

            ShowUI();
            
            Console.WriteLine("请输入玩家A的姓名:");
            playerName[0] = Console.ReadLine();
            while (playerName[0] == "")
            {
                Console.WriteLine("玩家姓名不能为空,请重新输入!");
                playerName[0] = Console.ReadLine();

            }
            Console.WriteLine("请输入玩家B的姓名:");
            playerName[1] = Console.ReadLine();
            while (playerName[1] == "" || playerName[0] == playerName[1])
            {
                if (playerName[1] == "")
                {
                    Console.WriteLine("玩家姓名不能为空,请重新输入!");
                    playerName[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("该玩家姓名已存在,请重新输入!");
                    playerName[1] = Console.ReadLine();
                }
            }
            Console.WriteLine("按任意键开始游戏。。。。。。");
            Console.ReadKey(true);
            Console.Clear();
            ShowUI();
            Console.WriteLine("对战开始......");
            Console.WriteLine("{0}的士兵用A表示", playerName[0]);
            Console.WriteLine("{0}的士兵用B表示", playerName[1]);
            InitialMap();

            DrawMap();
            while (playerPos[0] < 99 && playerPos[1] < 99)
            {
                if (isStop[0] == false)
                {
                    #region 玩家A开始掷骰子


                    Console.WriteLine("{0}开始掷骰子", playerName[0]);
                    step = r.Next(1, 7);
                    Console.WriteLine("{0}掷出了{1}", playerName[0], step);
                    Console.WriteLine("按任意键开始行动。。。。。。");
                    Console.ReadKey(true);
                  
                    playerPos[0] = playerPos[0] + step;
                    CheckPos();
                    if (playerPos[0] == playerPos[1])
                    {//如果A踩到了B B 退回原点
                        playerPos[1] = 0;
                        msg = string.Format("{0}玩家踩到了{1},{1}退回原点", playerName[0], playerName[1]);
                    }
                    else
                    {
                        switch (Map[playerPos[0]])
                        {
                            case 0://走到0 什么也不做
                                msg = "";
                                break;
                            case 1://走到了1 幸运轮盘 
                                Console.Clear();
                                DrawMap();
                                msg = string.Format("太好了,{0}走到了幸运轮盘。。。。。", playerName[0]);
                                Console.WriteLine("请选择你要的幸运 1——交换位置。2——轰炸对方");
                                int userSelect = SelectStr(1, 2);