SlideShare a Scribd company logo
1 of 7
To first give you an overall feeling for the finished game, we
first present a dialog to show how the game will be played. The
user is prompted for a "secret" number and w ill be told if it is
invalid. The user is also given the choice of entering a -1 to let
the program generate a secret number. A program generated
secret number is a five digit number stored as a String as in
"01234" or "92745" but not" 1234". "12344", or "
123z7". This number can be generated using the existing well-
tested method public string generate Secret Number (). The
game should then prompt the "game player" to guess the
number The input is error checked only to ensure the user enters
a string of length 5. Entering "what?" will not help at all. but
it should be allowed and should count as a try at the secret
number. If the user enters "123456" or "1234", notify the
user they must enter 5 digits but do not count this as an attempt
at the secret number. The game rules insist that you give the
player some feedback on guesses. Based on that feedback, the
player makes more guesses. Guessing continues until the
"secret'' number is guessed or until the maximum number of
tries (32) is reached. Here is one sample dialog that plays one
game where we, the users, actually input the 'secret' number as
12345. Later on. enter -i to let the real Master Mind experiences
begin. Enter the secret number as a 5 digit number where all
digits are unique, or enter -1 to have this program select the
secret number. Select your own valid secret number or enter -1:
what? 'what?' is not a valid secret number. Select your own
valid secret number or enter -1: 123456 '123456?' is not a
valid secret number. Select your own valid secret number or
enter -1: 12233 '12233' is not a valid secret number. Select
your own valid secret number or enter -1: 12345 Enter your S
digit guess: 11111 Try number: 1 Digits found: 1 Correct
position: 1 Enter your 5 digit guess: 22222 Try number: 2 Digits
found: 1 Correct position: 1 Enter your 5 digit guess: 99999 Try
number: 3 Digits found: 0 Correct position: 0 Enter your 5 digit
guess: 12333 Try number: 4 Digits found: 3 Correct position: 3
Solution
Java Code :
import java.util.Random;
import java.util.Scanner;
public class Game {
String secretNumber = "12345" ;
String userGuess =null;
int tryCount = 0, digitCount = 0, positionCount = 0;
public void digitsFound(){
digitCount=0;
for(int i=0;i < 5;i++){
for(int j=0;j<5;j++){
if(secretNumber.charAt(i) == userGuess.charAt(j)){
digitCount++;
}
}
}
}
public void correctPositions(){
positionCount=0;
for(int j=0;j<5;j++){
if(secretNumber.charAt(j) == userGuess.charAt(j)){
positionCount++;
}
}
}
public static void main(String args[]){
boolean isSecretNumber = false;
Scanner input = new Scanner(System.in);
Game player1 = new Game();
System.out.println("Enter the secret number as a 5 digit
number where all the digits are Unique, or enter -1 to have this
program select secret number.");
while(isSecretNumber == false){
System.out.println("select your own valid secret
number or enter -1");
player1.secretNumber = input.nextLine();
if(player1.secretNumber.equalsIgnoreCase( "-1")){
player1.secretNumber =
player1.generateSecretNumber();
isSecretNumber = true;
}
else{
if(player1.secretNumber.length() != 5){
System.out.println(player1.secretNumber + "? is
not a valid secret Number ");
continue;
}
if(!player1.secretNumber.matches( "d+")){
//regular expression to check input is numeric string.
System.out.println(player1.secretNumber + "? is
not a valid secret Number ");
continue;
}
for(int i=0;i<5;i++){
for(int j=i+1;j<5;j++){
if(player1.secretNumber.charAt(i)==
player1.secretNumber.charAt(j)){
System.out.println(player1.secretNumber +
"? is not a valid secret Number");
continue;
}
}
}
isSecretNumber = true;
}
}
while(player1.positionCount!=5 || player1.tryCount > 32){
System.out.print("Enter the 5 digit guess :");
player1.userGuess = input.nextLine();
if(player1.userGuess.length() != 5){
System.out.println(player1.secretNumber + "? is not a
valid guess Number");
continue;
}
player1.tryCount++;
player1.digitsFound();
player1.correctPositions();
System.out.println("Try Number : "+
player1.tryCount+" Digit Found : "+ player1.digitCount+ "
Current Position : " + player1.positionCount+" ");
}
if(player1.tryCount > 32){
System.out.println("Game Over , maximum guess
reached(32)");
}
if(player1.positionCount == 5 && player1.tryCount
<=32){
System.out.println("Game Won ! congratulations...");
}
}
private String generateSecretNumber() {
Random num = new Random();
int digit1,digit2,digit3,digit4,digit5;
digit1 = num.nextInt(9)+1;
do{
digit2 = num.nextInt(10);
}while(digit2==digit1);
do{
digit3 = num.nextInt(10);
}while(digit3 == digit1 || digit3 == digit2);
do{
digit4 = num.nextInt(10);
}while(digit4 == digit1 || digit4 == digit2 || digit4 ==
digit3);
do{
digit5 = num.nextInt(10);
}while(digit5 == digit1 || digit5 == digit2 || digit5 ==
digit3 || digit5 == digit4);
return
(digit1*10000+digit2*1000+digit3*100+digit4*10+digit5)+ "";
// int to string
}
}

More Related Content

More from Komlin1

Theory and Research Related to Social Issue By now, you have had t.docx
Theory and Research Related to Social Issue By now, you have had t.docxTheory and Research Related to Social Issue By now, you have had t.docx
Theory and Research Related to Social Issue By now, you have had t.docx
Komlin1
 
Thematic Issues Globalization; Islam & the West.docx
Thematic Issues Globalization; Islam & the West.docxThematic Issues Globalization; Islam & the West.docx
Thematic Issues Globalization; Islam & the West.docx
Komlin1
 
The Value of Research in Social PolicyWhile research can be intere.docx
The Value of Research in Social PolicyWhile research can be intere.docxThe Value of Research in Social PolicyWhile research can be intere.docx
The Value of Research in Social PolicyWhile research can be intere.docx
Komlin1
 
The United States’ foreign policy until the end of the nineteenth ce.docx
The United States’ foreign policy until the end of the nineteenth ce.docxThe United States’ foreign policy until the end of the nineteenth ce.docx
The United States’ foreign policy until the end of the nineteenth ce.docx
Komlin1
 

More from Komlin1 (20)

Theodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docx
Theodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docxTheodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docx
Theodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docx
 
Theory and Research Related to Social Issue By now, you have had t.docx
Theory and Research Related to Social Issue By now, you have had t.docxTheory and Research Related to Social Issue By now, you have had t.docx
Theory and Research Related to Social Issue By now, you have had t.docx
 
Theory and the White-Collar OffenderOur previous week’s discussion.docx
Theory and the White-Collar OffenderOur previous week’s discussion.docxTheory and the White-Collar OffenderOur previous week’s discussion.docx
Theory and the White-Collar OffenderOur previous week’s discussion.docx
 
There are 2 questions part A and B. All questions and relevant att.docx
There are 2 questions part A and B. All questions and relevant att.docxThere are 2 questions part A and B. All questions and relevant att.docx
There are 2 questions part A and B. All questions and relevant att.docx
 
There are 2 discussions Topic 1 & Topic 2 (They both require refere.docx
There are 2 discussions Topic 1 & Topic 2 (They both require refere.docxThere are 2 discussions Topic 1 & Topic 2 (They both require refere.docx
There are 2 discussions Topic 1 & Topic 2 (They both require refere.docx
 
Theoretical PerspectiveIdentify at least one human developme.docx
Theoretical PerspectiveIdentify at least one human developme.docxTheoretical PerspectiveIdentify at least one human developme.docx
Theoretical PerspectiveIdentify at least one human developme.docx
 
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docx
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docxTHEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docx
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docx
 
Theories of Behavior TimelineComplete the following tabl.docx
Theories of Behavior TimelineComplete the following tabl.docxTheories of Behavior TimelineComplete the following tabl.docx
Theories of Behavior TimelineComplete the following tabl.docx
 
Thematic Issues Globalization; Islam & the West.docx
Thematic Issues Globalization; Islam & the West.docxThematic Issues Globalization; Islam & the West.docx
Thematic Issues Globalization; Islam & the West.docx
 
The written portion of the research paper should be 9-11 pages in le.docx
The written portion of the research paper should be 9-11 pages in le.docxThe written portion of the research paper should be 9-11 pages in le.docx
The written portion of the research paper should be 9-11 pages in le.docx
 
The World since 1945Country Report- SAUDI ARABIA     Histo.docx
The World since 1945Country Report- SAUDI ARABIA     Histo.docxThe World since 1945Country Report- SAUDI ARABIA     Histo.docx
The World since 1945Country Report- SAUDI ARABIA     Histo.docx
 
The world runs on Big Data.  Traditionally, Data has been expressed .docx
The world runs on Big Data.  Traditionally, Data has been expressed .docxThe world runs on Big Data.  Traditionally, Data has been expressed .docx
The world runs on Big Data.  Traditionally, Data has been expressed .docx
 
the    1.The collaborative planning Methodology is the f.docx
the    1.The collaborative planning Methodology is the f.docxthe    1.The collaborative planning Methodology is the f.docx
the    1.The collaborative planning Methodology is the f.docx
 
The word stereotype originally referred to a method used by printers.docx
The word stereotype originally referred to a method used by printers.docxThe word stereotype originally referred to a method used by printers.docx
The word stereotype originally referred to a method used by printers.docx
 
The Value of Critical Thinking  Please respond to the followin.docx
The Value of Critical Thinking  Please respond to the followin.docxThe Value of Critical Thinking  Please respond to the followin.docx
The Value of Critical Thinking  Please respond to the followin.docx
 
The Value Chain Concept Please respond to the following·.docx
The Value Chain Concept Please respond to the following·.docxThe Value Chain Concept Please respond to the following·.docx
The Value Chain Concept Please respond to the following·.docx
 
The wealth and energy between 1880 and 1910 was a unique and dynamic.docx
The wealth and energy between 1880 and 1910 was a unique and dynamic.docxThe wealth and energy between 1880 and 1910 was a unique and dynamic.docx
The wealth and energy between 1880 and 1910 was a unique and dynamic.docx
 
The Value of Research in Social PolicyWhile research can be intere.docx
The Value of Research in Social PolicyWhile research can be intere.docxThe Value of Research in Social PolicyWhile research can be intere.docx
The Value of Research in Social PolicyWhile research can be intere.docx
 
The United States’ foreign policy until the end of the nineteenth ce.docx
The United States’ foreign policy until the end of the nineteenth ce.docxThe United States’ foreign policy until the end of the nineteenth ce.docx
The United States’ foreign policy until the end of the nineteenth ce.docx
 
The Value Chain Concept Please respond to the followingDescribe.docx
The Value Chain Concept Please respond to the followingDescribe.docxThe Value Chain Concept Please respond to the followingDescribe.docx
The Value Chain Concept Please respond to the followingDescribe.docx
 

Recently uploaded

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
Poster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdfPoster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdf
Alexander Litvinenko
 
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
Krashi Coaching
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
Poster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdfPoster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdf
 
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
 
The Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptxThe Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptx
 
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024
 

To first give you an overall feeling for the finished game, we first .docx

  • 1. To first give you an overall feeling for the finished game, we first present a dialog to show how the game will be played. The user is prompted for a "secret" number and w ill be told if it is invalid. The user is also given the choice of entering a -1 to let the program generate a secret number. A program generated secret number is a five digit number stored as a String as in "01234" or "92745" but not" 1234". "12344", or " 123z7". This number can be generated using the existing well- tested method public string generate Secret Number (). The game should then prompt the "game player" to guess the number The input is error checked only to ensure the user enters a string of length 5. Entering "what?" will not help at all. but it should be allowed and should count as a try at the secret number. If the user enters "123456" or "1234", notify the user they must enter 5 digits but do not count this as an attempt at the secret number. The game rules insist that you give the player some feedback on guesses. Based on that feedback, the player makes more guesses. Guessing continues until the "secret'' number is guessed or until the maximum number of tries (32) is reached. Here is one sample dialog that plays one game where we, the users, actually input the 'secret' number as 12345. Later on. enter -i to let the real Master Mind experiences begin. Enter the secret number as a 5 digit number where all digits are unique, or enter -1 to have this program select the secret number. Select your own valid secret number or enter -1: what? 'what?' is not a valid secret number. Select your own valid secret number or enter -1: 123456 '123456?' is not a valid secret number. Select your own valid secret number or enter -1: 12233 '12233' is not a valid secret number. Select your own valid secret number or enter -1: 12345 Enter your S digit guess: 11111 Try number: 1 Digits found: 1 Correct position: 1 Enter your 5 digit guess: 22222 Try number: 2 Digits found: 1 Correct position: 1 Enter your 5 digit guess: 99999 Try number: 3 Digits found: 0 Correct position: 0 Enter your 5 digit guess: 12333 Try number: 4 Digits found: 3 Correct position: 3
  • 2. Solution Java Code : import java.util.Random; import java.util.Scanner; public class Game { String secretNumber = "12345" ; String userGuess =null; int tryCount = 0, digitCount = 0, positionCount = 0; public void digitsFound(){ digitCount=0; for(int i=0;i < 5;i++){ for(int j=0;j<5;j++){ if(secretNumber.charAt(i) == userGuess.charAt(j)){ digitCount++; } } }
  • 3. } public void correctPositions(){ positionCount=0; for(int j=0;j<5;j++){ if(secretNumber.charAt(j) == userGuess.charAt(j)){ positionCount++; } } } public static void main(String args[]){ boolean isSecretNumber = false; Scanner input = new Scanner(System.in); Game player1 = new Game(); System.out.println("Enter the secret number as a 5 digit number where all the digits are Unique, or enter -1 to have this program select secret number."); while(isSecretNumber == false){ System.out.println("select your own valid secret number or enter -1"); player1.secretNumber = input.nextLine();
  • 4. if(player1.secretNumber.equalsIgnoreCase( "-1")){ player1.secretNumber = player1.generateSecretNumber(); isSecretNumber = true; } else{ if(player1.secretNumber.length() != 5){ System.out.println(player1.secretNumber + "? is not a valid secret Number "); continue; } if(!player1.secretNumber.matches( "d+")){ //regular expression to check input is numeric string. System.out.println(player1.secretNumber + "? is not a valid secret Number "); continue; } for(int i=0;i<5;i++){ for(int j=i+1;j<5;j++){ if(player1.secretNumber.charAt(i)== player1.secretNumber.charAt(j)){ System.out.println(player1.secretNumber +
  • 5. "? is not a valid secret Number"); continue; } } } isSecretNumber = true; } } while(player1.positionCount!=5 || player1.tryCount > 32){ System.out.print("Enter the 5 digit guess :"); player1.userGuess = input.nextLine(); if(player1.userGuess.length() != 5){ System.out.println(player1.secretNumber + "? is not a valid guess Number"); continue; } player1.tryCount++; player1.digitsFound(); player1.correctPositions(); System.out.println("Try Number : "+ player1.tryCount+" Digit Found : "+ player1.digitCount+ " Current Position : " + player1.positionCount+" "); }
  • 6. if(player1.tryCount > 32){ System.out.println("Game Over , maximum guess reached(32)"); } if(player1.positionCount == 5 && player1.tryCount <=32){ System.out.println("Game Won ! congratulations..."); } } private String generateSecretNumber() { Random num = new Random(); int digit1,digit2,digit3,digit4,digit5; digit1 = num.nextInt(9)+1; do{ digit2 = num.nextInt(10); }while(digit2==digit1); do{ digit3 = num.nextInt(10); }while(digit3 == digit1 || digit3 == digit2);
  • 7. do{ digit4 = num.nextInt(10); }while(digit4 == digit1 || digit4 == digit2 || digit4 == digit3); do{ digit5 = num.nextInt(10); }while(digit5 == digit1 || digit5 == digit2 || digit5 == digit3 || digit5 == digit4); return (digit1*10000+digit2*1000+digit3*100+digit4*10+digit5)+ ""; // int to string } }