Codi simple Java 2 - Skel Terrario

per Jordi Farrero darrera modificació 2020-03-25T14:52:33+01:00

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class main {

private final static int ymax=10;
private final static int xmax=10;

private static int[][] terrario=new int[xmax][ymax];
private static int[][] terrario_temp=new int[xmax][ymax];

private static Random oRand =new Random();

public static void main(String[] args) {

inicializa_terrario();
inicializa_semillas();
// Bucle de juego
for(int mloop=0;mloop<5;mloop++){
dibuja_terrario();
evalua_terrario();



}

}

public static void inicializa_terrario(){
for(int y=0;y<ymax;y++){
for(int x=0;x<xmax;x++){
terrario[x][y]=0;
terrario_temp[x][y]=0;
}
}
}

public static void dibuja_terrario(){
for(int y=0;y<ymax;y++){
for(int x=0;x<xmax;x++){
switch(terrario[x][y])
{
case 0:
System.out.print("_");
break;
// Semilla pequeña
case 1:
System.out.print("v");
break;
// Semilla grande (reproduccion)
case 2:
System.out.print("V");
break;
// Semilla muerta
case 3:
System.out.print("X");
break;

default:
break;

}

}
System.out.println("");
}
System.out.println("------------------------------------");
}

public static void inicializa_semillas(){
for(int s=0;s<3;s++){
terrario[oRand.nextInt(5)+3][oRand.nextInt(5)+3]=1;
}
}

public static void evalua_terrario(){
for(int y=0;y<ymax;y++){
for(int x=0;x<xmax;x++){
switch(terrario[x][y]){
// Semilla pequeña
case 1:
terrario[x][y]++;
break;
// Semilla grande (reproduccion)
case 2:
terrario[x][y]++;
break;
// Semilla muerta
case 3:
terrario[x][y]=0;
break;

default:
break;

}


}
}
}
/*
int a = 0;
String inputa;


int[] myarr=new int[3];
myarr[1]=3;


BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter a number: ");
try
{
inputa = stdin.readLine();
a = Integer.parseInt(inputa);
}
catch(IOException ioex)
{
System.out.println("Input error");
System.exit(1);
}
catch ( NumberFormatException e)
{
System.out.println (e.getMessage() + " is not a valid format for aninteger.");
System.exit(1);
}

if(a==3) System.out.println ("Number 3!!!: " + a);

System.out.println ("You entered: " + a);
*/    

}