当前位置:网站首页>Trial writing C language sanbang
Trial writing C language sanbang
2022-07-30 19:21:00 【kongqizyd146】
This backgammon code should mostly be at the endsan.c中最后的iswinThere is a problem in the code block,But the logic is a bit tricky.
7.28练习.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "san.h"
void menu()
{
printf("************************\n");
printf("****** 1. 开始 *******\n");
printf("****** 0. 结束 *******\n");
printf("************************\n");
}
void game()
{
char ret = 0;
char board[ROW][COL];
init_board(board, ROW, COL);
display_board(board, ROW, COL);
while (1)
{
player_board(board, ROW, COL);
display_board(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'C')
break;
computer_board(board, ROW, COL);
display_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
}
if (ret == '*')
printf("玩家赢\n");
else if (ret == '#')
printf("电脑赢\n");
else if (ret == 'Q')
printf("平局\n");
display_board(board, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请输入: >");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
san.h
#pragma once
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 7
#define COL 7
//初始化数组
void init_board(char board[ROW][COL], int row, int col);
//打印数组
void display_board(char board[ROW][COL], int row, int col);
//玩家下棋
void player_board(char board[ROW][COL], int row, int col);
//电脑下棋
void computer_board(char board[ROW][COL], int row, int col);
//判断输赢
char is_win(char board[ROW][COL], int row, int col);
san.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "san.h"
void init_board(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
j = 0;
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void display_board(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
j = 0;
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 player_board(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋\n");
while (1)
{
printf("请输入坐标: >");
scanf("%d %d", &x, &y);
if (x <= row && x >= 0 && y > 1 && y <= col)
{
if (board[x][y] == ' ')
{
board[x][y] = '*';
break;
}
else
{
printf("该坐标已被占用,请重新输入\n");
}
}
else
{
printf("Enter coordinates that do not match,请重新输入\n");
}
}
}
void computer_board(char board[ROW][COL], int row, int col)
{
printf("电脑下棋\n");
while (1)
{
int n = rand() % row;
int m = rand() % col;
if (board[n][m] == ' ')
{
board[n][m] = '#';
break;
}
}
}
static int full(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
j = 0;
for (j = 0; j < col - 1; j++)
{
if (' ' == board[i][j])
{
return 0;
}
}
}
return 1;
}
char is_win(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int a = 0;
int b = 0;
for (i = 0; i <= row; i++)
{
j = 0;
a = 0;
b = 0;
for (j = 0; j < col - 1; j++)
{
if (board[i][j] == board[i][j + 1] && board[i][j] == '*')
a++;
if (board[i][j] == board[i][j + 1] && board[i][j] == '#')
b++;
if (board[i][j + 2] != '*')
a = 0;
if (board[i][j + 2] != '#')
b = 0;
if (2 == a)
return '*';
else if (2 == b)
return '#';
}
}
i = 0;
j = 0;
a = 0;
b = 0;
for (j = 0; j <= row; j++)
{
i = 0;
a = 0;
b = 0;
for (i = 0; i < col - 1; i++)
{
if (board[i][j] == board[i + 1][j] && board[i][j] == '*')
a++;
if (board[i][j] == board[i + 1][j] && board[i][j] == '#')
b++;
if (board[i + 2][j] != '*')
a = 0;
if (board[i + 2][j] != '#')
b = 0;
if (2 == a)
return '*';
else if (2 == b)
return '#';
}
}
i = 0;
j = 0;
a = 0;
b = 0;
for (j = 0; j <= row; j++)
{
i = 0;
a = 0;
b = 0;
for (i = 0; i < col - 1; i++)
{
if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '*')
a++;
if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '#')
b++;
if (board[i + 2][j + 2] != '*')
a = 0;
if (board[i + 2][j + 2] != '#')
b = 0;
if (2 == a)
return '*';
else if (2 == b)
return '#';
}
}
i = 0;
j = 0;
a = 0;
b = 0;
for (j = 0; j <= row; j++)
{
i = 0;
a = 0;
b = 0;
for (i = 0; i < col - 1; i++)
{
if (board[i][j] == board[i + 1][j - 1] && board[i][j] == '*')
a++;
if (board[i][j] == board[i + 1][j - 1] && board[i][j] == '#')
b++;
if (board[i + 2][j - 2] != '*')
a = 0;
if (board[i + 2][j - 2] != '#')
b = 0;
if (2 == a)
return '*';
else if (2 == b)
return '#';
}
}
if (isfull(board, row, col) == 1)
{
return 'Q';
}
return 'C';
}
结束
边栏推荐
- Object和Map的区别
- 【flink】报错整理 Could not instantiate the executor. Make sure a planner module is on the classpath
- 【PyTorchVideo教程01】快速实现视频动作识别
- The use of @ symbol in MySql
- The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary
- The problem of writing go run in crontab does not execute
- Entering the applet for the first time
- Zabbix 5.0 监控教程(一)
- MindSpore:Cifar10Dataset‘s num_workers=8, this value is not within the required range of [1, cpu_thr
- 第一次进入小程序判断
猜你喜欢

NC | Tao Liang Group of West Lake University - TMPRSS2 "assists" virus infection and mediates the host invasion of Clostridium sothrix hemorrhagic toxin...

Alibaba Cloud Martial Arts Headline Event Sharing

解决终极bug,项目最终能顺利部署上线。

VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误

【Pointing to Offer】Pointing to Offer 22. The kth node from the bottom in the linked list

牛客刷题系列之进阶版(组队竞赛,排序子序列,倒置字符串, 删除公共字符,修理牧场)

Talking about Contrastive Learning (Contrastive Learning) the first bullet

【剑指 Offe】剑指 Offer 18. 删除链表的节点

VBA 连接Access数据库和Excle

NXP IMX8QXP更换DDR型号操作流程
随机推荐
The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)
跨域问题的解决方法
Perfectly Clear QuickDesk & QuickServer图像校正优化工具
OneFlow源码解析:Op、Kernel与解释器
监听开机广播
VBA batch import Excel data into Access database
VBA 连接Access数据库和Excle
谷歌AlphaFold近日宣称预测出地球上几乎所有蛋白质结构
部分分类网络性能对比
Scala学习:类和对象
Chapter 14 Type Information
NXP IMX8QXP更换DDR型号操作流程
已删除
JsonUtil基于字符串操作josn
几个GTest、GMock的例子
Encapsulates a console file selector based on inquirer
阿里云武林头条活动分享
VS Code 连接SQL Server
Swiper轮播图片并播放背景音乐
Alibaba Cloud Martial Arts Headline Event Sharing