当前位置:网站首页>C语言写简单三子棋
C语言写简单三子棋
2022-08-04 14:42:00 【new lin】
新手小白记录学习。
目录
前言
用C语言二维数组写三子棋,也可以是n子棋。
一、简单介绍
多文件形式。game.c里的是实现游戏逻辑的函数,game.h里的是函数声明和宏常量等,test.c作为测试游戏文件
二、设计步骤
1.先进入菜单选择进入游戏
2.初始化棋盘
3.打印棋盘
4.玩家下棋
5.电脑下棋
6.判断游戏状态
game.h代码如下(示例):
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
//初始化棋盘
void init_board(char board[ROW][COL], int row, int col);
//打印棋盘
void display_board(char board[ROW][COL], int row, int col);
//玩家下棋
void player_move(char board[ROW][COL], int row, int col);
//电脑下棋
void computer_move(char board[ROW][COL], int row, int col);
//判断游戏状态
char is_win(char board[ROW][COL], int row, int col);
test.c代码如下(示例):
#include "game1.h"
void menu()
{
printf("*********************************\n");
printf("******** 1. play ********\n");
printf("******** 0. exit ********\n");
printf("*********************************\n");
}
void game()
{
char ret = 0;
//数据的存储需要一个3*3的二维数组
char board[ROW][COL] = { 0 };
init_board(board, ROW, COL);
display_board(board, ROW, COL);
//玩游戏
while (1)
{
player_move(board, ROW, COL);
display_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
computer_move(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);
}
//玩家赢 - '*'
//电脑赢 - '#'
//平局了 - 'Q'
//游戏继续 - 'C'
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;
}
game.c代码如下(示例):
#include "game1.h"
void init_board(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void display_board(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
//数据
//printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
//---|---|---
if (i < row - 1)
{
//printf("---|---|---\n");
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void player_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋:>\n");
while (1)
{
printf("请输入要下棋的坐标:>");
scanf("%d %d", &x, &y);
//1.坐标的合法性
//2.坐标是否被占用
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该坐标被占用,请重新输入\n");
}
}
else
{
printf("坐标非法,重新输入\n");
}
}
}
//
//电脑随机下棋
//
void computer_move(char board[ROW][COL], int row, int col)
{
printf("电脑下棋:>\n");
//0~32726
//%3-->0~2
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//如果棋盘满了,返回1
//不满,返回0
static int is_full(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; 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;
for (i = 0; i < row; i++)
{
if (board[i][0] == ' ')
break;
for (j = 1; j < col; j++)
{
if (board[i][0] != board[i][j])
break;
}
if (j == col)
return board[i][0];
}
for (j = 0; j < col; j++)
{
if (board[0][j] == ' ')
break;
for (i = 1; i < row; i++)
{
if (board[0][j] != board[i][j])
break;
}
if(i==row)
return board[0][j];
}
for (i = 0; i < row; i++)
{
if (board[0][0] == ' ')
break;
for (j = 1; j < col; j++)
{
if (board[0][0] != board[j][j])
break;
}
if (j == col)
return board[0][0];
}
for (i = 0; i < row; i++)
{
if (board[0][col- 1] == ' ')
break;
for (j = col - 2; j > -1; j--)
{
i++;
if(board[0][col-1]!=board[i][j])
break;
}
if (j == -1)
return board[0][col - 1];
}
//判断平局
if (is_full(board, row, col) == 1)
{
return 'Q';
}
//继续
return 'C';
}
ps:将game.h中的宏常量改一下(改成n)可改成n子棋。
边栏推荐
- 【剑指offer33】二叉搜索树的后序遍历序列
- G. Mountaineering Squad (violence & dfs)
- leetcode:259. 较小的三数之和
- 企业级优化
- How to write SQL statements: the usage of Update, Case, and Select together
- The Internet of things application development trend
- [The Art of Hardware Architecture] Study Notes (1) The World of Metastability
- leetcode: 251. Expanding 2D Vectors
- Technology sharing | Mini program realizes audio and video calls
- 在腾讯,我的试用期总结!
猜你喜欢

并发程序的隐藏杀手——假共享(False Sharing)

Google plug-in. Download contents file is automatically deleted after solution

解题-->在线OJ(十八)

leetcode: 253. How many meeting rooms are required at least

爬虫——selenium基本使用、无界面浏览器、selenium的其他用法、selenium的cookie、爬虫案例

【 HMS core 】 【 Media 】 online video editing service 】 【 material can't show, or network anomalies have been Loading state

eNSP-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)

一看就会的Chromedriver(谷歌浏览器驱动)安装教程

MySQL优化学习笔记

leetcode: 250. Count subtrees of equal value
随机推荐
Find My Technology | Prevent your pet from getting lost, Apple Find My technology can help you
技术分享| 融合调度系统中的电子围栏功能说明
没有Project Facets的解决方法
leetcode: 254. Combinations of factors
本周讨论用户体验:Daedalus 的 Nemo 加入 Ambire,探索加密海洋
How to install postgresql and configure remote access in ubuntu environment
idea去掉spark的日志
How to fall in love with a programmer
【历史上的今天】8 月 4 日:第一位图灵奖女性得主;NVIDIA 收购 MediaQ;首届网络安全挑战大赛完成
leetcode:215无序数组中找第k大的元素
Rust from entry to proficient 04-variables
Zheng Qing freshmen school competition and middle-aged engineering selection competition
Oracle 数据库用户创建、重启、导入导出
数据库恢复
杭电校赛(ACM组队安排)
X射线掠入射聚焦反射镜
广告电商系统开发功能只订单处理
leetcode:259. 较小的三数之和
并发程序的隐藏杀手——假共享(False Sharing)
MySQL【触发器】