Bizarre Tic Tac Toe!

This is a discussion on Bizarre Tic Tac Toe! within the C Programming forums, part of the General Programming Boards category; I am trying to develope this tic tac toe game etc. and to check if there are three '1's or ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    Bizarre Tic Tac Toe!

    I am trying to develope this tic tac toe game etc. and to check if there are three '1's or three '2's in a row I wrote the fuction check
    I am trying many combinations of ways to check this but none really work, how would you design check?
    1 = player1 or X
    2 = player2 or O

    The possibilities I have tried are layed out in commented lines
    The code is really spread and confusing because it is late and I don't want to write more possibilites and post all the other ways I have tried.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    /* Included Libraries */
    int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,q=0;  //Global integers for each square's value
    int update(int player); //Prototype functions
    int print_chart(int a, int b, int c, int d, int e, int f, int g, int h, int i); //Prototype functions
    int check(int a,int b, int c, int d, int e, int f, int g, int h, int i); //Prototype functions
    int printmenu(); //Prototype functions
    int main()
    {
    	printmenu();  //Call printmenu()
    	printf("\n---Type z if you want to print the table again---\n");
    	printf("---Q or q to quit---\n");
    	while(check(a,b,c,d,e,f,g,h,i)==0 && (q!=1)) //Start loop looking for a win or a quit
    	{
    		update(1);
    		print_chart(a,b,c,d,e,f,g,h,i);	
    		update(2);
    		print_chart(a,b,c,d,e,f,g,h,i);
    	}
    	return 0;  //finish program by returning an int from main
    }
    
    int print_chart(int a, int b, int c, int d, int e, int f, int g, int h, int i)
    {  //Prints the game board with the players valye
    	printf("+-----------+\n\n");
    	printf(" %d | %d | %d\n",a,b,c);
    	printf("---+---+---\n");
    	printf(" %d | %d | %d\n",d,e,f);
    	printf("---+---+---\n");
    	printf(" %d | %d | %d\n\n",g,h,i);
    	printf("+-----------+\n\n\n");
    	return 0;
    }
    int update(int player) //recieve choice (input) from the player
    {
    	char choice;
    	printf("Player%d [%d] choose: ",player,player);
    
    		scanf("%s",&choice);
    		if(choice == 'a')
    			return a = player;
    		else if(choice == 'b')
    			return b = player;
    		else if(choice == 'c')
    			return c = player;
    		else if(choice == 'd')
    			return d = player;
    		else if(choice == 'e')
    			return e = player;
    		else if(choice == 'f')
    			return f = player;
    		else if(choice == 'g')
    			return g = player;
    		else if(choice == 'h')
    			return h = player;
    		else if(choice == 'i')
    			return i = player;
    		else if(choice == 'z'||choice == 'Z') //check for z or Z to display menu
    		{
    			printmenu();
    			return 0;
    		}
    		else if(choice == 'q'||choice == 'Q') //check for q or Q to quit game
    			return q=1;
    		else 
    			return 0;
    	
    	return 0;
    }
    
    int printmenu()
    {
    	printf(" a | b | c\n");
    	printf("---+---+---\n");
    	printf(" d | e | f\n");
    	printf("---+---+---\n");
    	printf(" g | h | i\n\n");
    	printf("+-----------+\n");
    	return 0;
    }
    int check(int a, int b, int c, int d, int e, int f, int g, int h, int i)
    {
    	
    	/*
    	{
    		printf("Tic Tac C, a combo of three!\n");
    		return 1;
    	}
    	else
    		return 0;
    	*/
    
    	/*
    
    	int aei=a+e+i,abc=a+b+c,adg=a+d+g,beh=b+e+h,cfi=c+f+i,gec=g+e+c,ghi=g+h+i,def=d+e+f;
    	if(aei==3||abc==3||adg==3||beh==3||cfi==3||gec==3||ghi==3||def==3)
    	{
    		printf("Player1 Wins!\n");
    		return 1;
    	}
    	else if(aei==6||abc==6||adg==6||beh==6||cfi==6||gec==6||ghi==6||def==6)
    	{
    		printf("Player2 Wins!\n");
    		return 2;
    	}
    	else
    		return 0;
    	*/
    
    }
    
    /*
    	if( ( ((a==e)&&(e==i)) || 
    		  ((a==b)&&(b==c)) ||
    		  ((a==d)&&(d==g)) ||
    		  ((b==e)&&(e==h)) ||
    		  ((c==f)&&(f==i)) ||
    		  ((g==e)&&(e==c)) ||
    		  ((g==h)&&(h==i)) ||
    		  ((d==e)&&(e==f))))
    */
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,854
    I would have used an array of int's

    int iBoard[3][3];//iBoard[ROW][COL]

    to store the values.

    Then the checking is easier
    Code:
    for(iRow=0;iRow<3;iRow++)
        if( (iBoard[iRow][0]==iBoard[iRow][1]) && (iBoard[iRow][1]==iBoard[iRow][2]) )
              iWinner=iBoard[iRow][0];
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Definately an array is the way to go. Alternately, you could just use a single integer and use bit masking. It's funner that way.

    Anyway, you need 3 checks:

    1 for rows
    1 for columns
    1 for diagonals

    00.01.02
    03.04.05
    06.07.08

    09.10.11
    12.13.14
    15.16.17

    Those are the bit shifts you use so just set up some macros and
    you can pull it off fairly easily.

    For example:

    #define ROW_1_WIN ((1<<0)&(1<<1)&(1<<2)
    #define ROW_2_WIN ((1<<3)&(1<<4)&(1<<5)
    #define ROW_3_WIN ((1<<6)&(1<<7)&(1<<8)


    You can then check the match a few ways. You can simply shift off the first nine bits if you want to check the other player, or just mask off the remaining bits if you want to check the first player.

    Another way to do it would be to just exclude the bits you want and check the value. If you mask off the first three bits, the value with them all set is 7. So if you shift over nine places first, then mask the first three, if it is 7, your second player wins.

    Shrug. Anyway, it'd be a change of pace from the "normal" way.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    The problem is.. well, consider this.
    Let's say that row abc has 1 X, 1 O, and 1 blank. The score of that row will add up to 1 + 2 + 0 = 3, so it will register as a victory for player 1.

    It seems to me that the way to do this, using the least amount of modification, would be to make each of Player 2's marks worth -1 points.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by QuestionC
    The problem is.. well, consider this.
    Let's say that row abc has 1 X, 1 O, and 1 blank. The score of that row will add up to 1 + 2 + 0 = 3, so it will register as a victory for player 1.

    It seems to me that the way to do this, using the least amount of modification, would be to make each of Player 2's marks worth -1 points.
    What is this a reply to? Not to bit shifts. I'm not sure who you're replying to, because this doesn't fit the pattern of anyone's reply.

    However, Using simple math like your example would be another way of doing it. -1/0/1

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    ok, thanks, at school (today) I was looking over my code and I also thought that an array is the way to go.
    thanks

    -Luke
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

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, 06:25 PM
  2. Tic Tac Toe... so close...
    By SlayerBlade in forum C Programming
    Replies: 14
    Last Post: 10-10-2005, 08:58 PM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  4. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 11:52 AM
  5. Replies: 22
    Last Post: 11-08-2001, 10:01 PM

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