TIC TAC TOE help

This is a discussion on TIC TAC TOE help within the C Programming forums, part of the General Programming Boards category; Hey im tryng to make a tic tac toe game but no matter what my game keeps returning "game is ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    8

    TIC TAC TOE help

    Hey im tryng to make a tic tac toe game but no matter what my game keeps returning "game is a draw" after the first round of turns. any ideas?



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define ROW 3
    #define COL 3
    
    
    void printboard (char b[ROW][COL]);
    void clearboard (char b[ROW][COL]);
    void play(char b[ROW][COL], char p);
    int checkwinner(char b [ROW][COL]);
    char testrow(char b[ROW][COL]);
    char testcols(char b[ROW][COL]);
    char testdiag(char b[ROW][COL]);
    char testdraw(char b[ROW][COL]);
    
    int main(){
    	char player;
    	int check=0;
    	char board[3][3];
    	clearboard(board);
    	printboard(board);
    	while(check==0){
    	player='X';
    	play(board, player);
    	printboard(board);
    	player = 'O';
    	play (board,player);
    	printboard(board);
    	check=checkwinner(board);
    	}
    	return 0;
    }
    
    void clearboard (char b[ROW][COL]){
    	int row, col;
    	for (row=0; row<3; ++row){
    		for (col=0; col<3; ++col){
    			b[row][col] = '-';
    		}
    	}
    
    }
    
    void printboard(char b[ROW][COL]){
    	int row, col;
    	printf("\t0\t1\t2\n");
    	for (row=0; row<3; ++row){
    		printf("%i\t", row);
    		for (col=0; col<3; ++col){
    			printf ("%c\t",b[row][col]);
    		}
    		printf("\n");
    	}
    	
    }
    	
    void play(char b[ROW][COL], char p){
    	int x, y;
    	printf("Player %c, please enter a row and column between 0 and 2:\n", p);
    	scanf("%i %i", &y, &x);
    	b[y][x]= p;
    }
    	
    int checkwinner(char b[ROW][COL]){
    	if (testrow(b)!=' '){
    		printf("Player %c is the winner", testrow(b));
    		return 1;
    		}
    	else if (testcols(b)!=' '){
    		printf("Player %c is the winner", testcols(b));
    		return 1;
    		}
    	else if (testdiag(b)!=' '){
    		printf("Player %c is the winner", testdiag(b));
    		return 1;
    		}
    	else if (testdraw(b)!=' '){
    		printf("The Game is a Draw");
    		return 1;
    		}
    	else{
    	return 0;
    	}}
    
    char testrow(char b[ROW][COL]){
    	int r=0;
    	char winner='n';
    	for (r=0;r<3; ++r){
    	if (b[r][0]==b[r][1]&&b[r][0]==b[r][2]){
    		return (b[r][0]);
    	}//no else- do nothing
    	}
    	return ' ';
    }
    
    char testcols(char b[ROW][COL]){
    	int c=0;
    	for (c=0;c<3;++c){
    		if (b[0][c]==b[1][c]&& b[0][c]==b[2][c]){
    			return (b[0][c]);
    			//no else- do nothing
    		}
    	}
    	return ' ';
    }
    
    char testdiag(char b[ROW][COL]){
    	if (b[0][0]==b[1][1]&&b[0][0]==b[2][2]){
    		return (b[0][0]);
    	}
    	if (b[0][2]==b[1][1]&&b[0][2]==b[2][0]){
    		return (b[0][2]);
    		}
    	return ' ';
    }
    
    char testdraw(char b[ROW][COL]){
    	int a,b;	
    	for (a=0; a<3; ++a){
    		for (b=0;b<3;++b){
    		if (b[a][b]=='-'){
    		return ' ';
    		}
    		
    		if (b[a][b]=='-'){
    		return ' ';
    		}
    		if (a*b==9){
    		return 'draw';
    		}}}
    		return ' ';}

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Code:
    if (b[a][b]=='-'){
    		return ' ';
    		}
    How does having an array and a subscript with the same name work for you?

    Also, you should only return from this function AFTER you have checked all 9 squares, not after checking the first one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 07:25 PM
  3. Tic Tac Toe Neural Network
    By glo in forum General AI Programming
    Replies: 4
    Last Post: 06-15-2008, 04:39 PM
  4. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 10:59 AM
  5. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 09:33 AM

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