当前位置:网站首页>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;
}仅供参考啊!
边栏推荐
- 2022最新 wifi大师小程序独立版3.0.8
- How to synchronize when the primary and sub warehouses are modified?
- DOD and Dor, two artifacts to reduce "cognitive bias"
- [image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
- Exclusive interview | Cheng Li, chief technology officer of Alibaba: cloud + open source together form a credible foundation for the digital world
- LeetCode_1049_最后一块石头的重量Ⅱ
- INVALID_ ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
- TCP and UDP
- INVALID_ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
- Great golang Road
猜你喜欢

Alluxio为Presto赋能跨云的自助服务能力

Discussion on the application of arcing smart electricity in elderly care institutions

8. Interleave - understand ThreadPoolExecutor thread pool from architecture design to practice

IPv6 Foundation

Alibaba architects spent a year sorting out the "Lucene advanced document", and you are also a big factory employee!

Drawing box and ellipse of WPF screenshot control (IV) "imitating wechat"

StarRocks 技术内幕:实时更新与极速查询如何兼得

即学即用的问题解决思维,给无意识的生活装上“后视镜”

Paddlelite compilation and code running through the disk
![[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code](/img/c1/f962f1c1d9f75732157d49a5d1d0d6.png)
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
随机推荐
多线程顺序运行的 4 种方法,面试随便问!
深入理解C# 可空类型
Matplotlib Chinese question
HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
"Knowledge Collection" article to understand mysql index!!(recommended collection)
TCP and UDP
如何使用“COPY –link”加速 Docker 构建和优化缓存
如何使用 grep 跨多行查找模式匹配
PHP basics uses arrays to save data
Niuke net brush questions
专访 | 阿里巴巴首席技术官程立:云 + 开源共同形成数字世界的可信基础
Alibaba P8 broke out this interview guide for big factories. After reading it, the salary soared by 30K!
Xiaoxiao authorization system V5.0 happy version
Drawing box and ellipse of WPF screenshot control (IV) "imitating wechat"
Zhou Hongyi: 360 is the largest secure big data company in the world
TCP和UDP
Detailed arrangement of JVM knowledge points (long text warning)
带你浅聊一下PHP搭建的电商商城系统
PyQt5快速开发与实战 6.6 QFormLayout(表单布局) && 6.7 嵌套布局 && 6.8 QSplitter
基于flask写的一个小商城mall项目