C#三人飞行棋

C#三人飞行棋

c 复制代码
#region 1控制台设置

int w = 50, h = 30;
ConsoleInit(w, h);
#endregion

#region 2 场景选择实例

//声明一个表示场景标识的变量
E_SceneType nowSceneType = new E_SceneType();
while (true)
{
    switch (nowSceneType)
    {
        case E_SceneType.Begion:
            //开始场景逻辑
            Console.Clear();
            BeginOrEndScene(w, h,ref nowSceneType);
            break;
        case E_SceneType.Game:
            //游戏场景逻辑
            Console.Clear();
            GameScene(w, h,ref nowSceneType);
            break;
        case E_SceneType.End:
            //结束场景逻辑
            Console.Clear();
            BeginOrEndScene(w, h, ref nowSceneType);
            break;
        default:
            break;
    }
}

#endregion

#region 1 控制台初始化
static void ConsoleInit(int w, int h)
{
    //基础设置
    //光标设置
    Console.CursorVisible = false;
    //舞台大小
    Console.SetWindowSize(w, h);
    Console.SetBufferSize(w, h);
}

#endregion

#region 3 开始场景逻辑


static void BeginOrEndScene(int w, int h, ref E_SceneType nowSceneType)
{
    Console.ForegroundColor = ConsoleColor.White;
    Console.SetCursorPosition(nowSceneType == E_SceneType.Begion ? w / 2 - 3 : w / 2 - 4, 8);
    Console.Write(nowSceneType==E_SceneType.Begion ? "飞行棋" : "结束游戏");
    //默认开始游戏
    int nowSelIndex = 0;
    bool isQuitBegin = false;
    while (true)
    {
        Console.SetCursorPosition(nowSceneType == E_SceneType.Begion ? w / 2 - 4 : w / 2 - 5, 13);
        Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
        Console.Write(nowSceneType == E_SceneType.Begion ? "开始游戏" : "游戏主菜单");
        Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
        Console.SetCursorPosition(w / 2 - 4, 15);
        Console.Write("退出游戏");

        //通过Readkey可以得到一个输入的枚举类型
        switch (Console.ReadKey(true).Key)
        {
            case ConsoleKey.W:
                --nowSelIndex;
                if (nowSelIndex < 0)
                {
                    nowSelIndex = 1;
                }
                break;
            case ConsoleKey.S:
                ++nowSelIndex;
                if (nowSelIndex > 1)
                {
                    nowSelIndex = 0;
                }
                break;
            case ConsoleKey.J:
                if (nowSelIndex == 0)
                {
                    nowSceneType = nowSceneType == E_SceneType.Begion ? E_SceneType.Game : E_SceneType.Begion;
                    isQuitBegin = true;
                }
                else
                {
                    Environment.Exit(0);
                }
                break;
        }
        if (isQuitBegin)
        {
            break;
        }
    }
}
#endregion

#region 4 游戏场景逻辑
static void GameScene(int w, int h,ref E_SceneType nowSceneType)
{
    DrawWall(w, h);
    //Grid grid = new Grid(5,5,E_Grid_Type.Boom);
    //grid.Draw();
    //绘制地图
    Map map = new Map(10,3,111);
    map.Draw();

    //绘制玩家
    Player player1 = new Player(0,E_Player_Type.Player1);
    Player player2 = new Player(0,E_Player_Type.Player2);
    Player computer =new Player(0,E_Player_Type.Computer);
    DrawPlayer(player1,player2,computer,map);
    
    bool isGameOver=false;
    while (true)
    {
        Console.ReadKey(true);
        isGameOver = RandomMove(w,h,ref player1,ref player2,ref computer,map);
        map.Draw();
        DrawPlayer(player1, player2, computer, map);
        if (isGameOver)
        {
            Console.ReadKey(true);
            nowSceneType = E_SceneType.End;
            break;
        }

        Console.ReadKey(true);
        isGameOver = RandomMove(w, h, ref player2, ref player1, ref computer, map);
        map.Draw();
        DrawPlayer(player1, player2, computer, map);
        if (isGameOver)
        {
            Console.ReadKey(true);
            nowSceneType = E_SceneType.End;
            break;
        }

        Console.ReadKey(true);
        isGameOver = RandomMove(w, h, ref computer, ref player1, ref player2, map);
        map.Draw();
        DrawPlayer(player1, player2, computer, map);
        if (isGameOver)
        {
            Console.ReadKey(true);
            nowSceneType = E_SceneType.End;
            break;
        }
    }
}

#endregion

#region 4 绘制不变内容(红墙 提示等)

static void DrawWall(int w, int h)
{
    Console.ForegroundColor = ConsoleColor.Red;
    for (int i = 0; i < w; i += 2)
    {
        Console.SetCursorPosition(i, 0);
        Console.Write("■");
        Console.SetCursorPosition(i, h - 1);
        Console.Write("■");
        Console.SetCursorPosition(i, h-6);
        Console.Write("■");
        Console.SetCursorPosition(i, h-11);
        Console.Write("■");
    }
    for (int i = 0; i < h; i++)
    {
        Console.SetCursorPosition(0, i);
        Console.Write("■");
        Console.SetCursorPosition(w - 2, i);
        Console.Write("■");
    }
    //样式●■★◎ ▲ ◆ ⊙ ¤
    Console.ForegroundColor = ConsoleColor.White;
    Console.SetCursorPosition(2,h-10);
    Console.Write("□:普通格子");

    Console.ForegroundColor = ConsoleColor.Gray  ;
    Console.SetCursorPosition(22, h - 10);
    Console.Write("◆:玩家3");

    Console.ForegroundColor = ConsoleColor.Blue;
    Console.SetCursorPosition(2, h - 9);
    Console.Write("⊙:暂停,一回合不动");

    Console.ForegroundColor = ConsoleColor.DarkRed;
    Console.SetCursorPosition(26, h - 9);
    Console.Write("●:炸弹,倒退5格");

    Console.ForegroundColor = ConsoleColor.White;
    Console.SetCursorPosition(2, h - 8);
    Console.Write("¤:时空隧道,随机倒退,前进,暂停");

    Console.ForegroundColor = ConsoleColor.Cyan;
    Console.SetCursorPosition(2, h - 7);
    Console.Write("★:玩家");

    Console.ForegroundColor = ConsoleColor.Magenta;
    Console.SetCursorPosition(12, h - 7);
    Console.Write("▲:玩家2");
    ///
    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.SetCursorPosition(22, h - 7);
    Console.Write("◎:玩家和电脑重合");

    Console.ForegroundColor = ConsoleColor.White;
    Console.SetCursorPosition(2, h - 5);
    Console.Write("按任意键开始扔骰子");

}

#endregion

#region 8 扔骰子 函数
static void ClearInfo(int w,int h)
{
    Console.SetCursorPosition(2, h - 5);
    Console.Write("                                        ");
    Console.SetCursorPosition(2, h - 4);
    Console.Write("                                        ");
    Console.SetCursorPosition(2, h - 3);
    Console.Write("                                        ");
    Console.SetCursorPosition(2, h - 2);
    Console.Write("                                        ");
}
static bool RandomMove(int w, int h, ref Player p, ref Player p2,ref Player c, Map map)
{
    ClearInfo(w,h);
    if (p.type == E_Player_Type.Player1)
    {
        Console.ForegroundColor = ConsoleColor.Cyan;
    }
    else if (p.type == E_Player_Type.Player2)
    {
        Console.ForegroundColor = ConsoleColor.Magenta;
    }
    else
    {
        Console.ForegroundColor = ConsoleColor.Gray;
    }

    if (p.isPause)
    {
        Console.SetCursorPosition(2,h-5);
        
        if (p.type == E_Player_Type.Player1)
        {
            Console.Write("玩家1处于暂停点,玩家1需要暂停一回合");
        }
        else if (p.type == E_Player_Type.Player2)
        {
            Console.Write("玩家2处于暂停点,玩家2需要暂停一回合");
        }
        else
        {
            Console.Write("玩家3处于暂停点,玩家3需要暂停一回合");
        }
        p.isPause = false;
        return false;
    }
    Random r = new Random();
    int randomNum = r.Next(1, 7);
    p.nowIndex += randomNum;

    Console.SetCursorPosition(2,h-5);
    if (p.type == E_Player_Type.Player1)
    {
        Console.Write("玩家1扔出的点数为:"+randomNum);
    }
    else if (p.type == E_Player_Type.Player2)
    {
        Console.Write("玩家2扔出的点数为:"+randomNum);
    }
    else
    {
        Console.Write("玩家3扔出的点数为:" + randomNum);
    }


    if (p.nowIndex >= map.grids.Length - 1)
    {
        p.nowIndex = map.grids.Length - 1;
        Console.SetCursorPosition(2, h - 4);
        if (p.type == E_Player_Type.Player1)
        {
            Console.Write("玩家1获胜");
            Console.SetCursorPosition(2, h - 3);
            Console.Write("按任意键结束游戏");
            return true;
        }
        else if (p.type == E_Player_Type.Player2)
        {
            Console.Write("玩家2获胜");
            Console.SetCursorPosition(2, h - 3);
            Console.Write("按任意键结束游戏");
            return true;
        }
        else
        {
            Console.Write("玩家3获胜");
            Console.SetCursorPosition(2, h - 3);
            Console.Write("按任意键结束游戏");
            return true;
        }
        return true;
    }
    else
    {
        Grid grid = map.grids[p.nowIndex];
        switch (grid.type)
        {
            case E_Grid_Type.Normal:
                Console.SetCursorPosition(2,h-4);
                if (p.type == E_Player_Type.Player1)
                {
                    Console.Write("玩家1处于安全位置");
                }
                else if (p.type == E_Player_Type.Player2)
                {
                    Console.Write("玩家2处于安全位置");
                }
                else
                {
                    Console.Write("玩家3处于安全位置");
                }
                Console.SetCursorPosition(2, h - 3);
                if (p.type == E_Player_Type.Player1)
                {
                    Console.Write("请按任意键,玩家2开始仍骰子");
                }
                else if (p.type == E_Player_Type.Player2)
                {
                    Console.Write("请按任意键,玩家3开始仍骰子");
                }
                else
                {
                    Console.Write("请按任意键,玩家1开始仍骰子");
                }
                break;
            case E_Grid_Type.Boom:
                //炸弹退格
                p.nowIndex -= 5;
                if (p.nowIndex < 0)
                {
                    p.nowIndex = 0;
                }
                Console.SetCursorPosition(2, h - 4);
                if (p.type == E_Player_Type.Player1)
                {
                    Console.Write("玩家1踩到了炸弹,退后5格");
                }
                else if (p.type == E_Player_Type.Player2)
                {
                    Console.Write("玩家2踩到了炸弹,退后5格");
                }
                else
                {
                    Console.Write("玩家3踩到了炸弹,退后5格");
                }
                Console.SetCursorPosition(2, h - 3);
                if (p.type == E_Player_Type.Player1)
                {
                    Console.Write("请按任意键,玩家2开始仍骰子");
                }
                else if (p.type == E_Player_Type.Player2)
                {
                    Console.Write("请按任意键,玩家3开始仍骰子");
                }
                else
                {
                    Console.Write("请按任意键,玩家1开始仍骰子");
                }
                break;
            case E_Grid_Type.Pause:
                //暂停一回合
                p.isPause = true;
                Console.SetCursorPosition(2, h - 4);
                if (p.type == E_Player_Type.Player1)
                {
                    Console.Write("玩家1暂停一回合");
                }
                else if (p.type == E_Player_Type.Player2)
                {
                    Console.Write("玩家2暂停一回合");
                }
                else
                {
                    Console.Write("玩家3暂停一回合");
                }
                Console.SetCursorPosition(2, h - 3);
                if (p.type == E_Player_Type.Player1)
                {
                    Console.Write("请按任意键,玩家2开始仍骰子");
                }
                else if (p.type == E_Player_Type.Player2)
                {
                    Console.Write("请按任意键,玩家3开始仍骰子");
                }
                else
                {
                    Console.Write("请按任意键,玩家1开始仍骰子");
                }
                break;
            case E_Grid_Type.Tunnel:
                Console.SetCursorPosition(2, h - 4);
                if (p.type == E_Player_Type.Player1)
                {
                    Console.Write("玩家1进入了时空隧道");
                }
                else if (p.type == E_Player_Type.Player2)
                {
                    Console.Write("玩家2进入了时空隧道");
                }
                else
                {
                    Console.Write("玩家3进入了时空隧道");
                }
                //随机
                randomNum = r.Next(1,91);
                if (randomNum<30)
                {
                    p.nowIndex -= 5;
                    if(p.nowIndex < 0)
                    {
                        p.nowIndex = 0;
                    }
                    Console.SetCursorPosition(2, h - 3);
                    if (p.type == E_Player_Type.Player1)
                    {
                        Console.Write("玩家1退5格");
                    }
                    else if (p.type == E_Player_Type.Player2)
                    {
                        Console.Write("玩家2退5格");
                    }
                    else
                    {
                        Console.Write("玩家3退5格");
                    }
                }

                else if (randomNum <= 60)
                {
                    p.isPause = true;
                    Console.SetCursorPosition(2, h - 3);
                    if (p.type == E_Player_Type.Player1)
                    {
                        Console.Write("玩家1暂停一回合");
                    }
                    else if (p.type == E_Player_Type.Player2)
                    {
                        Console.Write("玩家2暂停一回合");
                    }
                    else
                    {
                        Console.Write("玩家3暂停一回合");
                    }
                }
                else
                {
                    p.nowIndex += 3;
                    if(p.nowIndex >= map.grids.Length - 1){
                        p.nowIndex = map.grids.Length - 2;
                    }
                    Console.SetCursorPosition(2, h - 3);
                    if (p.type == E_Player_Type.Player1)
                    {
                        Console.Write("玩家1前进格3格");
                    }
                    else if (p.type == E_Player_Type.Player2)
                    {
                        Console.Write("玩家2前进格3格");
                    }
                    else
                    {
                        Console.Write("玩家3前进格3格");
                    }

                }
                Console.SetCursorPosition(2, h - 2);
                if (p.type == E_Player_Type.Player1)
                {
                    Console.Write("请按任意键,玩家2开始仍骰子");
                }
                else if (p.type == E_Player_Type.Player2)
                {
                    Console.Write("请按任意键,玩家3开始仍骰子");
                }
                else
                {
                    Console.Write("请按任意键,玩家1开始仍骰子");
                }
                break;
            default:
                break;
        }

    }
    return false;
}


