Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
Java-e002-Nim
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
Yovan Martinez
Java-e002-Nim
Commits
3b9ca677
Commit
3b9ca677
authored
Apr 22, 2022
by
Yovan Martinez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Primer commit.Terminado juego Nim
parent
57524bf4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
130 additions
and
0 deletions
+130
-0
.gitignore
+2
-0
Nim.java
+128
-0
No files found.
.gitignore
0 → 100644
View file @
3b9ca677
*.txt
*.class
Nim.java
0 → 100644
View file @
3b9ca677
import
java.util.Scanner
;
public
class
Nim
{
int
pila1
;
int
pila2
;
int
pila3
;
int
turnoJugador
;
int
cantidadFichas
;
public
Nim
(){
pila1
=
3
;
pila2
=
4
;
pila3
=
5
;
setTurnoJugador
(
1
);
}
public
int
getTurnoJugador
(){
return
turnoJugador
;
}
public
void
setTurnoJugador
(
int
valor
){
turnoJugador
=
valor
;
}
public
int
getPila
(
String
pila
){
int
devolvedor
=
0
;
switch
(
pila
)
{
case
"A"
:
devolvedor
=
pila1
;
break
;
case
"B"
:
devolvedor
=
pila2
;
break
;
case
"C"
:
devolvedor
=
pila3
;
break
;
default
:
devolvedor
=
0
;
break
;
}
return
devolvedor
;
}
//Remueve las fichas de las pilas
public
void
quitarFichas
(
String
pile
,
int
number
){
switch
(
pile
)
{
case
"A"
:
pila1
=
pila1
-
number
;
break
;
case
"B"
:
pila2
=
pila2
-
number
;
break
;
case
"C"
:
pila3
=
pila3
-
number
;
break
;
default
:
break
;
}
}
//Controla si sobran fichas para continuar con la partida
public
boolean
controlPartida
(){
int
contador
=
0
;
if
(
pila1
>
0
||
pila2
>
0
||
pila3
>
0
)
{
return
true
;
}
else
{
return
false
;
}
}
//Se muestra el tablero en pantalla
public
void
mostrarTablero
(){
System
.
out
.
println
(
"A: "
+
pila1
+
" B: "
+
pila2
+
" C: "
+
pila3
);
}
//Se cambia de turno en la jugada
public
void
cambioDeTurnos
()
{
if
(
getTurnoJugador
()
==
1
)
{
setTurnoJugador
(
2
);
}
else
{
setTurnoJugador
(
1
);
}
}
public
static
void
main
(
String
[]
args
)
{
String
lector
=
""
;
int
fichas
=
0
;
boolean
check
=
false
;
Nim
partida
=
new
Nim
();
Scanner
in
=
new
Scanner
(
System
.
in
);
//Se muestra el tablero
partida
.
mostrarTablero
();
while
(
partida
.
controlPartida
()){
System
.
out
.
println
(
"Turno jugador"
+
partida
.
getTurnoJugador
()
+
" :"
);
//El jugador elige la columna(Se previene el ingreso de datos invalidos)
while
(!
check
){
System
.
out
.
print
(
"Elija una columna: "
);
lector
=
System
.
console
().
readLine
();
lector
=
lector
.
toUpperCase
();
if
((
lector
.
equals
(
"A"
)
||
lector
.
equals
(
"B"
)
||
lector
.
equals
(
"C"
))
&&
partida
.
getPila
(
lector
)
>
0
){
check
=
true
;
}
else
{
System
.
out
.
println
(
"Columna invalida, por favor intente de nuevo"
);
check
=
false
;
}
// if (par) {
// }
}
//El jugador elige la cantidad de fichas a retirar de la columna seleccionada(Se previene el ingreso de datos invalidos)
while
(
check
){
System
.
out
.
print
(
"Elija la cantidad de fichas a mover: "
);
fichas
=
Integer
.
parseInt
(
System
.
console
().
readLine
());
if
(
fichas
>
partida
.
getPila
(
lector
)){
System
.
out
.
println
(
"Cantidad invalida, por favor intente de nuevo"
);
check
=
true
;
}
else
{
check
=
false
;
};
}
partida
.
quitarFichas
(
lector
,
fichas
);
partida
.
mostrarTablero
();
partida
.
controlPartida
();
partida
.
cambioDeTurnos
();
}
System
.
out
.
println
(
"Fin de la partida"
);
System
.
out
.
println
(
"Gana = jugador"
+
partida
.
getTurnoJugador
());
partida
.
cambioDeTurnos
();
System
.
out
.
println
(
"Pierde = Jugador"
+
partida
.
getTurnoJugador
());
}
}
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