I'm trying to write a Tic Tac Toe game but I'm stuck at the computer AI. Basically for the computer move I need a random generator to pick a number and insert it into a 2D array.

The parts that I'm stuck at is writing a code so that computer doesn't pick a number that has been taken either by player X or has been previously been picked by computer O. I've tried several different things but can't get anything to work. If you guys know of any solutions that would be of great help.

Here is what I got:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

#define MAX_ROWS 3
#define MAX_COLS 3

// Function Decleration
 void welcomeMsg ();
 void printGrid (char grid[][MAX_COLS]);
 void gridMove (char grid[][MAX_COLS]);

int main (void)
{
	//Local Declarations (most of these variables are just for testing, they are
        //not actually used by program.
	char position, move;
	int rowOne, rowTwo, rowThree;
	int colmOne, colmTwo, colmThree;
	int diagOne, diagTwo;
	int row, colum;
	int randRow, randColum;
	int range, randNo;
	int index;
	char empty;
	int i, j;
	int availableSp = 9;
	srand ( time(NULL) );
	
        //2D array Initialization		
	char table[MAX_ROWS][MAX_COLS] = 
	{
		{'1', '2', '3'},
		{'4', '5', '6'},
		{'7', '8', '9'}
	};	//table
	
        //This is just a function that prints welcome message
	welcomeMsg(position);
	
	printf("Select X or O (X moves first)? ");
	scanf(" %c",  &move);

	do
	{
        //Checks rows, colums, and diagonals for a winner.
	rowOne = (table[0][0] == table[0][1] && table[0][0] == table[0][2]);
	rowTwo = (table[1][0] == table[1][1] && table[1][0] == table[1][2]);
	rowThree = (table[2][0] == table[2][1] && table[2][0] == table[2][2]);
	colmOne = (table[0][0] == table[1][0] && table[0][0] == table[2][0]);
	colmTwo = (table[0][1] == table[1][1] && table[0][1] == table[2][1]);
	colmThree = (table[0][2] == table[1][2] && table[0][2] == table[2][2]);
	diagOne = (table[0][0] == table[1][1] && table[0][0] == table[2][2]);
	diagTwo = (table[0][2] == table[1][1] && table[0][2] == table[2][0]);
	if (rowOne || rowTwo || rowThree || colmOne || colmTwo || colmThree || diagOne ||      diagTwo == 1) break;

	printGrid (table);
	
	//Player Move
	printf("Enter the number of an available space, you are X: ");
	scanf("%d", &index);
	row = (index - 1) / 3;
	colum = (index - 1) % 3;
	table[row][colum] = 'X';
	availableSp--;
	
	//Computer Move, and this is the part that I'm stuck at.
	range = (availableSp - 1) + 1;
	randNo = rand() % range + 1;
	randRow = (randNo - 1) / 3;
	randColum = (randNo - 1) % 3;
		
	if ((table[randRow][randColum] != 'X') && (table[randRow][randColum] != 'O'))
	{
		table[randRow][randColum] = 'O';
	}

	availableSp--;	
	printf("\nThe computer picked space %d\n", randNo);
	printf("\n%d\n\n", availableSp);

	} while (availableSp > 0);

	if (rowOne || rowTwo || rowThree || colmOne || colmTwo || colmThree || diagOne || diagTwo == 1)
		printf("You Won\n\n");
		printGrid (table);

	return 0;
	
}	// main

Any help would be greatly appreciated.

Sounds like for this program you should just put a while loop around your generator and if it picks a number that's already taken pick again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.