I just finished a tic-tac-toe program, actually. It’s got no AI at this point, but it’s worth a chicking and a checking. Hopefully pasting it won’t murder the formatting too badly.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
char board[2+1][2+1];
typedef enum {me, you} player;
typedef enum {X, O, none} marker_type;
void random_move(char);
void display();
void initialize();
void play(player, char, char);
bool winner(player, char, char);
void myplay(char);
void yourplay(char);
inline bool freecell(int row, int col)
{
if (board[row][col] == ' ')
return true;
else
return false;
}
int main()
{
srand((unsigned)time(0));
char me_marker, you_marker;
int first_player = 0;
int rotate;
int moves;
char c;
player who;
bool win;
do
{
rotate = first_player;
if (first_player == 0)
{
me_marker = 'X';
you_marker = 'O';
first_player = 1;
}
else
{
me_marker = 'O';
you_marker = 'X';
first_player = 0;
}
printf("Your marker is %c\n\n", you_marker);
moves = 0;
win = false;
initialize();
do
{
if (rotate == 0)
{
who = me;
rotate = 1;
}
else
{
who = you;
rotate = 0;
}
if (moves < 9)
{
moves++;
play(who, me_marker, you_marker);
display();
win = winner(who, me_marker, you_marker);
if (win == true)
{
if (who == me)
printf("\nYou LOSE!\n");
else
printf("\nYou WIN!\n");
moves = 9;
}
}
}while (moves < 9);
if (win == false)
printf("\nYou TIED!\n");
printf("\nPlay again(y/n)? ");
do
{
c = toupper(getchar());
}while(c != 'Y' && c != 'N');
}while(c == 'Y');
return 0;
}
void initialize()
{
int i, j;
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 2; j++)
board[i][j] = ' ';
}
}
void display()
{
printf("\n\t %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("\t---+---+---\n");
printf("\t %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("\t---+---+---\n");
printf("\t %c | %c | %c\n\n", board[2][0], board[2][1], board[2][2]);
}
void play(player who, char me_marker, char you_marker)
{
if (who == me)
myplay(me_marker);
else
yourplay(you_marker);
}
void myplay(char me_marker)
{
int i, j, k;
do
{
k = rand() % 9;
switch (k)
{
case 0: i = 0; j = 0; break;
case 1: i = 0; j = 1; break;
case 2: i = 0; j = 2; break;
case 3: i = 1; j = 0; break;
case 4: i = 1; j = 1; break;
case 5: i = 1; j = 2; break;
case 6: i = 2; j = 0; break;
case 7: i = 2; j = 1; break;
case 8: i = 2; j = 2; break;
}
}while(freecell(i,j) == false);
board[i][j] = me_marker;
printf("My move: %d %d\n", i+1, j+1);
}
void yourplay(char you_marker)
{
int i, j;
do
{
printf("\nYour turn: ");
scanf("%d %d", &i, &j);
getchar(); //remove new line
i--;
j--;
if ((i < 0 || i > 2 || j < 0 || j > 2))
printf("You must place your marker on the grid.\n");
else if (freecell(i, j) == true)
{
board[i][j] = you_marker;
break;
}
else
printf("There's already a marker there!\n");
}while(1);
}
bool winner(player who, char me_marker, char you_marker)
{
int i, j, t;
bool win;
char c;
if(who == me)
c = me_marker;
else
c = you_marker;
win = false;
//diagonals
if(board[0][0] == c && board[1][1] == c && board[2][2] == c)
win = true;
if(board[2][0] == c && board[1][1] == c && board[0][2] == c)
win = true;
//horizontals
for(i = 0; i <= 2; i++)
{
t=0;
for(j = 0; j <= 2; j++)
{
if(board[i][j] == c)
t++;
}
if(t == 3)
win = true;
}
//verticals
for(j = 0; j <= 2; j++)
{
t=0;
for(i = 0; i <= 2; i++)
{
if(board[i][j] == c)
t++;
}
if(t == 3)
win = true;
}
return win;
}
EDIT:
Uh… for semi-better formatting, hit quote, and copy/paste from there.