#endregion

#region 7 绘制玩家
static void DrawPlayer(Player player1,Player player2,Player computer,Map map)
{
    if (player1.nowIndex == computer.nowIndex && player1.nowIndex == player2.nowIndex && player2.nowIndex == computer.nowIndex)
    {
        Grid grid = map.grids[player2.nowIndex];
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.SetCursorPosition(grid.pos.x, grid.pos.y);
        Console.Write("◎");
    }
    else if (player1.nowIndex == computer.nowIndex)
    {
        Grid grid = map.grids[player1.nowIndex];
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.SetCursorPosition(grid.pos.x, grid.pos.y);
        Console.Write("◎");
        player2.Draw(map);
    }
    else if (player1.nowIndex == player2.nowIndex)
    {
        Grid grid = map.grids[player1.nowIndex];
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.SetCursorPosition(grid.pos.x, grid.pos.y);
        Console.Write("◎");
        computer.Draw(map);
    }
    else if (player2.nowIndex == computer.nowIndex)
    {
        Grid grid = map.grids[player2.nowIndex];
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.SetCursorPosition(grid.pos.x, grid.pos.y);
        Console.Write("◎");
        player1.Draw(map);
    }
    else
    {
        player1.Draw(map);
        player2.Draw(map);
        computer.Draw(map);
    }
}
#endregion

#region 2 场景选择设置
/// <summary>
/// 游戏场景枚举类型
/// </summary>
enum E_SceneType
{
    /// <summary>
    /// 开始场景
    /// </summary>
    Begion,
    /// <summary>
    /// 游戏场景
    /// </summary>
    Game,
    /// <summary>
    /// 结束场景
    /// </summary>
    End,
}
#endregion

#region 5 格子结构体和格子枚举
/// <summary>
/// 格子类型 枚举
/// </summary>
enum E_Grid_Type
{
    /// <summary>
    /// 普通格子
    /// </summary>
    Normal,
    /// <summary>
    /// 炸弹
    /// </summary>
    Boom,
    /// <summary>
    /// 暂停
    /// </summary>
    Pause,
    /// <summary>
    /// 时空隧道
    /// </summary>
    Tunnel,
}

