piggy.c

来自「C实现的MUD,对大家基本入门网络游戏很有帮助!」· C语言 代码 · 共 2,054 行 · 第 1/4 页

C
2,054
字号
		"north":  0,  ]),
]);

//  indicate  who  collect  all.
string  FULL="";
string  *PIGHEAD=({});

mapping  CLAIM=([
"claimer":  "",
"east":  "no",
"west":  "no",
"north":  "no",
"south":  "no",
]);

//  possible  states:  "等人",  "等发牌",  "等卖牌",  "出牌",  "算分",  

mapping  TABLE=([
"cond":  "等人",
"east":([  "player_name":  "「空」",
	    "player_id":  "no  one",
	    "status":  "empty",
	    "collected":  ({}),
	    "sold":  ({}),
	    "card":  0,  ]),
"north":([  "player_name":  "「空」",
	    "player_id":  "no  one",
	    "status":  "empty",
	    "collected":  ({}),
	    "sold":  ({}),
	    "card":  0,  ]),
"west":([  "player_name":  "「空」",
	    "player_id":  "no  one",
	    "status":  "empty",
	    "collected":  ({}),
	    "sold":  ({}),
	    "card":  0,  ]),
"south":([  "player_name":  "「空」",
	    "player_id":  "no  one",
	    "status":  "empty",
	    "collected":  ({}),
	    "sold":  ({}),
	    "card":  0,  ]),
]);

mapping  SOLD=([
"pig":  ({"not"}),
"blood":  ({"not"}),
"sheep":  ({"not"}),
"doubler":  ({"not"}),
]);


object  HERE=this_object();

/******************************************************************************/
//  declare  functions.

object  find_me(string  dir);

string  treat_sold(string  sold,  int  cnt);
string  sold_display(string  dir,  int  index,  int  flag);
string  display_table(string  cond);
string  look_table();
string  read_rules();
string  look_hand();
string  look_collected();
string  check_collected(string  dir,  string  suit);
string  table_cond();
string  card_suit(string  arg);
string  search_my_dir(object  me);
string  display_suit(string  dir,  string  suit);
string  next_player(string  dir);
string  find_large();
string  display_score();
string  look_note();
string  display_sell();

void  finish_round();
void  renew(string  type);

int  score_player(string  dir);
int  card_numb(string  card);
int  card_rank(string  arg);
int  is_empty(string  dir);
int  is_my_card(object  me,  int  cardno);
int  is_sellable(int  cardno);
int  is_dir(string  dir);
int  is_suit(string  suit);
int  in_table(object  me);
int  is_playable(object  me,  int  cardno);
int  search_suit(object  me,  string  suit);
int  all_have(string  arg);
int  deal_cards(string  dir);
int  display_cards(object  player);

/******************************************************************************/
void  create  ()
{
    set  ("short",  "拱猪房");
    set  ("long",  @LONG
这间房子是专门为了拱猪用而设的,整间屋子都是用不去皮的
松树搭成。一张石桌 (table)周围摆着四个石礅,桌面镶了一块大
理石,摸上去极为光滑。四壁挂着长明灯,把屋子照得有如白昼。
墙角里烧了一炉不知名的香,似乎颇有提神之效。香炉旁边挂着张
告示(gaoshi),上面写着这里的规矩;桌上有张小纸条(note),上
面草草的记着拱猪成绩。
LONG);

    set("exits",  ([
	"south":  __DIR__"club3",
    ])  );
    set("item_desc",  ([
	"table":	(:  look_table  :),
	"gaoshi":	(:  read_rules  :),
	"hand":		(:  look_hand  :),
	"collected":	(:  look_collected  :),
	"note":		(:  look_note  :),
    ])  );
    set("no_clean_up",  1);
    set("no_fight",  1);
    set("no_magic",  1);
    set("outdoors",  0);

    setup();
}
/******************************************************************************/
//  init,  add  action.

void  init()
{
        add_action("do_leave",  "leave");
        add_action("do_leave",  "likai");
        add_action("do_leave",  "离开");
        add_action("do_sit",  "sit");
        add_action("do_sit",  "zuo");
        add_action("do_sit",  "坐");
        add_action("do_deal",  "deal");
        add_action("do_deal",  "fapai");
        add_action("do_deal",  "发牌");
        add_action("do_sell",  "sell");
        add_action("do_sell",  "liang");
        add_action("do_sell",  "亮");
        add_action("do_pass",  "pass");
        add_action("do_pass",  "tingmai");
        add_action("do_pass",  "停卖");
        add_action("do_play",  "play");
        add_action("do_play",  "da");
        add_action("do_play",  "打");
        add_action("do_claim",  "claim");
        add_action("do_cast",  "cast");
        add_action("do_exert",  "exert");
        add_action("do_perform",  "perform");
        add_action("do_xiudao",  "xiudao");
        add_action("do_check",  "check");
        add_action("do_find",  "find");
        add_action("do_bian",  "bian");
        add_action("do_get",  "get");
        add_action("do_practice",  "practice");
        add_action("do_read",  "read");
        add_action("do_study",  "study");
        add_action("do_learn",  "learn");
        add_action("do_steal",  "steal");
        add_action("do_fight",  "fight");
        add_action("do_kill",  "kill");
        add_action("do_quit",  "quit");
        add_action("do_exercise",  "exercise");
        add_action("do_meditate",  "meditate");

        return;
}
/******************************************************************************/
int  valid_leave(object  me,  string  dir)
{
        if  (in_table(me))
	return  notify_fail("对不起,你现在在拱猪,就这么走掉不太好吧?\n");
        return  ::valid_leave(me,  dir);
}
int  do_quit(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里离开游戏,谢谢。\n");
        return  1;
}
int  do_steal(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里偷东西,谢谢。\n");
        return  1;
}
int  do_fight(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里打架,谢谢。\n");
        return  1;
}
int  do_kill(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里杀戮,谢谢。\n");
        return  1;
}
int  do_exercise(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里练功,谢谢。\n");
        return  1;
}
int  do_meditate(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里修炼,谢谢。\n");
        return  1;
}
int  do_get(string  arg)
{
        object  ob;
        string  thing,  person;

        sscanf(arg,  "%s  from  %s",  thing,  person);
        if  (!person)  person=arg;
        if  (ob=present(person,  this_object()))
	if(userp(ob))
	{
	        tell_object(this_player(),  "请不要占别人的便宜,谢谢。\n");
	        return  1;
	}
        return  0;
}
int  do_cast(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里施用法术,谢谢。\n");
        return  1;
}
int  do_exert(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里施用内功,谢谢。\n");
        return  1;
}
int  do_perform(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里施用外功,谢谢。\n");
        return  1;
}
int  do_xiudao(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里修道,谢谢。\n");
        return  1;
}
int  do_check(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里查别人的功夫,谢谢。\n");
        return  1;
}
int  do_find(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里查别人的踪迹,谢谢。\n");
        return  1;
}
int  do_bian(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里变来变去,谢谢。\n");
        return  1;
}
int  do_practice(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里练功,谢谢。\n");
        return  1;
}
int  do_read(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里读书,谢谢。\n");
        return  1;
}
int  do_study(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里读书,谢谢。\n");
        return  1;
}
int  do_learn(string  arg)
{
        tell_object(this_player(),  "请不要在拱猪房里学习,谢谢。\n");
        return  1;
}
/******************************************************************************/
int  is_suit(string  suit)
{

        if  (member_array(suit,  SUIT)  ==  -1)
	return  0;
        return  1;
}
int  is_dir(string  dir)
{

        if  (member_array(dir,  DIR)  ==  -1)
	return  0;
        return  1;
}
int  is_empty(string  dir)
{
        if(TABLE[dir]["player_id"]  ==  "no  one")
	return  1;
        if(!present(TABLE[dir]["player_id"],  HERE))
        {
	TABLE[dir]["player_id"]="no  one";
	return  1;
        }
        return  0;
}
int  is_my_card(object  me,  int  cardno)
{
        if(CARDS[cardno]["status"]  !=  search_my_dir(me))
	return  0;
        return  1;
}
object  search_dir(string  dir)
{
        return  present(TABLE[dir]["player_id"],  HERE);
}
string  card_suit(string  arg)
{
        string  card=capitalize(arg);

        switch  ((string)card[0..0])
        {
	case  "S":
	        return  "spade";
	case  "H":
	        return  "heart";
	case  "D":
	        return  "diamond";
	case  "C":
	        return  "club";
	default:
	        return  "花色错误:card_suit。\n";
        }
}
int  card_rank(string  card)
{
        int  i=strlen(card)-1;

        switch  ((string)card[i..i])
        {
	case  "2":
	        return  2;
	case  "3":
	        return  3;
	case  "4":
	        return  4;
	case  "5":
	        return  5;
	case  "6":
	        return  6;
	case  "7":
	        return  7;
	case  "8":
	        return  8;
	case  "9":
	        return  9;
	case  "T":
	case  "t":
	        return  10;
	case  "J":
	case  "j":
	        return  11;
	case  "Q":
	case  "q":
	        return  12;
	case  "K":
	case  "k":
	        return  13;
	case  "A":
	case  "a":
	        return  14;
	default:
	        return  notify_fail("无法理解输入的牌,"+card+"。请重新输入。\n");
        }
}
int  card_numb(string  card)
{
        int  base,  rank;
        string  suit;
        if  (!(suit=card_suit(card))  ||  !(rank=card_rank(card)))
	return  notify_fail("函数错误:card_numb。\n");
        switch  (suit)
        {
	case  "spade":
	        base=1;
	        break;
	case  "heart":
	        base=14;
	        break;
	case  "diamond":
	        base=27;
	        break;
	case  "club":
	        base=40;
	        break;
	default:
	        return  notify_fail("函数错误:card_numb。\n");
        }
        return  (base+15-rank);
}
int  is_sellable(int  cardno)
{
        switch(cardno)
        {
	case  3:
	case  14:
	case  30:
	case  44:
	        return  1;
	default:
	        return  0;
        }
}
/******************************************************************************/

