Hola:
No se la causa de estos errores.
Main.java
Código:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mypkg;
import java.util.Scanner;
import java.util.InputMismatchException;
/**
*
* @author Hunter
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner lector = new Scanner(System.in);
int[] palillos = {7,5,3};
JuegoPalillos juego;
String[] jugador = new String[2];
jugador[0] = "Jugador 1";
jugador[1] = "Jugador 2";
int turno = 0;
int fila;
int cuantos;
juego = new JuegoPalillos(palillos);
do{
System.out.println(juego);
System.out.printf(jugador[turno]+". elige fila");
fila = lector.nextInt();
System.out.printf(jugador[turno]+". ¿cuántos palillos quieres quitar?");
cuantos = lector.nextInt();
if (juego.quitaPalillos(fila.cuantos)){
turno = (turno + 1) % 2;
}else{
System.out.printf("Introduce bien la fila y los palillos");
}
}catch (InputMismatchException e){
System.out.printf("por favor introduce un número.");
lector.next();
}cath (Exception exc){
System.out.printf("Se ha producido algún error " + exc.toString());
}
}while (!juego.finDeJuego());
System.out.println("El ganador ha sido " + jugador[turno]);
}
JuegoPalillos.java
Código:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mypkg;
/**
*
* @author Hunter
*/
public class JuegoPalillos {
private FilaPalillos[] filas;
public JuegoPalillos(int[] palillos){
filas = new FilaPalillos[palillos.length];
for (int i = 0; i < filas.length; i++){
filas[i] = new FilaPalillos(palillos[i]);
}
}
public boolean quitaPalillos(int fila, int cuantos){
if (fila < 0 || fila >= filas.length)
return false;
else
return filas[fila].quitaPalillos(cuantos);
}
public boolean finDeJuego(){
for (int i = 0; i < filas.length; ++i){
if(filas[i].cuantosPalillos() != 0) return false;
}
return true;
}
public String toString(){
String s = "";
for (int i = 0; i < filas.length; i++){
s += i + " " + filas[i] + "\n";
}
return s;
}
}
FilaPalillos.java
Código:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mypkg;
/**
*
* @author Hunter
*/
public class FilaPalillos {
private int numPalillos;
public FilaPalillos(int tamaño){
numPalillos = tamaño;
}
public boolean quitaPalillos(int cuantos){
if (cuantos > numPalillos){
return false;
}else{
numPalillos -= cuantos;
return true;
}
}
public String toString(){
String s = "";
for (int i=0; i < numPalillos; i++){
s += "|";
}
return s;
}
public void añadePalillos(int cuantos){
numPalillos += cuantos;
}
public int cuantosPalillos(){
return numPalillos;
}
}
Quiero solucionar los errores de este programa.