# 1.菜单

``````void menu()
{
printf("**********************************************n");
printf("************       1.play         ************n");
printf("************       0.exit         ************n");
printf("**********************************************n");
}``````

``````	int input = 0;
do
{
printf("请输入(1/0):");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏n");
game();
break;
case 0:
printf("退出游戏n");
break;
default:
printf("输入错误，请重新输入n");
break;
}
} while (input);``````

# 2.初始化棋盘与打印棋盘

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

``````void DisplayBoard(char board[ROW][COL], int row, int col)//打印棋盘
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}       //“   |”这是一次循环
}           //当打印最后一次时不打印“|”
//打印出来是   |   |
printf("n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");//“---|”是一次循环
}               //打印最后一次时不打印“|”
}                   //打印出来是---|---|---
}
printf("n");//             上面的两行加一起是一次循环
}                              //最后一次循环不打印---|
}

//打印出来是这样的效果
//   |   |
//---|---|---
//   |   |
//---|---|---
//   |   |   ``````

# 3.玩家下棋和电脑下棋

``````void PlayMove(char board[ROW][COL], int row, int col)//玩家下棋
{
int x, y;
printf("玩家输入n");
while (1)
{
printf("请输入坐标:");
scanf("%d %d", &x, &y);
if (x > 0 && x <= row && y > 0 && y <= col&&board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else if (x <= 0 || x > row || y <= 0 || y > col)
{
printf("坐标非法，请重新输入n");
}
else if (board[x - 1][y - 1] != ' ')
{
printf("该坐标已被占用，请选择其他坐标n");
}
}
}``````

``````void ComputerMove0(char board[ROW][COL], int row, int col)
{
Sleep(200);
printf("电脑下棋n");
int x, y;
while (1)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}``````

（下面是按照优先序排列的）

1）如果己方（电脑）在横排、竖排、斜排已有两个棋子，如果剩下的位置没有棋子，则下在此处

2）如果对方（玩家）在横排、竖排、斜排已有两个棋子，如果剩下的位置没有棋子，则下在此处

3）如果以上情况都没有那么中间优先下，没有则下四角，如果都没有空位，那么随机下。

``````int ComputerMove1(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0, m = 1,n = 1;
printf("电脑输入n");
while(i < row)//判断电脑是否有两个
{
if (board[i][0] == '#' && board[i][1] == '#' && board[i][2] == ' ')
{
board[i][2] = '#';
return 1;
}
if (board[i][0] == '#' && board[i][2] == '#' && board[i][1] == ' ')
{
board[i][1] = '#';
return 1;
}
if (board[i][1] == '#' && board[i][2] == '#' && board[i][0] == ' ')
{
board[i][0] = '#';
return 1;
}
if (board[0][j] == '#' && board[1][j] == '#' && board[2][j] == ' ')
{
board[2][j] = '#';
return 1;
}
if (board[0][j] == '#' && board[2][j] == '#' && board[1][j] == ' ')
{
board[1][j] = '#';
return 1;
}
if (board[1][j] == '#' && board[2][j] == '#' && board[0][j] == ' ')
{
board[0][j] = '#';
return 1;
}
i++;
j++;
}
i = 0;
j = 0;
while (i < col)//判断玩家是否有两个
{
if (board[i][0] == '*' && board[i][1] == '*' && board[i][2] == ' ')
{
board[i][2] = '#';
return 1;
}
if (board[i][0] == '*' && board[i][2] == '*' && board[i][1] == ' ')
{
board[i][1] = '#';
return 1;
}
if (board[i][1] == '*' && board[i][2] == '*' && board[i][0] == ' ')
{
board[i][0] = '#';
return 1;
}
if (board[0][j] == '*' && board[1][j] == '*' && board[2][j] == ' ')
{
board[2][j] = '#';
return 1;
}
if (board[0][j] == '*' && board[2][j] == '*' && board[1][j] == ' ')
{
board[1][j] = '#';
return 1;
}
if (board[1][j] == '*' && board[2][j] == '*' && board[0][j] == ' ')
{
board[0][j] = '#';
return 1;
}
i++;
j++;
}
if (board[1][1] == ' ')//中间
{
board[1][1] = '#';
return 1;
}
else if (board[1][1] != ' ')//四角
{
if (board[0][0] == ' ')
{
board[0][0] = '#';
return 1;
}
else if (board[0][2] == ' ')
{
board[0][2] = '#';
return 1;
}
else if (board[2][0] == ' ')
{
board[2][0] = '#';
return 1;
}
else if (board[2][2] == ' ')
{
board[2][2] = '#';
return 1;
}
}
else//随机
{
int x, y;
while (1)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
return 1;
}
}``````

# 4.判断输赢

``````InitBoard(board, ROW, COL);//初始化棋盘
DisplayBoard(board, ROW, COL);//打印棋盘
while (1)
{
PlayMove(board, ROW, COL);//玩家下棋
DisplayBoard(board, ROW, COL);//打印棋盘
ret = who_win(board, ROW, COL);//判断输赢
if (ret == 0)
break;
ComputerMove0(board, ROW, COL);//电脑下棋
DisplayBoard(board, ROW, COL);//打印棋盘
ret = who_win(board, ROW, COL);//判断输赢
if (ret == 0)
break;
}``````
``````int who_win(char board[ROW][COL],int row, int col)
{
int i, j, count = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == '*' && board[i][1] == '*' && board[i][2] == '*')
{
printf("玩家赢n");
Sleep(2000);
system("cls");
return 0;
}
if (board[i][0] == '#' && board[i][1] == '#' && board[i][2] == '#')
{
printf("电脑赢n");
Sleep(2000);
system("cls");
return 0;
}
}
for (i = 0; i < col; i++)
{
if (board[0][i] == '*' && board[1][i] == '*' && board[2][i] == '*')
{
printf("玩家赢n");
Sleep(2000);
system("cls");
return 0;
}
if (board[0][i] == '#' && board[1][i] == '#' && board[2][i] == '#')
{
printf("电脑赢n");
Sleep(2000);
system("cls");
return 0;
}
}
if ((board[0][0] == '*' && board[1][1] == '*' && board[2][2] == '*') || (board[0][2] == '*' && board[1][1] == '*' && board[2][0] == '*'))
{
printf("玩家赢n");
Sleep(2000);
system("cls");
return 0;
}
if ((board[0][0] == '#' && board[1][1] == '#' && board[2][2] == '#') || (board[0][2] == '#' && board[1][1] == '#' && board[2][0] == '#'))
{
printf("电脑赢n");
Sleep(2000);
system("cls");
return 0;
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] != ' ')
{
count++;
}
}
}
if (count == row * col)
{
printf("平局了n");
Sleep(2000);
system("cls");
return 0;
}
return 1;
}``````

