## imagine written by Yさ BEGIN{ srand(); # '場'のサイズ決定 gm=FIELDSIZE; ## 5=normal, 3=training if(gm!="5" && gm!="3"){ print " " print " training - 3" print " normal - 5" printf("\n Which?(3 or 5) >"); do{ gm=""; getline gm; gm=tolower(gm); }while(gm!="5" && gm!="3"); } FIELDSIZE=gm+0; print "\n"; ## 初期設定 # カード split("S,D,C", StrSuit, ","); StrSuit[0]="H"; split("2,3,4,5,6,7,8,9,0,J,Q,K", StrRank, ","); StrRank[0]="A"; # Cards[key, 52]; ## カード本体 for(s=0; s<4; ++s){ for(r=0; r<13; ++r){ p=s*13 +r; Cards["suit", p] = s; # マーク Cards["rank", p] = r; # 数字 } } # ガイド split("a,b,c,d,e", NtoA, ","); for(i=1; i<=9; ++i) Guide[i]=i ""; Guide[10]="0"; Guide[11]="j"; Guide[12]="q"; Guide[13]="k"; YOU=0; COM=1; RED=0; BLACK=1; # Field[who, kind, FIELDSIZE]; ## 場札 0="A"〜12="K",-1=空き # who(0=人,1=COM) # kind(RED=攻, BLACK=守) # Hands[who, suit, rank] ## 手札 0=未使用,1-5=指定済,9=当り,-1=取られ済 # who(0=人,1=COM) # suit(攻,守 (人:0="H",1="S" / COM:2="D",3="C")) # rank(0="A"〜12="K") ## ゲームメイン do{ setUp(); do{ you(RED); com(RED); display(); judge(); }while( ! allOpen()); result(); }while(decision("Continue")=="y"); exit; } function setUp( who,n,suit,rank) { HIDE=1; ## COMのBLACK='守'を伏せて表示 # '場'をクリア for(who=0; who<=1; ++who) for(n=1; n<=FIELDSIZE; ++n) Field[who,RED,n]=Field[who,BLACK,n]=-1; # '手札'をクリア for(who=0; who<=1; ++who) for(suit=0; suit<4; ++suit) for(rank=0; rank<13; ++rank) Hands[who,suit,rank]=0; # COM用 ## 候補リスト for(rank=0; rank<13; ++rank) TryField[rank,0]=0; you(BLACK); com(BLACK); print "\n"; } ## setUp用表示 function setUpDisplay( i,n) { print "\n----"; dispSpace(13-FIELDSIZE); dispFieldGuide(); dispSpace(13-FIELDSIZE); dispField(YOU, BLACK); print " BLACK 当り (BLACK 取られる) setHands(who, RED, r, 9); remove(another, BLACK, r, n); }else list[cnt++]=r; } for(n=1; n<=FIELDSIZE; ++n){ r=Field[who,RED,n]; if(r!=-1 && getHands(who,RED,r)!=9 && find(list,r)<0) #RED 取られる remove(who, RED, r, n); } } # 勝敗結果表示 function result( y,c, s) { print "\n\n ****** GAME OVER ******"; HIDE=0; ## COMのBLACK='守'をオープン表示 display(); c=noOpen(YOU,BLACK); y=noOpen(COM,BLACK); if(y==0 && c>0) s=" YOU WIN!"; if(y>0 && c==0) s=" YOU LOSE"; if(y==0 && c==0) s=" ...DRAW "; printf("\n%s (SCORE:%05d0)\n\n", s, ((FIELDSIZE-y)*100+c)); } function allOpen() { if(noOpen(YOU,BLACK)==0 || noOpen(COM,BLACK)==0) return 1; return 0; } function noOpen(who,kind, n,q) { q=0; for(n=1; n<=FIELDSIZE; ++n) if(Field[who,kind,n]!=-1) ++q; # 残り有り return q; } ## 場に出す(COM) function com(kind, n,r,v,handList,priority,cnt,i,j,p) { if(kind==RED) printf("\n>>>> COM"); # '場'に残った手札をローテーション delete handList; cnt=0; for(r=0; r<13; ++r){ n=getHands(COM,kind,r); if(1<=n && n<=FIELDSIZE) handList[cnt++]=r; } if(cnt>0){ for(p=0; p>>> YOU"); while(choice(kind)) ; } function choice(kind, z,x,y,w,what,where) { do{ (kind==RED)?display():setUpDisplay(); printf("\n Which?(1a-ke/z) >"); z=input(); if(length(z)>1){ gsub(/,/," ",z); split(z, x); for(y in x){ if(length(x[y])>=2){ what=substr(x[y],1,1); if(isInGuide(what)){ where=substr(x[y],2,1); } else{ what=substr(x[y],2,1); where=substr(x[y],1,1); } put(kind,what,where); } } } }while(z!="z"); if(noOpen(YOU,kind)==FIELDSIZE){ if(decision("Ready")=="y"){ print ""; return 0; } # 入力終了 }else{ printf("\n ...Not Yet"); pushEnter(); } print ""; return 1; # 入力続行 } function isInGuide(what, p) { if(what2Rank(what)>=0) return 1; return 0; } function put(kind,what,where, rank,pos,v) { if((rank=what2Rank(what))<0) return; if((pos=where2Pos(where))<0) return; if(Field[YOU,kind,pos]!=-1 && getHandsBy(pos,YOU,kind)==9) return; v=getHands(YOU,kind,rank); if(0<=v && v<=FIELDSIZE){ # 使える'手札'なら... if(Field[YOU,kind,pos]!=-1) # '場'が空いていなければ空けて... reset(YOU,kind,pos); if(v!=0) Field[YOU,kind,v]=-1; # 使用済みなら一旦戻して... placement(YOU,kind,rank,pos); # '手札'を'場'に置く } } function find(list,val, p) { for(p in list) if(list[p]==val) return p; return -1; } function what2Rank(what, p) { if((p=find(Guide,what))>=0) return p-1; return -1; } function where2Pos(where, n) { if((n=find(NtoA,where))>=0) return n; return -1; } function remove(who,kind,rank,pos) { setHands(who, kind, rank, -1); Field[who,kind,pos]=-1; } function reset(who,kind,pos) { setHandsBy(pos, who, kind, 0); Field[who,kind,pos]=-1; } function placement(who,kind,rank,pos) { setHands(who, kind, rank, pos); Field[who,kind,pos]=rank; } function getHands(who,kind,rank){ return Hands[who,cnvSuit(who,kind),rank]; } function getHandsBy(pos, who,kind){ return getHands(who,kind,Field[who,kind,pos]); } function setHands(who,kind,rank,v){ Hands[who,cnvSuit(who,kind),rank]=v; } function setHandsBy(pos, who,kind,v){ setHands(who,kind,Field[who,kind,pos],v); } function input( tmp){ tmp=""; getline tmp; return tolower(tmp); } function yorn( yn){ do{ yn=input(); }while(yn!="y" && yn!="n"); return yn; } function decision(msg){ printf(" %s?(y/n) >", msg); return yorn(); } function pushEnter( ){ printf(" "); input(); }