Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Gestione del random.

  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2007
    Messaggi
    32

    Gestione del random.

    Ciao a tutti, ho un problema con un codice..!!
    "Spero" che sia di facile comprensione perchè commentato.
    Mi spiego: ho degli array con valori numerici. Carico i valori degli array in base al numero
    (che corrisponde ad un colore).
    In seguito con il metodo scegli prendo le posizioni dei valori già raggruppati per numero,
    ed una volta riempiti i rispettivi devo estrarre random una posizione.
    Il mio problema è che il random dovrebbe essere richiamato una sola volta per ogni array,
    invece viene effettuato ogni volta che carico un valore.
    Dunque il problema non è nel metodo scegli che pare funzionare ma nella gestione generale...

    Il metodo funziona ma..vedete se si puoi far qualcosa per gestire meglio il random....
    gRAzIe TaNtE...vi prego aiutatemi!!!

    public class MatriceFunzione
    {
    public static void main(String[] args)
    {
    int mat[][] = new int[8][4];
    int i,j; // PUNTATORI PER IL NORD
    int i1,j1; // PUNTATORI PER IL SUD
    int i2,j2; // PUNTATORI PER L'OVEST
    int i3,j3; // PUNTATORI PER L'EST

    // Il confronto va fatto tra : OVEST ed EST, e tra NORD e SUD.
    // seguendo questo schema:
    // j = 0 1 2 3
    // NORD SUD OVEST(sin) EST(des)
    // i = 0
    // 1
    // 2
    // 3
    // 4
    // 5
    // 6
    // 7
    // 8

    // Quadrati colorati a cui assegno
    // i rispettivi colori in base a:

    /* LEGENDA:
    i = 1 : Rosso;
    i = 2 : Giallo;
    i = 3 : Verde;
    i = 4 : Blu; */

    // ----- Primo Tiles : a ----

    mat[0][0]=1; // NORD
    mat[0][1]=3; // SUD
    mat[0][2]=3; // OVEST (s)
    mat[0][3]=1; // EST (d)

    // ----- Secondo Tiles : b ----

    mat[1][0]=2; // NORD
    mat[1][1]=1; // SUD
    mat[1][2]=3; // OVEST (s)
    mat[1][3]=3; // EST (d)

    // ----- Terzo Tiles : c -----

    mat[2][0]=2; // NORD
    mat[2][1]=1; // SUD
    mat[2][2]=1; // OVEST (s)
    mat[2][3]=1; // EST (d)

    // ----- Quarto Tiles : d ----

    mat[3][0]=2; // NORD
    mat[3][1]=1; // SUD
    mat[3][2]=1; // OVEST (s)
    mat[3][3]=3; // EST (d)

    // ----- Quinto Tiles : e ----

    mat[4][0]=2; // NORD
    mat[4][1]=3; // SUD
    mat[4][2]=1; // OVEST (s)
    mat[4][3]=3; // EST (d)

    // ----- Sesto Tiles : f ----

    mat[5][0]=2; // NORD
    mat[5][1]=3; // SUD
    mat[5][2]=1; // OVEST (s)
    mat[5][3]=1; // EST (d)

    // ----- Settimo Tiles : g -----

    mat[6][0]=2; // NORD
    mat[6][1]=1; // SUD
    mat[6][2]=3; // OVEST(s)
    mat[6][3]=3; // EST (d)

    // ----- Ottavo Tiles : h ----

    mat[7][0]=3; // NORD
    mat[7][1]=2; // SUD
    mat[7][2]=3; // OVEST (s)
    mat[7][3]=1; // EST (d)

    // ARRAY USATI PER CARICARE I COLORI DEL NORD E SUD

    int copiaRosso[] = new int[64]; // COPIA ARRAY PER IL ROSSO TRA NORD E SUD
    int copiaGiallo[] = new int[64]; // COPIA ARRAY PER IL GIALLO TRA NORD E SUD
    int copiaVerde[] = new int[64]; // COPIA ARRAY PER IL VERDE TRA NORD E SUD
    int copiaBlu[] = new int[64]; // COPIA ARRAY PER IL BLU TRA NORD E SUD

    // FORZIAMO LA DIMENSIONE DEGLI ARRAY A ZERO...IN MODO CHE POSSANO ESSERE INCREMENTATI

    int p=0;
    for (p=0; p<64;p++)
    {
    copiaRosso[p]=0;
    copiaGiallo[p]=0;
    copiaVerde[p]=0;
    copiaBlu[p]=0;
    }

    // ARRAY USATI PER CARICARE I COLORI DEL EST ED OVEST

    int copiaRosso1[] = new int[64]; // COPIA ARRAY PER IL ROSSO TRA OVEST ED EST
    int copiaGiallo1[] = new int[64]; // COPIA ARRAY PER IL GIALLO TRA OVEST ED EST
    int copiaVerde1[] = new int[64]; // COPIA ARRAY PER IL VERDE TRA OVEST ED EST
    int copiaBlu1[] = new int[64]; // COPIA ARRAY PER IL BLU TRA OVEST ED EST

    // FORZIAMO LA DIMENSIONE DEGLI ARRAY A ZERO : IN MODO CHE POSSANO ESSERE INCREMENTATI

    int p1=0;
    for (p1=0; p1<64;p1++)
    {
    copiaRosso1[p1]=0;
    copiaGiallo1[p1]=0;
    copiaVerde1[p1]=0;
    copiaBlu1[p1]=0;
    }

    // ---------- CONFRONTO TRA NORD E SUD --------------------

    int temp=0;
    int temp1=0;
    int pos=0;

    for(i=0; i<=7; i++)
    {
    temp=(mat[i][0]); // I VALORI DELLA PRIMA COLONNA = NORD

    for(i1=0; i1<=7; i1++)
    {
    temp1=(mat[i1][1]); // I VALORI DELLA SECONDA COLONNA = SUD

    if (temp==temp1) // CONFRONTA I VALORI DEL NORD CON I VALORI SUD
    {
    //System.out.println (temp);
    //System.out.println (temp1);
    System.out.println ("NORD & SUD SONO UGUALI");
    salva(temp,pos,copiaRosso,copiaGiallo,copiaVerde,c opiaBlu); // CON pos INDICHIAMO IL CONT DEI VALORI
    pos++; // INCREMENTO
    }
    else // ALTRIMENTI SE DIVERSI
    {
    //System.out.println (temp);
    //System.out.println (temp1);
    System.out.println ("NORD & SUD SONO DIVERSI");
    }
    }
    }

    // ------------- CONFRONTO TRA OVEST ED EST ---------------------

    int temp2=0;
    int temp3=0;
    int pos1=0;

    for(i2=0; i2<=7; i2++)
    {
    temp2=(mat[i2][2]); // I VALORI DELLA TERZA COLONNA = OVEST

    for(i3=0; i3<=7; i3++)
    {
    temp3=(mat[i3][3]); // I VALORI DELLA QUARTA COLONNA = EST

    if (temp2==temp3) // CONFRONTA I VALORI DEL NORD CON I VALORI SUD
    {
    // System.out.println (temp2);
    // System.out.println (temp3);
    System.out.println ("OVEST & EST SONO UGUALI");
    salva1 (temp2,pos1,copiaRosso1,copiaGiallo1,copiaVerde1,c opiaBlu1); // RICHIAMO LA FUNZIONE SALVA
    pos1++; // INCREMENTO CON pos INDICHIAMO IL CONT DEI VALORI
    }
    else // ALTRIMENTI SE DIVERSI
    {
    //System.out.println (temp2);
    //System.out.println (temp3);
    System.out.println ("OVEST & EST SONO DIVERSI");
    }
    }
    }
    }

    public static void salva (int temp,int pos,int copiaRosso[],int copiaGiallo[],int copiaVerde[],int copiaBlu[])
    {
    // ARRAY CHE MEMORIZZANO IL CONFRONTO TRA NORD E SUD

    if (temp==1) // TEMP 0 : COLORE ROSSO del NORD e SUD
    {
    copiaRosso[pos] = temp;
    System.out.println ("Colore ROSSO: "+copiaRosso[pos]);
    scegli(copiaRosso); // ENTRA NELLA CLASSE SCEGLI E DELL'ARRAY copiaRosso FA IL RANDOM
    }
    if (temp==2) // TEMP 1 : COLORE GIALLO del NORD e SUD
    {
    copiaGiallo[pos] = temp;
    System.out.println ("Colore GIALLO: "+copiaGiallo[pos]);
    scegli(copiaGiallo); // ENTRA NELLA CLASSE SCEGLI E DELL'ARRAY copiaGiallo FA IL RANDOM
    }
    if (temp==3) // TEMP 3 : COLORE VERDE del NORD e SUD
    {
    copiaVerde[pos] = temp;
    System.out.println ("Colore Verde: "+copiaVerde[pos]);
    scegli(copiaVerde); // ENTRA NELLA CLASSE SCEGLI E DELL'ARRAY copiaVerde FA IL RANDOM
    }
    if (temp==4) // TEMP 4 : COLORE BLU del NORD e SUD
    {
    copiaBlu[pos] = temp;
    System.out.println ("Colore BLU: "+copiaBlu[pos]);
    scegli(copiaBlu); // ENTRA NELLA CLASSE SCEGLI E DELL'ARRAY copiaBlu FA IL RANDOM
    }
    // }
    }

    // METODO salva1 usato per memorizzare i valori di ovest ed est

    public static void salva1 (int temp2,int pos1,int copiaRosso1[],int copiaGiallo1[],int copiaVerde1[],int copiaBlu1[])
    {
    // CONFRONTO DELL' EST E DELL'OVEST

    // ARRAY CHE MEMORIZZANO IL CONFRONTO TRA OVEST ED EST

    if (temp2==1) // TEMP2 0 : COLORE ROSSO dell' OVEST e EST
    {
    copiaRosso1[pos1] = temp2;
    System.out.println ("Uguale Colore ROSSO dell' OVEST e EST: "+copiaRosso1[pos1]);
    scegli(copiaRosso1); // ENTRA NELLA CLASSE SCEGLI E DELL'ARRAY copiaRosso1 e FA IL RANDOM
    }
    if (temp2==2) // TEMP 1 : COLORE GIALLO dell' OVEST e EST
    {
    copiaGiallo1[pos1] = temp2;
    System.out.println ("Uguale Colore GIALLO dell' OVEST e EST: "+copiaGiallo1[pos1]);
    scegli(copiaGiallo1); // ENTRA NELLA CLASSE SCEGLI E DELL'ARRAY copiaGiallo1 e FA IL RANDOM
    }
    if (temp2==3) // TEMP 0 : COLORE VERDE dell' OVEST e EST
    {
    copiaVerde1[pos1] = temp2;
    System.out.println ("Uguale Colore Verde dell' OVEST e EST: "+copiaVerde1[pos1]);
    scegli(copiaVerde1); // ENTRA NELLA CLASSE SCEGLI E DELL'ARRAY copiaVerde e FA IL RANDOM
    }
    if (temp2==4) // TEMP2 0 : COLORE BLU dell' OVEST e EST
    {
    copiaBlu1[pos1] = temp2;
    System.out.println ("Uguale Colore BLU dell' OVEST e EST: "+copiaBlu1[pos1]);
    scegli(copiaBlu1); // ENTRA NELLA CLASSE SCEGLI E DELL'ARRAY copiaBlu1 e FA IL RANDOM
    }
    }

    // METODO CHE MI FA IL RANDOM ALL'INTERNO DEGLI
    // ARRAY RIEMPITI CON I COLORI UGUALI

    public static int scegli (int array[])
    {
    int casuale=0;
    int c=0;
    int count=0;

    // Voglio effettuare il random di questo array:

    for(c=0;c<array.length;c++) // Lettura dell'array (es: copiaRosso)
    {
    if(array[c]!=0) // Effettua il controllo su l'array se ha valori diversi da 0
    {
    count++; // Incrementa se ci sono valori diversi da 0
    // System.out.println(c);
    }
    }
    // System.out.println(count);

    int arrayVuoto[] = new int[count]; // Array usato per contare le posizioni
    int count1=0; // count1 usato per scorrere l'array delle posizioni
    for(c=0;c<array.length;c++) // Lettura dell'array (es: copiaRosso)
    {
    if(array[c]!=0) // Effettua il controllo su l'array se ha valori diversi da 0
    {
    arrayVuoto[count1]=c; // Inserisce le posizioni nel nuovo array
    count1++; // Incrementa per gli elementi diversi da 0
    // System.out.println(c);
    }
    }
    System.out.println ("Quanti valori ci sono caricati: "+arrayVuoto.length);

    if (arrayVuoto.length==0) // Se l'array con le posizioni è vuoto?
    {
    return 0; // Ritorna l'eccezzione
    }
    else
    {
    casuale = (int) (Math.random () * count); // Random dei valori presenti nell'array diversi da 0
    int trovato=arrayVuoto[casuale]; // Assegno il valore casuale trovato alla variablile trovato
    //System.out.println ("Numero estratto all'interno dell'array : "+ arrayVuoto[trovato]);
    //return arrayVuoto[trovato];
    System.out.println ("Numero estratto all'interno dell'array : "+ arrayVuoto[casuale]); // Stampa l'estrazione
    return arrayVuoto[casuale]; // Ritorna l'estrazione
    }
    }
    }

  2. #2
    Utente di HTML.it L'avatar di nether
    Registrato dal
    Dec 2006
    Messaggi
    376
    poco codice da leggere e ben indentato eh :P

  3. #3
    Utente di HTML.it
    Registrato dal
    Mar 2007
    Messaggi
    32

    lo sò...

    Seguo la legge del regolamento di norma...ma è un caso semplice...
    Perchè il codice risulta funzionante, infatti va solo copiato e provato...non chiedo miracoli...ma un modo per aver una efficente gestione...Grazie ancora!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.