有三只小猪,老大盖了个草房子,老二盖了个木头房子,老三盖了个石头房子,来了一只大灰狼,吹倒了草房子,吃了老大,吹倒了木头房子吃了老二,吹不倒石头房子,老三活了下来。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 小猪猪
{
class Program
{
class Pig
{
/*定义一个猪类,里面包含房子,生命状态,名字*/
public string house;
public int live;
public string name;
public Pig(string name, string house, int live)
{
this.name = name;
this.house = house;
this.live = live;
Console.WriteLine("{0}的{1}建起来了!", name, house);
}
public void show()
{//显示当前小猪状态
string q = (house == null) ? "摧毁了" : "完好无损";
string z = (live == 1) ? "活着" : "死亡";
Console.WriteLine("{0}的{1}{2},{3}状态:{4}", name, house, q, name, z);
}
}
class wolf
{
public void eat(ref Pig p)
{
//如果是石头房子推不倒,否则房子被推倒,小猪被只掉
if (p.house == "石头房子")
{
Console.WriteLine("石头房子房子未被吹倒!");
return;
}
Console.WriteLine("{0}房子被吹倒了!", p.house);
p.house = null;
p.live = -1;
}
}
static void Main(string[] args)
{
Pig p1 = new Pig("大猪", "草房子", 1);
Pig p2 = new Pig("二猪", "木头房子", 1);
Pig p3 = new Pig("三猪", "石头房子", 1);
wolf w = new wolf();
w.eat(ref p1);
w.eat(ref p2);
w.eat(ref p3);
p1.show();
p2.show();
p3.show();
Console.ReadLine();
}
}
}