当前位置:网站首页>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子棋。
边栏推荐
- 四平方和,激光炸弹
- Chinese valentine's day, of course, to learn SQL optimization better leave work early to find objects
- CF1527D MEX Tree(mex&树&容斥)
- leetcode: 241. Designing precedence for arithmetic expressions
- 【北亚数据恢复】IBM System Storage存储lvm信息丢失数据恢复方案
- Theory 1: Deep Learning - Detailed Explanation of the LetNet Model
- 基于数据库实现分布式锁
- FRED应用:毛细管电泳系统
- 基本介绍PLSQL
- CF1527D MEX Tree (mex & tree & inclusive)
猜你喜欢
Kyushu Cloud attended the Navigator Online Forum to discuss the current status, challenges and future of 5G MEC edge computing
Theory 1: Deep Learning - Detailed Explanation of the LetNet Model
I/O stream summary
CCF GLCC正式开营|九州云开源专家携丰厚奖金,助力高校开源推广
用于X射线聚焦的复合折射透镜
基于数据库实现分布式锁
FRED Application: Capillary Electrophoresis System
Basic Introduction for PLSQL
leetcode:253. 至少需要多少间会议室
基于 Next.js实现在线Excel
随机推荐
分布式链路追踪Jaeger + 微服务Pig在Rainbond上的实践分享
[Problem solving] QT update component appears "To continue this operation, at least one valid and enabled repository is required"
idea removes spark logs
1403. Minimum Subsequence in Non-Increasing Order
idea去掉spark的日志
Chinese valentine's day, of course, to learn SQL optimization better leave work early to find objects
leetcode: 251. Expanding 2D Vectors
How to fall in love with a programmer
一看就会的Chromedriver(谷歌浏览器驱动)安装教程
Set partition minimum difference problem (01 knapsack)
企业级优化
[The Art of Hardware Architecture] Study Notes (1) The World of Metastability
word2003按空格键为什么会出现小数点
Win10无法访问移动硬盘怎么解决
leetcode: 241. Designing precedence for arithmetic expressions
数据库恢复
【北亚数据恢复】IBM System Storage存储lvm信息丢失数据恢复方案
Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)
用了TCP协议,就一定不会丢包吗?
Android Sqlite3 basic commands