当前位置:网站首页>试写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';
}
结束
边栏推荐
- What is the difference between a cloud database and an on-premises database?
- 谷歌AlphaFold近日宣称预测出地球上几乎所有蛋白质结构
- DM8: Single database and single instance to build a local data guard service
- Deepen school-enterprise cooperation and build an "overpass" for the growth of technical and skilled talents
- Node encapsulates a console progress bar plugin
- redis
- MindSpore:mindspore有没有类似tf.GradientTape()用来求解梯度的?
- Does the satellite phone communicate directly with the satellite or through a ground station?
- MindSpore:【resnet_thor模型】尝试运行resnet_thor时报Could not convert to
- 防抖和节流有什么区别,分别用于什么场景?
猜你喜欢

After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...

The use of @ symbol in MySql

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

Recommendation | People who are kind to you, don't repay them by inviting them to eat

中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳

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

阿里云武林头条活动分享

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

MindSpore: CV.Rescale(rescale,shift)中参数rescale和shift的含义?

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
随机推荐
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!
[Use of Qt Designer tool]
还有三天忙完
VBA 连接Access数据库和Excle
OneFlow source code analysis: Op, Kernel and interpreter
LocalDate时间生成
【Pointing to Offer】Pointing to Offer 22. The kth node from the bottom in the linked list
Critical Reviews | 南农邹建文组综述全球农田土壤抗生素与耐药基因分布
AI Basics: Graphical Transformer
第14章 类型信息
VBA batch import Excel data into Access database
【网站放大镜效果】两种方式实现
运营 23 年,昔日“国内第一大电商网站”黄了...
How architects grow
浅聊对比学习(Contrastive Learning)第一弹
跨进程启动后台服务
What is the value of biomedical papers? How to translate the papers into Chinese and English?
What is the difference between a cloud database and an on-premises database?
NXP IMX8QXP replacement DDR model operation process
AWS console