Click here to Skip to main content
15,436,489 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Two doubts about this code :

# What is the condition which will show if the game results in a tie ?
# If the choice the player choose is already taken , how did i do to make he choose again ?
C++
char mat[9];

int main(void)
{
  int i,jogada,casa,jogo=0;

  for(i=0; i<= 9; i++) 
    mat[i] = ' ';

 do{
  printf("\n");
  printf("\t|\t|\n");
  printf("\t|\t|\n");
  printf("%c\t|%c\t|%c\n",mat[1],mat[2],mat[3]);
  printf("--------|-------|-------\n");
  printf("\t|\t|\n");
  printf("%c\t|%c\t|%c\n",mat[4],mat[5],mat[6]);
  printf("--------|-------|-------\n");
  printf("\t|\t|\n");
  printf("\t|\t|\n");
  printf("%c\t|%c\t|%c\n\n",mat[7],mat[8],mat[9]);
  printf("\nJOGADOR X ");
  printf("\nInforme uma posicao livre (1-9): ");
  scanf("%d",&casa);
  }while((casa>=1)&&(casa<=9));

  if((casa>=1)&&(casa<=9))
  {

  if(mat[casa] == 'X' || mat[casa] == 'O')
     printf(" ");
  else     
     mat[casa] = 'X';

  }      
  printf("\n");
  printf("\t|\t|\n");
  printf("\t|\t|\n");
  printf("%c\t|%c\t|%c\n",mat[1],mat[2],mat[3]);
  printf("--------|-------|-------\n");
  printf("\t|\t|\n");
  printf("%c\t|%c\t|%c\n",mat[4],mat[5],mat[6]);
  printf("--------|-------|-------\n");
  printf("\t|\t|\n");
  printf("\t|\t|\n");
  printf("%c\t|%c\t|%c\n\n",mat[7],mat[8],mat[9]);



  if(mat[1] != ' ' && mat[1] == mat[2] && mat[2] == mat[3]) {
              jogo++;
              printf("X Ganhou!\n"); 
              break;  }
       else if(mat[4] != ' ' && mat[4] == mat[5] && mat[5] == mat[6]) {
              printf("X Ganhou!\n");
              jogo++;
              break; }
       else if(mat[7] != ' ' && mat[7] == mat[8] && mat[8] == mat[9]) {
              printf("X Ganhou!\n");
              jogo++;
                break; }
       else if(mat[1] != ' ' && mat[1] == mat[4] && mat[4] == mat[7]) {
              printf("X Ganhou!\n");
              jogo++; 
                break;}
       else if(mat[2] != ' ' && mat[2] == mat[5] && mat[5] == mat[8]) {
              printf("X Ganhou!\n");
              jogo++;
                break; }
       else if(mat[3] != ' ' && mat[3] == mat[6] && mat[6] == mat[9]) {
              printf("X Ganhou!\n");
              jogo++;
                break; }
       else if(mat[1] != ' ' && mat[1] == mat[5] && mat[5] == mat[9]){ 
               printf("X Ganhou!\n");
              jogo++; 
                break;}
       else if(mat[7] != ' ' && mat[7] == mat[5] && mat[5] == mat[3]) { 
               printf("X Ganhou!\n"); 
              jogo++;
                break; }
       else if((mat[1] != ' ') && (mat[2] != ' ') && (mat[3] != ' ') && (mat[4] != ' ') && (mat[5] != ' ') && (mat[6] != ' ') && (mat[7] != ' ') && (mat[8] != ' ') && (mat[9] = ' ')){
                printf("Velha!\n");
                jogo++;
                break;
                }

     printf("\nJOGADOR O ");
     printf("\nInforme uma posicao livre (1-9): ");
     scanf("%d",&casa);
     if((casa>=1)&&(casa<=9))
     {

     if(mat[casa] == 'X' || mat[casa] == 'O')
     ;
     else     
        mat[casa] = 'O';

     }            
     printf("\n");
     printf("\t|\t|\n");
     printf("\t|\t|\n");
     printf("%c\t|%c\t|%c\n",mat[1],mat[2],mat[3]);
     printf("--------|-------|-------\n");
     printf("\t|\t|\n");
     printf("%c\t|%c\t|%c\n",mat[4],mat[5],mat[6]);
     printf("--------|-------|-------\n");
     printf("\t|\t|\n");
     printf("\t|\t|\n");
     printf("%c\t|%c\t|%c\n\n",mat[7],mat[8],mat[9]);


       if(mat[1] != ' ' && mat[1] == mat[2] && mat[2] == mat[3]) {
              printf("O Ganhou!\n");  
              jogo++;
                break; }
       else if(mat[4] != ' ' && mat[4] == mat[5] && mat[5] == mat[6]) {
              printf("O Ganhou!\n");
              jogo++;
                break; }
       else if(mat[7] != ' ' && mat[7] == mat[8] && mat[8] == mat[9]) {
              printf("O Ganhou!\n");
              jogo++;
                break; }
       else if(mat[1] != ' ' && mat[1] == mat[4] && mat[4] == mat[7]) {
              printf("O Ganhou!\n");
              jogo++;
                break; }
       else if(mat[2] != ' ' && mat[2] == mat[5] && mat[5] == mat[8]) {
              printf("O Ganhou!\n");
              jogo++; 
                break;}
       else if(mat[3] != ' ' && mat[3] == mat[6] && mat[6] == mat[9]) {
              printf("O Ganhou!\n");
              jogo++;
                break; }
       else if(mat[1] != ' ' && mat[1] == mat[5] && mat[5] == mat[9]){ 
               printf("O Ganhou!\n");
              jogo++; 
                break;}
       else if(mat[7] != ' ' && mat[7] == mat[5] && mat[5] == mat[3]) { 
               printf("O Ganhou!\n"); 
              jogo++;
                break; }
      else if((mat[1] != ' ') && (mat[2] != ' ') && (mat[3] != ' ') && (mat[4] != ' ') && (mat[5] != ' ') && (mat[6] != ' ') && (mat[7] != ' ') && (mat[8] != ' ') && (mat[9] = ' ')){
                printf("Velha!\n");
                jogo++;
                break;
                }

}

  system("PAUSE");  
  return 0;

}
Posted
Updated 30-Jun-13 10:36am
v2
Comments
BiteForce 30-Jun-13 12:09pm     CRLF
add another else if, where you check whether no field is empty, if this conditon is true, then the game results in a tie. OR you create an integer, which you increase after each round and if the integer is higher than the number of fields, it will be a dice. BG
João Guilherme Silva 30-Jun-13 12:31pm     CRLF
Thanks! That worked! But about the second question, do you have any idea ?
BiteForce 30-Jun-13 16:18pm     CRLF
You´re welcome. Actually, I don´t understand your second question? Could you explain your problem in more detail to me?
João Guilherme Silva 30-Jun-13 16:30pm     CRLF
My question is the following : if the choice the player picks is already taken, how do I make him/her choose again? If I compile the code now, it will change the the position for the one the user chose, and this can't happen. Obs: the "drawing" of the tic-tac-toe game should appear before this ("\nInform your choice (1-9): ") Obs2: I've changed the code in the question .
_Maxxx_ 30-Jun-13 22:01pm     CRLF
Where you are checking for either x or o to win, it would be far easier to write that code as a function into which you pass either X or O - it can then return a boolean telling you whether that player has won or not. Same with drawing the grid; put it in a function so you don't have duplicate code. It might be pedantic at this level of coding, but it will save you heaps of time if you need to make changes as there's only one place to change it! Oh - and there's a bug in your code as you are checking the mat array from 1 through to 9 but it is a zero based index so should be from 0 to 8 - now, if only you'd written the code in a function you'd only have one place to change it :)

1 solution

For the 2nd part you probablt need to change your

while((casa>=1)&&(casa<=9));


to add in

C++
&& mat[casa] == ' '


In other words, keep requesting input until the number is between 1 and 9 AND the value of the square at that number is empty
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900