标题:c#模拟打斗程序
取消只看楼主
qq645485165
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:197
注 册:2013-3-16
结帖率:100%
已结贴  问题点数:20 回复次数:3 
c#模拟打斗程序
编写一个控制台程序,实现一个简单的战斗程序。
首先定义一个步兵的类Footman,包涵属性有ID(int) ,Name(名称,string), Life(生命值,int),AttackLow(攻击下限,int),AttackHigh(攻击上限,int)。同时定义一个构造函数用于初始化这些信息。
再定义一个Battle类,用于控制交战。类中包涵有一个方法void Fight(Footman a, Footman b),主要作用是: a先开始攻击b一下,然后b攻击a一下,互相来回攻击直至某个对象生命值为0为止,每次攻击对方扣除的生命值是在自己Attacklow和AttackHigh之间取一个随即值。方法中显示每次来回打斗的信息,包括对方名字,扣除的生命值及剩余的生命值等。
在main方法中定义2个Footman对象,并显示出两个对象的详细信息,然后执行Fight方法,来完成一次战斗过程。

请教高手
搜索更多相关主题的帖子: 战斗 
2013-03-19 11:03
qq645485165
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:197
注 册:2013-3-16
得分:0 
回复 2楼 yhlvht
o ,会了
主要是对构造函数和属性不熟悉,我写的你看看有什么需要改进的没.

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

namespace fight
{
    public class Footman
    {
        int id;
        public int Id1
        {
            get { return id; }
            set
            {
                    id = value;
            }
        }

        string name;
        public string Name
        {
            get { return name; }
            set
            {
                    name = value;
            }
        }

        int life;
        public int Life
        {
            get { return life; }
            set
            {               
                life = value;
            }
        }

        int attackLow;
        public int AttackLow1
        {
            get { return attackLow; }
            set
            {
                attackLow = value;
            }
        }

        int attackhigh;
        public int Attackhigh1
        {
            get { return attackhigh; }
            set
            {
                attackhigh = value;
            }
        }

        public Footman()
        {}

    }
    public class Battle
    {
        public static void Fight(Footman a, Footman b)
        {
            Random r = new Random();
            int attack1,attack2;
            int lf1, lf2;
            do
            {
                attack1 = r.Next(a.AttackLow1, a.Attackhigh1);
                attack2 = r.Next(a.AttackLow1, a.Attackhigh1);

                lf2 = b.Life - attack1;
                b.Life = lf2;
                Console.WriteLine("{0}攻击{1},{2}掉了{3}滴血,还剩{4}滴血", a.Name, b.Name, b.Name, attack1, lf2);
                lf1 = a.Life - attack2;
                a.Life = lf1;
                Console.WriteLine("{0}攻击{1},{2}掉了{3}滴血,还剩{4}滴血", b.Name, a.Name, a.Name, attack2, lf1);
                if (b.Life <= 0)
                {
                    Console.WriteLine("{0}被{1}殴打死了",b.Name,a.Name);
                }
                else if(a.Life <= 0)
                {
                    Console.WriteLine("{0}被{1}殴打死了",a.Name , b.Name);
                }


            } while (a.Life > 0 && b.Life > 0);
        }
        static void Main(string[] args)
        {
            Footman a=new Footman ();
            Footman b= new Footman();
            a.Id1 = 1;
            b.Id1 = 2;
            a.Name = "小飞";
            b.Name = "小田";
            a.Life = 100;
            b.Life = 100;
            a.AttackLow1 = 8;
            b.AttackLow1 = 8;
            a.Attackhigh1 = 12;
            b.Attackhigh1 = 12;
            Console.WriteLine("{0}的生命是{1},最小攻击{2},最大攻击{3}",a.Name ,a.Life ,a.AttackLow1 ,a.Attackhigh1);
            Console.WriteLine("{0}的生命是{1},最小攻击{2},最大攻击{3}", b.Name, b.Life, b.AttackLow1, b.Attackhigh1);
            Fight(a, b);            
            Console.ReadLine();
        }
    }
}
2013-03-19 17:41
qq645485165
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:197
注 册:2013-3-16
得分:0 
回复 4楼 yhlvht
但是写到构造函数里后,name,id,等字段只能定义一个,怎么才能定义一个Footman a和Footman b,还有name,attacklow等属性基本没用,直接定义不就行啦,能说明一下那些属性的作用吗?
2013-03-19 20:03
qq645485165
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:197
注 册:2013-3-16
得分:0 
恩,谢啦!
2013-03-19 21:35



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-401254-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.088814 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved