net学习之C#面向对象 大富翁(飞行棋)V2.0 面向对象实现.

这个版本使用了类,对象.

加入了人机对战功能。

加入了清除使用过的特效,使用过的特效将从地图上消失。

新建了 Player 玩家类.

通过Player类来控制 玩家的行为

Player 类 有以下方法:

掷塞子  ThrowTheDice()

移动 Move()

检查状态(检查即将走到的位置是否有特效) CheckStatus()

检查是否胜利 CheckWinLose()

行动 Action()

通过调用 行动Action() 来完成玩家的一系列动作,掷骰子,移动,检查胜负,检查状态。

然后就是  class Paint 类,该类为输出信息类,可以输出地图,菜单,帮助,关于,行动信息。

亲测可以人机对战。待逐步完善。边学习新知识,边完善。



using System;

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


namespace 大富翁1
{
    public class Player{
        public Player(){}


        private string name;//名称
        private int site;//位置
        private int status;//状态
        private int step;//筛子结果/步数
        private int userChoice;//用户的选择


        public int gameMode;
        public int Step { set { step = value; } get { return step; } }
        public int Status { set { status = value; } get { return status; } }
        public string Name { set { name = value; } get { return name;} }
        public int Site { set { site = value; } get { return site;} }
        //public int Status{set;get;}
        public int times;//可以行动的次数
        public Player other;//另一个玩家的引用,实现对另一个玩家的位置交换或者轰炸。操作对方的位置。
        public int[] map;//地图


        public bool win = false;
        public Random ra;


        
        private void ThrowTheDice()
        {
            step = ra.Next(1, 7);
            
            Console.WriteLine("{0}掷出了{1}",this.name,step);
            
        }
        private void Move()
        {
            this.site += step;


            Console.WriteLine("{0}走到了{1}",name,site);
        }
        private void CheckStatus()//检测是否遇到特效,遇到就行动并清除地图上该特效
        {
            if (map[this.site]==1)//如果遇到幸运
            {
                
                //提示用户可以选择前进或者交换位置或者轰炸对方
                Console.WriteLine("恭喜 {0} 获得一次幸运轮盘机会,请选择:\n1.前进3步. 2.交换位置. 3.轰炸对方(倒退6步).",name);
                try
                {
                    if (gameMode == 1)//p2默认0由电脑控制,1为玩家控制,所以此刻由玩家做出选择
                    {
                        userChoice = Convert.ToInt32(Console.ReadLine());
                    }
                    else
                    {
                        if (site < other.Site)
                        {
                            userChoice = 2;
                        }
                        else
                        {
                            if (site - other.Site < 6)
                            {
                                userChoice = 3;
                            }
                            else
                                userChoice = 1;


                        }
                    }
                   if (1 == userChoice)//前进3步
                   {
                       map[site] = 0;//将地图这个位置的特效移除
                       this.site += 3;
                       Console.WriteLine("{0}向前走了3步到了{1}.",name,site);
                   }
                   else if (2 == userChoice)//交换位置
                   {
                       map[site] = 0;//将地图这个位置的特效移除
                       int tmp;
                       tmp=this.site;
                       this.site=other.Site;
                       this.other.Site = tmp;
                       Console.WriteLine("{0}与{1}交换了位置.",name,other.Name);
                   }
                   else if (3 == userChoice)
                   {
                       map[site] = 0;//将地图这个位置的特效移除
                       this.other.Site = other.Site - 6;
                       Console.WriteLine("{1}被{0}轰炸,倒退了6步,到了位置{2}",name,other.name,other.Site);
                   }
                }
                catch
                {
                    Console.WriteLine("你的选择有误.请重新选择.");
                    this.CheckStatus();
                }
            }
            else if(map[site]==2)//如果遇到穿越
            {
                map[site] = 0;//将地图这个位置的特效移除
                //本人向前穿越6格
                this.site += 6;
                Console.WriteLine("{0}幸运的进入了时空隧道,向前穿越6步,到了{1}",name,site);
            }
            else if(map[site]==3)//如果遇到障碍
            {
                map[site] = 0;//将地图这个位置的特效移除
                //检测对方状态是否为暂停一轮.
                //本人状态转为暂停一轮
                this.times--;//行动次数减一
                Console.WriteLine("{0}遇到了雨,要休息一轮.",name);
            }
            
        }
        private void CheckWinLose()
        {
            if (site >= 100)
            {
                Console.WriteLine("恭喜 {0} 获得胜利!!!",this.name);
                other.Status = 0;
                this.Status = 0;
                this.win = true;
            }
        }
        public void Action()
        {
            if (this.times > 0)//行动次数大于1才能行动
            {
                this.status = 1;
                this.ThrowTheDice();


                this.Move();
                this.CheckWinLose();
                if (this.win == false)
                {
                    this.CheckStatus();
                }
                this.status = 0;
                Console.ReadKey();
            }
            else //否则行动次数加1,本轮行动结束。
            { 
                this.times++;
                this.status = 0;
                Console.WriteLine("{0}本轮无法行动,行动结束.",name);
                Console.ReadKey();
            }
        }
    }




