Thread: 9x9 tic-tac-toe

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    9x9 tic-tac-toe

    Hey there, i'm new to this forum and i am in need of a little help.

    As part of a C programming assignment i have been asked to design a tic-tac-toe program using a 7x7 array and pointers.

    I have just about got my head around the pointers but i am having problems creating a function to take co-ordinate values from the user and inputting them as either an 'X' or 'O'.
    Here is my code so far.

    Code:
    #include <stdio.h>
    #define I 7
    #define J 7
    #define player1 88
    #define player2 79
    
    printboard(int (*board)[7]);
    mainmenu();
    TwoPlayer();
    OnePlayer();
    
    void main() 
    {
    	int select;
    	
    	select=mainmenu();
    		switch(select)
    		{
    			case 1 : printf("You selected 2 player mode\n\n");
    			TwoPlayer();
    			break;
    			case 2 : printf("You selected 1 player mode\n\n");
    			break;
    			case 3 : printf("\n\n");
    			exit(0);
    			break;	
    		}
    		
    	//printboard(board);
    }	
     TwoPlayer()
    {
    	int board[I][J];
    	int i, j, select;
    	int x,y; 
    	for (i=0; i<7; i++)	// clear board
    	for (j=0; j<7; j++)
    	board[i][j]='\0';
    	x=1;
    	y=1;
    	//FirstPlayer();
    	board[1][2]=player1;
    	board[x][y]=player2;
    	printboard(board);
    }
    
    mainmenu()
    {
    	int select;
    	printf("*****************************************************\n");
    	printf(" Please select wich game mode you would like to play \n");
    	printf("        1 - Two player         2 - One Player        \n"); 
    	printf("                      3 - Quit                       \n");
    	printf("*****************************************************\n");
    	scanf("%d",&select);
    	return(select);	
    }
    	
    printboard(int (*board)[7])	
    {	
    	int i, j;
    	printf(" |");
    	
    	for (j=0; j<J; j++)
    		{
    		printf(" %d |",j);
    		}
    	printf("\n");
    	for (i=0; i<I; i++)
    		{
    		printf("------------------------------\n");
    		printf("%d",i);
    		printf("|");
    	for (j=0; j<J; j++)
    		{
    		//printf(" %c |",board[i][j]);
    		printf(" %c |",*(*(board+i)+j));
    		}
    		printf("\n");	
    		}
    	printf("------------------------------\n");
    }
    Any advice on where to go next would be greatly appreciated.

    Greg

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So you can get the row and column of user input by using scanf calls like you did for your menu selection. Just prompt the user to "Enter x-coordinate: " and the same for the y-coordinate. Then you can just put the player1 or player2 character in your grid by doing board[x][y] = player1. There are a number of other problems you need to resolve:

    1. It's int main(void), not main(): Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
    2. All your function should specify a return type (or void) and have something in the parameter list, even if it's void to signify you don't intend to pass any parameters.
    3. Initialize your board to all spaces ' ' instead of nulls '\0'. The null character takes up no space on output, and thus your grid is all disorganized. Space will fill in the gap where you would put an X or an O, and keep things aligned.
    4. Define player1 as 'X' and player 2 as 'O' instead of their ascii values.

    You should turn on all warnings in your compiler and make sure they're all resolved. It will encourage good practice and help you find errors and potential errors. Good indenting and code formatting really helps here as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe check winner
    By dhardin in forum C++ Programming
    Replies: 15
    Last Post: 12-20-2009, 07:57 PM
  2. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  3. Tic Tac Toe program
    By muzihc in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 08:08 PM
  4. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  5. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM