C#实现飞行棋源码

 更新时间:2020年02月05日 15:18:49   作者:张三千8800  
这篇文章主要为大家详细介绍了C#实现飞行棋源码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#实现飞行棋的具体代码,供大家参考,具体内容如下

游戏规则

如果玩家A踩到了玩家B,玩家B退6格
踩到了1幸运轮盘,a交换位置,b轰炸对方使对方退6格
踩到了2地雷,退6格
踩到了3暂停,暂停一回合
踩到了4时空隧道,进10格
踩到了方块,什么都不干
0表示普通关卡
1表示幸运轮盘◎
2表示地雷★
3表示暂停▲
4表示时空隧道卍

关于飞行棋源码的解析,下一篇文章发出。

源码

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

namespace 飞行棋
{
 class Program
 {
  //我们用静态字段来模拟全局变量
  static int[] Maps=new int[100];
  //声明一个静态字段的数组用来存储玩家A和玩家B的坐标
  static int[] playerPos = new int[2];
  //存储两个玩家的姓名
  static string[] playerNames = new string[2];
  //两个玩家的标记
  static bool[] flags = new bool[2]; //flags[0]玩家A和flags[1]玩家B默认都是false
  static void Main(string[] args)
  {
   GameShow(); //加载游戏头
   #region 输入玩家姓名
   Console.WriteLine("请输入玩家A的姓名");
   playerNames[0] = Console.ReadLine();
   while (playerNames[0]=="")
   {
    Console.WriteLine("玩家A姓名不能为空,请重新输入");
    playerNames[0] = Console.ReadLine();
   }
   Console.WriteLine("请输入玩家B的姓名");
   playerNames[1] = Console.ReadLine();
   while (playerNames[1] == ""||playerNames[1]==playerNames[0])
   {
    if (playerNames[1] == "")
    {
     Console.WriteLine("姓名不能为空,请重新输入");
     playerNames[1] = Console.ReadLine();
    }
    else
    {
     Console.WriteLine("玩家B姓名不能重复,请重新输入");
     playerNames[1] = Console.ReadLine();
    }
   }
   #endregion
   //玩家姓名写好后,进行清屏
   Console.Clear(); //清屏
   GameShow();
   Console.WriteLine("{0}的士兵用A表示", playerNames[0]);
   Console.WriteLine("{0}的士兵用B表示", playerNames[1]);
   Initailmap(); //初始化地图
   DrowMap();  //画出地图---注意:画地图前首先应初始化地图

   //当玩家A和玩家B没有一个人到达终点的时候都在游戏中
   while (playerPos[0] < 99 && playerPos[1] < 99)
   {
    if (flags[0] == false)
    {
     PlayGame(0);     
    }
    else
    {
     flags[0] = false;
    } 
    if (playerPos[0] >= 99)
    {
     Console.WriteLine("玩家{0}赢了玩家{1}", playerNames[0], playerNames[1]);
     break;
    }
    if (flags[1] == false)
    {
     PlayGame(1);
    }
    else
    {
     flags[1] = false;
    }
    if (playerPos[1] >= 99)
    {
     Console.WriteLine("玩家{0}赢了玩家{1}", playerNames[1], playerNames[0]);
     break;
    }
   }
   Console.ReadKey();
  }
  /// <summary>
  /// 设置游戏头及输出内容的颜色
  /// </summary>
  public static void GameShow()
  {
   Console.ForegroundColor = ConsoleColor.Blue;  //设置输出内容的前景色
   Console.WriteLine("**********************");
   Console.ForegroundColor = ConsoleColor.Cyan;
   Console.WriteLine("**********************");
   Console.ForegroundColor = ConsoleColor.Red;
   Console.WriteLine("******飞行棋大战******");
   Console.ForegroundColor = ConsoleColor.Yellow;
   Console.WriteLine("**********************");
   Console.ForegroundColor = ConsoleColor.DarkGray;
   Console.WriteLine("**********************");
   Console.ForegroundColor = ConsoleColor.Green;
   Console.WriteLine("**********************");
  }
  /// <summary>
  /// 初始化地图
  /// </summary>
  public static void Initailmap()
  {
   int[] lucklyturn = { 6, 23, 40, 55, 69, 83 }; //幸运轮盘
   for (int i = 0; i < lucklyturn.Length; i++)
   {
    int index = lucklyturn[i];  
    Maps[index] = 1; //将幸运轮盘的位置处值都设为1
   }
   int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷
   for (int i = 0; i < landMine.Length; i++)
   {
    int index = landMine[i];
    Maps[index] = 2;  //将地雷的位置处值都设为2
   }
   int[] pause = { 9, 27, 60, 93 }; //暂停
   for (int i = 0; i < pause.Length; i++)
   {
    int index = pause[i];
    Maps[index] = 3;  //将暂停的位置处值都设为3
   }
   int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道
   for (int i = 0; i <timeTunnel.Length; i++)
   {
    int index=timeTunnel[i];
    Maps[index] = 4;  //将时空隧道的位置处值都设为4
   }
  }
  /// <summary>
  /// 画出地图
  /// </summary>
  public static void DrowMap()
  {
   Console.WriteLine("图例:普通方块:□ 幸运轮盘:◎ 地雷:★  暂停:▲  时空隧道:卍");
   //第一横行0--29
   for (int i = 0; i <= 29; i++)
   {
    Console.Write(DrowStringMap(i)); //输出返回的当前单元格的图形
   }
   //画完第一行后换行
   Console.WriteLine();
   //第一竖行30--34
   for (int i = 30; i <= 34; i++)
   {
    for (int j = 0; j < 29; j++)
    {
     Console.Write(" "); //两个空格
    }
    Console.Write(DrowStringMap(i)); 
    Console.WriteLine(); 
   }
   //第二横行35--64
   for (int i = 64; i >=35; i--)
   {
    Console.Write(DrowStringMap(i));
   }
   //第二横行打印完后换行
   Console.WriteLine();
   //第二竖行65--69
   for (int i = 65; i <= 69; i++)
   {
    Console.Write(DrowStringMap(i));
    Console.WriteLine();
   }
   //第三横行70--99
   for (int i = 70; i <= 99; i++)
   {
    Console.Write(DrowStringMap(i));
   }
   Console.WriteLine(); //画完最后一行地图后应该换行
  }
  /// <summary>
  /// 画当前的单元格的图案
  /// </summary>
  /// <param name="i">传入当前单元格的索引</param>
  /// <returns>返回当前单元格的图形</returns>
  public static string DrowStringMap(int i)
  {
   string str = " ";
    //如果玩家A和玩家B的坐标相同并且都在地图上画一个<>(因为刚开始和结束的时候玩家可能去地图外)
    if (playerPos[0] == playerPos[1] && playerPos[0] == i)
    {
     Console.ForegroundColor = ConsoleColor.Blue;
     str="<>";
    }
    else if (playerPos[0] == i)
    {
     Console.ForegroundColor = ConsoleColor.Blue;
     str="A";
    }
    else if (playerPos[1] == i)
    {
     Console.ForegroundColor = ConsoleColor.Blue;
     str="B";
    }
    else
    {
     switch (Maps[i])
     {
      case 0:
       Console.ForegroundColor = ConsoleColor.DarkYellow;
       str="□";
       break;
      case 1:
       Console.ForegroundColor = ConsoleColor.Green;
       str="◎";
       break;
      case 2:
       Console.ForegroundColor = ConsoleColor.Red;
       str="★";
       break;
      case 3:
       Console.ForegroundColor = ConsoleColor.Blue;
       str="▲";
       break;
      case 4:
       Console.ForegroundColor = ConsoleColor.DarkCyan;
       str="卍";
       break;
     }
    } //else
    return str;
  }
  /// <summary>
  /// 玩游戏
  /// </summary>
  public static void PlayGame(int playerNumber)
  {
   Random r = new Random();
   int rNumber = r.Next(1, 7);
   Console.WriteLine("{0}玩家开始掷骰子", playerNames[playerNumber]);
   Console.ReadKey(true); //ReadKey是重载函数,里面参数为true表示不显示按下的键。
   Console.WriteLine("{0}掷出了{1}", playerNames[playerNumber], rNumber);
   Console.ReadKey(true);
   Console.WriteLine("{0}按任意键开始行动", playerNames[playerNumber]);
   Console.ReadKey(true);
   playerPos[playerNumber] += rNumber;
   ChangePos();
   Console.WriteLine("玩家{0}行动完了", playerNames[playerNumber]);
   Console.ReadKey(true);
   //玩家A有可能踩到玩家B
   if (playerPos[playerNumber] == playerPos[1 - playerNumber])
   {
    Console.WriteLine("玩家{0}踩到了玩家{1},{2}退6格", playerNames[0], playerNames[1], playerNames[1]);
    playerPos[1 - playerNumber] -= 6;  //如果玩家A踩到了玩家B,玩家B退6格
    ChangePos();
    Console.ReadKey(true);
   }
   //玩家A有可能踩到方块,时空隧道,暂停,地雷,幸运轮盘
   else
   {
    switch (Maps[playerPos[playerNumber]])
    {
     case 0:
      Console.WriteLine("玩家{0}踩到了方块,安全", playerNames[playerNumber]);
      Console.ReadKey(true);
      break;
     case 1:
      Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择a换位置;b轰炸对方,让对方退6格", playerNames[playerNumber]);
      string input = Console.ReadLine();
      while (true)
      {
       if (input == "a")
       {
        Console.WriteLine("玩家{0}和玩家{1}交换位置", playerNames[playerNumber], playerNames[1 - playerNumber]); //*******
        Console.ReadKey(true);
        int temp = playerPos[playerNumber];
        playerPos[playerNumber] = playerPos[1 - playerNumber];
        playerPos[1 - playerNumber] = temp;
        Console.WriteLine("玩家{0}和玩家{1}位置交换成功,按任意键继续游戏。", playerNames[playerNumber], playerNames[1 - playerNumber]);
        Console.ReadKey(true);
        break;
       }
       else if (input == "b")
       {
        Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}的位置退6格", playerNames[playerNumber], playerNames[1 - playerNumber], playerNames[1 - playerNumber]);
        Console.ReadKey(true);
        playerPos[1 - playerNumber] -= 6;
        ChangePos();
        Console.WriteLine("玩家{0}的位置退6格", playerNames[1 - playerNumber]);
        Console.ReadKey(true);
        break;
       }
       else
       {
        Console.WriteLine("只能输入a或者b,输入错误,请重新输入");
        input = Console.ReadLine();
       }
      }
      break;
     case 2:
      Console.WriteLine("玩家{0}踩到了地雷,退6格", playerNames[playerNumber]);
      Console.ReadKey(true);
      playerPos[playerNumber] -= 6;
      ChangePos();
      break;
     case 3:
      Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", playerNames[playerNumber]);
      flags[playerNumber] = true;
      Console.ReadKey(true);
      break;
     case 4:
      Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", playerNames[playerNumber]);
      playerPos[playerNumber] += 10;
      ChangePos();
      break;
    } //switch
   } //else
   ChangePos();
   Console.Clear();
   //清屏
   DrowMap();  //重新画
  }
  /// <summary>
  /// 当玩家坐标发生改变的时候调用
  /// </summary>
  public static void ChangePos()
  {
   if (playerPos[0] < 0)
   {
    playerPos[0] = 0;
   }
   else if (playerPos[0] >99)
   {
    playerPos[0] = 99;
   }
   else if (playerPos[1] < 0)
   {
    playerPos[1] = 0;
   }
   else if (playerPos[1] > 99)
   {
    playerPos[1] = 99;
   }
   
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 在Linux上运行C#的方法

    在Linux上运行C#的方法

    这篇文章主要介绍了在Linux上运行C#的方法,实例分析了Linux平台下Mono软件包的应用技巧,以及在此基础之上的C#运行方法,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-12-12
  • C#如何自动选择出系统中最合适的IP地址

    C#如何自动选择出系统中最合适的IP地址

    这篇文章介绍了C#如何自动选择出系统中最合适的IP地址,非常具有实用价值,需要的朋友可以参考下
    2015-08-08
  • 浅析C#中的AsnycLocal与ThreadLocal

    浅析C#中的AsnycLocal与ThreadLocal

    这篇文章主要给大家介绍了关于C#中AsnycLocal与ThreadLocal的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2021-09-09
  • c#中的yield return用法详解

    c#中的yield return用法详解

    这篇文章主要介绍了c#中的yield return用法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-09-09
  • DevExpress实现TreeList向上递归获取符合条件的父节点

    DevExpress实现TreeList向上递归获取符合条件的父节点

    这篇文章主要介绍了DevExpress实现TreeList向上递归获取符合条件的父节点,需要的朋友可以参考下
    2014-08-08
  • C#单线程和多线程端口扫描器详解

    C#单线程和多线程端口扫描器详解

    这篇文章主要为大家详细介绍了C#单线程和多线程端口扫描器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • 深入分析c# 继承

    深入分析c# 继承

    这篇文章主要介绍了c# 继承的相关资料,文中讲解的非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • C#中读取XML文件的四种常用方法

    C#中读取XML文件的四种常用方法

    Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧
    2025-02-02
  • choosesubject c# switch

    choosesubject c# switch

    c# switch 实例代码。
    2009-07-07
  • C#并查集(union-find)算法详解

    C#并查集(union-find)算法详解

    本文详细讲解了C#并查集(union-find)算法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04

最新评论