Why is not subtraction applied to a variable?

0

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);
}
}
    
asked by Vampy95 29.11.2017 в 19:01
source

2 answers

1

You should change the comprobarPalillos method, it could be of type void since it does not matter which value you return, only the operation you perform and what variable you assign:

public void comprobarPalillos(int palillos) {
    if(palillos > elegir) {
        System.out.println("Error: ese montón no tiene tantos palillos");
        elegirPalillos();
    }
    if (elegir == 1 ){
      monton1 =  monton1- palillos;
    } else  if (elegir == 2 ){
      monton2 =  monton2- palillos;
    } else {
      monton3 =  monton3- palillos;
    }
    //también puedes usar switch en lugar de estos if

    //Codigo que he tenido que cambiar para que me funcione
    if (elegir == monton1 ){
      monton1 =  monton1- palillos;
    } else  if (elegir == monton2 ){
      monton2 =  monton2- palillos;
    } else {
      monton3 =  monton3- palillos;
    }
}
    
answered by 29.11.2017 в 19:14
0

Because you never equal it, you would have to have a setter and equal it

public boolean comprobarPalillos(int palillos) {
    if(palillos > elegir) {
        System.out.println("Error: ese montón no tiene tantos palillos");
        return false
    }
    switch(elegir) {
       case 1: setMonton1(getMonton1() - palillos); break;
       case 2: setMonton2(getMonton2() - palillos); break;
       case 3: setMonton3(getMonton3() - palillos); break;
   }
   return true
}

This part checks the chosen heap and applies the set to the heap. now the retun boolean is going to serve you because in case of being false you will have to execute chopsticks again, this would be better outside the method, since in case of being inside when the chopsticks function finishes, it will return to where it was in checking sticks

now where you send it to call you have this

public void elegirPalillos(){
    System.out.println("Cuantos paillos quieres retirar, 1 o 2: ");
    int palillos = sc.nextInt();
    if(!comprobarPalillos(palillos)){
      elegirPalillos();
    }
}

Although in theory this would not be necessary since you are doing it in the while, so it might look like this

public void elegirPalillos(){
        System.out.println("Cuantos paillos quieres retirar, 1 o 2: ");
        int palillos = sc.nextInt();
        comprobarPalillos(palillos);

}
    
answered by 29.11.2017 в 19:25