It does not generate a correct matrix

0

When doing the cycles so that the matrix generates me, it arrives the point in which when having to generate the matrix it remains motionless, and it does not happen of there. The code is for a "game", inside the code come the instructions. I have already done all the code and if it compiles and executes, but it does not generate the matrix after giving the instructions What did I do wrong?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <wchar.h>
#include <locale.h>
#include <time.h>
#include <string.h>

main()
{
    system("cls");
    setlocale(LC_ALL, "");

    int Matriz[4][4], Contador1, Contador2, x, y, TomJerryEncontrados, c1, PuntajeJugador1, PuntajeJugador2;
    PuntajeJugador1=0;
    PuntajeJugador2=0;
    TomJerryEncontrados=0;
    char c2[10];
    bool V1, V2, V3;
    V1=false;
    V2=false;
    V3=true;

    char NombreJugador1[30], NombreJugador2[30];

            printf("Introduce el nombre del jugador 1:\n");
            fflush(stdin);
            scanf("%s", &NombreJugador1);
            printf("Introduce el nombre del jugador 2:\n");
            fflush(stdin);
            scanf("%s", &NombreJugador2);

            system("cls");

            printf("Hola %s y %s ! Bienvenidos al juego Tom y Jerry\n", NombreJugador1, NombreJugador2);
            printf("Las intrucciones del juego son las siguientes:\n\n");
            printf("%s será Tom y %s será Jerry\n", NombreJugador1, NombreJugador2);
            printf("La matriz tendrá escondidas 3 palabras -Tom- y 3 palabras -Jerry- más aparte una palabra -Queso- y otra -Captura-\n");
            printf("Para encontrar las palabras cada jugador debe dar un número de casilla por turnos\n");
            printf("El juego termina cuando todas las casillas estén abiertas\n");
            printf("El ganador será quién acumule más puntos\n");
            printf("Al encontrar la palabra -Tom- serán 10 puntos para Tom y menos 10 puntos para Jerry\n");
            printf("Al encontrar la palabra -Jerry- serán 10 puntos para Jerry y menos 10 puntos para Tom\n");
            printf("Si Tom encuentra la palabra -Captura- ganará 50 puntos y finalizará el juego\n");
            printf("Si Jerry encuentra la palabra -Queso- ganará 50 puntos y finalizará el juego\n");
            printf("Si Tom encuentra la palabra -Queso- perderá 10 puntos\n");
            printf("Si Jerry encuentra la palabra -Captura- perderá 10 puntos\n");
            printf("Si la casilla está vacía los dos perderán 5 puntos\n");
            system("PAUSE");


            //printf("Cargando...");

            Contador1=0;
            while(Contador1<4)
            {
                while(Contador2<4)
                {
                    Matriz[Contador1][Contador2]=0;
                    Contador2++;
                }
            Contador1++;
            }

            Contador1=0;
            while(Contador1<3)
            {
                srand((unsigned)time(NULL));
                c1=rand()%16;
                y=c1/4;
                x=c1-(4*y);
                if (Matriz[x][y]==0)
                {
                    Matriz[x][y]=1;              // 3 Tom
                    Contador1++;
                }
            }
            Contador1=0;
            while(Contador1<3)
            {
                srand((unsigned)time(NULL));
                c1=rand()%16;
                y=c1/4;
                x=c1-(4*y);
                if (Matriz[x][y]==0)
                {
                    Matriz[x][y]=2;              // 3 Jerry
                    Contador1++;
                }
            }
            Contador1=0;
            while(Contador1<1)
            {
                srand((unsigned)time(NULL));
                c1=rand()%16;
                y=c1/4;
                x=c1-(4*y);
                if (Matriz[x][y]==0)
                {
                    Matriz[x][y]=3;              // 1 Queso
                    Contador1++;
                }
            }
            Contador1=0;
            while(Contador1<1)
            {
                srand((unsigned)time(NULL));
                c1=rand()%16;
                y=c1/4;
                x=c1-(4*y);
                if (Matriz[x][y]==0)
                {
                    Matriz[x][y]=4;              // 1 Captura
                    Contador1++;
                }
            }

            system("cls");
            while (TomJerryEncontrados<6)
            {
                printf("Puntaje de %s = %d  || Puntaje de %s = %d \n", NombreJugador1, PuntajeJugador1, NombreJugador2, PuntajeJugador2);
                if (V1==false)
                {
                    if (V2==true)
                    {
                        printf("La casilla ya fue seleccionada anteriormente\n\n");
                        V2==false;
                    }
                    else 
                    {
                        if (V3==false)
                        {
                            printf("El número de casilla no es válido\n\n");
                            V3=true;
                        }
                        else 
                        {
                            printf("Es turno de %s\n\n", NombreJugador1);
                        }
                    }
                }
                else
                {
                    if (V2==true)
                    {
                        printf("La casilla ya fue seleccionada anteriormente\n\n");
                        V2=false;
                    }
                    else
                    {
                        if (V3==false)
                        {
                            printf("El número de casilla no es válido\n\n");
                            V3=true;
                        }
                        else
                        {
                            printf("Es turno de %s\n\n", NombreJugador2);
                        }
                    }
                }

            Contador1=0;
            while (Contador1<4)
            {
                Contador2=0;
                while (Contador2<4)
                {
                    switch (Matriz[Contador2][Contador1])
                    {
                        case 0:
                            printf("%d\t", (4*Contador1)+Contador2+1);
                            break;
                        case 1:
                            printf("%d\t", (4*Contador1)+Contador2+1);
                            break;
                        case 2:
                            printf("%d\t", (4*Contador1)+Contador2+1);
                            break;
                        case 3:
                            printf("%d\t", (4*Contador1)+Contador2+1);
                            break;
                        case 4:
                            printf("Vacío\t");
                            break;
                        case 5:
                            printf("Tom\t");
                            break;
                        case 6:
                            printf("Jerry\t");
                            break;
                        case 7:
                            printf("Queso\t");
                            break;
                        case 8:
                            printf("Captura\t");
                            break;
                    }
                    Contador2++;
                }
                printf("\n");
                Contador1++;
            }

            printf("Selecciona el número de una casilla\n");
            scanf("%s", &c2);
            int c1=atoi(c2);
            c1=c1-1;
            if (c1>=0 && c1<=15)
            {
                y=c1/4;
                x=c1-(4*y);
                switch (Matriz[x][y])
                {
                    case 0:
                        Matriz[x][y]=4;
                        if (V1==true)
                        {
                            PuntajeJugador2=PuntajeJugador2-5;
                            V1=false;
                        }
                        else
                        {
                            PuntajeJugador1=PuntajeJugador1-5;
                            V1=true;
                        }
                        break;
                    case 1:
                        Matriz[x][y]=5;
                        TomJerryEncontrados++;
                        if (V1==true)
                        {
                            PuntajeJugador2=PuntajeJugador2-10;
                            V1=false;
                        }
                        else
                        {
                            PuntajeJugador1=PuntajeJugador1+10;
                            V1=true;
                        }
                        break;
                    case 2:
                        Matriz[x][y]=6;
                        if (V1==true)
                        {
                            TomJerryEncontrados++;
                            PuntajeJugador2=PuntajeJugador2+10;
                            V1=false;
                        }
                        else
                        {
                            PuntajeJugador1=PuntajeJugador1-10;
                            V1=true;
                        }
                        break;
                    case 3:
                        Matriz[x][y]=7;
                        TomJerryEncontrados=7;
                        if (V1==true)
                        {
                            PuntajeJugador2=PuntajeJugador2+50;
                        }
                        else
                        {
                            PuntajeJugador1=PuntajeJugador1-50;
                        }
                        break;
                    case 4:
                        Matriz[x][y]=8;
                        TomJerryEncontrados=8;
                        if (V1==true)
                        {
                            PuntajeJugador2=PuntajeJugador2-50;
                        }
                        else
                        {
                            PuntajeJugador1=PuntajeJugador1+50;
                        }
                    default:
                        V2=true;
                        break;
                }
            }
            else
            {
                V3=false;
            }
                system("cls");
            }
            system("cls");
            if(TomJerryEncontrados==6)
            {
                printf("Encontraron todos los Tom y Jerry disponibles\n");
            }
            else
            {
                printf("Fin del juego\n");
            }
            if(PuntajeJugador1>PuntajeJugador2)
            {
                printf("El ganador es %s\n", NombreJugador1);
            }
            else
            {
                printf("El ganador es %s\n", NombreJugador2);
            }
        getch();
    }
    
