📄 seaf.c
字号:
// seaf.c 海战棋// Make by 猫部猫(Catyboy)#include <ansi.h>#define WIDTH 9#define HEIGHT 9#define SHIP_NUM 10inherit ITEM;int has_start;int game_mode; // 开始攻击?0-布局 1-火力 2-开火 3-结束int who_win; // 胜利方 0=没有结束 -1=和手mixed table; // 两个棋盘 table = cell[9][9] // cell = ({ HasShip , HasFire })mapping player; // 两个玩家 [id:tableNumber(1,2)]mixed player_action; // 玩家的活动mapping visitor; // 两个玩家观战的人 [id:({id,...})]mixed ship; // 两个玩家的战船 ({ mixed({hasDest,x,y,fireX,fireY,fireValue}) ,...})mixed numbers = ({"0","1","2","3","4","5","6","7","8","9"});mixed cell_status = ({ // 自己的棋盘 ({"~","*","~"}), ({"船","@","船"}), // 敌方的棋盘 ({"~","*","#"}), ({"~","X","#"}) }); // 棋格的状态mixed fire_x,fire_y;int reset_game(int all);void msg(object me,object who,string msg){ if(me) message_vision(msg,me,who); else tell_room(environment(this_object()),msg,0);}int player_id(string key){ return player[key];}object get_player(string id){ object ob; ob= find_player(id); if( !ob || environment(ob) != environment(this_object()) ) return 0; return ob;}string enemy_id(string key){ mixed *idx; int i; idx = keys(player); for(i=0;i<sizeof(idx);i++) if(idx[i]!=key) return idx[i]; return 0;}object get_player2(int wHIYh){ mixed *idx; int i; idx = keys(player); for(i=0;i<sizeof(idx);i++) if(player[idx[i]]==wHIYh) return get_player(idx[i]); return 0; }int is_visitor(object who,string which){ if(!get_player(which)) return 0; if(!visitor[which]||member_array(who->query("id"),visitor[which])==-1) return 0; return 1;}int is_playing(object ob){ string id; id = ob->query("id"); if(player[id]) return 1; return 0;}mixed get_table(string id){ int i; if(i=player[id]) return table[i-1]; return 0;}mixed get_ship(string id){ int i; if(i=player[id]) return ship[i-1]; return 0;}int remain_ship(string who){ int i,c; mixed *sh; sh = get_ship(who); c = 0; for(i=0;i<SHIP_NUM;i++) if(!sh[i][0])c++; return c;}int remain_place(string who){ int i,c; mixed *sh; sh = get_ship(who); c = 0; for(i=0;i<SHIP_NUM;i++) if(!sh[i][1])c++; return c;}int remain_fire(string who){ int i,c; mixed *sh; sh = get_ship(who); c = 0; for(i=0;i<SHIP_NUM;i++) if(!sh[i][0]&&!sh[i][3])c++; return c;}void create(){ set_name("海战棋", ({ "sea board","board","seaf"}) ); set_weight(10000000); if( clonep() ) set_default_object(__FILE__); else { set("unit", "副"); set("long", "这是一副海战棋,使用方法请使用(helpboard)命令。\n"); set("value", 1); set("no_get",1); set("material", "wood"); } reset_game(1); setup();}string player_status(string key){ mixed* sh; int i; string r; sh = get_ship(key); if(!sh) return "???"; r = ""; for(i=0;i<SHIP_NUM;i++) { if(sh[i][0]) r+= HIR"X"NOR" "; else r+= HIB"船"NOR" "; } return r;}string build_board(string key){ mixed *mt,*et,*d; string r; int x,y,id,mid,eid; id = player_id(key); if(!id) return ""; if(id==1) { mid = 0; eid = 1; } else { mid = 1; eid = 0; } mt = table[mid]; et = table[eid]; r = "\n"; r += "*我军的情况* *敌军的情况*\n"; r += " 123456789 123456789\n"; for(y=0;y<HEIGHT;y++) { r = r+numbers[y+1]; for(x=0;x<WIDTH;x++) { d = mt[x][y]; if(game_mode == 2 && x == fire_x[mid]-1 && y == fire_y[mid]-1) r += BLINK + HIY + cell_status[d[0]][d[1]] + NOR; else r += cell_status[d[0]][d[1]]; } r = r+" " + numbers[y+1]; for(x=0;x<WIDTH;x++) { d = et[x][y]; if(game_mode == 2 && x == fire_x[eid]-1 && y == fire_y[eid]-1) r += cell_status[d[0]+2][d[1]]; else r += cell_status[d[0]+2][d[1]]; } r+="\n"; } r += "船只状态:" + player_status(key) + "\n"; return r;}int reset_game(int all){ int i,x,y; table = ({0,0}); if(all) { player = ([]); visitor = ([]); } ship = ({0,0}); player_action = ([]); for(i=0;i<2;i++) { table[i] = allocate(WIDTH); for(x=0;x<WIDTH;x++) { table[i][x] = allocate(HEIGHT); for(y=0;y<HEIGHT;y++) table[i][x][y] = ({0,0}); } ship[i] = allocate(SHIP_NUM); for(x=0;x<SHIP_NUM;x++) ship[i][x] = ({0,0,0,0,0,0}); } if(all) has_start = 0; return 1;}void display(int who){ string b,id,m; int i,j; mixed* v; object ob,vtor; for(i=1;i<=2;i++) { if(!who||who==i) { ob = get_player2(i); if(ob) { id = ob->query("id"); b = build_board(id); tell_object(ob,b); v = visitor[id]; for(j=0;j<sizeof(v);j++) { vtor = find_player(v[j]); if(vtor)//&&environment(vtor) == environment(this_object())) tell_object(vtor,b); } switch(game_mode) { case 0: m = sprintf("布置船只:%d只\n",remain_place(ob->query("id"))); m += "使用["HIY"place X坐标 Y坐标"NOR"]布置船只\n"; m += "使用["HIY"unplace X坐标 Y坐标"NOR"]取消布置\n"; m += "使用["HIY"rplace"NOR"]随机摆放船只\n"; break; case 1: m = sprintf("布置火力:%d个\n",remain_fire(ob->query("id"))); m += "使用["HIY"fire X坐标 Y坐标"NOR"]设置攻击位置\n"; m += "使用["HIY"unfire X坐标 Y坐标"NOR"]取消设置\n"; break; case 3: m = "战斗结束了。\n战况:"; if(who_win==-1) m+="和手"; else if(who_win==i) m+=HIY"胜利了"NOR; else m+=HBWHT BLK"失败了"NOR; break; } if(m) tell_object(ob,m); } } }}string extra_long(){ object who; string r,key; mixed *idx; int i; if(sizeof(player)>0) { idx = keys(player); r = "******************************************\n"; for(i=0;i<sizeof(idx);i++) { key = idx[i]; who = get_player(key); if(who) r = r + key + ":"+player_status(key)+"\n"; } r += "******************************************\n"; } return r;}int view_board(object who,string which){ string b; object ob; mixed *v; int i,see; ob = get_player(which); if(!ob) return 0; see = 0; if(ob==who) see = 1; if(!see) see = is_visitor(who,which); if(see) { b = build_board(which); tell_object(who,b); return 1; } return 0;}void start_game(){ has_start = 1; game_mode = 0; who_win = 0; msg(0,0,"开始游戏\n"); display(0);}int before_fire(){ game_mode = 1; player_action = ([]); msg(0,0,"\n\n"HIC"!!!进入战备状态!!!"NOR"\n\n"); display(0);}int start_fire(){ player_action = ([]); msg(0,0,"\n\n"HIR"!!!开始战斗状态!!!"NOR"\n\n"); game_mode = 2; fire_x = ({0,0}); fire_y = ({0,0}); remove_call_out("fire_it"); call_out("fire_it",2);}void init(){ add_action("do_help","helpboard"); // 帮助 add_action("do_reset","reset"); // 重置游戏 add_action("do_start","start"); // 重新开始 add_action("do_join","join"); // 加入游戏 add_action("do_view","view"); // 查看情况 add_action("do_visitor","visit"); // 查看情况 add_action("do_finish","finish"); // 完成 add_action("do_place","place"); // 布置船只 add_action("do_rplace","rplace"); // 随机摆放船只 add_action("do_unplace","unplace"); // 取消船只 add_action("do_fire","fire"); // 布置火力 add_action("do_unfire","unfire"); // 取消火力}int do_reset(string arg){ mixed *idx; int i; if(!this_object()->id(arg)) return 0; if(!is_playing(this_player())) { if(get_player2(1)||get_player2(2)) return notify_fail("你都不玩啊!\n"); } if(reset_game(1)) msg(this_player(),0,"$N重置了游戏。\n"); return 1;}int do_start(string arg){ mixed *idx; int i; if(!is_playing(this_player())) return notify_fail("你都不玩啊!\n"); reset_game(0); start_game(); return 1;}int do_view(string arg){ mixed *idx; int i; if(!is_playing(this_player())) { if(!arg) return notify_fail("你都不玩啊!\n"); if(!is_visitor(this_player(),arg)) return notify_fail("你不能够看"+arg+"的棋,可以使用[visit "+arg+"]命令成为"+arg+"的观众。\n"); } if(!arg) arg = this_player()->query("id"); if(!view_board(this_player(),arg)) return notify_fail("???\n"); return 1;}int do_join(string arg){ object me; me = this_player(); if(has_start) return notify_fail("游戏已经开始,不能加入了,请使用(reset)命令重置。\n"); if(is_playing(me)) return notify_fail("你已经参加了。\n"); player[me->query("id")]=sizeof(player)+1; msg(me,0,"$N加入游戏了。\n"); if(sizeof(player)==2) start_game(); return 1;}int do_place(string arg){ int i,x,y; mixed *sh,*tb; string id; if(!is_playing(this_player())) return notify_fail("你都不玩啊!\n"); if(!has_start||game_mode!=0) return notify_fail("现在不是布置船只的时候。\n"); if( !arg || sscanf(arg, "%d %d", x, y)!=2 ) return notify_fail("place X坐标 Y坐标\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -