[ create a new paste ] login | about

Link: http://codepad.org/J1MceaG7    [ raw code | fork ]

ninwa - C, pasted on Oct 14:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

struct State
{
	char board[3][3];
};


void initializeBoard(struct State**);
void drawBoard(struct State**);
void placeRandomPiece(struct State**);
char checkForWinner(struct State** state);

void clearScreen();

/* TaccyAI: A Tic-Tac-Toe AI Engine */
void TaccyRandomPiece(struct State** );

int main(void)
{
	struct State *state;  /* The current state of our game. */
	int x = -1, y = -1;   /* Used for holding input of user coordinates */
	short validCoordinates = 0;  /* Used for looping until user enters valid coordinates. */
	short gameInProgress; /* Main game loop variable. */
	
	srand(time(0)); /* Seed RNG */

	state = malloc(sizeof(struct State));
	initializeBoard(&state);

	gameInProgress = 1;
	while(gameInProgress)
	{
		clearScreen();

		/* Assess Board, is there a winner? */
		if(checkForWinner(&state) != '\0'){
			clearScreen();
			drawBoard(&state);

			printf("There was a winner! \nCongratulations player '%c'\n\n",checkForWinner(&state));
			gameInProgress = !gameInProgress;
			continue;
		}

		drawBoard(&state);
		
		validCoordinates = 0; /* Reset checker. */
		while(validCoordinates == 0){
			printf("Enter coordinates to place your mark (e.g 0,1): ");
			scanf("%i,%i",&y,&x);

			/* Check range on coordinates */
			if( x < 3 && x >= 0 && y < 3 && y >= 0 ){
				/* Check that a piece is not already there. */
				if( state->board[y][x] == '\0' ){
					/* Everything checks out, place our piece. */
					validCoordinates = 1;
					state->board[y][x] = 'O';
				}
				else
					printf("\nThose were invalid coordinates, try again!\n");
				
			}
			else{
				printf("\nThose were invalid coordinates, try again!\n");
				fflush(stdin);
			}
		}
		
		/* CPU's Turn (TaccyAI) */
		TaccyRandomPiece(&state);
	}
	
	return 0;
}

void TaccyRandomPiece(struct State** state)
{
	short piecePlaced = 0;
	int y,x;
	while(piecePlaced == 0)
	{
		x = rand()%3;
		y = rand()%3;

		if( (*state)->board[y][x] == '\0' ){
			piecePlaced = 1;
			(*state)->board[y][x] = 'X';
		}
	}
}

void initializeBoard(struct State** state)
{
	int i, j;
	for(i = 0; i < 3; i++){
		for(j = 0; j <3; j++){
			(*state)->board[i][j] = '\0';
		}
	}
}

/* Checks the board within the state for a winner.
/* Return Values:
/* '\0' - No Winner
/* '0' - Player ('O') is the winner
/* 'X' - CPU    ('X') is the winner */

char checkForWinner(struct State** state)
{
	if( (*state)->board[0][0] != '\0' ){
		if( (*state)->board[0][0] == (*state)->board[0][1] &&
			(*state)->board[0][1] == (*state)->board[0][2] )
			return (*state)->board[0][0];
	}
	if( (*state)->board[1][0] != '\0' ){
		if( (*state)->board[1][0] == (*state)->board[1][1] &&
			(*state)->board[1][1] == (*state)->board[1][2] )
			return (*state)->board[1][0];
	}
	if( (*state)->board[2][0] != '\0' ){
		if( (*state)->board[2][0] == (*state)->board[2][1] &&
			(*state)->board[2][1] == (*state)->board[2][2] )
			return (*state)->board[2][0];
	}
	if( (*state)->board[0][0] != '\0' ){
		if( (*state)->board[0][0] == (*state)->board[1][0] &&
			(*state)->board[1][0] == (*state)->board[2][0] )
			return (*state)->board[0][0];
	}
	if( (*state)->board[0][1] != '\0' ){
		if( (*state)->board[0][1] == (*state)->board[1][1] &&
			(*state)->board[1][1] == (*state)->board[2][1] )
			return (*state)->board[0][1];
	}
	if( (*state)->board[0][2] != '\0' ){
		if( (*state)->board[0][2] == (*state)->board[1][2] &&
			(*state)->board[1][2] == (*state)->board[2][2] )
			return (*state)->board[0][2];
	}
	if( (*state)->board[0][0] != '\0' ){
		if( (*state)->board[0][0] == (*state)->board[1][1] &&
			(*state)->board[1][1] == (*state)->board[2][2] )
			return (*state)->board[0][0];
	}
	if( (*state)->board[0][2] != '\0' ){
		if( (*state)->board[0][2] == (*state)->board[1][1] &&
			(*state)->board[1][1] == (*state)->board[2][0] )
			return (*state)->board[0][2];
	}

	return '\0';
}

void drawBoard(struct State** state)
{
	printf("+-----------+\n");
	printf("| %c | %c | %c |\n", 
		  (*state)->board[0][0], 
		  (*state)->board[1][0], 
		  (*state)->board[2][0]);

	printf("| %c | %c | %c |\n", 
		  (*state)->board[0][1], 
		  (*state)->board[1][1], 
		  (*state)->board[2][1]);

	printf("| %c | %c | %c |\n", 
		  (*state)->board[0][2], 
		  (*state)->board[1][2], 
		  (*state)->board[2][2]);
	printf("+-----------+\n\n");
}

void clearScreen() { /* Stupid utility function to clear the screen by printing 80 newlines. */
	int i;
	for(i = 0; i < 80; i++)
		printf("\n");
}


Create a new paste based on this one


Comments: