Home > Blockchain >  Check the win by means of bool function
Check the win by means of bool function

Time:11-09

// Main e conversão controlada de int via igualdade --> teste 1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define SPN 4
#define BTT 10 
#define MARKER 'X'



void print_card(int matriz[SPN][SPN], int list[BTT], int i)
{
    printf("\n");
    for(int j=0; j<SPN; j  )
    {
        for(int n=0; n<SPN; n  )
        {
            if(matriz[j][n] == 88 || matriz[j][n] == list[i])
            {
                matriz[j][n]=MARKER;
                printf("|");
                printf(",", matriz[j][n]);
            }
            else
            {
                printf("|");
                printf("-", matriz[j][n]);
            }
        }
        printf("|\n");
    }
    printf("\n");
    printf(" e o numero escolhido e: -", list[i]);
    printf("\n");
    getchar();
}



bool victoria_arow(int matriz[SPN][SPN])
{
    int j=0;
    int i=0;
    for(int i=0; i<SPN; i  )
    {
        if(matriz[i][j]==88 && matriz[i][j 1]==88 && matriz[i][j 2]==88 && matriz[i][j 3]==88 && matriz[i][j 4]==88)
        {
            return true;
        }
        else
        {
            return false;
       }
    }
}



bool victoria_coll(int matriz[SPN][SPN])
{
    int j=0;
    int i=0;
    for(int i=0; i<SPN; i  )
    {
        if(matriz[i][j]==88 && matriz[i 1][j]==88 && matriz[i 2][j]==88 && matriz[i 3][j]==88 && matriz[i 4][j]==88)
        {
            return true;
        }
        else
        {
            return false;
       }
    }
}



int main()
{
    int pirolito[SPN][SPN]={11,10,8,9,4,3,4,17,9,16,15,13,12,15,1,8};
    int retardado[BTT]={11,10,8,9,11,3,4,17,12,16};
    
    srand(time(NULL));
    
    for(int n=0; n<BTT; n  )
    {
        print_card(pirolito, retardado, n);
        if(victoria_arow(pirolito) || victoria_coll(pirolito))
        {
            printf("vitoria");
            break;
        }
    }
    
    
    return 0;
}

The idea behind the code is primarily with the matrix (pirolito) and list (retardado), both predefined, check equalities between them and when affirmative the number in question is exchanged for 88 and printed as 'X', in the second part, in the vitória_coll and victória_row function, the matrix is traveled looking for equalities between the same matrix but only when the number is 88 and if there is a row or column equal break the code. Like that:

| X|10| 8| 9| | 4| 3| 4|17| | 9|16|15|13| |12|15| 1| 8|

e o numero escolhido e: 11

| X| X| 8| 9| | 4| 3| 4|17| | 9|16|15|13| |12|15| 1| 8|

e o numero escolhido e: 10

| X| X| X| 9| | 4| 3| 4|17| | 9|16|15|13| |12|15| 1| X|

e o numero escolhido e: 8

| X| X| X| X| | 4| 3| 4|17| | X|16|15|13| |12|15| 1| X|

e o numero escolhido e: 9

vitoria

CodePudding user response:

In victoria_arow and victoria_coll you will always return in the very first iteration of the loop. Drop the else part, and return false after the loop instead.

For example:

bool victoria_arow(int matriz[SPN][SPN])
{
    int j=0;
    int i=0;
    for(int i=0; i<SPN; i  )
    {
        if(matriz[i][j]==88 && matriz[i][j 1]==88 && matriz[i][j 2]==88 && matriz[i][j 3]==88 && matriz[i][j 4]==88)
        {
            return true;
        }
    }

    return false;
}

The above solves only *one of the problems in your code. You have a much worse problem: You use matriz[i][j 4] where j 4 will access the fifth element in your four-element array.

Going out of bounds of an array like that leads to undefined behavior.

  • Related