当前位置:网站首页>C语言—————三子棋游戏
C语言—————三子棋游戏
2020-11-09 12:58:00 【osc_rmqoxylv】
三子棋游戏
问题描述:
3*3的棋盘中,只要一条线上出现三个一样的棋子就获胜(玩家或电脑);如果棋盘已经放满还未出现三个棋子一条线则打成平手。
具体细节:
- 初始化棋盘(用空格初始化)
//初始化棋盘
void initChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
chessbox[row][col] = ' ';
}
}
}
- 打印棋盘
//打印棋盘
void printChess(char chessbox[ROW][COL]){
system("cls");
printf("+---+---+---+\n");
for (int row = 0; row < ROW; row++){
printf("| %c | %c | %c |\n",
chessbox[row][0], chessbox[row][1],
chessbox[row][2]);
printf("+---+---+---+\n");
}
}
- 电脑落子(用o表示电脑落子)
//电脑落子(用o表示)
void computerMove(char chessbox[ROW][COL]){
srand(time(0));
while (1){
int row = rand() % 3;
int col = rand() % 3;
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'o';
break;
}
}
}
- 玩家落子
//玩家落子(用x表示)
void playerMove(char chessbox[ROW][COL]){
int row, col;
while (1){
printf("请输入您的落子地点:");
scanf("%d %d", &row, &col);
if (row >= 3 || col >= 3){
printf("您输入的落子位置有误,请重新输入:");
continue;
}
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'x';
break;
}
printf("该位置已有棋子,请重新输入:");
}
}
- 三个棋子一条线
- 在一行或一列实现三个棋子一条线
//行
for (int row = 0; row < ROW; row++){
if (chessbox[row][0] != ' '
&&chessbox[row][0] == chessbox[row][1]
&& chessbox[row][0] == chessbox[row][2]){
return chessbox[row][0];
}
}
//列
for (int col = 0; col < COL; col++){
if (chessbox[0][col] != ' '
&&chessbox[0][col] == chessbox[1][col]
&& chessbox[0][col] == chessbox[2][col]){
return chessbox[0][col];
}
}
- 对角线实现三个棋子一条线
if (chessbox[0][0] != ' '
&&chessbox[0][0] == chessbox[1][1]
&& chessbox[0][0] == chessbox[2][2]){
return chessbox[0][0];
}
if (chessbox[2][0] != ' '
&&chessbox[2][0] == chessbox[1][1]
&& chessbox[2][0] == chessbox[0][2]){
return chessbox[2][0];
}
- 和棋
- 棋盘放满还未获胜,则为和棋,打成了平手。
在这里插入代码片
//和棋
if(isFull(checkbox)){
return 'a';
}
- 输赢约定:
- 返回x代表玩家获胜
if (isWinner(chessbox) == 'x'){
printf("恭喜您赢啦!\n");
break;
}
- 返回o代表电脑获胜
if (isWinner(chessbox) == 'o'){
printf("很遗憾,您输了!\n");
break;
}
- 返回a代表和棋(打成平手)
if (isWinner(chessbox) == 'a'){
printf("你和电脑同一水平呦!\n");
break;
}
- 判断棋盘是否放满:
- 返回1代表棋盘已满
- 返回0代表棋盘未满
//判断棋盘是否摆满
//1表示满;0表示不满。
int isFullChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
//找到空格,说明未满
if (chessbox[row][col] == ' '){
return 0;
}
}
}
return 1;
}
源代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define ROW 3
#define COL 3
//玩家落子(用x表示)
void playerMove(char chessbox[ROW][COL]){
int row, col;
while (1){
printf("请输入您的落子地点:");
scanf("%d %d", &row, &col);
if (row >= 3 || col >= 3){
printf("您输入的落子位置有误,请重新输入:");
continue;
}
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'x';
break;
}
printf("该位置已有棋子,请重新输入:");
}
}
//电脑落子(用o表示)
void computerMove(char chessbox[ROW][COL]){
srand(time(0));
while (1){
int row = rand() % 3;
int col = rand() % 3;
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'o';
break;
}
}
}
//初始化棋盘
void initChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
chessbox[row][col] = ' ';
}
}
}
//打印棋盘
void printChess(char chessbox[ROW][COL]){
system("cls");
printf("+---+---+---+\n");
for (int row = 0; row < ROW; row++){
printf("| %c | %c | %c |\n",
chessbox[row][0], chessbox[row][1],
chessbox[row][2]);
printf("+---+---+---+\n");
}
}
//判断棋盘是否摆满,摆满返回1;未满返回0
int isFullChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
//找到空格,说明未满
if (chessbox[row][col] == ' '){
return 0;
}
}
}
return 1;
}
//约定:返回x代表玩家赢了;返回o代表电脑赢了;返回a代表和棋
char isWinner(char chessbox[ROW][COL]){
//行
for (int row = 0; row < ROW; row++){
if (chessbox[row][0] != ' '
&&chessbox[row][0] == chessbox[row][1]
&& chessbox[row][0] == chessbox[row][2]){
return chessbox[row][0];
}
}
//列
for (int col = 0; col < COL; col++){
if (chessbox[0][col] != ' '
&&chessbox[0][col] == chessbox[1][col]
&& chessbox[0][col] == chessbox[2][col]){
return chessbox[0][col];
}
}
//对角线
if (chessbox[0][0] != ' '
&&chessbox[0][0] == chessbox[1][1]
&& chessbox[0][0] == chessbox[2][2]){
return chessbox[0][0];
}
if (chessbox[2][0] != ' '
&&chessbox[2][0] == chessbox[1][1]
&& chessbox[2][0] == chessbox[0][2]){
return chessbox[2][0];
}
//和棋
if (isFullChess(chessbox)){
return 'a';
}
return 0;
}
//开始一局游戏
void game(){
char chessbox[ROW][COL] = {
0 };
initChess(chessbox);
printf("游戏开始啦!\n");
printChess(chessbox);
while (1){
//玩家落子
playerMove(chessbox);
//打印棋牌
printChess(chessbox);
//判断胜负
if (isWinner(chessbox) == 'x'){
printf("恭喜您赢啦!\n");
break;
}
if (isWinner(chessbox) == 'a'){
printf("你和电脑同一水平呦!\n");
break;
}
//电脑落子
computerMove(chessbox);
//打印棋牌
printChess(chessbox);
//判断胜负
if (isWinner(chessbox) == 'o'){
printf("很遗憾,您输了!\n");
break;
}
if (isWinner(chessbox) == 'a'){
printf("你和电脑同一水平呦!\n");
break;
}
}
}
int menu(){
printf("===============\n");
printf("1.开始游戏\n");
printf("0.结束游戏\n");
printf("===============\n");
int choice;
printf("请输入您的选择:");
scanf("%d", &choice);
return choice;
}
int main()
{
ile (1){
int choice=menu();
if (choice == 1){
game();
continue;
}
else if (choice == 0){
break;
}else{
printf("输入有误,请您重新输入!\n");
continue;
}
}
system("pause");
return 0;
}
运行结果:
版权声明
本文为[osc_rmqoxylv]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4313784/blog/4709402
边栏推荐
- Vscode plug-in configuration pointing North
- vscode 插件配置指北
- 分库分表的 9种分布式主键ID 生成方案,挺全乎的
- 【分布式】分布式锁都有哪些实现方案?
- Impact of libssl on CentOS login
- Reading design patterns adapter patterns
- Learn with me. NETCORE EF core practical introduction, a look will
- Android studio import customized framework classess.jar As 4.0.1 version is valid for pro test
- IDEA解决yml配置文件中文输出乱码问题
- AI fresh student's annual salary has increased to 400000, you can still make a career change now!
猜你喜欢
彩虹排序 | 荷兰旗问题
【分布式】分布式锁都有哪些实现方案?
Adobe experience design / XD 2020 software installation package (with installation tutorial)
Well, the four ways to query the maximum value of sliding window are good
AI fresh student's annual salary has increased to 400000, you can still make a career change now!
Mobile security reinforcement helps app achieve comprehensive and effective security protection
Detailed explanation of [golang] GC
Using rem, the font size changes when the screen zooms
Android 集成支付的四部曲
手写Koa.js源码
随机推荐
Adobe experience design / XD 2020 software installation package (with installation tutorial)
面试了一位33岁Android程序员,只会面向百度编程,居然要25k,被我一顿怼
JVM learning (5) - execution subsystem
Explain Python input() function: get user input string
Android 集成支付的四部曲
vscode 插件配置指北
Show profile analysis of SQL statement performance overhead
未来中国电信将把云计算服务打造成为中国电信的主业
7-10倍写入性能提升:剖析WiredTiger数据页无锁及压缩黑科技
Reduce of Flink
Download Netease cloud music 10W + music library with Python
阿里、腾讯、百度、网易、美团Android面试经验分享,拿到了百度、腾讯offer
03.优先链接模型
Method of creating flat panel simulator by Android studio
Safety (miscellany)
“开源软件供应链点亮计划 - 暑期 2020”公布结果 基于 ChubaoFS 开发的项目获得最佳质量奖
SEO见风使舵,是对还是错?
Jsliang job series - 08 - handwritten promise
医疗项目管理的三种实用技巧
接口测试如何在post请求中传递文件