Monton 1 should be worth 3
Result by console
Program
package Nim;
import java.util.*;
public class Nim {
Scanner sc = new Scanner(System.in);
private int monton1;
private int monton2;
private int monton3;
public static int elegir;
//CONSTRUCTOR
Nim(){
monton1 = (int) (Math.random()*(6-3)+3);
monton2 = (int) (Math.random()*(6-3)+3);
monton3 = (int) (Math.random()*(6-3)+3);
}
//METODOS GET AND SET
public int getMonton1() {
return monton1;
}
public void setMonton1(int monton1) {
this.monton1 = monton1;
}
public int getMonton2() {
return monton2;
}
public void setMonton2(int monton2) {
this.monton2 = monton2;
}
public int getMonton3() {
return monton3;
}
public void setMonton3(int monton3) {
this.monton3 = monton3;
}
//METODOS
public void elegirMonton() {
System.out.println("Elegir un monton: ");
System.out.println("(1) Monton 1 que tiene: " + getMonton1());
System.out.println("(2) Monton 2 que tiene: " + getMonton2());
System.out.println("(3) Monton 3 que tiene: " + getMonton3());
elegir = sc.nextInt();
if(elegir == 1) {
elegir = monton1;
}else if(elegir == 2){
elegir = monton2;
}else {
elegir = monton3;
}
comprobarMonton(elegir);
}
public int comprobarMonton(int elegir) {
if(elegir<=0) {
System.out.println("Error: ese montón ya no tiene palillos");
elegirMonton();
}
return elegir;
}
public void elegirPalillos(){
System.out.println("Cuantos paillos quieres retirar, 1 o 2: ");
int palillos = sc.nextInt();
comprobarPalillos(palillos);
}
public int comprobarPalillos(int palillos) {
if(palillos > elegir) {
System.out.println("Error: ese montón no tiene tantos palillos");
elegirPalillos();
}
return elegir - palillos;
}
public void comprobarFinJuego() {
do {
elegirMonton();
elegirPalillos();
}while(monton1 + monton2 + monton3 != 0);
}
}