【黑马程序员】9. 骑士飞行棋

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------       

        本项目使用的是面向过程的程序设计思想,用到的主要知识点有,用数组存放同类型的数据,类,方法的声明和调用,循环结构,选择结构等。代码是我一句句敲出来的,可能会有小的bug和需要改善的地方,如有发现请指出以便改正,谢谢。不多说了,上代码:

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

namespace 骑士飞行棋
{
    #region
    class Program
    {
       static string[] playerName;//存放玩家姓名
        static int[] Map = new int[100];//存入地图上的100个格儿
        static int[] playerPos = { 0, 0 };//存放玩家当前的位置坐标   
        static bool[] isStop = { false, false };//控制是否允许玩家掷骰子

        static void Main(string[] args)
        {
            ShowUI();
            playerName = InputPlayerName();//调用让玩家输入姓名的方法
              Console.Clear();//清屏
              ShowUI();//显示游戏名称界面
              Console.WriteLine("对战开始......");
            Console.WriteLine("玩家[{0}]用A表示...", playerName[0]);
            Console.WriteLine("玩家[{0}]用B表示...", playerName[1]);
            Console.WriteLine("如果A、B在同一位置上,则用<>来表示...");            
            SetGatePos();//设置关卡的坐标位置
              DrawMap();//绘制地图
              Console.WriteLine("开始游戏......");
            //玩家A、B轮流掷骰子
              while (playerPos[0] < 99 & playerPos[1] < 99)
            {	// isStop为false玩家可以掷骰子,为true则不能掷骰子
                 if (isStop[0] == false)
               {
                    Start(0);//玩家A调用掷骰子方法
                  }
                else
                {   //说明isStop==true
                    isStop[0] = false;
                }
                if (playerPos[0] >= 99)
                {
                    Console.WriteLine("玩家A获胜!");
                    break;
                }
                if (isStop[1] == false)
                {
                    Start(1);//玩家B调用掷骨子方法
                   }
                else
                {
                    isStop[1] = false;
                }
            }
            if (playerPos[0] >= 99)
            {
                Console.WriteLine("玩家A胜利了!!");
            }
            else if(playerPos[1]>=99)
            {
                Console.WriteLine("玩家B胜利了!!");
            }

            Console.ReadKey();
        }
	#endregion
        /// <summary>
        /// 显示游戏名称
        /// </summary>
        static void ShowUI()
        {
            Console.WriteLine("*******************************************");
            Console.WriteLine("*                                         *");
            Console.WriteLine("*          骑  士  飞  行  棋                  *");
            Console.WriteLine("*                                         *");           
            Console.WriteLine("*******************************************");
        }

