segunda-feira, 23 de novembro de 2015

Matriz - preenchendo posições aleatoriamente

Algumas vêzes precisamos preencher uma matriz em posições aleatórias,
isto é, escolher exatamente onde inserir o elemento aceito pelo tipo
da matriz. Isto é perfeitamente possível e muito útil.
Num jogo da velha por exemplo as posições são escolhidas deste modo.
Neste exemplo mostro como isto pode ser feito, num código bem identado,
com recursos da biblioteca conio.h, inserindo coordenadas na tela
com os textos bem posicionados, com côres e fundo bem definido.
Código indicado aos interessados por linguagem c.


Veja o código abaixo:



#include <stdio.h>
#include <conio.h>
#define tam 3
void Janela ( ) {
     int l, c;
     for ( l = 1; l <= 30 ; l++ ) {
         for ( c = 1; c < 80 ; c++ ) {
              gotoxy ( c , l );
              if ( l == 2 ) {
                   textattr ( 200 );
                   printf ( " " );
              }
              if ( c == 1 ) {
                   textattr ( 200 );
                   printf ( "  " );
              }
              if ( c == 79 ) {
                   textattr ( 200 );
                   printf ( "  " );
              }
         }
     }
}
int main ( ) {
     Janela ( );
     textbackground ( WHITE );
     system ( "title MATRIZ - PREENCHENDO POSIÇÕES ALEATORIAMENTE" );
     int lin, col;
     int i;
     char vet [ tam ] [ tam ];
     gotoxy ( 20 , 3 );
     textcolor ( LIGHTRED );
     printf ( "MATRIZ - PREENCHENDO POSIÇÕES ALEATORIAMENTE" );
     for ( lin = 0; lin < 3 ; lin++ ) {
         for ( col = 0; col < 3 ; col++ ) {
              vet [ lin ] [ col ] = ' ';
         }
     }
     for ( i = 0; i < 9 ; i++ ) {
         gotoxy ( 20 , 5 );
         textcolor ( LIGHTBLUE );
         printf ( "Escolha a " );
         textcolor ( LIGHTRED );
         printf ( "%d°" , i + 1 );
         textcolor ( LIGHTBLUE );
         printf ( " posição :  " );
         for ( lin = 0; lin <= 2 ; lin++ ) {
              for ( col = 0; col <= 2 ; col++ ) {
                   textcolor ( LIGHTRED );
                   scanf ( "%d" , &lin );
                   fflush ( stdin );
                   gotoxy ( 46 , 5 );
                   scanf ( " %d" , &col );
                   fflush ( stdin );
                   if ( vet [ lin ] [ col ] != ' ' ) {
                        textcolor ( LIGHTRED );
                        gotoxy ( 20 , 7 );
                        printf ( "Posicao invalida ou ocupada" );
                        gotoxy ( 20 , 8 );
                        printf ( "tente novamente" );
                        getche ( );
                        gotoxy ( 20 , 7 );
                        clreol ( );
                        gotoxy ( 20 , 8 );
                        clreol ( );
                        gotoxy ( 43 , 5 );
                        clreol ( );
                        continue;
                   }
                   gotoxy ( 10 , 5 );
                   clreol ( );
                   gotoxy ( 1 , 6 );
                   clreol ( );
                   textcolor ( LIGHTBLUE );
                   gotoxy ( 27 , 7 );
                   printf ( "Escolha um caractere : " );
                   textcolor ( LIGHTRED );
                   scanf ( "%c" , &vet [ lin ] [ col ] );
                   fflush ( stdin );
                   gotoxy ( 10 , 7 );
                   clreol ( );
                   textcolor ( BLACK );
                   for ( lin = 0; lin <= 2 ; lin++ ) {
                        gotoxy ( 33 , lin + 11 );
                        for ( col = 0; col <= 2 ; col++ ) {
                            printf ( " %3c " , vet [ lin ] [ col ] );
                        }
                   }
              }
         }
     }
     Sleep ( 1800 );
     gotoxy ( 24 , 15 );
     textcolor ( LIGHTBLUE );
     printf ( "A matriz foi preenchida com sucesso" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 17 , 19 );
     printf ( "Por: " );
     textcolor ( LIGHTMAGENTA );
     printf ( "Samuel Lima" );
     textcolor ( BLACK );
     gotoxy ( 17 , 20 );
     printf ( "sa_sp10@hotmail.com" );
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 36 , 23 );
     printf ( "MUITO OBRIGADO" );
     getch ( );
     exit ( 0 );
}

Nenhum comentário:

Postar um comentário

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