tic tac toe AI

This is a discussion on tic tac toe AI within the C++ Programming forums, part of the General Programming Boards category; How would I check if the user gets 3 in a row in a tic tac toe game? I'm stumped ...

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

    tic tac toe AI

    How would I check if the user gets 3 in a row in a tic tac toe game? I'm stumped

    Here is the code I have:

    Code:
    #include <iostream>
    #include <time.h>
    #include <windows.h>
    using namespace std;
    
    void draw(char board[3][3])
    {
    	for(int i = 0; i < 3; i ++)
    	{
    		for(int t = 0; t < 3; t ++)
    		{
    			cout << board[i][t] << ' ';
    		}
    
    		cout << endl;
    	}
    }
    
    void moveEnemey(char board[3][3], char icon[2])
    {
    	srand(time(NULL));
    
    	int i, t;
    
    	while(1)
    	{
    		i = rand()%3;
    		t = rand()%3;
    
    		if(board[i][t] == '_')
    		{
    			board[i][t] = icon[1];
    			break;
    		}
    	}
    }
    
    int main()
    {
    	int input;
    
    	char icon[2] = { 'X', 'O' };
    	char board[3][3] = 
    	{
    		'_', '_', '_',
    		'_', '_', '_',
    		'_', '_', '_'
    	};
    
    	while(1)
    	{
    		draw(board);
    
    		cout << "\nMake your move: ";
    		cin >> input;
    
    		if(board[0][input - 1] != icon[1])
    		{
    			board[0][input - 1] = icon[0];
    			moveEnemey(board, icon);
    		}
    		else cout << '\a';
    
    		system("cls");
    	}
    
    	return 0;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    You want the code to step through and check all 8 possible rows of three in the grid. That's 3 horizontal, 3 vertical and 2 diagonal. As it does so, it needs to check for all 3 places being occupied by whichever piece the user is playing '0' or 'X'. You could either do this 'on the fly' and simply step through each route incrementing a counter and checking if the counter equals 3 at the end or maybe you will find it easier to load each 3 in a row path into a seperate array before checking the arrays. There are no doubt countless more and better ways to do it but these are 2 simple ones that spring to mind.
    Last edited by minesweeper; 12-04-2002 at 06:19 PM.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>srand(time(NULL));
    You should only call srand() once in your program, so move it to the start of main().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Code:
    bool checkUserWin(char board[3][3], char icon[2])
    {
    	int counter = 0, counter2 = 0;
    
    	while(counter2 <= 3)
    	{
    		for(int i = 0; i < 3; i ++)
    		{
    			if(board[counter2][i] == icon[0]) counter ++;
    		}
    
    		if(counter == 3) return TRUE;
    
    		counter = 0;
    		counter2 ++;
    	}
    
    	return FALSE;
    }
    This code only accounts for the top row (board[0][i])...any reason why it would do this?
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  5. #5
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Thanks, I got alot of of that!
    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, 07:25 PM
  2. tic tac toe AI roadblock >:-(|)
    By dark_rocket in forum Game Programming
    Replies: 5
    Last Post: 06-12-2006, 06:13 AM
  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 AI
    By Isamo in forum C++ Programming
    Replies: 9
    Last Post: 01-01-2004, 11:32 AM
  5. not 3x3 tic tac toe game AI
    By Unregistered in forum Game Programming
    Replies: 9
    Last Post: 08-29-2001, 05:02 AM

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