代码在结尾!!!(供侦错)
更新不易,麻烦多多点赞,欢迎你的建议·提问·想法,感谢你的转发,
关注我,关注我,关注我,你会看到更多有趣的博客哦!!!
你对我真的很重要,喵喵喵!
目录
第一步:创建项目
test.h--测试逻辑
game.h和game.c--游戏的实现
第二步:养成一个好习惯
接下来,我们才准备好,正式开始写代码了!
第三步:打印菜单
第四步: 正式开始游戏,初始化游戏的行和列
第五步:初始化棋盘,使每个棋位为空格
先声明
test.c引用game.h
再game.h实现
阶段性测试实现!!!
第六步打印棋盘
老规矩先声明!!!
在头文件声明
test.c调用
game.c实现
没显现棋盘,看不出来耶?
棋盘显现(三字棋)
对print_board进行修改,棋盘与棋格显现,进行阶段性调试。
调试完,记得改回来!!!
棋盘显现(n字棋)
注意%c前后的空格记得加上!!!否则这就是下场。别问我怎么知道的。哈哈哈哈~
第七步:玩家下棋啦!
记着玩家下棋要循环!!!
因为玩家没有0行0列的概念,所以x和y都要减一来实现程序!
阶段性测试数据,养成一个好习惯!!!
第八步:电脑下棋(随机下棋)
太菜,目前只能这样了,以后优化.
反向操作让电脑赢。
使用rand之前要在test函数中调用srand,且srand只调用一次,给上一个空指针time(NULL),time(NULL)是时间戳,形成一个随机且不重复的值。rand生成的随机数是0~32767的数字,我们要0~2,行row是3,rand%3,余数只能是0~2.
test引用srand
头文件要进行声明,使用srand和time需要调用的。
声明电脑下棋
在game.c中 电脑下棋的实现
阶段性测试!!!
第九步:判断输赢,游戏结束!
定义返回值ret及结果返回函数
判断赢得结果横,竖,对角线。
判断棋盘平局,棋盘满了
为平局定义一个函数
is_full仅在is_win中使用不用在头文件声明
游戏结束
更新不易,麻烦多多点赞,欢迎你的提问,感谢你的转发,
最后的最后,关注我,关注我,关注我,你会看到更多有趣的博客哦!!!
我的多次翻车现场中最搞心态的,啊啊啊啊啊啊~~~~~~~~
我谢谢你。
代码附上:
game.h
#pragma once
#define ROW 3
#define COL 3
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
void init_board(char board[ROW][COL], int row, int col);
void print_board(char board[ROW][COL], int row, int col);
void player_move(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, int col);
test.c
void test()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init_board(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++)
{
board[i][j] = ' ';
}
}
}
void print_board(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
//printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{ //printf("---|---|---\n");
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void player_move(char board[ROW][COL], int row, int col)
{
printf("玩家下棋\n");
while (1)
{
printf("请输入下棋的坐标:>");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
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] = '#';
break;
}
}
}
static int is_full(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 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] && board[i][0] != ' ')
{
return board[i][0];
}
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
if (is_full(board, row, col) == 1)
{
return 'Q';
}
return 'C';//游戏继续
}