0

Btw it had no answer when i search so plz don't close it :)
Before we start this code is for Tic-Tac-Toe game

#include <stdio.h>

#define xo printf("\n\t %c │ %c │ %c\n\t───┼───┼───\n\t %c │ %c │ %c\n\t───┼───┼───\n\t %c │ %c │ %c\n",game[0],game[1],game[2],game[3],game[4],game[5],game[6],game[7],game[8]);

#define clr printf("\033[0;0H\033[2J");//only in linux

#define W getchar();getchar();//wait for 'enter'

#define P printf("\nChoose a number to play in it\n");scanf("%d",&play);

int place(char t[],int play,int k);

int main(void)
{
  char game[10]="123456789";
  int play;
  do
  {
  clr xo P
  if(!place(game, play, 1))
  {
    printf("\nWrong Input\nPress enter to continue\n");
    W
  }
  printf("%d",place(game, play, 0));W
  }while( place(game, play, 0)!=3 );
  clr xo P
  return 0;
}

int place(char t[],int play,int k)
{
  int d;
    switch (t[play-1])
    {
      case'X':
      {
        d=0;
        break;
      }
      case 'O':
      {
        d=0;
        break;
      }
      default:
      {
        if(k==0)
          t[play-1]='X';
        else
          t[play-1]='O';
          /*
        if(!verification(t,k,play))
          return 1;
        else
          return 3;*/
        //printf("******\n");W
        d=1;
        break;
      }
    }
  printf("%d----\n",d);
  return d;
}

(sorry for my bad English and anyway thank u for only enter and try help)
That's my code the problem is in the function i don't know why but it just return 0 for no reason by the way
the zero that the function return the main just don't think it's a zero and don't think it's what i want to return i don't know why
my bug
if u want to try it with yourself: my replit code

s1f2z3
  • 1
  • 1
  • 6
    `clr xo P`. No no no. Don't abuse macros like that. It just makes your code difficult to read and debug. I would go so far as to refuse to work on or debug such code. – kaylum Nov 05 '21 at 10:41
  • You should be range-testing `play` before using it to address an array. – paddy Nov 05 '21 at 10:43
  • 1
    char game[9] = "123456789"; : you put 10 chars in 9 char array. You forget \0. – Ptit Xav Nov 05 '21 at 10:43
  • 1
    `play` is uninitialized and used to access index `t[play-1]` – TruthSeeker Nov 05 '21 at 10:44
  • @TruthSeeker : initialized by xo – Ptit Xav Nov 05 '21 at 10:55
  • 2
    @PtitXav: `char game[9] = "123456789";` does not put ten characters into a nine-character array. Initialization with a string does not put the terminating null character into the array if there is no room for it, per C 2018 6.7.9 14. And `xo` does not initialize `play`. `P` does, if the `scanf` succeeds. – Eric Postpischil Nov 05 '21 at 11:37
  • @EricPostpischil : thanks for the info – Ptit Xav Nov 05 '21 at 15:32
  • the size of char it's not the cause cuz i fix it and still same problem , i think the roots problem is the if else in `play`, idk why but if i remove it , it will work fine @EricPostpischil @PtitXav – s1f2z3 Nov 06 '21 at 12:34

0 Answers0