Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
BootcampWebDia1
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Cesar Giulano Gonzalez Maqueda
BootcampWebDia1
Commits
2be5420d
Commit
2be5420d
authored
Oct 20, 2021
by
Cesar Giulano Gonzalez Maqueda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
parents
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
107 additions
and
0 deletions
+107
-0
TorosVacas.java
+107
-0
No files found.
TorosVacas.java
0 → 100644
View file @
2be5420d
import
java.util.Scanner
;
public
class
TorosVacas
{
public
static
void
main
(
String
[]
args
){
play
();
}
private
static
int
readUserInput
(){
/*
* Funcion que lee entrada de usuario y la validad de acuerdo a la especificaciones.
* En caso correcto retorna el numero
* En caso invalido retorna -1
*/
Scanner
in
=
new
Scanner
(
System
.
in
);
String
guess
;
int
intValue
;
guess
=
in
.
nextLine
();
try
{
intValue
=
Integer
.
parseInt
(
guess
);
}
catch
(
NumberFormatException
e
){
System
.
out
.
println
(
"La entrada no es un numero entero\nIntenta de Nuevo"
);
return
-
1
;
}
if
(
guess
.
length
()
!=
4
){
System
.
out
.
println
(
"La entrada debe poseer 4 digitos exactos\nIntente de Nuevo"
);
return
-
1
;
}
for
(
int
i
=
0
;
i
<
4
;
i
++){
char
number
=
guess
.
charAt
(
i
);
if
(
guess
.
indexOf
(
number
,
i
+
1
)
!=
-
1
){
System
.
out
.
println
(
"Existe un numero repetido en su entrada\nIntente de nuevo"
);
return
-
1
;
}
}
return
intValue
;
}
private
static
int
generateTargetNumber
(){
/*
* Funcion que retorna un numero random de 4 digitos para el juego de vaca, toros.
*/
StringBuilder
target
=
new
StringBuilder
();
while
(
target
.
toString
().
length
()
<
4
){
char
randomInt
=
Character
.
forDigit
(((
int
)(
Math
.
random
()*
10
)),
10
);
if
(
target
.
toString
().
indexOf
(
randomInt
)
==
-
1
){
target
.
append
(
randomInt
);
}
}
return
Integer
.
parseInt
(
target
.
toString
());
}
private
static
int
checkUserGuess
(
int
guess
,
int
target
){
/*
* Function que descubre la cantidad de aciertos (Toros y Vacas) del usuario.
* Entrada:
* guess: Integer de 4 digitos.
* target: Integer de 4 digitos.
* Salida:
* 1 si el usuario adivino el numero
* -1 si el usuario no adivino el numero.
* */
String
guess_string
=
String
.
valueOf
(
guess
);
String
target_string
=
String
.
valueOf
(
target
);
int
vacas
=
0
;
int
toros
=
0
;
for
(
int
index_guess
=
0
;
index_guess
<
4
;
index_guess
++){
int
index_target
=
target_string
.
indexOf
(
guess_string
.
charAt
(
index_guess
));
if
(
index_guess
==
index_target
){
toros
++;
}
else
if
(
index_target
!=
-
1
)
{
vacas
++;
}
}
System
.
out
.
println
(
"RESPUESTA A: "
+
toros
+
" TOROS, "
+
vacas
+
" VACAS"
);
if
(
toros
==
4
){
return
1
;
}
else
{
return
-
1
;
}
}
private
static
void
play
(){
/*
* Function que ejectuta el juego de toros y vacas.
* */
int
target_number
=
generateTargetNumber
();
int
guess_number
;
System
.
out
.
println
(
"NUMERO A: "
+
target_number
);
for
(
int
i
=
0
;
i
<
12
;
i
++){
System
.
out
.
print
(
"INTENTO B "
+
i
+
":"
);
guess_number
=
readUserInput
();
if
(
guess_number
==-
1
){
i
--;
continue
;
}
System
.
out
.
println
(
guess_number
);
if
(
checkUserGuess
(
guess_number
,
target_number
)
==
1
){
System
.
out
.
println
(
"FIN DEL JUEGO - GANO B en "
+
(
i
+
1
)
+
" INTENTOS"
);
return
;
}
}
System
.
out
.
println
(
"FIN DEL JUEGO - GANO A"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment