#include <iostream>
#include <conio2.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#define maxfil 20
#define maxcol 80
#define maxcolor 16
using namespace std;
int main() {
srand(time(NULL));
int fila,columna;
int color;
char resp;
cout<<"Desea ejecutar el programa s/n:";
cin>>resp;
clrscr();
color=rand()%maxcolor;
textcolor(color);
fila=rand()%maxfil+1;
columna=rand()%maxcol+1;
gotoxy(columna,fila);
printf("%c",char(219));
if (fila<=10)
{
textcolor(WHITE);
gotoxy(1,23);
cout<<"ZONA SUPERIOR";
}
else
{
textcolor(WHITE);
gotoxy(1,23);
cout<<"ZONA INFERIOR";
}
if (columna<=40)
{
textcolor(WHITE);
gotoxy(16,23);
cout<<"ZONA IZQUIERDA";
}
else
{
textcolor(WHITE);
gotoxy(16,23);
cout<<"ZONA DERECHA";
}
//aqui empiezan los colores
/*se podria usar tambien switch del siguiente modo*/
//switch (color)
//{
//case 0: quiere decir, en caso de que color valga 0
if (color==0)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color negro";
}
//break;
/*haria que quitar la condicion if y sus consiguientes llaves
y se podrian poner tantos casos como se quisiera*/
else{
if (color==1)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color azul";
}
else{
if (color==2)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color verde";
}
else{
if (color==3)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color cyan";
}
else{
if (color==4)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color rojo";
}
else{
if (color==5)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color magenta";
}
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color magenta";
}
else{
if (color==6)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color marron";
}
else{
if (color==7)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color gris claro";
}
else{
if (color==8)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color gris oscuro";
}
if (color==8)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color gris oscuro";
}
else{
if (color==9)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color azul claro";
}
else{
if (color==10)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color verde claro";
}
else{
if (color==11)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color cyan claro";
}
else{
if (color==12)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color rojo claro";
}
else{
if (color==13)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color magenta claro";
}
else{
if (color==14)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color amarillo";
}
else{
if (color==15)
{
textcolor(WHITE);
gotoxy(32,23);
cout<<"Color blanco";
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
system("PAUSE>NUL");/*esto hace que el programa se pause y no se muestre
ningun mensaje, ya que antes se mezclaba con los mensajes de los colores*/
return 0;
}
El programa quedaría de esta forma, mostrándonos un cuadrado colocado y dibujado aleatoriamente en una pantalla de 20 filas y 80 columnas e indicándonos su posición y color.
Gracias al comando srand(time(NULL)); cada vez que iniciemos el programa, la semilla de números aleatorios creada será distinta.
No hay comentarios:
Publicar un comentario