当前位置:网站首页>试写C语言三子棋
试写C语言三子棋
2022-07-30 19:08:00 【kongqizyd146】
这个三子棋代码应该主要是在最后san.c中最后的iswin代码块中出现问题,但是逻辑有点绕。
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("输入不符合的坐标,请重新输入\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';
}
结束
边栏推荐
- vxe-table实现复选框鼠标拖动选中
- Spark学习:编译Spark项目时遇到的报错
- OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.解决方法
- Scrapy framework is introduced
- golang日志库zerolog使用记录
- Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group
- AI Basics: Graphical Transformer
- Swiper轮播图片并播放背景音乐
- 跨进程启动后台服务
- 常见链表题及其 Go 实现
猜你喜欢

Pytorch foundation -- tensorboard use (1)

Deepen school-enterprise cooperation and build an "overpass" for the growth of technical and skilled talents

【总结】1396- 60+个 VSCode 插件,打造好用的编辑器

The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)

6 yuan per catty, why do Japanese companies come to China to collect cigarette butts?
![【Prometheus】Prometheus联邦的一次优化记录[续]](/img/5d/56e171b7a02584337a0cfe5c731fb2.png)
【Prometheus】Prometheus联邦的一次优化记录[续]

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

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

【剑指 Offer】剑指 Offer 22. 链表中倒数第k个节点

MindSpore:【语音识别】DFCNN网络训练loss不收敛
随机推荐
【科普】无线电波怎样传送信息?
还有三天忙完
MySQL data types
SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
【Swords Offer】Swords Offer 17. Print n digits from 1 to the largest
Pytorch foundation -- tensorboard use (1)
第十七届“振兴杯”全国青年 职业技能大赛——计算机程序设计员(云计算平台与运维)参赛回顾与总结
【刷题篇】计算质数
MindSpore:mindspore有没有类似tf.GradientTape()用来求解梯度的?
JsonUtil基于字符串操作josn
什么是 RESTful API?
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
[TypeScript]编译配置
Swiper轮播图片并播放背景音乐
尊重客观事实
Tensorflow2.0 混淆矩阵与打印准确率不符
Common linked list problems and their Go implementation
一文读懂“语言模型”
又一家公司面试的内容
The advanced version of the Niu Ke brushing series (team competition, sorting subsequences, inverting strings, deleting common characters, repairing pastures)