int  is_playable(object  me,  int  cardno)
{
        string  dir=search_my_dir(me),  suit;

        if  (!is_my_card(me,  cardno))
        {
	tell_object(me,  "对不起,"+CARDS[cardno]["name"]+"不在你手里。\n");
	return  0;
        }
        if  (dir==GAMEINFO["rlead"])
	if  (!is_sellable(cardno))
	        return  1;
        suit=CARDS[cardno]["suit"];
        if  (suit  !=  GAMEINFO["suit"]
          &&  search_suit(me,  GAMEINFO["suit"]))
        {
	tell_object(me,  "对不起,你手中还有"+CSUIT[GAMEINFO["suit"]]+",不能出"+CARDS[cardno]["name"]+"。\n");
	return  0;
        }
        if  (!GAMEINFO[CARDS[cardno]["suit"]]
          &&  is_sellable(cardno))
        {
	if  (SOLD[CARDS[cardno]["misc"]][0]  ==  "m"
                  &&  search_suit(me,  (GAMEINFO["suit"]  ==  ""?CARDS[cardno]["suit"]:GAMEINFO["suit"]))  >  1)
	{
	        tell_object(me,"对不起,明卖了的牌不能在本花色第一轮出。\n");
	        return  0;
	}
        }
        return  1;
}
int  in_table(object  me)
{
        int  i=NUM;
        string  id=me->query("id");

        while(i--)
        {
	if  (id  ==  TABLE[DIR[i]]["player_id"])
	{
	        me->set_temp("piggy/seating",  DIR[i]);
	        return  1;
	}
	continue;
        }
        return  0;
}

string  search_my_dir(object  me)
{
        int  i=NUM;
        string  id=me->query("id");

        while(i--)
        {
	if  (id  ==  TABLE[DIR[i]]["player_id"])
	        return  DIR[i];
	continue;
        }
        return  "none";
}
/******************************************************************************/
//  do_sit,  to  enter  the  table.

int  do_sit(string  dir)
{
        object  me=this_player(),  pl;

        if(!is_dir(dir))
	return  notify_fail("请选择「east、north、south  或  west」。\n");
        if(in_table(me))
	return  notify_fail("你已经在座了。请用「leave」站起来。\n");
        if  (!is_empty(dir))
	return  notify_fail(search_dir(dir)->query("name")+"正在"+CDIR[dir]+"边坐着呢,你还是等空位吧。\n");
        me->set_temp("piggy/seating",  dir);
        TABLE[dir]["player_id"]=me->query("id");
        TABLE[dir]["player_name"]=me->query("name");
        TABLE[dir]["status"]="filled";
        if  (all_have("players"))
	TABLE["cond"]="等发牌";
        message_vision("$N坐入了"+CDIR[dir]+"边的空位。\n",  me);
        return  1;
}
/******************************************************************************/
//  do_leave,  to  leave  the  table.

int  do_leave(string  arg)
{
        string  dir;
        object  me=this_player();

        if  (!in_table(me))
	return  notify_fail("对不起,你并没在拱猪桌边坐着。\n");
        dir  =  search_my_dir(me);
        if  (is_empty(dir)  ||  search_dir(dir)  !=  me)
	return  notify_fail("函数错误:do_leave。请告诉酸黄瓜。\n");
        me->delete_temp("piggy/seating",  dir);
        TABLE[dir]["status"]="empty";
        TABLE[dir]["player_name"]="「空」";
        TABLE[dir]["player_id"]="no  one";
        TABLE["cond"]="等人";

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?