change number 3 in line [closed]

0

I have to do a tic-tac-toe and I do not change the file in the site that is chosen and I do not know why I leave the code here, thanks in advance

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>



int main(){

    char j1[50];
    char j2[50];
    char numerost[2][2];
    char a,f1,f2;

    int i,j,fpre1,fpre2,lugar,n,aux;

do{

    {   //jugadores(hecho)

    system("cls");
    printf("bienvenidoa Tres en Raya.\n");
    printf("Presione una tecla para comenzar.\n");

    getch();
    system("cls");

    printf("Indique nombre del jugador 1:");
    //gets(j1);
    scanf("%s",&j1);
    fflush(stdin);

    system("cls");

    printf("Indique nombre del jugador 2:");
    //gets(j2);
    scanf("%s",&j2);
    fflush(stdin);

    system("cls");

    }
    {   //eleccion ficha jugadores (hecho)
    {//jugador 1
    do{

        printf("%s,elija ficha con la que jugar.\n",j1);

        printf("1.X.\n");
        printf("2.O.\n");
        printf("3.€.\n");
        printf("4.$.\n");
        printf("5.@.\n");

            scanf("%d",&fpre1);
            fflush(stdin);

        switch(fpre1){


            case 1:
                f1='X';
                fflush(stdin);
                break;

            case 2:
                f1='O';
                fflush(stdin);
                break;

            case 3:
                f1='€';
                fflush(stdin);
                break;

            case 4:
                f1='$';
                fflush(stdin);
                break;

            case 5:
                f1='@';
                fflush(stdin);
                break;


            default: printf("introduzca un numero valido");
                break;
    }
}
    while(0>fpre1||fpre1>5);

    printf("%s jugara con %c\n\n",j1,f1);}

    {//jugador 2

    do{
        printf("%s,elija ficha con la que jugar.\n",j2);

        printf("1.X.\n");
        printf("2.O.\n");
        printf("3.€.\n");
        printf("4.$.\n");
        printf("5.@.\n");

            scanf("%d",&fpre2);
            fflush(stdin);



        switch(fpre2){

            case 1:
                f2='X';

                break;

            case 2:
                f2='O';

                break;

            case 3:
                f2='€';

                break;

            case 4:
                f2='$';

                break;

            case 5:
                f2='@';

                break;

            default: 
            printf("introduzca un numero valido\n");
                break;
        }
        if(fpre1==fpre2){
                printf("las fichas no pueden ser iguales.\n\n");
            }
        else{
            continue;
        }
    } 
    while(fpre1==fpre2||0>fpre2||fpre2>5);

    printf("%s jugara con %c\n\n",j2,f2);}




    }
    {   //tablero(hecho)
    {   //numeros tablero(hecho)
    a='1';
        for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                numerost[i][j]=a++;
            }
        }   
    }
    {   //tablero estructura(hecho)
        for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                if(j<2){

                printf(" %c|",numerost[i][j]);
                }

                else{
                    printf(" %c",numerost[i][j]);
                }

        }
        if(i<2){
        printf("\n---------");
        printf("\n");
        }
        else{
            printf("\n");
        }   
            }

    }
    }
    {   //colocacion de fichas 

    do{

        printf("Coloca la ficha.\n");
        scanf("%d",&aux);
        fflush(stdin);

    }while(aux<1||aux>9);


        switch(aux){
            case'1':
                i=0;
                j=0;
                if(numerost[i][j]==f1||numerost[i][j]==f2){
                    printf("La casilla esta ocupada.\n");
                    }
                break;  
            case'2':
                i=0;
                j=1;
                if(numerost[i][j]==f1||numerost[i][j]==f2){
                    printf("La casilla esta ocupada.\n");
                    }
                    break;
            case'3':
                i=0;
                j=2;
                if(numerost[i][j]==f1||numerost[i][j]==f2){
                    printf("La casilla esta ocupada.\n");
                    }
                    break;  
            case'4':
                i=1;
                j=0;
                if(numerost[i][j]==f1||numerost[i][j]==f2){
                    printf("La casilla esta ocupada.\n");
                    }
                    break;
            case'5':
                i=1;
                j=1;
                if(numerost[i][j]==f1||numerost[i][j]==f2){
                    printf("La casilla esta ocupada.\n");
                    }
                    break;
            case'6':
                i=1;
                j=2;
                if(numerost[i][j]==f1||numerost[i][j]==f2){
                    printf("La casilla esta ocupada.\n");
                    }
                    break;
            case'7':
                i=2;
                j=0;
                if(numerost[i][j]==f1||numerost[i][j]==f2){
                    printf("La casilla esta ocupada.\n");
                    }
                    break;
            case'8':
                i=2;
                j=1;
                if(numerost[i][j]==f1||numerost[i][j]==f2){
                    printf("La casilla esta ocupada.\n");
                    }       
                    break;
            case'9':
                i=2;
                j=2;
                if(numerost[i][j]==f1||numerost[i][j]==f2){
                    printf("La casilla esta ocupada.\n");
                    }
                    break;      

        }
        numerost[i][j]=f1;

        {   //tablero(hecho)
    {   //numeros tablero(hecho)
    a='1';
        for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                numerost[i][j]=a++;
            }
        }   
    }
    {   //tablero estructura(hecho)
        for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                if(j<2){

                printf(" %c|",numerost[i][j]);
                }

                else{
                    printf(" %c",numerost[i][j]);
                }

        }
        if(i<2){
        printf("\n---------");
        printf("\n");
        }
        else{
            printf("\n");
        }   
            }

    }
    }       








    }
    {   //jugar otra vez
    printf("¿desea volver a jugar?\n");
    printf("1.Si.\n");
    printf("2.No.\n");
    scanf("%d",&n);
    }
    }while(n==1); 

}
    
asked by juanso 20.05.2018 в 20:51
source

1 answer

2

Apart from the general structure of the code, which is very improvable (you have a lot of repeated code, which could be removed from functions, you have blocks delimited by keys without knowing why, since they are not the body of any loop or conditional, etc.), you have some important errors that you must solve before continuing to work on your solution. They are the following:

  • The variable aux on which you read the response of the player that indicates which box you want to play, has been declared as int , and is read correctly as such ( "%d" ) but the switch (aux) that follows the variable as type char , since the cases listed are '1' , '2' , etc. You should remove the quotes around those cases.

    It would be even better if instead of a switch with a repeated amount of code, you would try to find a formula that allows you to calculate the row and column values from the number given by the player. For example, the quotient of dividing by 3 could be the row, and the rest of the division could be the column.

  • Once you have stored the player's "card" in the box it has indicated to you, a loop comes:

        {   //numeros tablero(hecho)
            a='1';
            for(i=0;i<3;i++){
                for(j=0;j<3;j++){
                    numerost[i][j]=a++;
                }
            }
       }
    

    This code, besides having those inexplicable keys delimiting it, rewrites the positions of the array with the digits 1 to 9, thus erasing the previous play. Remove this piece of code so that the board correctly contains the previous moves.

  • On the other hand, I understand that after showing this board I should ask the second player for a play again, but instead the game ends. I understand that you are still working on this part.

        
    answered by 20.05.2018 в 23:02