Tic - Tac - Toe...

This is a discussion on Tic - Tac - Toe... within the Game Programming forums, part of the General Programming Boards category; Hey people, I've made an attempt at a tic tac toe game, it's not completely finished yet ( the player ...

  1. #1
    TheUnknowingOne
    Guest

    Post Tic - Tac - Toe...

    Hey people,

    I've made an attempt at a tic tac toe game, it's not completely finished yet ( the player turns bit hasn't been sorted out, and various other things), but it draws the grid and allows me to input the X's and O's, thing is when checking for a win condition something goes horribly wrong... Here's my code, any pointers are appreciated:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #include <ctype.h>
    
    
    #define BOARD_PTRS char *tl, char *tc, char *tr, char *ml,char *mc,char *mr,char *bl,char *bc,char *br
    /*BOARD_PTS is to be used in function headers*/
    #define BOARD tl,tc,tr,ml,mc,mr,bl,bc,br
    /*BOARD is to be used when passing values to a function*/
    #define PLAYER char ply1[20],char ply2[20]
    /*PLAYER is to be used in function headers*/
    #define PLAYERS ply1, ply2
    /*PLAYERS is to be used when passing values to a function*/
    
    /*Functions*/
    void play_game(BOARD_PTRS,PLAYER);
    char options (PLAYER);
    void exit_game (void);
    int win_condition(BOARD_PTRS);
    void draw_grid(BOARD_PTRS);
    void play1(BOARD_PTRS,char ply1[20]);
    void play2(BOARD_PTRS,char ply2[20]);
    
    
    
    
    int main(void)
    	{
    
    	int option = 0;
    
    	char *tl,*tc,*tr,*ml,*mc,*mr,*bl,*bc,*br;
       char ply1[20] = "Player 1", ply2[20] = "Player 2";
       char grid [3][3] = {{0,0,0},
      						     {0,0,0},
                           {0,0,0}};
    
    
       tl = &grid[0][0];
       tc = &grid[1][0];
       tr = &grid[2][0];
       ml = &grid[0][1];
       mc = &grid[1][1];
       mr = &grid[2][1];
       bl = &grid[0][2];
       bc = &grid[1][2];
       br = &grid[2][2];
    
    
    
       while(option != 4)
       	{
       	 clrscr();
    		 printf("\n\n\t\t\t Noughts and Crosses");
    		 printf("\n\t\t\t    by Adam Leach\n");
    		 printf("\n\n\t\t\t Select an option:");
    		 printf("\n\t\t\t 1. Play game");
    		 printf("\n\t\t\t 2. Options");
    		 printf("\n\t\t\t 3. Exit game");
           printf("\n\t\t\t -> ");
    		 scanf ("%d", &option);
           while( getchar() != '\n' );
    
       	switch(option)
       		{
          	case 1:play_game(BOARD,PLAYERS);
             		 break;
          	case 2:options(PLAYERS);
             		 break;
          	
          	
          	case 3:printf("\n\n\t\t\t Exiting Game");
                    getch();
          			 break;
          	default: ;
             }
        	}
    	}
    
    
    
    /* Function definations*/
    
    
    void play_game(BOARD_PTRS,PLAYER)
    	{
    
    
    		while(!win_condition(BOARD))
          {
    
     		clrscr();
    		printf("\n\n\t\t\t   NOUGHTS AND CROSSES\n\n\n");
    		draw_grid(BOARD);
    		printf("\n\n\t When entering the grid-reference enter the number first,\n\t then"
    				 " the letter, e.g. 1A, 2C, etc");
    
       	play1(BOARD,ply1);
    
       	clrscr();
    		printf("\n\n\t\t\t   NOUGHTS AND CROSSES\n\n\n");
    	 	draw_grid(BOARD);
    		printf("\n\n\t When entering the grid-reference enter the number first,\n\t then"
    				 " the letter, e.g. 1A, 2C, etc");
    
        	play2(BOARD,ply2);
    
    		}
       getch();
    	}
    
    
    char options (PLAYER)
    {
    
    int select = 0;
    
    while (select != 3)
    	{
       clrscr();
    	printf("\n\n\t\t\t\tOPTIONS\n\n\n");
    	printf("\n1. Change Player 1 name - currently %s\n", ply1);
    	printf("\n2. Change Player 2 name - currently %s\n", ply2);
    	printf("\n3. Back to the main menu");
       printf("\n>> ");
    	scanf("%d", &select);
       while( getchar() != '\n' );
    
       switch(select)
       	{
          case 1:printf("\n\tEnter new name for Player 1: ");
                 fgets(ply1, 20, stdin);
          	    break;
          case 2:printf("\n\tEnter new name for Player 2: ");
                 fgets(ply2, 20, stdin);
          	    break;
          case 3:printf("Returning to the main menu");
                 getch();
          		 break;
          default:;
          }
    
       }
       return(ply1[0],ply2[0]);
    }
    
    
    
    void draw_grid(BOARD_PTRS)
    {
    printf("\t\t\t     1       2      3   \n");
    printf("\t\t\t         |      |       \n");
    printf("\t\t\tA    %c   |   %c  |   %c \n", *tl,*tc,*tr);
    printf("\t\t\t   ______|______|______\n");
    printf("\t\t\t         |      |       \n");
    printf("\t\t\tB    %c   |   %c  |   %c \n", *ml,*mc,*mr);
    printf("\t\t\t   ______|______|______\n");
    printf("\t\t\t         |      |       \n");
    printf("\t\t\tC    %c   |   %c  |   %c \n", *bl,*bc,*br);
    printf("\t\t\t         |      |       \n");
    }
    
    
    int win_condition(BOARD_PTRS)
    {
    
    	if((*tl && *tc && *tr != 'X') && (*tl && *tc && *tr == 'O'))
       return 1;
       if((*tl && *ml && *bl != 'X') && (*tl && *ml && *bl == 'O'))
       return 1;
       if((*tr && *mr && *br != 'X') && (*tr && *mr && *br == 'O'))
       return 1;
    
       else
       return 0;
    }
    
    void play1(BOARD_PTRS,char ply1[20])
    {
    
    	int  acrs;
    	char vert;
    
    
    	printf("\n\n\t %s\n\t Where would you like to go? ", ply1);
    	scanf("%d%c", &acrs, &vert);
       vert = toupper(vert);
       while( getchar() != '\n' );
    
    
       switch(acrs)
       	{
    
          case 1:
          		 switch(vert)
    	     		 {
                 case 'A': *tl = 'X';
                          break;
                 case 'B': *ml = 'X';
                 			break;
                 case 'C': *bl = 'X';
                 			break;
    
                 default :;
                 }
                 break;
    
          case 2:switch(vert)
    
    	     		 {
                 case 'A': *tc = 'X';
                 			break;
                 case 'B': *mc = 'X';
                 			break;
                 case 'C': *bc = 'X';
                 			break;
                 default :;
                 }
                 break;
    
          case 3:switch(vert)
    	     		 {
                 case 'A': *tr = 'X';
                 			break;
                 case 'B': *mr = 'X';
                 			break;
                 case 'C': *br = 'X';
                 			break;
                 default :;
                 }
          		break;
          default :;
           }
    
    }
    
    void play2(BOARD_PTRS,char ply2[20])
    {
    	int  acrs;
    	char vert;
    
    
    	printf("\n\n\t %s\n\t Where would you like to go? ", ply2);
    	scanf("%d%c", &acrs, &vert);
       vert = toupper(vert);
       while( getchar() != '\n' );
    
    
       switch(acrs)
       	{
    
          case 1:
          		 switch(vert)
    	     		 {
                 case 'A': *tl = 'O';
                          break;
                 case 'B': *ml = 'O';
                 			break;
                 case 'C': *bl = 'O';
                 			break;
    
                 default :;
                 }
                 break;
    
          case 2:switch(vert)
    
    	     		 {
                 case 'A': *tc = 'O';
                 			break;
                 case 'B': *mc = 'O';
                 			break;
                 case 'C': *bc = 'O';
                 			break;
                 default :;
                 }
                 break;
    
          case 3:switch(vert)
    	     		 {
                 case 'A': *tr = 'O';
                 			break;
                 case 'B': *mr = 'O';
                 			break;
                 case 'C': *br = 'O';
                 			break;
                 default :;
                 }
          		break;
          default :;
           }
    
    }

    And I know I only have three win conditions stated here, but it's enough to stop the game working properly, AND I also know that only O's can win... what can I say, it's a wrok in progress... and I'm biased...

    Thanks for any help

  2. #2
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    Code:
    case 'A': *tl = 'X';
    I dont think that you can use the '*' here.

    What you are saying is that "tl" is to point to the address of "X", but "X" doesnt have an address...
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 07:25 PM
  2. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 10:59 AM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 09:33 AM
  4. tic tac toe game
    By Leeman_s in forum Game Programming
    Replies: 9
    Last Post: 04-24-2002, 04:24 AM
  5. my tic tac toe game, please try it
    By Leeman_s in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 06:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21