``````#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
{
printf("**********************************************n");
printf("************       1.play         ************n");
printf("************       0.exit         ************n");
printf("**********************************************n");
}

void game()
{
int ret = 1;
int n = 0;
char board[ROW][COL] = { 0 };
printf("请选择难度(0.简单/1.困难):");
scanf("%d", &n);
if (n == 0)
{
InitBoard(board, ROW, COL);//初始化棋盘
DisplayBoard(board, ROW, COL);//打印棋盘
while (1)
{
PlayMove(board, ROW, COL);//玩家下棋
DisplayBoard(board, ROW, COL);//棋盘
ret = who_win(board, ROW, COL);//判断输赢
if (ret == 0)
break;
ComputerMove0(board, ROW, COL);//电脑下棋
DisplayBoard(board, ROW, COL);//棋盘
ret = who_win(board, ROW, COL);//判断输赢
if (ret == 0)
break;
}
}
else if (n == 1)
{
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
while (1)
{
PlayMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = who_win(board, ROW, COL);
if (ret == 0)
break;
ComputerMove1(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = who_win(board, ROW, COL);
if (ret == 0)
break;
}
}
else
{
printf("输入错误，默认选简单n");
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
while (1)
{
PlayMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = who_win(board, ROW, COL);
if (ret == 0)
break;
ComputerMove0(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = who_win(board, ROW, COL);
if (ret == 0)
break;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
printf("请输入(1/0):");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏n");
game();
break;
case 0:
printf("退出游戏n");
break;
default:
printf("输入错误，请重新输入n");
break;
}
} while (input);
return 0;
}``````

game.h头文件：

``````#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

#define ROW 3
#define COL 3

void InitBoard(char board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col);
void PlayMove(char board[ROW][COL], int row, int col);
void ComputerMove0(char board[ROW][COL], int row, int col);
int ComputerMove1(char board[ROW][COL], int row, int col);
int who_win(char board[ROW][COL], int row, int col);``````

game.cpp存放函数实现的文件：

``````#include "game.h"

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

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

void PlayMove(char board[ROW][COL], int row, int col)//玩家下棋
{
int x, y;
printf("玩家输入n");
while (1)
{
printf("请输入坐标:");
scanf("%d %d", &x, &y);
if (x > 0 && x <= row && y > 0 && y <= col&&board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else if (x <= 0 || x > row || y <= 0 || y > col)
{
printf("坐标非法，请重新输入n");
}
else if (board[x - 1][y - 1] != ' ')
{
printf("该坐标已被占用，请选择其他坐标n");
}
}
}

void ComputerMove0(char board[ROW][COL], int row, int col)//简单电脑
{
Sleep(200);
printf("电脑下棋n");
int x, y;
while (1)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}

int ComputerMove1(char board[ROW][COL], int row, int col)//困难电脑
{
int i = 0, j = 0, m = 1,n = 1;
printf("电脑输入n");
while(i < row)
{
if (board[i][0] == '#' && board[i][1] == '#' && board[i][2] == ' ')
{
board[i][2] = '#';
return 1;
}
if (board[i][0] == '#' && board[i][2] == '#' && board[i][1] == ' ')
{
board[i][1] = '#';
return 1;
}
if (board[i][1] == '#' && board[i][2] == '#' && board[i][0] == ' ')
{
board[i][0] = '#';
return 1;
}
if (board[0][j] == '#' && board[1][j] == '#' && board[2][j] == ' ')
{
board[2][j] = '#';
return 1;
}
if (board[0][j] == '#' && board[2][j] == '#' && board[1][j] == ' ')
{
board[1][j] = '#';
return 1;
}
if (board[1][j] == '#' && board[2][j] == '#' && board[0][j] == ' ')
{
board[0][j] = '#';
return 1;
}
i++;
j++;
}
i = 0;
j = 0;
while (i < col)
{
if (board[i][0] == '*' && board[i][1] == '*' && board[i][2] == ' ')
{
board[i][2] = '#';
return 1;
}
if (board[i][0] == '*' && board[i][2] == '*' && board[i][1] == ' ')
{
board[i][1] = '#';
return 1;
}
if (board[i][1] == '*' && board[i][2] == '*' && board[i][0] == ' ')
{
board[i][0] = '#';
return 1;
}
if (board[0][j] == '*' && board[1][j] == '*' && board[2][j] == ' ')
{
board[2][j] = '#';
return 1;
}
if (board[0][j] == '*' && board[2][j] == '*' && board[1][j] == ' ')
{
board[1][j] = '#';
return 1;
}
if (board[1][j] == '*' && board[2][j] == '*' && board[0][j] == ' ')
{
board[0][j] = '#';
return 1;
}
i++;
j++;
}
if (board[1][1] == ' ')
{
board[1][1] = '#';
return 1;
}
else if (board[1][1] != ' ')
{
if (board[0][0] == ' ')
{
board[0][0] = '#';
return 1;
}
else if (board[0][2] == ' ')
{
board[0][2] = '#';
return 1;
}
else if (board[2][0] == ' ')
{
board[2][0] = '#';
return 1;
}
else if (board[2][2] == ' ')
{
board[2][2] = '#';
return 1;
}
}
else
{
int x, y;
while (1)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
return 1;
}
}

int who_win(char board[ROW][COL],int row, int col)//判断输赢
{
int i, j, count = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == '*' && board[i][1] == '*' && board[i][2] == '*')
{
printf("玩家赢n");
Sleep(2000);
system("cls");
return 0;
}
if (board[i][0] == '#' && board[i][1] == '#' && board[i][2] == '#')
{
printf("电脑赢n");
Sleep(2000);
system("cls");
return 0;
}
}
for (i = 0; i < col; i++)
{
if (board[0][i] == '*' && board[1][i] == '*' && board[2][i] == '*')
{
printf("玩家赢n");
Sleep(2000);
system("cls");
return 0;
}
if (board[0][i] == '#' && board[1][i] == '#' && board[2][i] == '#')
{
printf("电脑赢n");
Sleep(2000);
system("cls");
return 0;
}
}
if ((board[0][0] == '*' && board[1][1] == '*' && board[2][2] == '*') || (board[0][2] == '*' && board[1][1] == '*' && board[2][0] == '*'))
{
printf("玩家赢n");
Sleep(2000);
system("cls");
return 0;
}
if ((board[0][0] == '#' && board[1][1] == '#' && board[2][2] == '#') || (board[0][2] == '#' && board[1][1] == '#' && board[2][0] == '#'))
{
printf("电脑赢n");
Sleep(2000);
system("cls");
return 0;
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] != ' ')
{
count++;
}
}
}
if (count == row * col)
{
printf("平局了n");
Sleep(2000);
system("cls");
return 0;
}
return 1;
}``````

THE END

)">