I need some help with my code for my bulls and cows game... the requirements for the program ar below
To start your gaming career, you have decided to write a math game. You want to create the following Menu:
Bulls and CowsBulls and Cows Game is also known as MasterMind. Computer selects a four digit number 0 - 9, all four digits are different. In current implementation number may not begin with 0.
"Exist" column displays total number of digits you guessed right - "Cow", "Match" shows how many of those that exists was placed at the right spots - "Bull".
Play by entering your guess and then validate your entry.
Player: Rider Storm
Guess Exist Match
3412 3 1
3721 3 3
Keep track of the player and how many tries it takes to win In 2, single dim arrays, 1 string and 1 int. When the players are finished; append the ââ?¬Å?best playerââ?¬Â? and ââ?¬Å?scoreââ?¬Â? to the text file, ââ?¬Å?winners.txtââ?¬Â?.
The ââ?¬Å?Display Scoresââ?¬Â? menu should display all the top scores already in the ââ?¬Å?winners.txtââ?¬Â? file and also the ones added during the play. You will need to determine who has the best and worse score of the groups so you can display a message in the report like below. It should look something like:
Math Wizards: Guser Loop 76 ââ?¬â?? you have the worst score. It is time to Kick you out of the club.
heres the code I have so far for that:
import java.util.*;
public class bullGame {
public static void main (String[] args) {
int rnd = chooseRandomNumber();
System.out.println("I have chosen a four-digit number. Can you guess what it is?");
int guesses = 0;
int yourGuess = 0;
Scanner scan = new Scanner(System.in);
do {
cows = 0;
bulls = 0;
System.out.print( "Guess " + ++guesses + ": ");
yourGuess = scan.nextInt();
calcCowsAndBulls( rnd, yourGuess);
System.out.print( "Score: "+cows);
System.out.print( cows==1 ? " cow" : " cows");
System.out.print(" and "+bulls);
System.out.println( bulls==1 ? " bull." : " bulls.");
}
while (bulls<4 && guesses<25);
if (bulls==4) {
System.out.println("Congratulations� You got it in "+ guesses+ " guesses.");
}
else {
System.out.println("Unlucky! The answer was "+ rnd);
}
}
public static void calcCowsAndBulls(int rnd, int yourGuess) {
String str1 = ""+ rnd;
String str2 = ""+ yourGuess;
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++) {
if (str1.charAt(i)==str2.charAt(j)) {
if (i==j) {
bulls++;
}
else {
cows++;
}
}
}}}
public static int chooseRandomNumber() {
Random ran = new Random();
int r = 1000 + ran.nextInt(9000);
return r;
}
static int cows;
static int bulls;
}
then after there a second part to the assignment:
Since you have written the bulls and cows game, your coding knowledge has come a long way and you want to update your system. Add the following options to your menu
Bulls and CowsYou now want to start your array with the existing data in the ââ?¬Å?score.txtââ?¬Â? file. You now feel comfortable working with 2-dim arrays so you want to modify your existing program containing 2, single dims into one 2-dim array.
You will still keep track of the player and how many tries it takes to win in the new array. You also realize that your file may have saved one player multiple times.
You need to modify your program so that you are storing unique plays. You want to make sure your system keeps its modular design.
any help would be greatly appreciated...
Here is an example of Bulls and Cows game.
import java.util.Random; import java.util.Scanner; import java.util.regex.Pattern; class BullsAndCows{ public static void main(String args[]){ int i,j,bull=0,cow=0; int[] secretDigit,arr; Scanner scan=new Scanner(System.in); String msg, ans, playNextGame; secretDigit=new int[4]; arr=new int[4]; System.out.println("Bulls and Cows"); System.out.println("This is an ancient game with guessing numbers."); Random rand=new Random(); Pattern regex=Pattern.compile("^\\d{4}$|^quit$"); do{ String secret=""; playNextGame=""; for(i=0;i<4;i++){ secretDigit[i]=rand.nextInt(10); secret+=secretDigit[i]; } System.out.println("A secret number of 4-digit has been draw!"); do{ do{ System.out.print("Please enter a 4-digit number (or type 'quit' to exit):"); ans=scan.nextLine().trim(); System.out.println(ans); }while(!regex.matcher(ans).find()); if(ans.equals("quit")){ System.out.println("The secret number is: " + secret); }else{ cow=0; bull=0; for(i=0;i<4;i++) try{ arr[i]=Integer.parseInt(ans.substring(i,i+1)); if(arr[i]==secretDigit[i]) bull++; }catch(NumberFormatException nfe){} for(i=0;i<4;i++) for(j=0;j<4;j++) if(arr[j]==secretDigit[i]){ cow++; arr[j]=-1; break; } cow-=bull; 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."; } } } System.out.println(msg); } }while(bull!=4 && !ans.equals("quit")); if(!ans.equals("quit")){ do{ System.out.print("Do you want to play a new game (Y/N)?"); playNextGame=scan.nextLine().trim(); System.out.println(playNextGame); }while(!playNextGame.toUpperCase().equals("Y") && !playNextGame.toUpperCase().equals("N")); } } while (playNextGame.toUpperCase().equals("Y") && !ans.equals("quit")); System.out.println("Thank you for playing Bulls and Cows!"); } }
Ads