    class Paint
    {
        public int choice=0;
        public int[] map;
        public Player p1;
        public Player p2;
        private string[] menu=new string[]{"1.人机对战","2.双人游戏","3.游戏说明","4.关    于","请按上下键选择,按回车键确定选择。Esc键退出。"};
        private string tag = "           <=========";
        public void OutMenu()//显示主界面
        {
            Console.Clear();
            Console.WriteLine("******************************");
            Console.WriteLine("         大富翁 V2.0");
            Console.WriteLine("******************************");


            
            for (int i = 0; i < 5; i++)
            {
                if(choice==i)
                    Console.WriteLine(menu[i]+tag);
                else
                    Console.WriteLine(menu[i]);
            }


        }
        public void OutAbout()//显示关于
        {


        }
        public void OutHelp()//显示帮助
        {


        }
        public void OutState()//显示行动信息
        {


            Console.WriteLine("{0}的位置是:{1}",p1.Name,p1.Site);
            Console.WriteLine("{0}的位置是:{1}", p2.Name, p2.Site);
            if (p1.Status == 1)
            {
                Console.WriteLine("{0}走了{1}步.",p2.Name,p2.Step);
                Console.WriteLine("现在轮到{0}行动了。", p1.Name);
            }
            else if (p2.Status == 1)
            {
                Console.WriteLine("{0}走了{1}步.", p1.Name, p1.Step);
                Console.WriteLine("现在轮到{0}行动了。", p2.Name);
            }
            Console.WriteLine("任意键继续.");
        }


