一 .首先打开vs软件
二 .创建空项目
三. 添加新建项main.c,复制下面代码进去
#include"game.h"
void mean()
{
printf("***************************\n");
printf("********** 1.paly *********\n");
printf("********** 0.exit *********\n");
printf("***************************\n");
}
void game()
{
char board[ROW][COL];
InitBoard(board,ROW,COL); //初始化数组
DisplayBoard(board, ROW, COL); //打印数组
char ret = 0;
while (1)
{
PlayerBoard(board, ROW, COL); //玩家下棋
DisplayBoard(board, ROW, COL); //打印棋谱
ret = Iswin(board, ROW, COL); //得到返回值 用于判断输赢
if (ret != 'C') //判断是否继续
break; //若返回值为C则跳出循环 判断输赢
ComputerBoard(board, ROW, COL); //电脑下
DisplayBoard(board, ROW, COL); //打印棋谱
ret = Iswin(board, ROW, COL); //上同
if (ret != 'C')
break;
}
if (ret == '*') //判断输赢
printf("玩家赢了\n");
if (ret == '#')
printf("电脑赢了\n");
if(ret == 'Q')
printf("平局\n");
}
int main() //主函数
{
int input = 0; //输入1/0.开始游戏
srand((unsigned)time(NULL)); //电脑随即下棋
do
{ //循环游戏开始
mean();
printf("请输入 >");
scanf_s("%d", &input);
switch (input)
{
case 1:
printf("开始游戏:\n");
game(); //执行游戏 game函数在上面调用
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("请重新选择\n");
break;
}
} while (input);
return 0;
}
四.再次新建项 game.c 将下面代码复制进去
#include"game.h"
void InitBoard(char board[ROW][COL], int row, int col) //初始化
{
int i = 0;
int j = 0;
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 = 0;
int j = 0;
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)
{
int j = 0;
for(j = 0; j<col;j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void PlayerBoard(char board[ROW][COL], int row, int col) //玩家下棋
{
int x = 0;
int y = 0;
printf("玩家走\n");
while (1)
{
printf("请输入游戏坐标:>\n");
scanf_s("%d %d", &x, &y);
if (x <= row && x >= 1 && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
printf("位置被占用请重新输入:>\n");
}
else
printf("位置不对,请重新输入:>\n");
}
}
void ComputerBoard(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] = '#';
break;
}
}
}
int Isfull(char board[ROW][COL], int row, int col) //判断是否平局(还有没有格子)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char Iswin(char board[ROW][COL], int row, int col) //判断是否赢
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
return board[i][1];
}
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
return board[1][j];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[0][0];
if (board[2][1] == board[2][2] && board[2][2] == board[1][2] && board[1][2] != ' ')
return board[2][1];
int ret = Isfull(board, ROW, COL);
if (ret == 1)
return 'Q';
else
return 'C';
}
五.再次添加新建项 game.h 赋值下面代码
#define ROW 3
#define COL 3
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void InitBoard(char board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col);
void PlayerBoard(char board[ROW][COL], int row, int col);
void ComputerBoard(char board[ROW][COL], int row, int col);
char Iswin(char board[ROW][COL], int row, int col);
int Isfull(char board[ROW][COL], int row, int col);
之后运行,即可
运行出来之后