头文件:
Tic_Tac_Toe.h
#ifndef __Tic_Tac_Toe_h_
#define __Tic_Tac_Toe_h_
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define ROW 3
#define COL 3
void start(char chess[ROW][COL], int row, int col);
void displayboard(char chess[ROW][COL], int row, int col);
void player(char chess[ROW][COL], int row, int col);
void computer(char chess[ROW][COL], int row, int col);
int check_win(char chess[ROW][COL], int row, int col);
#endif
#define _CRT_SECURE_NO_WARNINGS
#include "Tic_Tac_Toe.h"
void start(char chess[ROW][COL], int row, int col)
{
int i=0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
chess[i][j] = ' ';
}
}
}
void displayboard(char chess[ROW][COL], int row, int col)
{
int i;
printf("|---|---|---|\n");
for (i = 0; i < row; i++)
{
printf("|%c | %c | %c |\n", chess[i][0], chess[i][1], chess[i][2]);
printf("|---|---|---|\n");
}
}
void player(char chess[ROW][COL], int row, int col)
{
int a, b;
do
{
printf(" Player move(2,2): \n");
scanf("%d,%d", &a, &b);
getchar();
if (chess[a - 1][b - 1] == ' ')
{
chess[a - 1][b - 1] = 'X';
break;
}
} while (1);
}
void computer(char chess[ROW][COL], int row, int col)
{
do
{
row=rand() % 3 + 0;
col = rand() % 3 + 0;
if (chess[row ][col] == ' ')
{
chess[row ][col] = '0';
break;
}
} while (1);
}
int check_win(char chess[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int count = 0;
for (i = 0; i < row;i++)
{
if (chess[i][0] != ' '&& chess[i][1] == chess[i][0] && chess[i][0] == chess[i][2])
{
return 1;
}
}
for (i = 0; i < col; i++)
{
if ( (chess[0][i] != ' ') && chess[0][i] == chess[2][i] && (chess[0][i] == chess[1][i]) )
{
return 1;
}
}
if ((chess[0][0] != ' ') &&(chess[0][0] == chess[2][2])&&(chess[0][0] == chess[1][1]))
{
return 1;
}
if ((chess[0][2] != ' ') + (chess[0][2] == chess[1][1]) +(chess[0][2] == chess[2][0])==3)
{
return 1;
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (chess[i][j] != ' ')
{
count++;
}
}
}
if (count == 9)
{
printf(" Dogfall!\n");
return 2;
}
return 0;
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include "Tic_Tac_Toe.h"
void menu()
{
printf(" ****************************************************************\n");
printf(" ********** Do you want to play the Tic_Tac_Toe Game? **********\n");
printf(" *********** ***********\n");
printf(" ************* 1.play 2.exit ************\n");
printf(" ****************************************************************\n");
}
void game(void)
{
int i = 0;
char chessboard[ROW][COL] = { '0'};
int row = ROW;
int col = COL;
int ret;
start(chessboard, row, col);
displayboard(chessboard, row, col);
do
{
player(chessboard, row, col);
displayboard(chessboard, row, col);
ret=check_win(chessboard, row, col);
if (ret == 1)
{
printf("Player Win!\n");
break;
}
else if (ret == 2)
{
break;
}
computer(chessboard, row, col);
displayboard(chessboard, row, col);
ret=check_win(chessboard, row, col);
if (ret == 1)
{
printf("Computer Win!\n");
break;
}
else if (ret == 2)
{
break;
}
} while (1);
}
int main()
{
int n;
do
{
menu();
scanf("%d", &n);
if (n == 1)
{
printf(" ~~~Game start~~~\n");
game();
}
else if (n == 2)
{
break;
}
else
{
printf(" Input error! \n");
}
} while (1);
return 0;
}