sábado, 4 de março de 2017

Array randômico automático

A interface é preenchida com números entre 1 e 12,
más a impressão é totalmente aleatória, isto sim.
Se a interface é aleatória, os chamados dos números
posicionados na interface também é aleatório.
E segue assim, até que todos os números sejam
chamados, más o programa só finaliza quando 3
sequencias seguindo o mesmo padrão são feitas.

Veja abaixo uma imagem do programa em execução:




Veja abaixo o código do programa:

import java.util.Random;

public class PROJETO {
     public static int  tam  = 12;
     J                       jht  = new J ( );

     public static void Grade ( ) {
         J jht = new J ( );
         int c, i = 0;
         jht.textbackground ( 0 );
         jht.textcolor ( 12 );
         // Abaixo as rotinas pras colunas
         for ( i = 2; i <= 16; i++ ) {
              jht.gotoxy ( 66, i + 5 );
              System.out.printf ( " " );
         }
         for ( i = 2; i <= 16; i++ ) {
              jht.gotoxy ( 56, i + 5 );
              System.out.printf ( " " );
         }
         for ( i = 2; i <= 16; i++ ) {
              jht.gotoxy ( 46, i + 5 );
              System.out.printf ( " " );
         }
         for ( i = 2; i <= 16; i++ ) {
              jht.gotoxy ( 36, i + 5 );
              System.out.printf ( " " );
         }
         for ( i = 2; i <= 16; i++ ) {
              jht.gotoxy ( 26, i + 5 );
              System.out.printf ( " " );
         }
         // Abaixo as rotinas pras linhas
         jht.gotoxy ( 26, 7 );
         for ( c = 26; c < 66; c++ ) {
              System.out.printf ( " " );
         }
         jht.gotoxy ( 26, 12 );
         for ( c = 26; c < 66; c++ ) {
              System.out.printf ( " " );
         }
         jht.gotoxy ( 26, 17 );
         for ( c = 26; c < 66; c++ ) {
              System.out.printf ( " " );
         }
         jht.gotoxy ( 26, 22 );
         for ( c = 26; c <= 66; c++ ) {
              System.out.printf ( " " );
         }
     }

     public static int Sorteia ( int A [ ], int vet [ ] ) {
         J jht = new J ( );
         Random random = new Random ( );
         int i, r, temp;
         for ( i = 0; i < tam; i++ )
              A [ i ] = i;
         for ( i = 0; i < tam; i++ ) {
              r = random.nextInt ( tam );
              temp = A [ i ];
              A [ i ] = A [ r ];
              A [ r ] = temp;
         }
         for ( i = 0; i < tam; i++ ) {
              if ( A [ i ] == 0 ) {
                   A [ i ] = tam;
              }
         }
         for ( i = 0; i < tam; i++ ) {
              vet [ i ] = A [ i ];
         }
         return 0;
     }

     public static void main ( String [ ] args ) {
         J jht = new J ( );
         jht.textbackground ( 15 );
         jht.Moldura ( 1, 31, 2, 78, 15 );
         jht.title ( " " );
         int a = 0;
         int y = 1;
         int vet [ ] = new int [ tam ];
         int A [ ] = new int [ tam ];
         int i, x = 0;
         Sorteia ( A, vet );
         do {
              jht.textcolor ( 12 );
              jht.gotoxy ( 7, 3 );
              System.out.printf ( "%d°", y + 0 );
              jht.textcolor ( 9 );
              System.out.printf ( " Sorteio" );
              Sorteia ( A, vet );
              for ( i = 0; i < tam; i++ ) {
                   jht.textcolor ( 12 );
                   jht.gotoxy ( 33, 3 );
                   System.out.printf ( "ARRAY RANDÔMICO AUTOMÁTICO" );
                   jht.textcolor ( 9 );
                   jht.gotoxy ( 29, 5 );
                   System.out.printf ( "Escolhendo posições automaticamente" );
                   jht.textcolor ( 5 );
                   jht.gotoxy ( 8, 23 );
                   for ( i = 0; i < tam; i++ ) {
                        do {
                            a = vet [ i ];
                            if ( a == 12 )
                                 a = 1;
                            jht.textcolor ( 9 );
                            jht.gotoxy ( 30, 24 );
                            System.out.printf ( "Na posição " );
                            jht.textcolor ( 12 );
                            System.out.printf ( "%d", a );
                            jht.textcolor ( 9 );
                            System.out.printf ( " está o número " );
                            jht.textcolor ( 12 );
                            System.out.printf ( "%d", vet [ a ] );
                            for ( a = 0; a < 12; a++ ) {
                                 if ( a != vet [ i ] ) {
                                      if ( a == 0 ) {
                                           jht.gotoxy ( 30, 9 );
                                      }
                                      if ( a == 4 ) {
                                           jht.gotoxy ( 30, 14 );
                                      }
                                      if ( a == 8 ) {
                                           jht.gotoxy ( 30, 19 );
                                      }
                                      if ( a == 12 ) {
                                           jht.gotoxy ( 30, 24 );
                                      }
                                      jht.textcolor ( 9 );
                                      System.out.printf ( "%2d        ", vet [ a ] );
                                 } else {
                                      if ( a == 0 ) {
                                           jht.gotoxy ( 30, 9 );
                                      }
                                      if ( a == 4 ) {
                                           jht.gotoxy ( 30, 14 );
                                      }
                                      if ( a == 8 ) {
                                           jht.gotoxy ( 30, 19 );
                                      }
                                      if ( a == 12 ) {
                                           jht.gotoxy ( 30, 24 );
                                      }
                                      jht.textcolor ( 12 );
                                      System.out.printf ( "%2d        ", vet [ a ] );
                                 }
                            }
                            Grade ( );
                            i++;
                            jht.getche ( );
                            jht.textbackground ( 15 );
                            jht.Apaga ( 24, 25, 58, 61, 15 );

                            break;
                        } while ( x < 12 );
                        i = i - 1;
                   }
                   i = i - 1;
              }
              y++;
         } while ( y <= 3 );
         jht.Sleep ( 1800 );
         jht.textcolor ( 9 );
         jht.gotoxy ( 26, 26 );
         System.out.printf ( "Por: " );
         jht.textcolor ( 5 );
         System.out.printf ( "Samuel Lima" );
         jht.textcolor ( 0 );
         jht.gotoxy ( 26, 27 );
         System.out.printf ( "sa_sp10@hotmail.com" );
         jht.Sleep ( 1800 );
         jht.textcolor ( 12 );
         jht.gotoxy ( 35, 29 );
         System.out.printf ( "MUITO OBRIGADO" );
         jht.getche ( );
     }

}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.