#include <stdio.h> /* include information about standard library stdio.h */
#include <string.h> /* include information about standard library string.h */
#include <Windows.h> /* include information about standard library Windows.h */
#include<stdlib.h> // 使用了srand和rand函数
#include<time.h> // 使用了time函数
int row = 0; /* declare six variables of type of integer namely player,winner,choice,row,column and line */
int column = 0; /* and their value all equal to 0 */
int choice = 0;
int player;
int winner = 0;
int line = 0;
int selection = 0;
char board[3][3] = /* Declaring a two - dimensional array of length 3 namely board */
{
{ '1','2','3' },
{ '4','5','6' },
{ '7','8','9' }
};
void gambo() {
printf(" # # \n"); /*print out the chess board on the screen */
printf(" %c # %c # %c \n", board[0][0], board[0][1], board[0][2]);
printf(" # # \n");
printf("#################\n");
printf(" # # \n");
printf(" %c # %c # %c \n", board[1][0], board[1][1], board[1][2]);
printf(" # # \n");
printf("#################\n");
printf(" # # \n");
printf(" %c # %c # %c \n", board[2][0], board[2][1], board[2][2]);
printf(" # # \n");
}
void end() {
printf("\n\n");
gambo();
if (winner == 0)
printf("\nIt is drawn that no one is winner.\n");
else if (player == 1)
printf("\nCongrations! player ,YOU ARE THE WINNER!\n");
else printf("\ncomputer is the winner\n");
printf("Do you want to play again?press y/n\n");
int rep;
rep = getchar();
if (rep == 'y') {
system("cls");
board[0][0] = '1'; board[0][1] = '2'; board[0][2] = '3';
board[1][0] = '4'; board[1][1] = '5'; board[1][2] = '6';
board[2][0] = '7'; board[2][1] = '8'; board[2][2] = '9';
winner = 0;
main();
}
else
printf("\n\nThank you for playing the tic-tac-toe game, please press a key to close the program\n\n");/*End the program by pressing a key*/
exit(0);
}
void check() {
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2]) ||
(board[0][2] == board[1][1] && board[0][2] == board[2][0]))
{
winner = player;
end();
}
else
for (line = 0; line <= 2; line++)
if ((board[line][0] == board[line][1] && board[line][0] == board[line][2]) ||
(board[0][line] == board[1][line] && board[0][line] == board[2][line]))
{
winner = player; /*Check rows and columns for a winning line */
end();
}
}
void com() {
int dice;
srand((unsigned int)time(NULL));
while (board[row][column]>'9') {
dice = rand() % 9 + 1;
row = --dice / 3; /*give the value to the variables named row and column depending on the choice*/
column = dice % 3;
};
board[row][column] = 'O';
}
void human() {
printf("\n(If you enter the number out of range, "
"you need to reenter the number until the right number.)\n");
do {
printf("\nPlayer, please enter the number of the chess board where you want to place your %c: ",'X');
scanf("%d", &choice);
getchar();
rewind(stdin);
row = --choice / 3;
column = choice % 3;
} while (choice<0 || choice>9 || board[row][column]>'9');
board[row][column] = 'X';
}
int main() /* define a function named main */
{
do {
printf("Do you want to go first or seconf? press 1 or 2 to select.\n");
scanf("%d", &selection);
getchar();
} while (selection < 1 || selection > 2);
rewind(stdin);
int i,j;
if (selection == 1)j = 5;
else j = 4;
for (i = 0; i<j && winner == 0; i++) /*using a loop to renew the chess board */
{
system("cls");
gambo();
if (selection == 1) {
player = 1;
human();
check();
player = 2;
com();
check();
}
else {
player = 2;
com();
system("cls");
gambo();
check();
player = 1;
human();
check();
}
}
end();
}
#include <string.h> /* include information about standard library string.h */
#include <Windows.h> /* include information about standard library Windows.h */
#include<stdlib.h> // 使用了srand和rand函数
#include<time.h> // 使用了time函数
int row = 0; /* declare six variables of type of integer namely player,winner,choice,row,column and line */
int column = 0; /* and their value all equal to 0 */
int choice = 0;
int player;
int winner = 0;
int line = 0;
int selection = 0;
char board[3][3] = /* Declaring a two - dimensional array of length 3 namely board */
{
{ '1','2','3' },
{ '4','5','6' },
{ '7','8','9' }
};
void gambo() {
printf(" # # \n"); /*print out the chess board on the screen */
printf(" %c # %c # %c \n", board[0][0], board[0][1], board[0][2]);
printf(" # # \n");
printf("#################\n");
printf(" # # \n");
printf(" %c # %c # %c \n", board[1][0], board[1][1], board[1][2]);
printf(" # # \n");
printf("#################\n");
printf(" # # \n");
printf(" %c # %c # %c \n", board[2][0], board[2][1], board[2][2]);
printf(" # # \n");
}
void end() {
printf("\n\n");
gambo();
if (winner == 0)
printf("\nIt is drawn that no one is winner.\n");
else if (player == 1)
printf("\nCongrations! player ,YOU ARE THE WINNER!\n");
else printf("\ncomputer is the winner\n");
printf("Do you want to play again?press y/n\n");
int rep;
rep = getchar();
if (rep == 'y') {
system("cls");
board[0][0] = '1'; board[0][1] = '2'; board[0][2] = '3';
board[1][0] = '4'; board[1][1] = '5'; board[1][2] = '6';
board[2][0] = '7'; board[2][1] = '8'; board[2][2] = '9';
winner = 0;
main();
}
else
printf("\n\nThank you for playing the tic-tac-toe game, please press a key to close the program\n\n");/*End the program by pressing a key*/
exit(0);
}
void check() {
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2]) ||
(board[0][2] == board[1][1] && board[0][2] == board[2][0]))
{
winner = player;
end();
}
else
for (line = 0; line <= 2; line++)
if ((board[line][0] == board[line][1] && board[line][0] == board[line][2]) ||
(board[0][line] == board[1][line] && board[0][line] == board[2][line]))
{
winner = player; /*Check rows and columns for a winning line */
end();
}
}
void com() {
int dice;
srand((unsigned int)time(NULL));
while (board[row][column]>'9') {
dice = rand() % 9 + 1;
row = --dice / 3; /*give the value to the variables named row and column depending on the choice*/
column = dice % 3;
};
board[row][column] = 'O';
}
void human() {
printf("\n(If you enter the number out of range, "
"you need to reenter the number until the right number.)\n");
do {
printf("\nPlayer, please enter the number of the chess board where you want to place your %c: ",'X');
scanf("%d", &choice);
getchar();
rewind(stdin);
row = --choice / 3;
column = choice % 3;
} while (choice<0 || choice>9 || board[row][column]>'9');
board[row][column] = 'X';
}
int main() /* define a function named main */
{
do {
printf("Do you want to go first or seconf? press 1 or 2 to select.\n");
scanf("%d", &selection);
getchar();
} while (selection < 1 || selection > 2);
rewind(stdin);
int i,j;
if (selection == 1)j = 5;
else j = 4;
for (i = 0; i<j && winner == 0; i++) /*using a loop to renew the chess board */
{
system("cls");
gambo();
if (selection == 1) {
player = 1;
human();
check();
player = 2;
com();
check();
}
else {
player = 2;
com();
system("cls");
gambo();
check();
player = 1;
human();
check();
}
}
end();
}