いにしえの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)");
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX