# 一、分析及实现

## 1.棋盘

``````//初始化棋盘
void InitBoard(char board[][COL], int row, int col)
{
int i = 0;
for (i = 0; i < ROW; i++)
{
int j = 0;
for (j = 0; j < COL; j++)
{
board[i][j] = ' ';
}
}
}
``````

``````//打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
printf("+---+---+---+n");
int j = 0;
for (j = 0; j < col; j++)
{
printf("| %c ", board[i][j]);
}
printf("|n");
}
printf("+---+---+---+n");
}
``````

## 2.落子

``````//玩家下棋
void Player_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家下棋:->");
scanf("%d %d", &x, &y);

if (x > 0 && x <= row && y > 0 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'O';
break;
}
else
{
printf("坐标已被占用！n");
}
}
else
{
printf("坐标非法，请重新输入!n");
}
}
}
``````

``````//电脑下棋
void Computer_move(char board[ROW][COL], int row, int col)
{
printf("电脑下棋：n");
while (1)
{
int x = rand() % ROW;
int y = rand() % COL;
if (board[x][y] == ' ')
{
board[x][y] = 'X';
break;
}
}
}
``````

## 3.判断输赢

``````//判断输赢
char Is_win(char board[ROW][COL], int row, int col)
{
//判断行列
int i = 0;
for (i = 0; i < ROW; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
{
return board[i][0];
}

if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
{
return board[0][i];
}

}
//判断对角线
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2])
|| (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
{
return board[1][1];
}

if (Is_full(board, row, col))
{
return 'D';  //平局
}
return 'C'; //Continue表示继续
}
``````

``````//判断棋盘是否满了
int Is_full(char board[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 (board[i][j] == ' ')
return 0;
}
}
return 1;
}
``````

# 三.完整代码

## 1.main.c

``````#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

int main()
{
test();
return 0;
}
``````

## 2.game.c

``````#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

{
printf("**********************************n");
printf("**********   1.play   ************n");
printf("**********   0.exit   ************n");
printf("**********************************n");
}
void game()
{
char board[ROW][COL] = {0};
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);

char ret = 0;
while (1)
{
Player_move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = Is_win(board, ROW, COL);
if (ret == 'O')
{
printf("玩家赢！n");
break;
}
if (ret == 'D')
{
printf("平局n");
break;
}
Computer_move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = Is_win(board, ROW, COL);
if (ret == 'X')
{
printf("电脑赢!n");
break;
}
}

}
void test()
{
srand((unsigned int)time(NULL));
int input = 0;

do
{
printf("请选择:->");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏！n");
break;
default:
printf("输入错误，请重新输入!n");
break;
}
} while (input);
}

//初始化棋盘
void InitBoard(char board[][COL], int row, int col)
{
int i = 0;
for (i = 0; i < ROW; i++)
{
int j = 0;
for (j = 0; j < COL; j++)
{
board[i][j] = ' ';
}
}
}
//打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
printf("+---+---+---+n");
int j = 0;
for (j = 0; j < col; j++)
{
printf("| %c ", board[i][j]);
}
printf("|n");
}
printf("+---+---+---+n");
}
//玩家下棋
void Player_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家下棋:->");
scanf("%d %d", &x, &y);

if (x > 0 && x <= row && y > 0 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'O';
break;
}
else
{
printf("坐标已被占用！n");
}
}
else
{
printf("坐标非法，请重新输入!n");
}
}
}
//电脑下棋
void Computer_move(char board[ROW][COL], int row, int col)
{
printf("电脑下棋：n");
while (1)
{
int x = rand() % ROW;
int y = rand() % COL;
if (board[x][y] == ' ')
{
board[x][y] = 'X';
break;
}
}
}
//判断棋盘是否满了
int Is_full(char board[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 (board[i][j] == ' ')
return 0;
}
}
return 1;
}
//判断输赢
char Is_win(char board[ROW][COL], int row, int col)
{
//判断行列
int i = 0;
for (i = 0; i < ROW; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
{
return board[i][0];
}

if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
{
return board[0][i];
}

}
//判断对角线
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2])
|| (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
{
return board[1][1];
}

if (Is_full(board, row, col))
{
return 'D';  //平局
}
return 'C'; //Continue表示继续
}

``````

## 3.game.h

``````#define _CRT_SECURE_NO_WARNINGS 1

#define ROW 3
#define COL 3

#include<stdio.h>
#include<math.h>
#include<time.h>

void test();

void InitBoard(char board[ROW][COL], int, int);
void DisplayBoard(char board[ROW][COL], int, int);
void Player_move(char board[ROW][COL], int, int);
void Computer_move(char board[ROW][COL], int, int);
char Is_win(char board[ROW][COL], int, int);
``````

THE END