asked by Manoel Rico 04.12.2017 в 21:03
source

1 answer

0
Contador1=0;
while(Contador1<4)
{
  while(Contador2<4)
  {
    Matriz[Contador1][Contador2]=0;
    Contador2++;
  }
  Contador1++;
}

That initializes Contador1 , correct, but ... what happens with Contador2 ? Do not initialize it then it will have a random value with practically 50% chance of being less than 4 ... think that -100000000 is less than 4 but it is not an index you would like to access ...

To avoid this type of misleading it is preferable, in iteration of ranges, to use for . Also consider reducing the life of the variables to the minimum essential ... reusing variables helps certain errors are difficult to detect:

for( int Contador1=0; Contador1<4; Contador1++ )
{
  for( int Contador2=0; Contador2<4; Contador2++ )
    Matriz[Contador1][Contador2] = 0;
}

By the way, to calculate the coordinates of the table you use this:

y=c1/4;
x=c1-(4*y);

When you could take advantage of the module operation:

y=c1/4;
x=c1%4;

Not only does it create a cleaner code ... it is also a significantly faster solution and the reason is that when the processor calculates the division it usually stores the rest in some registry and vice versa, then putting the two operations together can lead to to have the program reuse those values instead of calculating them again.

This can be checked by examining the assembler that is generated with the following code:

int func1(int a, int b)
{
  return a/b;
}

int func2(int a, int b)
{
  return a%b;
}

int func3(int a, int b)
{
  return a/b + a%b;
}

Which turns out to be the following:

func1(int, int):
    mov     eax, edi
    cdq
    idiv    esi
    ret
func2(int, int):
    mov     eax, edi
    cdq
    idiv    esi
    mov     eax, edx
    ret
func3(int, int):
    mov     eax, edi
    cdq
    idiv    esi
    add     eax, edx
    ret

Here we see how only one division operation is carried out in each function, although in the last one, two divisions are apparently done (the division itself and the one that allows calculating the module).

You can see the example working here

    
answered by 05.12.2017 в 07:38