Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Kihby en 11 Noviembre 2015, 02:38 am



Título: Ayuda! Duda de llenado aleatorio de matriz
Publicado por: Kihby en 11 Noviembre 2015, 02:38 am
Bueno, como dice el título tengo una duda de llenado aleatorio para una matriz, estaba trabajando en un juego de memoria para el colegio, pero se me ocurrió la idea de hacer la ubicación de las parejas o algo parecido aleatorio, estoy atascado y no sé qué hacer, este es el programa que hice:
Código:
#include<iostream>
#include <conio.h>
#include <stdio.h>
#include <windows.h>

using namespace std;

#define ESC 27

struct estructura{
char simbolo;
char valor;

};
estructura matriz[4][4];

int parejas=0, f1,c1,f2,c2;
int x,y,i;
char opcion1;
int jugador;


void llenar(){//asignación de los valores de la mariz

matriz[0][0].valor = 'A' ;
    matriz[0][1].valor = 'B';
    matriz[0][2].valor = 'Z';
    matriz[0][3].valor = 'C';
    matriz[1][1].valor = 'E';
    matriz[1][0].valor = 'D';
    matriz[1][2].valor = 'Y';
    matriz[1][3].valor = 'P';
    matriz[2][2].valor = 'H';
    matriz[2][0].valor = 'K';
    matriz[2][1].valor = 'G';
    matriz[2][2].valor = 'H';
    matriz[2][3].valor = 'X';
matriz[3][0].valor = 'J';
    matriz[3][1].valor = 'M';
    matriz[3][2].valor = 'V';
    matriz[3][3].valor = 'U';


    matriz[0][0].simbolo = '%';
matriz[0][1].simbolo = '$';
matriz[0][2].simbolo = '#';
matriz[0][3].simbolo = 'X';
matriz[1][0].simbolo ='$';
matriz[1][1].simbolo = '&';
matriz[1][2].simbolo = 'W';
matriz[1][3].simbolo = '&';
matriz[2][0].simbolo = '*';
matriz[2][1].simbolo = '@';
matriz[2][2].simbolo = '#';
matriz[2][3].simbolo = '%';
matriz[3][0].simbolo = '@';
matriz[3][1].simbolo = 'W';
matriz[3][2].simbolo = '*';
matriz[3][3].simbolo = 'X';




}
void gotoxy(int col, int fil){
HANDLE hCon;
hCon = GetStdHandle (STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = col;
    dwPos.Y =fil;
SetConsoleCursorPosition (hCon, dwPos);

}
void cuadro(){ //decoracion
for (int i = 2; i<78; i++)
{
system ("color 3E");
   gotoxy(i,2); cout<<"*";
   gotoxy(i, 22); cout<<"*";
}
for (int i= 2; i<23;i++)
{
system ("color 3E");
gotoxy(2,i); cout<<"*";
     gotoxy(78,i); cout<<"*";
}
}
void mostrar(){//mostrar la matriz
system("color 33");
    gotoxy(34,5);
cout<<matriz[0][0].valor<<"  |  " <<matriz[0][1].valor<<"  |  "<<matriz[0][2].valor<<"  |  "<<matriz[0][3].valor<<endl;
    gotoxy(34,6);
  cout<<"-------------------"<<endl;
    gotoxy(34,7);
cout<<matriz[1][0].valor<<"  |  " <<matriz[1][1].valor<<"  |  "<<matriz[1][2].valor<<"  |  "<<matriz[1][3].valor<<endl;
    gotoxy(34,8);
  cout<<"-------------------"<<endl;
    gotoxy(34,9);
cout<<matriz[2][0].valor<<"  |  " <<matriz[2][1].valor<<"  |  "<<matriz[2][2].valor<<"  |  "<<matriz[2][3].valor<<endl;
    gotoxy(34,10);
  cout<<"-------------------"<<endl;
gotoxy(34,11);
cout<<matriz[3][0].valor<<"  |  " <<matriz[3][1].valor<<"  |  "<<matriz[3][2].valor<<"  |  "<<matriz[3][3].valor<<endl;

}

void jugar(){
for(i=1;i<=2;i++){
gotoxy (33,15);
cout<< " Elija la opcion : ";
cin>> opcion1;


if (opcion1 == 'A') {//determinacion de las parejas
x=0; y=0; gotoxy(34,5); cout << matriz[x][y].simbolo;
}
if (opcion1 == 'B') {
x=0; y=1; gotoxy(39,5); cout << matriz[x][y].simbolo;
}

       if (opcion1 == 'Z') {
x=0; y=2; gotoxy(44,5); cout << matriz[x][y].simbolo;
}

   if (opcion1 == 'C') {
x=0; y=3; gotoxy(49,5); cout << matriz[x][y].simbolo;
}
     if (opcion1 == 'D') {
x=1; y=0; gotoxy(34,7); cout << matriz[x][y].simbolo;
}
  if (opcion1 == 'E') {
x=1; y=1; gotoxy(39,7); cout << matriz[x][y].simbolo;
}
   if (opcion1 == 'Y') {
x=1; y=2; gotoxy(44,7); cout << matriz[x][y].simbolo;
}
     if (opcion1 == 'P') {
x=1; y=3; gotoxy(49,7); cout << matriz[x][y].simbolo;
}
   if (opcion1 == 'K') {
x=2; y=0; gotoxy(34,9); cout << matriz[x][y].simbolo;
}
    if (opcion1 == 'G') {
x=2; y=1; gotoxy(39,9); cout<<" "; cout << matriz[x][y].simbolo;
}
    if (opcion1 == 'H') {
x=2; y=2; gotoxy(44,9); cout<<" "; cout << matriz[x][y].simbolo;
}
if (opcion1 == 'X') {
x=2; y=3; gotoxy(49,9); cout<<" "; cout << matriz[x][y].simbolo;
}
if (opcion1 == 'J') {
x=3; y=0; gotoxy(34,11); cout<<" "; cout << matriz[x][y].simbolo;
}

if (opcion1 == 'M') {
x=3; y=1; gotoxy(39,11); cout<<" "; cout << matriz[x][y].simbolo;
}

            if (opcion1 =='V') {
x=3; y=2; gotoxy(44,11); cout<<" "; cout << matriz[x][y].simbolo;
}
if (opcion1 == 'U') {
x=3; y=3; gotoxy(49,11); cout<<" "; cout << matriz[x][y].simbolo;
}

if(i==1){
f1=x; c1=y;
}else
f2=x; c2=y;
}
if((f1==0 && c1==0) && (f2==2 && c2==3) ||(f2==0 && c2==0) && (f1==2 && c1==3)){
parejas= parejas+1;
matriz[0][0].valor=matriz[0][0].simbolo;
matriz[2][3].valor=matriz[2][3].simbolo;
}
if((f1==0 && c1==1) && (f2==1 && c2==0) ||(f2==0 && c2==1) && (f1==1 && c1==0)){
parejas= parejas+1;
matriz[0][1].valor=matriz[0][1].simbolo;
matriz[1][0].valor=matriz[1][0].simbolo;
}
if((f1==0 && c1==2) && (f2==2 && c2==2) ||(f2==0 && c2==2) && (f1==2 && c1==2)){
parejas= parejas+1;
matriz[0][2].valor=matriz[0][2].simbolo;
matriz[2][2].valor=matriz[2][2].simbolo;
}
if((f1==1 && c1==1) && (f2==1 && c2==3) ||(f2==1 && c2==3) && (f1==1 && c1==1)){
parejas= parejas+1;
matriz[1][1].valor=matriz[1][1].simbolo;
matriz[1][3].valor=matriz[1][3].simbolo;
}
if((f1==0 && c1==3) && (f2==3 && c2==3) ||(f2==0 && c2==3) && (f1==3 && c1==3)){
parejas= parejas+1;
matriz[0][3].valor=matriz[0][3].simbolo;
matriz[3][3].valor=matriz[3][3].simbolo;
}
if((f1==1 && c1==2) && (f2==3 && c2==1) ||(f2==1 && c2==2) && (f1==3 && c1==1)){
parejas= parejas+1;
matriz[1][2].valor=matriz[1][2].simbolo;
matriz[3][1].valor=matriz[3][1].simbolo;
}
if((f1==2 && c1==1) && (f2==3 && c2==0) ||(f2==2 && c2==1) && (f1==3 && c1==0)){
parejas= parejas+1;
matriz[2][1].valor=matriz[2][1].simbolo;
matriz[3][0].valor=matriz[3][0].simbolo;
}
if((f1==2 && c1==0) && (f2==3 && c2==2) ||(f2==2 && c2==0) && (f1==3 && c1==2)){
parejas= parejas+1;
matriz[2][0].valor=matriz[2][0].simbolo;
matriz[3][2].valor=matriz[3][2].simbolo;
}
}//ciclo






int main(){
    cuadro();
llenar();
do{
mostrar();
jugar();
gotoxy(34,17);
cout<<"Parejas descubiertas: "<<parejas;
}while(parejas<=8);
if(parejas==8){
cout<< "HAS GANADO";
}
getch();
}
Agradezco mucho la ayuda  :D