        /// <summary>
        /// 输入玩家姓名
        /// </summary>
        static string[] InputPlayerName()
        {
            string[] names = new string[2];//存放玩家A、B的姓名,names[0]是玩家A,names[1]是玩家B
            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();
                }
            }
            return names;
        }

        /// <summary>
        /// 定义各关卡的坐标
        /// </summary>
        static void SetGatePos()
        {
            //定义各关卡的坐标
            int[] LuckyTurn = { 15, 23, 44, 56, 69, 78, 83, 97 };//幸运轮盘
            int[] TimeTunnel = { 10, 30, 37, 45, 63, 72 };//时空隧道
            int[] Pause = { 8, 17, 27, 40, 60, 88, 92 };//暂停一次
            int[] LandMine = { 4, 19, 33, 50, 68, 80, 95 };//地雷
            //把定义的关卡的坐标应用到游戏地图中,分别用0、1、2、3、4表示。0表示普通,1幸运轮盘,2表示时空隧道,3表示暂停一次,4表示地雷
            for (int i = 0; i < LuckyTurn.Length; i++)
            {
                //int pos = LuckyTurn[i];
                //Map[pos] = 1;
                Map[LuckyTurn[i]] = 1;
            }
            for (int i = 0; i < TimeTunnel.Length; i++)
            {
                Map[TimeTunnel[i]] = 2;
            }
            for (int i = 0; i < Pause.Length; i++)
            {
                Map[Pause[i]] = 3;
            }
            for (int i = 0; i < LandMine.Length; i++)
            {
                Map[LandMine[i]] = 4;
            }
        }

        /// <summary>
        /// 判断某个坐标上应该画什么图案
         /// </summary>        
        static string GetMapString(int pos)
        {
            string mapString = "";
            if (playerPos[0] == pos && playerPos[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                mapString = "<>";
            }
            else if (playerPos[0] == pos)
            {
                mapString = "Α";
            }
            else if (playerPos[1] == pos)
            {
                mapString = "Β";
            }
            else
            {
                switch (Map[pos])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.White;
                        mapString = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        mapString = "◎";//幸运轮盘
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Green;
                        mapString = "※";//时空隧道
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Red;
                        mapString = "▲";//暂停一次
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        mapString = "☆";//地雷
                        break;
                }
            }
            return mapString;
        }

        /// <summary>
        /// 绘制飞行棋地图
         /// </summary>
        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.WriteLine(GetMapString(i));
            }
            for (int i = 64; i >= 35; i--)//第二行
              {
                Console.Write(GetMapString(i));
            }
            Console.WriteLine();
            for (int i = 65; i < 70; i++)//第二列
              {
                Console.WriteLine(GetMapString(i));
            }
            for (int i = 70; i < 100; i++)//第三行
              {
                Console.Write(GetMapString(i));
            }
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.White;
        }

        /// <summary>
        /// 掷骰子,对玩家所在的关卡进行判断
         /// </summary>
        /// <param name="playerID"></param>
        static void Start(int playerID)
        {
            int step;
            string msg = "";
            Random r = new Random();
            Console.WriteLine("{0}按任意键开始掷骰子...", playerName[playerID]);
            Console.ReadKey(true);
            step = r.Next(1, 7);
            //小窍门1
            //ConsoleKeyInfo cki = Console.ReadKey(true);
            //step = r.Next(1, 7);
            //if (cki.Key == ConsoleKey.Tab)
            //{
            //    ConsoleKeyInfo cki2 = Console.ReadKey(true);
            //    if (cki2.Key == ConsoleKey.F1)
            //    {
            //        step = ReadInt(1, 100);
            //    }
            //}
            //小窍门2
            //if (cki.Key == ConsoleKey.Tab)
            //{
            //    step = 10;
            //}
            //else
            //{
            //    step = r.Next(1, 7);
            //}

            Console.WriteLine("{0}掷出了:{1}", playerName[playerID], step);
            Console.WriteLine("按任意键开始行动...");
            Console.ReadKey(true);
            playerPos[playerID] += step;
            CheckPos(playerID);//检查玩家坐标是否越界
              if (playerPos[playerID] == playerPos[1 - playerID])
            {
                playerPos[1 - playerID] = 0;
                msg = string.Format("{0}踩到了{1},{1}后退到起点!", playerName[playerID], playerName[1 - playerID]);
            }
            else
            {
                switch (Map[playerPos[playerID]])
                {
                    case 0: break;
                    case 1:
                        Console.Clear();
                        DrawMap();
                        Console.WriteLine("{0}踩到了幸运轮盘,请选择一个幸运数字:", playerName[playerID]);
                        Console.WriteLine("1、与对方交换位置\t2、轰炸对方");
                        int select = ReadInt(1,2);
                        if (select == 1)
                        {
                            int temp = playerPos[playerID];
                            playerPos[playerID] = playerPos[1 - playerID];
                            playerPos[1 - playerID] = temp;
                            msg = string.Format("{0}选择了与对方交换位置!", playerName[playerID]);
                        }
                        else
                        {
                            playerPos[1 - playerID] -= 6;
                            CheckPos(playerID);
                            msg = string.Format("{0}轰炸了{1},{1}退后6格!", playerName[playerID], playerName[1 - playerID]);
                        }
                        break;
                    case 2:
                        playerPos[playerID]+=10;
                        CheckPos(playerID);
                        msg = string.Format("{0}踩到了时空隧道,前进10格",playerName[playerID]);
                        break;
                    case 3:
                        isStop[playerID] = true;
                        msg = string.Format("{0}踩到了暂停一次!", playerName[playerID]);
                        break;
                    case 4:
                        playerPos[playerID] -= 6;
                        CheckPos(playerID);
                        msg = string.Format("{0}踩到地雷,退后6格!", playerName[playerID]);
                        break;
                }
            }
            Console.Clear();
            DrawMap();
            if (msg != "")
            {
                Console.WriteLine(msg);
            }
            Console.WriteLine("{0}掷出了:{1},行动完成!", playerName[playerID], step);
            Console.WriteLine("==================玩家A、B的最新位置如下====================");
            Console.WriteLine("{0}当前的位置为:{1}", playerName[0], playerPos[0] + 1);
            Console.WriteLine("{0}当前的位置为:{1}", playerName[1], playerPos[1] + 1);
            Console.WriteLine("============================================================");
        }
        
        /// <summary>
        /// 检查玩家当前坐标是否越界
         /// </summary>
        static void CheckPos(int playerID)
        {
            if (playerPos[playerID] < 0)
            {
                playerPos[playerID] = 0;
            }
            if (playerPos[playerID] > 99)
            {
                playerPos[playerID] = 99;
                Console.WriteLine("{0}先到达终点,取得胜利!",playerName[playerID]);
            }
        }

        /// <summary>
        /// 读取用户的输入并做出判断
         /// </summary>
        /// <returns></returns>
        static int ReadInt(int min,int max)
        {
            int input;
            while (true)
            {
                try
                {
                    input = Convert.ToInt32(Console.ReadLine());
                    if (input < min || input > max)
                    {
                        Console.WriteLine("只能输入{0}~{1}之间的数,请重新输入!",min,max);
                        continue;
                    }
                    else
                    {
                        return input;
                    }
                }
                catch
                {
                    Console.WriteLine("只能输入数字,请重新输入!");
                }
            }
        }
    }
}


 

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java骑士飞行棋源码示例,供参考: ```java import java.util.Scanner; public class KnightFlyingChess{ public static void main(String[] args){ System.out.println("欢迎来到骑士飞行棋游戏!"); // 初始化棋盘 int[][] board = new int[8][8]; for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ board[i][j] = -1; } } // 初始化骑士的位置 int x = 0; int y = 0; board[x][y] = 0; // 开始游戏 Scanner scanner = new Scanner(System.in); while(true){ // 打印棋盘 for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ System.out.print(board[i][j] + "\t"); } System.out.println(); } // 获取玩家输入 System.out.println("请输入行动方向(1-8):"); int direction = scanner.nextInt(); // 根据方向移动骑士 switch (direction) { case 1: x += 1; y -= 2; break; case 2: x += 2; y -= 1; break; case 3: x += 2; y += 1; break; case 4: x += 1; y += 2; break; case 5: x -= 1; y += 2; break; case 6: x -= 2; y += 1; break; case 7: x -= 2; y -= 1; break; case 8: x -= 1; y -= 2; break; default: break; } // 判断骑士是否越界 if(x < 0 || x > 7 || y < 0 || y > 7){ System.out.println("骑士越界了,游戏结束!"); break; } // 判断骑士是否到达终点 if(board[x][y] == 99){ System.out.println("骑士到达终点,游戏胜利!"); break; } // 判断骑士是否经过之前的位置 if(board[x][y] != -1){ System.out.println("骑士经过之前的位置,游戏结束!"); break; } // 在棋盘上标记骑士的位置 board[x][y] = board[x][y] + 1; } } } ``` 这只是一个简单的示例,骑士飞行棋游戏还有很多其他的规则和玩法,你可以根据自己的需求进行扩展和改进。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值