struct Vector2
{
    public int x;
    public int y;
    public Vector2(int x, int y)
    {
        this.x = x; this.y = y;
    }
}
struct Grid
{
    //格子类型
    public E_Grid_Type type;
    //格子位置
    public Vector2 pos;
    //初始化构造函数
    public Grid(int x,int y,E_Grid_Type type)
    {
        pos.x = x; pos.y = y;
        this.type = type;
    }
    public void Draw()
    {
        Console.SetCursorPosition(pos.x, pos.y);
        switch (type)
        {
            case E_Grid_Type.Normal:
                Console.ForegroundColor= ConsoleColor.White;
                Console.Write("□");
                break;
            case E_Grid_Type.Boom:
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.Write("●");
                break;
            case E_Grid_Type.Pause:
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.Write("⊙");
                break;
            case E_Grid_Type.Tunnel:
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("¤");
                break;
            default:
                break;
        }
    }
}

#endregion

#region 6 地图结构体

struct Map
{
    public Grid[] grids;
    public Map(int x,int y,int num)
    {
        grids = new Grid[num];
        //用于位置改变计数的变量
        int indexX = 0, indexY = 0, stepNum = 2;    //x的步长
        Random random = new Random();
        int randomNum;
        for (int i = 0; i < num; i++)
        {
            randomNum = random.Next(0, 101);

            if (randomNum < 85 || i == 0 || i == num - 1)
            {
                grids[i].type = E_Grid_Type.Normal;
            }
            else if (randomNum>=85 && randomNum<90)
            {
                grids[i].type = E_Grid_Type.Boom;
            }
            else if (randomNum >=90 && randomNum<95)
            {
                grids[i].type = E_Grid_Type.Pause;
            }
            else
            {
                grids[i].type = E_Grid_Type.Tunnel;
            }

            grids[i].pos = new Vector2(x, y);
            if (indexX ==14)
            {
                y += 1;
                ++indexY;
                if (indexY ==2)
                {
                    indexX = 0;
                    indexY = 0;
                    stepNum = -stepNum; //反向步长
                }
            }
            else
            {
                x += stepNum;
                ++indexX;
            }
            
        }
    }

    public void Draw()
    {
        for (int i = 0; i < grids.Length; i++)
        {
            grids[i].Draw();
        }
    }
}

#endregion

#region 7 玩家枚举和玩家结构体
enum E_Player_Type
{
    /// <summary>
    /// 玩家1
    /// </summary>
    Player1,
    /// <summary>
    /// 玩家2
    /// </summary>
    Player2,
    /// <summary>
    /// 电脑
    /// </summary>
    Computer,
}

struct Player
{
    public E_Player_Type type;
    public int nowIndex;
    public bool isPause; 

    public Player(int index,E_Player_Type type)
    {
        nowIndex = index;
        this.type = type;
        isPause = false;
    }

    public void Draw(Map mapInfo)
    {
        Grid grid = mapInfo.grids[nowIndex];
        Console.SetCursorPosition(grid.pos.x, grid.pos.y);
        switch (type)
        {
            case E_Player_Type.Player1:
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write("★");
                break;
            case E_Player_Type.Player2:
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.Write("▲");
                break;
            case E_Player_Type.Computer:
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("◆");
                break;
            default:
                break;
        }
    }
}
#endregion
相关推荐
sukalot12 分钟前
windows C#-查询表达式基础(一)
开发语言·c#
我是苏苏15 分钟前
C# Main函数中调用异步方法
前端·javascript·c#
Envyᥫᩣ7 小时前
深入浅出C#编程语言
开发语言·c#
机器人天才一号9 小时前
C#从入门到放弃
开发语言·c#
吾与谁归in9 小时前
【C#设计模式(10)——装饰器模式(Decorator Pattern)】
设计模式·c#·装饰器模式
冷眼Σ(-᷅_-᷄๑)16 小时前
Path.Combine容易被忽略的细节
c#·.net
SongYuLong的博客21 小时前
C# (定时器、线程)
开发语言·c#
百锦再1 天前
详解基于C#开发Windows API的SendMessage方法的鼠标键盘消息发送
windows·c#·计算机外设
无敌最俊朗@1 天前
unity3d————协程原理讲解
开发语言·学习·unity·c#·游戏引擎
程序设计实验室1 天前
在网页上调起本机C#程序
c#