三子棋

简介: 关于c语言设计的三子棋小游戏

@TOC

前言

所有代码详解 都在代码中

1. test.c---内容框架

#include"game.h"
void menu()
{
    printf("********************\n");
    printf("***1.play 0.exit****\n");
    printf("********************\n");
}
void game()
{
    char ret = 0;
    char board[row][col] = { 0 };
    initboard(board, row, col);//初始化棋盘
    displayboard(board, row, col);//打印棋盘
    while (1)//使用while才可以把玩家和电脑下棋的过程多次进行
    {
        pepplayer( board,  row,  col);//玩家
        ret = iswin(board, row, col);//iswin用来判断是谁赢
        if (ret != 'c')//c代表继续  如果ret的值不为继续 就跳出循环
        {
            break;
        }
        displayboard(board, row, col);
        complayer( board,  row, col);//电脑
        ret = iswin(board, row, col);
        if (ret != 'c')
        {
            break;
        }
        displayboard(board, row, col);
    }
    if (ret == '*')
    {
        printf("玩家赢\n");
    }
    else if (ret == '#')
    {
        printf("电脑赢\n");
    }
    else
    {
        printf("平局\n");
    }
}
int main()
{
    int input = 0;
    do
    {
        menu();
        printf("输入数字:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("输入错误\n");
            break;
        default:
            break;
        }
    } while (input);
    return 0;
}

2. game .h---函数的声明和头文件

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define col 3//代表行
#define row 3//代表列
void initboard(char board[row][col], int rows, int cols);//这里参数本想都为 row 和col但是常量不能作为参数
void displayboard(char board[row][col], int rows, int cols);
void pepplayer(char board[row][col], int rows, int cols);
void complayer(char board[row][col], int rows, int cols);
char iswin(char board[row][col], int rows, int cols);//判断谁赢

3. game .c--函数的定义

#include"game.h"
void initboard(char board[row][col], int rows, int cols)//初始化
{
    int i = 0;
    int j = 0;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            board[i][j] = ' ';//将每个都赋值成 空格  如果赋值 0会报错
        }
    }
}
void displayboard(char board[row][col], int rows, int cols)//打印
{
    int i = 0;
    int j = 0;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            printf("%c ", board[i][j]);//先打印出空格
            if (j < cols - 1)//最后一列没有|
            {
                printf(" |");//然后再打印 | 这里需要加一个空格 否则会发现与后面的|连接不上
            }
        }
        printf("\n");//换行
        if (i < rows - 1)//因为最后一行不需要---
        {
            for (j = 0; j < rows; j++)
            {
                printf("---");
                if (j < row - 1)//最后一列没有|
                {
                    printf("|");
                }
            }
        }
        printf("\n");//这里还需要空格 
    }
}
void pepplayer(char board[row][col], int rows, int cols)
{
    int x = 0;
    int y = 0;
    while (1)
    {
        printf("请输入坐标:>");//因为要多次调用所以放在里面
        scanf("%d%d", &x, &y);
        if (x >= 1 && x <= rows && y >= 1 && y <= cols)//范围
        {
            if (board[x - 1][y - 1] == ' ')//输入的是从1开始的值  而数组下标是值-1
            {
                board[x - 1][y - 1] = '*';//*代表玩家
                break;
            }
            else
            {
                printf("坐标被占用\n");
            }
        }
        else
        {
            printf("坐标非法\n");
        }
    }
}
void complayer(char board[row][col], int rows, int cols)
{
    while (1)
    {
        int x = rand() % row;//使用rand()%3 返回0到2
        int y = rand() % col;
            if (board[x][y] == ' ')
            {
                board[x][y] = '#';//这里本身就从0开始所以不需要-1
                break;
            }
    }
}
int isfull(char board[row][col], int rows, int cols)//判断棋盘是否满 若满了返回 1
{
    int i = 0;
    int j = 0;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            if (board[i][j] == ' ')
            {
                return 0;
            }
        }
    }
    return 1;
}
char iswin(char board[row][col], int rows, int cols)
{
    int i = 0;
    for (i = 0; i < col; i++)//每一列的情况
    {
        if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')//若每一列相等时
        {
            return board[0][i]; //返回一个值 就知道是 '*' / '#'
        }
    }
    for (i = 0; i< row; i++)//每一行的情况
    {
        if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')//若每一行相等时
        {
            return board[i][0];//返回一个值 就知道是 '*' / '#'
        }
    }
    if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
    {
        return board[0][0];//对角线
    }
    if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
    {
        return board[0][2];
    }
    if (isfull(board, row, col) == 1)//棋盘满了
    {
        return 'Q';
    }
    else
    return 'c';//以上情况都没发生 即棋盘继续执行
}
目录
相关文章
|
4天前
|
弹性计算 运维 搜索推荐
三翼鸟携手阿里云ECS g9i:智慧家庭场景的效能革命与未来生活新范式
三翼鸟是海尔智家旗下全球首个智慧家庭场景品牌,致力于提供覆盖衣、食、住、娱的一站式全场景解决方案。截至2025年,服务近1亿家庭,连接设备超5000万台。面对高并发、低延迟与稳定性挑战,全面升级为阿里云ECS g9i实例,实现连接能力提升40%、故障率下降90%、响应速度提升至120ms以内,成本降低20%,推动智慧家庭体验全面跃迁。
|
4天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
380 93
|
5天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
5天前
|
SQL 人工智能 自然语言处理
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
随着生成式AI的普及,Geo优化(Generative Engine Optimization)已成为企业获客的新战场。然而,缺乏标准化流程(Geo优化sop)导致优化效果参差不齐。本文将深入探讨Geo专家于磊老师提出的“人性化Geo”优化体系,并展示Geo优化sop标准化如何帮助企业实现获客效率提升46%的惊人效果,为企业在AI时代构建稳定的流量护城河。
389 156
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
|
5天前
|
数据采集 缓存 数据可视化
Android 无侵入式数据采集:从手动埋点到字节码插桩的演进之路
本文深入探讨Android无侵入式埋点技术,通过AOP与字节码插桩(如ASM)实现数据采集自动化,彻底解耦业务代码与埋点逻辑。涵盖页面浏览、点击事件自动追踪及注解驱动的半自动化方案,提升数据质量与研发效率,助力团队迈向高效、稳定的智能化埋点体系。(238字)
270 158
|
13天前
|
机器人 API 调度
基于 DMS Dify+Notebook+Airflow 实现 Agent 的一站式开发
本文提出“DMS Dify + Notebook + Airflow”三位一体架构,解决 Dify 在代码执行与定时调度上的局限。通过 Notebook 扩展 Python 环境,Airflow实现任务调度,构建可扩展、可运维的企业级智能 Agent 系统,提升大模型应用的工程化能力。