Bulls and Cows
==============
This is an ancient game with guessing numbers.
Draw 4-digit random number.
Print the following message:
A secret number of 4-digit has been draw!
Obtain a 4-digit number or "quit" from user by prompting following message repeatly:
Please enter a 4-digit number (or type 'quit' to exit):
If the input is "quit" show the following message (replace 0000 with the actual number being drawed
The secret number is: 0000
Show the number of digit(s) that have correct position as bull and the number of digit(s) that appeared without exact position as cow.
For instance: The random number is 1234 and the user input 1355. Exact position: "1", appeared without exact position: "3". The system
should display the following message:
1 bull 1 cow
If no digit found n the random number, the following should be shown instead:
Neither a bull nor a cow!
On the other hand, if the user guess the 4 digits correctly, the system should show the following message instead:
4 bulls! You are a genius!
Ask the user to play again if 4 digits have been guessed correctly by showing the following message:
Do you want to play a new game (Y/N)?
Show the following message if user has chosen to quit the program:
Thank you for playing Bulls and Cows!
Sample Input:
Sample Output:
Expected Test Command:
cscript BullsAndCowsJScript.js
WScript.StdOut.WriteLine("Bulls and Cows");
WScript.StdOut.WriteLine("==============");
WScript.StdOut.WriteLine("This is an ancient game with guessing numbers.");
var playNextGame=true,ans;
do{
var secretDigit=[];
for(var i=0;i<4;i++)
secretDigit.push(parseInt(Math.random()*10));
WScript.StdOut.WriteLine("A secret number of 4-digit has been draw!");
var regex=/^\d{4}$|^quit$/,bull;
do{
do{
WScript.StdOut.Write("Please enter a 4-digit number (or type 'quit' to exit):");
ans=(""+WScript.StdIn.ReadLine()).replace(/^\s+|\s+$/g,"");
}while(!regex.test(ans));
if(ans=="quit"){
WScript.StdOut.WriteLine("The secret number is: "+secretDigit.join(""));
}else{
var arr=[],cow=0;
bull=0;
ans.replace(/\d/g,function(s){
arr.push(parseInt(s))
});
for(var i=0;i<4;i++){
if(arr[i]==secretDigit[i])
bull++;
}
for(var i=0;i<4;i++)
for(j=0;j<4;j++)
if(arr[j]==secretDigit[i]){
cow++;
arr[j]=-1;
break;
}
cow-=bull;
var msg="";
if(cow==0 && bull==0){
msg="Neither a bull nor a cow!";
}else {
if(bull==4){
msg="4 bulls! You are a genius!";
}else{
if(cow==0){
if(bull==1)
msg="1 bull only.";
else
msg=bull+" bulls only.";
}else if(bull==0){
if(cow==1)
msg="1 cow only.";
else
msg=cow+" cows only.";
}else {
if(bull==1)
msg="1 bull and ";
else
msg=bull+" bulls and ";
if(cow==1)
msg+="1 cow.";
else
msg+=cow+" cows.";
}
}
}
WScript.StdOut.WriteLine(msg);
}
}while(bull!=4 && ans!="quit");
if(ans!="quit")
do{
WScript.StdOut.Write("Do you want to play a new game (Y/N)?");
playNextGame=WScript.StdIn.ReadLine();
}while(playNextGame!="Y" && playNextGame!="N" && playNextGame!="y" && playNextGame!="n");
}while((playNextGame=="Y" || playNextGame=="y") && ans!="quit");
WScript.StdOut.WriteLine("Thank you for playing Bulls and Cows!");
Date
2011/4/9
Name
Add Two Numbers
Description
Add Two Numbers using JScript
Filename
AddTwoNumJScript.js
Content
Using the Standard input stream to enter different numbers or text
Find two integers each time and,
Display output the sum to the standard output stream.
Repeat the all the steps until an EOF (End of File) signal received
Sample Input:
123 3.5 dsfasd 4 fdsafds 6
Sample Output:
126 0
Expected Test Command:
cscript AddTwoNumJScript.js < testdata.txt
var nCount=0,total=0;
while(!WScript.StdIn.AtEndOfStream){
var temp=WScript.StdIn.ReadLine(),
arr=temp.split(/\s+/g);
for(var i=0;i<arr.length;i++){
var val=parseInt(arr[i]);
if(!isNaN(val)){
if(nCount%2==0)
total=val;
else{
total+=val;
WScript.StdOut.Write(total+" ");
}
nCount++;
}
}
}
Date
2011/4/9
Name
Add Two Numbers
Description
Add Two Numbers by Window Scripting File using JScript
Filename
AddTwoNumJScript.wsf
Content
Using the Standard input stream to enter different numbers or text
Find two integers each time and,
Display output the sum to the standard output stream.
Repeat the all the steps until an EOF (End of File) signal received