        public void OutMap()//显示地图
        {
            Console.Clear();
            Console.WriteLine("{0}:● {1}:▲ 两人共同位置:<>  幸运轮盘:☆ 时空隧道:¤ 下雨:≡ 普通:□",p1.Name,p2.Name);
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    if (i * 20 + j == p1.Site && i * 20 + j == p2.Site)
                    {
                        Console.Write("<>");
                    }
                    else if (i * 20 + j == p1.Site)
                    {
                        Console.Write("●");
                    }
                    else if (i * 20 + j == p2.Site)
                    {
                        Console.Write("▲");
                    }
                    else  if (map[i * 20 + j] == 1)
                        Console.Write("☆");
                    else if (map[i * 20 + j] == 2)
                        Console.Write("¤");
                    else if (map[i * 20 + j] == 3)
                        Console.Write("≡");
                    else
                        Console.Write("□");
                }
                Console.Write("\n");
            }
        }
    }
    class Program
    {
        


        static void Main(string[] args)
        {
            //int gameMod = 0;//0为人机模式,1为玩家对战
            int[] map=new int[100];
            ConsoleKeyInfo cki;
            Paint paint = new Paint();
            Player p1 = new Player();
            Player p2 = new Player();
            Random ra = new Random();
            Random ra2 = new Random();
            int raNum;
            paint.p1 = p1;
            paint.p2 = p2;
            p1.other = p2;
            p2.other = p1;
            p1.ra = ra;
            p2.ra = ra;
            //大循环,程序伊始,初始化数据,进入菜单。
            while (true)
            {
                p1.win = false;
                p2.win = false;
                p1.Site = 0;
                p2.Site = 0;
                p1.times = 1;
                p2.times = 1;
                p1.Status = 1;
                p2.Status = 0;
                p2.gameMode = 0;
                p1.Name = "";
                p2.Name = "";
                //初始化地图数据
                for (int i = 0; i < 100; i++)
                {
                    raNum=ra2.Next(1, 10);
                    if (raNum > 3)
                        map[i] = 0;
                    else
                        map[i] = raNum;
                }
                p1.map = map;
                p2.map = map;
                paint.map = map;
                //进入菜单,等待用户做出选择
                while (true)//用户选择界面
                {
                    paint.OutMenu();//输出菜单,在控制台里显示文本。
                    cki = Console.ReadKey();//接收用户按键,上,下,ENTER,ESC
                    if (cki.Key == ConsoleKey.DownArrow)//用户按下 下按键    向下调整选择
                    {
                        if (paint.choice < 3)
                            paint.choice += 1;
                        else
                            paint.choice = 0;
                        paint.OutMenu();
                    }
                    else if (cki.Key == ConsoleKey.UpArrow)//用户按下  上按键  向上调整选择
                    {
                        if (paint.choice > 0)
                            paint.choice -= 1;
                        else
                            paint.choice = 3;
                        paint.OutMenu();
                    }
                    else if (cki.Key == ConsoleKey.Escape)//用户按下  Esc按键  退出
                    {
                        paint.choice = -1;
                        return;
                    }
                    else if (cki.Key == ConsoleKey.Enter)//用户按下  Enter键   选择结束。跳出选择循环。
                    {
                        break;
                    }


                }//直到用户选择一项之后跳出循环
                if (paint.choice == 0)//如果选择的是0,那么就开始人机对战
                {
                    //gameMod = 0;
                    while (p1.Name.Length<1)
                    {
                        Console.WriteLine("请输入你的名字:");
                        p1.Name = Console.ReadLine();
                    }
                    p1.gameMode = 1;//玩家控制
                    p2.Name = "电脑";
                    p2.gameMode = 0;//电脑控制
                    while (p1.win == false && p2.win == false)
                    {
                        paint.OutMap();
                        p1.Action();
                        paint.OutMap();
                        paint.OutState();
                        Console.ReadKey();
                        paint.OutMap();
                        p2.Action();
                        paint.OutMap();
                        paint.OutState();
                        Console.ReadKey();


                    }
                }
                else if (paint.choice == 1)//如果选择的是2,那么就开始玩家对战
                {
                    //gameMod = 1;
                    p1.gameMode = 1;
                    p2.gameMode = 1;


                    while (p1.Name.Length < 1)
                    {
                        Console.WriteLine("请输入玩家1的名字:");
                        p1.Name = Console.ReadLine();
                    }
                    while (p2.Name.Length < 1)
                    {
                        Console.WriteLine("请输入玩家2的名字:");
                        p2.Name = Console.ReadLine();
                    }
                    
                    while (p1.win == false && p2.win == false)
                    {
                        paint.OutMap();
                        paint.OutState();
                        p1.Action();
                        
                        
                        Console.ReadKey();
                        paint.OutMap();
                        paint.OutState();
                        p2.Action();
                        
                        
                        Console.ReadKey();
                    }
                }
                else if (paint.choice == 2)//如果选择的是2,那么显示游戏说明
                {
                    paint.OutHelp();
                }
                else if (paint.choice == 3)//如果选择的是3,那么显示 关于
                {
                    paint.OutAbout();
                }
                else
                {
                    break ;
                }
                //反馈完毕之后,要么是游戏结束,要么是显示完毕信息。应回到程序开头,初始化所有数据。
            }
            Console.ReadKey();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值