当前位置:网站首页>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';
}
结束
边栏推荐
- SimpleOSS third-party library libcurl and engine libcurl error solution
- Spark学习:编译Spark项目时遇到的报错
- After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...
- VBA connects Access database and Excel
- CIMC Shilian Dafeitong is the global industrial artificial intelligence AI leader, the world's top AI core technology, high generalization, high robustness, sparse sample continuous learning, industri
- What is the value of biomedical papers? How to translate the papers into Chinese and English?
- iPhone真是十三香?两代产品完全对比,或许上一代更值得买
- 来了!东方甄选为龙江农产品直播带货
- MySql中@符号的使用
- The use of terminal split screen tool Terminalx
猜你喜欢

MindSpore:【Resolve node failed】解析节点失败的问题

来了!东方甄选为龙江农产品直播带货

VBA runtime error '-2147217900 (80040e14): Automation error

The advanced version of the Niu Ke brushing series (team competition, sorting subsequences, inverting strings, deleting common characters, repairing pastures)

MindSpore:【resnet_thor模型】尝试运行resnet_thor时报Could not convert to

SimpleOSS第三方库libcurl与引擎libcurl错误解决方法

Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group

The Meta metaverse division lost 2.8 billion in the second quarter!Still want to keep betting?Metaverse development has yet to see a way out!

【hbuilder】运行不了部分项目 , 打开终端 无法输入指令

【PHPWord】Quick Start of PHPWord in PHPOffice Suite
随机推荐
LocalDate时间生成
来了!东方甄选为龙江农产品直播带货
牛客网——华为题库(100~108)
MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界
AWS console
又一家公司面试的内容
VBA connects Access database and Excel
防抖和节流有什么区别,分别用于什么场景?
DM8:单库单实例搭建本地数据守护服务
【私人系列】日常PHP遇到的各种稀奇古怪的问题
Multiple instances of mysql
VS Code 连接SQL Server
NXP IMX8QXP更换DDR型号操作流程
【刷题篇】计算质数
监听开机广播
VBA 连接Access数据库和Excle
Encapsulates a console file selector based on inquirer
云数据库和本地数据库有什么区别?
MindSpore:Cifar10Dataset‘s num_workers=8, this value is not within the required range of [1, cpu_thr
MindSpore:ImageFolderDataset数据读取问题