d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C Program Tic Tac Toe > Need Help Finishing
Add Reply New Topic New Poll
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 12 2013 05:51pm
here is my code so far
Code
#include <stdio.h>
#include <stdlib.h>
void ShowTable(char [][3]);
void ResetTable(char[] [3]);
char Marker[][3]={'1', '2', '3', '4', '5', '6', '7', '8', '9'};
int main(void)
{
char player_symbol = 'X';
char player_turn = '1';
char player_move;
char play_again = 'Y';
int R,C,times_played = 0; // just for FOR Loop

ShowTable(Marker);
do{
                 printf("Player No. %c turn \n", player_turn);
                 scanf("%c", &player_move);
                 for ( R = 0; R<3; R++)
                 {
                     for(C = 0; C<3; C++)
                     {
                           if(Marker[R][C] == player_move)
                           {
                                           Marker[R][C] = player_symbol;
                           }
                     }
                 }
                 for (R=0; R<3; R++)
                 {
                     if((Marker[R][0] == Marker[R][1] && Marker[R][1] == Marker[R][2]) || (Marker[0][R] == Marker[1][R]&& Marker[1][R] == Marker[2][R]))
                     {
                         ShowTable(Marker);
                         printf("Player No. %c wins\nCongratulations\n\n", player_turn);
                         printf("Do you want to player again\nEnter Y for yes ->\n");
                         scanf("%c", &play_again);
                         if(play_again == 'Y' || play_again == 'y')
                         {
                             ResetTable(Marker);
                             play_again = 'Y';
                             player_turn = '2';
                             times_played = 0;
                         }
                     }
                 }                    
                 if((Marker[0][0] == Marker[1][1] && Marker[1][1] == Marker[2][2]) || (Marker[0][2] == Marker[1][1]&& Marker[1][1] == Marker[2][0]))
                 {
                     ShowTable(Marker);
                     printf("Player No. %c wins\nCongratulations\n", player_turn);
                     printf("Do you wnat to player again\nEnter Y for yes ->\n");
                     scanf("%c", &play_again);
                     if(play_again == 'Y' || play_again == 'y')
                     {
                         ResetTable(Marker);
                         play_again = 'Y';
                         player_turn = '2';
                         times_played = 0;
                     }
                 }
                 if( play_again == 'Y')
                 {
                             if(player_turn == '1')
                             {
                                            player_turn = '2';
                                            player_symbol = 'O';
                                            ShowTable(Marker);
                             }
                                            else
                                            {
                                                player_turn = '1';
                                                player_symbol = 'X';
                                                ShowTable(Marker);
                                            }
                 }
                                                times_played++;
                                                if(times_played == 9)
                                                {
                                                printf("There's no winner\n\nDo you want to play again?\nEnter Y for yes->\n");
                                                scanf("%c", &play_again);
                                                if(play_again == 'Y' || play_again == 'y')
                                                              {
                                                              ResetTable(Marker);
                                                              play_again = 'Y';
                                                              player_turn = '1';
                                                              times_played = 0;
                                                              ShowTable(Marker);
                                                              }
                                                }
                 }while(play_again == 'Y');
}
void ShowTable(char M[][3])
{
   int i,j;
   for(i=0;i<3;i++)
   {
                   printf("| ");
                   for(j=0;j<3;j++)
                   {
                                   printf("%c | ", M[i][j]);
                   }
                                   printf("\n\n");
   }
}
void ResetTable(char M[][3])
{
    char cChar = '1';
    int R,C;
    for(R=0; R<3; R++)
    {
             for(C=0; C<3; C++)
             {
             M[R][C] = cChar;
             cChar++;
             system ("pause");
             }
    }
}


there are major flaws I forgot about
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 12 2013 11:24pm
what do you need help with?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 13 2013 12:46am
Quote (carteblanche @ Mar 13 2013 01:24am)
what do you need help with?


this. if you need help then post what you need help with in a way that others can convey a clear meaning of. posts like these are the reason people call you retarded and you dont get help with what you need.
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 13 2013 06:22pm
the problem is, that is skips player 2's turn and also does not display the play again menu after a win lose or cat
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 13 2013 07:58pm
Quote (Xarai @ Mar 13 2013 08:22pm)
the problem is, that is skips player 2's turn and also does not display the play again menu after a win lose or cat


go use a debugger on your application and go through it line by line. i can assure you that i just did and there are more than one problem with your code both logically and programically.

as for your player 2 issue its because scanf() is reading in the last character in the buffer (which is the newline when you hit enter) causing it to seem like it skipped. to fix this go google how to clear the STDIN buffer. there is a C function you can call to do this and it can be found on cprogramming.com or cplusplus.com
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 16 2013 08:35pm
ok well i finally finished this one
finished product isssss

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

char matrix[3][3];  /* the tic tac toe matrix */

char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);

int main(void)
{
 char done;

 printf("This is the game of Tic Tac Toe.\n");
 printf("You will be playing against the computer.\n");

 done =  ' ';
 init_matrix();

 do {
   disp_matrix();
   get_player_move();
   done = check(); /* see if winner */
   if(done!= ' ') break; /* winner!*/
   get_computer_move();
   done = check(); /* see if winner */
 } while(done== ' ');

 if(done=='X') printf("You won!\n");
 else printf("I won!!!!\n");
 disp_matrix(); /* show final positions */

 return 0;
}

/* Initialize the matrix. */
void init_matrix(void)
{
 int i, j;

 for(i=0; i<3; i++)
   for(j=0; j<3; j++) matrix[i][j] =  ' ';
}

/* Get a player's move. */
void get_player_move(void)
{
 int x, y;

 printf("Enter X,Y coordinates for your move: ");
 scanf("%d%*c%d", &x, &y);

 x--; y--;

 if(matrix[x][y]!= ' '){
   printf("Invalid move, try again.\n");
   get_player_move();
 }
 else matrix[x][y] = 'X';
}

/* Get a move from the computer. */
void get_computer_move(void)
{
 int i, j;
 for(i=0; i<3; i++){
   for(j=0; j<3; j++)
     if(matrix[i][j]==' ') break;
   if(matrix[i][j]==' ') break;
 }

 if(i*j==9)  {
   printf("draw\n");
   exit(0);
 }
 else
   matrix[i][j] = 'O';
}

/* Display the matrix on the screen. */
void disp_matrix(void)
{
 int t;

 for(t=0; t<3; t++) {
   printf(" %c | %c | %c ",matrix[t][0],
           matrix[t][1], matrix [t][2]);
   if(t!=2) printf("\n---|---|---\n");
 }
 printf("\n");
}

/* See if there is a winner. */
char check(void)
{
 int i;

 for(i=0; i<3; i++)  /* check rows */
   if(matrix[i][0]==matrix[i][1] &&
      matrix[i][0]==matrix[i][2]) return matrix[i][0];

 for(i=0; i<3; i++)  /* check columns */
   if(matrix[0][i]==matrix[1][i] &&
      matrix[0][i]==matrix[2][i]) return matrix[0][i];

 /* test diagonals */
 if(matrix[0][0]==matrix[1][1] &&
    matrix[1][1]==matrix[2][2])
      return matrix[0][0];

 if(matrix[0][2]==matrix[1][1] &&
    matrix[1][1]==matrix[2][0])
      return matrix[0][2];

 return ' ';
}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll