HOLAS! vereis, estoy practicando y he intentado hacer el juego del master mind, se trata de descubrir los numeros, os pego el codigo y os digo lo que no consigo hacer funcionar, es a la hora de guardar los resultados en un .txt con el nombre y la puntuacion
Código:
package mastermind;
import java.util.Scanner;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Main {
private static boolean verificarnumrepetidos(int valor,int array[] ){
boolean sortir = false;
int i = 0;
while (sortir == false && i < array.length){
if (array[i] == valor){
sortir = true;
} else {
i++;
}
}
return sortir;
}
private static void comparararrays(int arrayusuari[], int arraycombinacio[],int arraycerosyuns[]) {
int r = 0;
while (r < 5){
int t = 0;
while(t < 5){
if (arrayusuari[r] == arraycombinacio[t]) {
if (r == t){
arraycerosyuns[r] = 1;
}
else {
arraycerosyuns[r] = 0;
}
t = 5;
}
else{
arraycerosyuns[r] = 9;
}
t++;
}
r++;
}
}
private static void numerosecreto(int array[]) {
//CREAR EL MALDITO NUMERO ALEATORIO
int j = 0 ;
int valor = 0;
while (j < array.length){
valor = new Double(Math.random()*10).intValue() ;
if (valor >=2 && verificarnumrepetidos(valor,array) == false){
array[j] = valor;
j++;
}
}
}
private static void posarnumeros(int array[]) {
// EL USUARIO METE SUS 5 NUMEROS Y QUE SEAN DIFERENTES
int intentos = 0;
int numero = 0;
while (intentos < 5){
if (intentos == 0){
array [0] = numero ;
}
Scanner leer=new Scanner(System.in);
System.out.println("Introduce el numero");
numero = leer.nextInt();
//comprovamos que no es igual y que no es ni 1 ni 0
int zona = 0;
while (zona < 5) {
if (numero != array[zona] && numero != 0 && numero != 1) {
}
else {
System.out.println("El numero que has introducido no es correcto, "+numero+", recuerda que no puede ser igual que uno anterior, tampoco puede ser ni 0 ni 1");
numero = leer.nextInt();
}
zona++;
//acaba la comprovacion y hacemos la array con los 5 numeros
}
array [intentos] = numero ;
intentos ++;
}
}
public static void main(String[] args) {
//array maquina
int array_combinacio []= new int [5];
numerosecreto(array_combinacio);
System.out.println("el numero secreto era "+array_combinacio[0]+array_combinacio[1]+array_combinacio[2]+array_combinacio[3]+array_combinacio[4]);
int puntuacion = 11;
// array usuari
int a = 1;
while (a < 10) {
int arrayusuari []= new int [5];
posarnumeros(arrayusuari);
System.out.println("la "+a+"ºconbinacion es "+arrayusuari[0]+"-"+arrayusuari[1]+"-"+arrayusuari[2]+"-"+arrayusuari[3]+"-"+arrayusuari[4]);
// Comparacion de numeros
int arraycerosyuns []= new int [5];
comparararrays (arrayusuari,array_combinacio, arraycerosyuns);
System.out.println(" _____________________________________________");
System.out.println("| |");
System.out.println("| >"+arraycerosyuns[0]+"-"+arraycerosyuns[1]+"-"+arraycerosyuns[2]+"-"+arraycerosyuns[3]+"-"+arraycerosyuns[4]+"< |");
System.out.println("| 1 esta en su sitio, 0 esta, 9 no existe |");
System.out.println("|_____________________________________________|");
puntuacion --;
a++;
int q = 0;
while (q < 5) {
if (arrayusuari[q] == array_combinacio[q]){
a = 11;
}
else{
q = 5;
}
q++;
}
}
// puntuacion en un fitxer
Scanner leer=new Scanner(System.in);
System.out.println("Introduce el nuevo Nombre");
String nombre = leer.nextLine();
System.out.println(nombre+" tu puntuacion es: "+puntuacion);
// ESTO NO VA
try {
FileWriter fw = new FileWriter("puntuacion.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.newLine();
bw.write(nombre " tiene "+puntuacion);
bw.flush();
bw.close();
fw.close();
}
}
}
Saludos y gracias