当前位置:网站首页>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';
}
结束
边栏推荐
猜你喜欢

The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary

MindSpore:【MindSpore1.1】Mindspore安装后验证出现cudaSetDevice failed错误

node封装一个控制台进度条插件

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

OneFlow source code analysis: Op, Kernel and interpreter

OneFlow源码解析:Op、Kernel与解释器

Zabbix 5.0 监控教程(一)

Encapsulates a console file selector based on inquirer

第十七届“振兴杯”全国青年 职业技能大赛——计算机程序设计员(云计算平台与运维)参赛回顾与总结

MindSpore:【Resolve node failed】解析节点失败的问题
随机推荐
【MindSpore】用coco2017训练Model_zoo上的 yolov4,迭代了两千多batch_size之后报错,大佬们帮忙看看。
Go system collection
OneFlow源码解析:Op、Kernel与解释器
在华为云,见证迷你世界的神奇觉醒
Range.CopyFromRecordset method (Excel)
阿里云武林头条活动分享
LocalDate时间生成
经济新闻:错误# 15:初始化libiomp5md。dll,但发现libiomp5md。已经初始化dll。解决方法
The use of @ symbol in MySql
电脑死机的时候,发生了什么?
Swiper rotates pictures and plays background music
牛客网——华为题库(100~108)
How do radio waves transmit information?
The advanced version of the Niu Ke brushing series (team competition, sorting subsequences, inverting strings, deleting common characters, repairing pastures)
【网站放大镜效果】两种方式实现
云数据库和本地数据库有什么区别?
DM8: Single database and single instance to build a local data guard service
Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group
Witness the magical awakening of the mini world in HUAWEI CLOUD
Chapter 14 Type Information