SlideShare une entreprise Scribd logo
1  sur  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
}
}

Contenu connexe

Plus de Komlin1

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.docxKomlin1
 
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.docxKomlin1
 
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.docxKomlin1
 
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.docxKomlin1
 
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.docxKomlin1
 
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.docxKomlin1
 
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docx
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docxTHEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docx
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docxKomlin1
 
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.docxKomlin1
 
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.docxKomlin1
 
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.docxKomlin1
 
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.docxKomlin1
 
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 .docxKomlin1
 
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.docxKomlin1
 
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.docxKomlin1
 
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.docxKomlin1
 
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·.docxKomlin1
 
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.docxKomlin1
 
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.docxKomlin1
 
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.docxKomlin1
 
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.docxKomlin1
 

Plus de 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
 

Dernier

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 

Dernier (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

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 } }