game.h
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
void board_display(char arr[ROW][COL], int row, int col);//打印棋盘
void init_board(char arr[ROW][COL], int row, int col);//初始化棋盘
void PlayerMove(char arr[ROW][COL], int row, int col);//玩家走
void ComputerMove(char arr[ROW][COL], int row, int col);//电脑走
int check_win(char arr[ROW][COL], int row, int col);//判断输赢
int show(char arr[ROW][COL], int row, int col);//判断棋盘是否还有空位置
#endif//__GAME_H__
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init_board(char arr[ROW][COL], int row, int col)//初始棋盘
{
int i = 0;
int j = 0;
for (i = 0; i<ROW; i++)
{
for (j = 0; j<COL; j++)
{
arr[i][j] = ' ';
}
}
}
void board_display(char arr[ROW][COL], int row, int col)//打印棋盘
{
int i = 0;
for (i = 0; i<ROW; i++)
{
printf("%c %c %c\n", arr[i][0], arr[i][1], arr[i][2]);
if (i<ROW - 1)
printf("---|---|---\n");
}
}
void PlayerMove(char arr[ROW][COL], int row, int col)//玩家走一步
{
while (1)
{
printf("请输入坐标:");
scanf("%d%d", &row, &col);
row--;
col--;
if ((row >= 0) && (row <= ROW - 1) && (col >= 0) && (col <= COL - 1))
{
if (arr[row][col] == ' ')
{
arr[row][col] = 'X';
break;
}
else
printf("坐标被占用\n");
}
}
}
void ComputerMove(char arr[ROW][COL], int row, int col)//电脑走一步
{
srand((unsigned)time(NULL));
while (1)
{
row = rand() % ROW;//随机的位置
col = rand() % COL;
if (arr[row][col] == ' ')
{
arr[row][col] = '$';
break;
}
}//如果坐标被占用,即if不成立,将一直循环,找到一个空位的
}
int check_win(char arr[ROW][COL], int row, int col)//判断是否有赢的结果
{
int i = 0;
for (i = 0; i<ROW; i++)
{
if ((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2]) && (arr[i][1] != ' '))//一行相等
return arr[i][1];
if ((arr[0][i] == arr[1][i]) && (arr[1][i] == arr[2][i]) && (arr[1][i] != ' '))//一列相等
return arr[1][i];
if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) && (arr[1][1] != ' '))//正对角线相等
return arr[1][1];
if ((arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0]) && (arr[1][1] != ' '))//反对角线相等
return arr[1][1];
}
if (show(arr, row, col) == 0)
return ' ';//还能走,棋盘还有位置,并且没有相连的
else
return 'q';//平局
}
int show(char arr[ROW][COL], int row, int col)//判断棋盘是否还有空位置
{
int i = 0;
for (i = 0; i<ROW; i++)
{
int j = 0;
for (j = 0; j<COL; j++)
{
if (arr[i][j] == ' ')
return 0;
}
}
return 1;
}
play.c
#include<stdio.h>
#include"game.h"
void menu()
{
printf("********************************\n");
printf("***** 1.play 2.exit ****\n");
printf("********************************\n");
}
void game()
{
int row = 0;
int col = 0;
char arr[ROW][COL] = { 0 };//棋盘
char ret = 0;//判断输赢参数
init_board(arr, row, col);//初始化棋盘
do
{
board_display(arr, row, col);//打印棋盘
printf("玩家走:\n");
PlayerMove(arr, row, col);//玩家走
board_display(arr, row, col);//打印棋盘
ret = check_win(arr, row, col);//判断输赢
if (ret != ' ')
break;
ComputerMove(arr, row, col);//电脑走
printf("电脑走:\n");
ret = check_win(arr, row, col);//判断输赢
if (ret != ' ')
break;
} while (ret == ' ');
board_display(arr, row, col);//打印棋盘
if (ret == 'X')
printf("玩家赢了\n");
else if (ret == '$')
printf("电脑赢了\n");
else if (ret == 'q')
printf("平局\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:game();
break;
case 2:return 0;
break;
default:printf("选择错误\n");
break;
}
} while (input);
getchar();//缓冲一下,不玩游戏了也不会那么快关闭窗口
return 0;
}