0

Enter X, Y coordinates for your move : 2,3
_0|_0|__
_X|_X|_X
| |
Congratulation Player 1 ! you have WON!
Want to play again? y/n

How can I reset the array in order to continue playing after finish playing the first round?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char array[5][5]={' ','|',' ','|',' ','-',' ','-',' ','-',' ','|',' ','|',' ','-',' ','-',' ','-',' ','|',' ','|',' '};
void display();
char check(void);

int main()
{
    int i=0,playa=1,f,z,t,conti=1;
    char done,var1='y';


    for (t=1;t>0;t++)
    {
        if (conti==1)
        {

            for(i=0;i<9;i++)
            {
                if(playa==1)
                {

                printf("Player %d's turn.\n\n",playa);
                display();
                printf("\nEnter X,Y coordinates for your move :");
                scanf_s("%d %d",&f,&z);
                if (f==1&&z==1)
                {
                    array[0][0]='X';
                }
                if (f==1&&z==2)
                {
                    array[0][2]='X';
                }
                if (f==1&&z==3)
                {
                    array[0][4]='X';
                }
                if (f==2&&z==1)
                {
                    array[2][0]='X';
                }
                if (f==2&&z==2)
                {
                    array[2][2]='X';
                }
                if (f==2&&z==3)
                {
                    array[2][4]='X';
                    }
                if (f==3&&z==1)
                {
                    array[4][0]='X';
                }
                if (f==3&&z==2)
                {
                    array[4][2]='X';
                }
                if (f==3&&z==3)
                {
                    array[4][4]='X';
                }   
                display();
                if(check()  != 'c')
                break;
                playa++;
                }

                else 
                {
                    printf("Player %d's turn.\n\n",playa);
                    display();
                    printf("\nEnter X,Y coordinates for your move :");
                    scanf_s("%d %d",&f,&z);
                    if (f==1&&z==1)
                    {
                        array[0][0]='O';
                    }
                    if (f==1&&z==2)
                    {
                        array[0][2]='O';
                    }
                    if (f==1&&z==3)
                    {
                        array[0][4]='O';
                    }
                    if (f==2&&z==1)
                    {
                        array[2][0]='O';
                    }
                    if (f==2&&z==2)
                    {
                        array[2][2]='O';
                    }
                    if (f==2&&z==3)
                    {
                        array[2][4]='O';
                    }
                    if (f==3&&z==1)
                    {
                        array[4][0]='O';
                    }
                    if (f==3&&z==2)
                    {
                        array[4][2]='O';
                    }
                    if (f==3&&z==3)
                    {
                        array[4][4]='O';
                    }               
                    display();
                    if(check()  != 'c')
                    break;
                    --playa;
                }

            }

            done=check();

            if(done=='X')
            {   printf("\nthe winner is player 1\n");
            char array[5][5]={' ','|',' ','|',' ','-',' ','-',' ','-',' ','|',' ','|',' ','-',' ','-',' ','-',' ','|',' ','|',' '};
            }
            if(done=='O')
            {   printf("\nthe winner is player 2\n");
            }
            if(done!='X'&&done!='O')
            {   printf("No winners");
            }

            printf("\n\nPlease input 1 to continue, 0 to exit. ");
            scanf_s("%d",&conti);

        }
        else if(conti==0)
        {

            return 0;
        }
        else
        {

            printf("Please enter either 1 to continue, 0 to exit. ");
            scanf_s("%d",&conti);

        }


    }
        return 0;
}

char check(void)
{

 char letter='c';

 {
     if(array[0][0]==array[0][2] && array[0][0]==array[0][4]&&array[0][0]!= ' ') 
         letter= array[0][0]; 

     if(array[2][0]==array[2][2] && array[2][0]==array[2][4]&&array[2][0]!= ' ') 
         letter= array[2][0]; 

     if(array[4][0]==array[4][2] && array[4][0]==array[4][4]&&array[4][0]!= ' ') 
         letter= array[4][0]; 

 }

 {
     if(array[0][0]==array[2][0] && array[0][0]==array[4][0] &&array[0][0]!= ' ') 
         letter= array[0][0]; 

     if(array[0][2]==array[2][2] && array[0][2]==array[4][2] &&array[0][2]!= ' ') 
         letter= array[0][2]; 

     if(array[0][4]==array[2][4] && array[0][4]==array[4][4] &&array[0][4]!= ' ') 
         letter= array[0][4];

 }
 {
     if( array[0][0]==array[2][2] && array[0][0]==array[4][4] &&array[0][0]!= ' ')
         letter= array[0][0];

 }
 {
     if( array[0][4]==array[2][2] && array[0][4]==array[4][0]&&array[0][4]!= ' ' )
         letter= array[0][4];

 }
return letter;
}

void display()
{
    int i,l;

    for(i=0;i<=4;i++)
    {
        for(l=0;l<=4;l++)
        {
            printf("%c",array[i][l]);
        }

        printf("\n");
    }
}
2
Contributors
1
Reply
24
Views
3 Years
Discussion Span
Last Post by gusano79
0

You could write a function to loop over array, assigning the appropriate values to the right places.

You could have a duplicate of array that you don't change during the game, then just copy it into array when you want to reset.

My advice is that you don't mix the board-drawing parts like '|' with the game state. Just have a 3x3 array for the game, and handle drawing the lines in display(). Then all you have to do to reset is set everything in that array to spaces.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.