いにしえのHit&Blow from Node.js

rem:/* @echo off :【Hit&Brow For Node.js】 : 本プログラムを動 […]

rem:/* @echo off :【Hit&Brow For Node.js】 : 本プログラムを動 […]

  • タグ:
  • タグはありません
rem:/*
@echo off
:【Hit&Brow For Node.js】
: 本プログラムを動作させるには、
: Node.jsがインストールされている必要があります。
cls
node "%~f0" %*
@goto :EOF */

process.stdin.resume();
process.stdin.setEncoding("utf8");

(function main(){
	var active="head";
	var max,hit,blow,miss,count;
	var ans,anss=[];
	var user,users=[];
	console.log("【Hit&Blow】");
	head();
	process.stdin.on("data",function(data){
		data=data.replace(/\r\n$/,"");
		switch(active){
			case "head":
				console.log("");
				max=Number(data);
				ans="";
				count=0;
				if(isNaN(max)){
					console.log("数字で入力して下さい。");
					head();
					return;
				}
				if(10<max){
					console.log("桁数が大きすぎます。");
					head();
					return;
				}
				if(max<1){
					console.log("桁数が小さすぎます。");
					head();
					return;
				}
				for(var i=0;i<max;i++){
					rnds:while(1){
					anss[i]=Math.floor(Math.random()*10);
					for(var n=0;n<i;n++){
						if(anss[i]==anss[n]){
							continue rnds;
						}
					}
					break rnds;}
					ans+=anss[i];
				}
				b_head(max);
				active="body";
			break;
			
			case "body":
				hit=0;
				blow=0;
				miss=max;
				if(data.toLowerCase()=="z"){
					console.log("Give up...");
					console.log("Answer:"+ans);
					console.log("\nもう一回?(Y/N)");
					active="regame";
					return;
				}
				user=data;
				users=user.split("");
				if(users.length!=max){
					console.log("桁数が不当です。");
					b_head(max);
					return;
				}
				for(var i=0;i<max;i++){
					for(var n=0;n<max;n++){
						if(i!=n && users[i]==users[n]){
							console.log("各桁を異なる値にしてください。");
							b_head(max);
							return;
						}
					}
					if(isNaN(users[i])){
						console.log("数字のみを入力して下さい。");
						b_head(max);
						return;
					}
				}
				count++;
				for(var i in users){
					if(anss[i]==users[i]){
						hit++;
						miss--;
					}
					else{
						for(var n in users){
							if(anss[i]==users[n]){
								blow++;
								miss--;
							}
						}
					}
				}
				console.log("-> "+count+":"+hit+"hit "+blow+"blow "+miss+"miss");
				if(hit==max){
					console.log("All hit!\nCongratulation!!\n");
					console.log("もう一回?(Y/N)");
					active="regame";
					return;
				}
			break;
			
			case "regame":
				console.log("");
				var end=data;
				if(end.toLowerCase()=="n"){
					console.log("Good game playing!");
					active="exit";
					return;
				}
				head();
				active="head"
			break;
			
			case "exit":
				process.exit();
		}
	});
})();

function head(){
	console.log("桁数を入力してね。");
}

function b_head(max){
	console.log(max+"桁の数字を入力してね。(zでギブアップ)");
}