Thread: tic-tac-toe diagonal

  1. #1
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369

    tic-tac-toe diagonal

    Here is the code I have. Hopefully it's self explanatory:

    Code:
    // Game functions
    #include "game.h"
    
    bool check::win(char icon, char board[3][3])
    {
    	if(check::horizontal(icon, board))
    		return true;
    	else if(check::vertical(icon, board))
    		return true;
    	else if(check::diagonal(icon, board))
    		return true;
    
    	return false;
    }
    
    bool check::horizontal(char icon, char board[3][3])
    {
    	int counter = 0;
    
    	for(int i = 0; i < 3; i ++)
    	{
    		for(int t = 0; t < 3; t ++)
    		{
    			if(board[i][t] == icon)
    				counter ++;
    		}
    
    		if(counter == 3)
    			return true;
    
    		counter = 0;
    	}
    
    	return false;
    }
    
    bool check::vertical(char icon, char board[3][3])
    {
    	int counter = 0;
    
    	for(int i = 0; i < 3; i ++)
    	{
    		for(int t = 0; t < 7; t += 3)
    		{
    			if(board[i][t] == icon)
    				counter ++;
    		}
    
    		if(counter == 3)
    			return true;
    
    		counter = 0;
    	}
    
    	return false;
    }
    
    bool check::diagonal(char icon, char board[3][3])
    {
    	int counter = 0;
    
    	return false;
    }
    I'm stumped on how I could use for loops to check for a diagonal win. Any suggestions?
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    13
    bool check::horizontal(char icon, char board[3][3])
    {
    int counter = 0;


    for(int i = 0; i < 3; i ++)
    {
    if(board[i][i] == icon)
    counter ++;


    if(counter == 3)
    return true;

    counter = 0;
    }

    return false;
    }

  3. #3
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Ahhh, that'll work. Thanks!
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    _

    Code:
    int c = 0;
    
    for (int x = 0; x < width_of_item; ++x) {
          if (board[x][x] == icon)
                ++c;
    }
    
    if (c == 3) 
    /* ... */
    If you use the same incrementing value for both x and y then you're moving diagonally in 2 dimensions.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    13
    Code:
    bool check::diagonal(char icon, char board[3][3])
    {
            if[board[2][2] == icon)
            {
                if(board[1][1] == icon && board[3][1] == icon  || board[1][3] = icon && board[3][1]        )
                {
                 return true; 
                }
                else 
                {
                 return false;
                }
           }
    
              return false;
    }
    i don't have an other idea...
    the other ideas don't work correctly because
    it doesn't do when you have
    [ ] [ ] [x]
    [ ] [x ] [ ]
    [x ] [ ] [ ]
    Last edited by sphreak; 01-03-2003 at 12:46 PM.

  6. #6
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Thanks for the help, guys. I ended up doing this:

    Code:
    bool check::diagonal(char icon, char board[3][3])
    {
    	int counter = 0;
    
    	for(int i = 0; i < 3; i ++)
    	{
    		if(board[i][i] == icon)
    			counter ++;
    	}
    
    	if(counter == 3)
    		return true;
    
    	counter = 0;
    
    	int t;
    
    	for(i = 0, t = 2; i > 3, t >= 0; i ++, t --)
    	{
    		if(board[t][i] == icon)
    			counter ++; 
    	}
    
    	if(counter == 3)
    		return true;
    
    	return false;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

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 program
    By muzihc in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 08:08 PM
  3. Tic Tac Toe AI help please...
    By Rune Hunter in forum Game Programming
    Replies: 12
    Last Post: 11-05-2004, 04:24 PM
  4. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  5. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM