当前位置:网站首页>c语言:来实现一个小程序n子棋(已五子棋为例)
c语言:来实现一个小程序n子棋(已五子棋为例)
2022-07-29 11:10:00 【某某小卢】
首先是头文件的代码
#pragma once
#define ROW 5
#define COL 5
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void start__board(char board[ROW][COL], int row, int col);
void start__qipan(char board[ROW][COL], int row, int col);
//void qipan__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 win(char board[ROW][COL], int row, int col);然后是实现函数个个功能的代码
#include"game.h"
void start__board(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void start__qipan(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (int j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
}
printf("\n");
}
}
//void qipan__board(char board[ROW][COL], int row, int col)
//{
// int i = 0;
// for (i = 0; i < row; i++)
// {
// for (int n = 0; n < col;n++)
// {
//
// }
// }
//}
void player__board(char board[ROW][COL], int row, int col)
{
int x, y;
printf("玩家下棋:\n");
while (1)
{
printf("请输入坐标:->\n");
scanf_s("%d %d", &x, &y);
if (x > 0 && x < row+1&& y>0 && y < col+1)
{
if (board[x-1][y-1]==' ')
{
board[x-1][y-1] = '*';
break;
}
else
printf("非法占用,请重新输入:\n");
}
else
printf("超出范围,请重新输入\n");
}
}
void computer__board(char board[ROW][COL], int row, int col)
{
printf("电脑下棋:\n");
while (1)
{
int x = rand() % 6;
int y = rand() % 6;
if (x > 0 && x < row + 1 && y>0 && y < col + 1)
{
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
}
char win(char board[ROW][COL], int row, int col)
{
char p='c';
int i;
int j;
for (int i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2]&&board[i][2] == board[i][3]&&board[i][3] == board[i][4]&&board[i][0] == '*')
p = '*';
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][2] == board[i][3] && board[i][3] == board[i][4] && board[i][0] == '#')
p = '#';
}
for (int j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[2][j] == board[3][j] && board[3][j] == board[4][j] && board[j][4] == '*')
p = '*';
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[2][j] == board[3][j] && board[3][j] == board[4][j] && board[j][4] == '#')
p = '#';
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[3][3] == board[4][4] && board[0][0] == '*')
p = '*';
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[3][3] == board[4][4] && board[0][0] == '#')
p = '#';
if (board[0][4] == board[1][3] && board[1][3] == board[2][2] && board[2][2] == board[3][1] && board[3][1] == board[4][0])
{
if (board[0][4] == '*')
p = '*';
if (board[0][4] == '#')
p = '#';
}
int a = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] != ' ')
a = 1;
}
}
if (a == 0)
p = 'p';
return p;
}最后是主函数的代码啦
#include"game.h"
void menu()
{
printf("***************************\n");
printf("********* 1.play ********\n");
printf("********* 0.exit ********\n");
printf("***************************\n");
}
void game()
{
int ret;
printf("五子棋游戏开始\n");
char board[ROW][COL] = {0};
start__board(board, ROW, COL);
start__qipan(board, ROW, COL);
//qipan__board(board, ROW, COL);
while (1)
{
player__board(board, ROW, COL);
ret = win(board, ROW, COL);
if (ret != 'c')
break;
start__qipan(board, ROW, COL);
computer__board(board, ROW, COL);
ret =win(board, ROW, COL);
if (ret != 'c')
break;
start__qipan(board, ROW, COL);
}
if (ret == '*')
printf("玩家获胜\n");
if (ret == '#')
printf("电脑获胜\n");
if (ret == 'p')
printf("平局\n");
}
int main()
{
srand((unsigned int)time(NULL));
int m;
do
{
menu();
printf("请选择:->\n");
scanf_s("%d", & m);
switch (m)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择:\n");
break;
}
} while (m);
return 0;
}仅供参考啊!
边栏推荐
- LeetCode_ 1049_ Weight of the last stone II
- Package Delivery(贪心)
- 2.安装MySQL
- Out-of-the-box problem-solving thinking, putting a "rearview mirror" on the unconscious life
- Golang realizes file upload and download
- GBase8s核心数据备份
- KRYSTAL:审计数据中基于知识图的战术攻击发现框架
- From scratch Blazor Server (3) - add cookie authorization
- 如何在匹配行之前使用 grep 显示文件名和行号
- golang 实现文件上传下载
猜你喜欢
![[image processing] image skeleton extraction based on central axis transformation with matlab code](/img/34/80e777c5c0a2a791acd0892e3e0b04.png)
[image processing] image skeleton extraction based on central axis transformation with matlab code

如何开始为您的 Kubernetes 应用程序编写 Helm 图表

Detailed arrangement of JVM knowledge points (long text warning)

如何使用 grep 跨多行查找模式匹配

TCP and UDP

Leetcode binary tree series -- 144. Preorder traversal of binary trees

leetcode-位运算

一键搭建博客:如何使用WordPress插件搭建专属博客

The heavy | open atomic school source activity was officially launched

Pytorch 入门
随机推荐
DNS协议、ICMP协议、NAT技术
Talk about the establishment of performance testing environment
建议收藏丨sql行转列的一千种写法!!
重磅 | 基金会为白金、黄金、白银捐赠人授牌
HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
"100 Interview Knowledge Collections" 1. Interview Skills丨Do you really understand HR's careful thinking?
Alibaba architects spent a year sorting out the "Lucene advanced document", and you are also a big factory employee!
3.认识和操作一下mysql的基本命令
SkiaSharp of WPF custom painting to bounce ball (case)
QT's user-defined interface (borderless and movable)
ES6 arrow function this points to
IPv6 Foundation
什么是 Kubernetes 自定义资源定义 (CRD)?
大伟 GBase8s游标稳定性读ESQL测试用例
小笑授权系统V5.0开心版
[image processing] image skeleton extraction based on central axis transformation with matlab code
Peking University open classes are coming! Welcome to the "AI for science" class
LeetCode_ 278_ First wrong version
正则表达式匹配网址
Leetcode binary tree series -- 144. Preorder traversal of binary trees