当前位置:网站首页>C语言—————三子棋游戏
C语言—————三子棋游戏
2020-11-09 12:58:00 【osc_rmqoxylv】
三子棋游戏
问题描述:
3*3的棋盘中,只要一条线上出现三个一样的棋子就获胜(玩家或电脑);如果棋盘已经放满还未出现三个棋子一条线则打成平手。
具体细节:
- 初始化棋盘(用空格初始化)
//初始化棋盘
void initChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
chessbox[row][col] = ' ';
}
}
}
- 打印棋盘
//打印棋盘
void printChess(char chessbox[ROW][COL]){
system("cls");
printf("+---+---+---+\n");
for (int row = 0; row < ROW; row++){
printf("| %c | %c | %c |\n",
chessbox[row][0], chessbox[row][1],
chessbox[row][2]);
printf("+---+---+---+\n");
}
}
- 电脑落子(用o表示电脑落子)
//电脑落子(用o表示)
void computerMove(char chessbox[ROW][COL]){
srand(time(0));
while (1){
int row = rand() % 3;
int col = rand() % 3;
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'o';
break;
}
}
}
- 玩家落子
//玩家落子(用x表示)
void playerMove(char chessbox[ROW][COL]){
int row, col;
while (1){
printf("请输入您的落子地点:");
scanf("%d %d", &row, &col);
if (row >= 3 || col >= 3){
printf("您输入的落子位置有误,请重新输入:");
continue;
}
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'x';
break;
}
printf("该位置已有棋子,请重新输入:");
}
}
- 三个棋子一条线
- 在一行或一列实现三个棋子一条线
//行
for (int row = 0; row < ROW; row++){
if (chessbox[row][0] != ' '
&&chessbox[row][0] == chessbox[row][1]
&& chessbox[row][0] == chessbox[row][2]){
return chessbox[row][0];
}
}
//列
for (int col = 0; col < COL; col++){
if (chessbox[0][col] != ' '
&&chessbox[0][col] == chessbox[1][col]
&& chessbox[0][col] == chessbox[2][col]){
return chessbox[0][col];
}
}
- 对角线实现三个棋子一条线
if (chessbox[0][0] != ' '
&&chessbox[0][0] == chessbox[1][1]
&& chessbox[0][0] == chessbox[2][2]){
return chessbox[0][0];
}
if (chessbox[2][0] != ' '
&&chessbox[2][0] == chessbox[1][1]
&& chessbox[2][0] == chessbox[0][2]){
return chessbox[2][0];
}
- 和棋
- 棋盘放满还未获胜,则为和棋,打成了平手。
在这里插入代码片
//和棋
if(isFull(checkbox)){
return 'a';
}
- 输赢约定:
- 返回x代表玩家获胜
if (isWinner(chessbox) == 'x'){
printf("恭喜您赢啦!\n");
break;
}
- 返回o代表电脑获胜
if (isWinner(chessbox) == 'o'){
printf("很遗憾,您输了!\n");
break;
}
- 返回a代表和棋(打成平手)
if (isWinner(chessbox) == 'a'){
printf("你和电脑同一水平呦!\n");
break;
}
- 判断棋盘是否放满:
- 返回1代表棋盘已满
- 返回0代表棋盘未满
//判断棋盘是否摆满
//1表示满;0表示不满。
int isFullChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
//找到空格,说明未满
if (chessbox[row][col] == ' '){
return 0;
}
}
}
return 1;
}
源代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define ROW 3
#define COL 3
//玩家落子(用x表示)
void playerMove(char chessbox[ROW][COL]){
int row, col;
while (1){
printf("请输入您的落子地点:");
scanf("%d %d", &row, &col);
if (row >= 3 || col >= 3){
printf("您输入的落子位置有误,请重新输入:");
continue;
}
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'x';
break;
}
printf("该位置已有棋子,请重新输入:");
}
}
//电脑落子(用o表示)
void computerMove(char chessbox[ROW][COL]){
srand(time(0));
while (1){
int row = rand() % 3;
int col = rand() % 3;
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'o';
break;
}
}
}
//初始化棋盘
void initChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
chessbox[row][col] = ' ';
}
}
}
//打印棋盘
void printChess(char chessbox[ROW][COL]){
system("cls");
printf("+---+---+---+\n");
for (int row = 0; row < ROW; row++){
printf("| %c | %c | %c |\n",
chessbox[row][0], chessbox[row][1],
chessbox[row][2]);
printf("+---+---+---+\n");
}
}
//判断棋盘是否摆满,摆满返回1;未满返回0
int isFullChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
//找到空格,说明未满
if (chessbox[row][col] == ' '){
return 0;
}
}
}
return 1;
}
//约定:返回x代表玩家赢了;返回o代表电脑赢了;返回a代表和棋
char isWinner(char chessbox[ROW][COL]){
//行
for (int row = 0; row < ROW; row++){
if (chessbox[row][0] != ' '
&&chessbox[row][0] == chessbox[row][1]
&& chessbox[row][0] == chessbox[row][2]){
return chessbox[row][0];
}
}
//列
for (int col = 0; col < COL; col++){
if (chessbox[0][col] != ' '
&&chessbox[0][col] == chessbox[1][col]
&& chessbox[0][col] == chessbox[2][col]){
return chessbox[0][col];
}
}
//对角线
if (chessbox[0][0] != ' '
&&chessbox[0][0] == chessbox[1][1]
&& chessbox[0][0] == chessbox[2][2]){
return chessbox[0][0];
}
if (chessbox[2][0] != ' '
&&chessbox[2][0] == chessbox[1][1]
&& chessbox[2][0] == chessbox[0][2]){
return chessbox[2][0];
}
//和棋
if (isFullChess(chessbox)){
return 'a';
}
return 0;
}
//开始一局游戏
void game(){
char chessbox[ROW][COL] = {
0 };
initChess(chessbox);
printf("游戏开始啦!\n");
printChess(chessbox);
while (1){
//玩家落子
playerMove(chessbox);
//打印棋牌
printChess(chessbox);
//判断胜负
if (isWinner(chessbox) == 'x'){
printf("恭喜您赢啦!\n");
break;
}
if (isWinner(chessbox) == 'a'){
printf("你和电脑同一水平呦!\n");
break;
}
//电脑落子
computerMove(chessbox);
//打印棋牌
printChess(chessbox);
//判断胜负
if (isWinner(chessbox) == 'o'){
printf("很遗憾,您输了!\n");
break;
}
if (isWinner(chessbox) == 'a'){
printf("你和电脑同一水平呦!\n");
break;
}
}
}
int menu(){
printf("===============\n");
printf("1.开始游戏\n");
printf("0.结束游戏\n");
printf("===============\n");
int choice;
printf("请输入您的选择:");
scanf("%d", &choice);
return choice;
}
int main()
{
ile (1){
int choice=menu();
if (choice == 1){
game();
continue;
}
else if (choice == 0){
break;
}else{
printf("输入有误,请您重新输入!\n");
continue;
}
}
system("pause");
return 0;
}
运行结果:
版权声明
本文为[osc_rmqoxylv]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4313784/blog/4709402
边栏推荐
猜你喜欢
【分布式】分布式锁都有哪些实现方案?
After SQL group query, get the first n records of each group
Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive
The choice of domain name of foreign trade self built website
Windows must be installed with efficiency software!
IDEA rest-client,会了它我还没打开过postman
“开源软件供应链点亮计划 - 暑期 2020”公布结果 基于 ChubaoFS 开发的项目获得最佳质量奖
Android check box and echo
注意.NET Core进行请求转发问题
Android Development - service application, timer implementation (thread + service)
随机推荐
天啦撸!打印日志竟然只晓得 Log4j?
接口测试如何在post请求中传递文件
微信视频号播主排行榜2020年10月
The use of Android studio Aidl
使用TreeView树型菜单栏(递归调用数据库自动创建菜单)
Android架构之Navigation组件(二)
Where should wild card SSL certificate register and apply
Idea rest client, yes, I haven't opened postman yet
In the future, China Telecom will make cloud computing service the main business of China Telecom
IDEA rest-client,会了它我还没打开过postman
TiDB x 微众银行 | 耗时降低 58%,分布式架构助力实现普惠金融
Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive
After SQL group query, get the first n records of each group
解密未来数据库设计:MongoDB新存储引擎WiredTiger实现(事务篇)
通配符SSL证书应该去哪里注册申请
Explain Python input() function: get user input string
接口测试如何在post请求中传递文件
手写Koa.js源码
实现商品CRUD操作
Android Development - service application, timer implementation (thread + service)