当前位置:网站首页>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
边栏推荐
- Interface tests how to pass files in post requests
- In the future, China Telecom will make cloud computing service the main business of China Telecom
- Aren't you curious about how the CPU performs tasks?
- Rainbow sorting | Dutch flag problem
- TiDB x 微众银行 | 耗时降低 58%,分布式架构助力实现普惠金融
- Oh, my God! Printing log only knows log4j?
- 导师制Processing网课 双十一优惠进行中
- PAT_甲级_1074 Reversing Linked List
- A simple ability determines whether you will learn!
- Kubernetes business log collection and monitoring
猜你喜欢
随机推荐
From coding, network transmission, architecture design, Tencent cloud high quality, high availability real-time audio and video technology practice
大型项目Objective-C - NSURLSession接入短信验证码应用实例分享
Interview summary on November 7, 2020 (interview 12K)
零基础IM开发入门(四):什么是IM系统的消息时序一致性?
Explain Python input() function: get user input string
Python loading voice class custom dataset
Rainbow sorting | Dutch flag problem
块级元素和行内元素
微信视频号播主排行榜2020年10月
Well, the four ways to query the maximum value of sliding window are good
接口测试如何在post请求中传递文件
注意.NET Core进行请求转发问题
Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive
Adobe experience design / XD 2020 software installation package (with installation tutorial)
Reading design patterns adapter patterns
走进京东 | 中国空间技术研究院青年创新联盟成员莅临参观京东总部
The use of Android studio Aidl
Kubernetes业务日志收集与监控
Jsliang job series - 08 - handwritten promise
Interface tests how to pass files in post requests