小言_互联网的博客

简易三子棋

225人阅读  评论(0)

简易三子棋(C语言)

三子棋是每个同学都会遇到的一道有趣的练习题。
写三子棋所需知识点有 二维数组 函数 就可以了。

下面是我用到的头文件

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

首先定义棋盘大小

#define COLS 3
#define ROWS 3

然后写一下主函数,顺便把游戏菜单先写出来

int main()//三子棋主函数
{
    int key = 0;//菜单选择
    menu();//打印菜单
    printf("请输入你的选择\n");
    do
    {
        scanf("%d",&key);
        switch(key)
        {
        case 1:
            Play();
            break;
        case 0:
            break;
        default:
            printf("请重新输入:\n");
            break;
        }
    }while(key);
    return 0;
}

游戏菜单

void menu()//游戏菜单
{
    printf("***************************\n");
    printf("**********1.Play***********\n");
    printf("**********0.exit***********\n");
    printf("***************************\n");
}

这里缕一下整体大致思路
1.初始化棋盘 显示棋盘
2.输入落子 显示棋盘 判断获胜
3.电脑落子 显示棋盘 判断获胜
4.退出
然后写一下初始化棋盘的函数

void Clean(int board[COLS][ROWS], int col, int row)//初始化棋盘
{
    int i,j;
    for(i = 0; i < col; i++)
        for(j = 0; j < row; j++)
            board [i][j] = ' ';//棋盘置为空
}

打印棋盘的函数

void Show(int board[COLS][ROWS],int col, int row)//打印棋盘
{
    int i,j;
    for(i = 0; i < col; i++)
        {
            for(j = 0; j < row; j++)
            {
                printf("%c",board[i][j]);//打印棋盘内容
                if(j<row-1)
                printf(" | ");
            }
            printf("\n");
            if(i < col-1)
            printf("--|---|--");
            printf("\n");
        }
}

判断获胜的函数

int IsWin(int board[COLS][ROWS],int col, int row)//判断是否赢了
{
    //获胜条件:同行,同列,两个斜对角线;
    int i = 0;
    for (i = 0; i < col; i++)
    {
        if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != ' ')
            return 0;
        if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' ')
            return 0;
    }
    //对角线
    if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ')
        return 0;
    if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != ' ')
        return 0;
    return 1;
}

这是判断是否平局的函数,我把获胜和判平分开了,这样清晰一些

int IsDraw(int board[COLS][ROWS],int col, int row)
{
    int i,j;
    for(i = 0; i < col; i++)
        {
            for(j = 0; j < row; j++)
            {
                if (board[i][j] == ' ')
                    return 1;
            }
        }
    return 0;
}

最后这里就可以把所有的游戏函数集合在一起

void Play()
{
    int endplay = 1;//结束标识
    int x,y;//落子坐标
    int chessboard[COLS][ROWS] ;//棋盘
    srand( (unsigned)time( NULL ) );//随机种子
    Clean(chessboard,COLS,ROWS);//初始化棋盘
    system("cls");
    printf("游戏开始\n");
    Show(chessboard,COLS,ROWS);//打印棋盘
    while(endplay)//游戏开始循环
    {
    //玩家移动
        printf("请玩家输入要落子的位置:");
        do//判断落点是否有棋子
        {
            scanf("%d%d",&x,&y);
            if(chessboard[x-1][y-1] != ' ')
                printf("该落点已有棋子,请重新选择:");
        }while (chessboard[x-1][y-1] != ' ');
        chessboard[x-1][y-1] = 'X';//落子
        system("cls");
        printf("玩家移动\n");
        Show(chessboard,COLS,ROWS);//打印棋盘
        if (endplay != IsWin(chessboard,COLS,ROWS))//判断是否赢了
        {
            printf("好厉害,你赢了!\n");
            break;
        }
        if (endplay != IsDraw(chessboard,COLS,ROWS))//判断是否赢了
        {
            printf("棋盘都下满了,平局!\n");
            break;
        }
    //电脑移动
        Sleep(1000);//让电脑暂停一下再移动,显得智能一点
        system("cls");
        printf("电脑移动\n");
        do//判断落点是否有棋子
        {
            x = rand()*100%3;
            y = rand()*100%3;
        }while (chessboard[x][y] != ' ');
        chessboard[x][y] = 'O';//落子
        Show(chessboard,COLS,ROWS);//打印棋盘
        if (endplay != IsWin(chessboard,COLS,ROWS))//判断是否赢了
        {
            printf("差一点就赢了呢!\n");
            break;
        }
    }
    printf("再来一把 请输入 1 \n退出     请输入 0 \n");//回到主函数输入的界面了
}

现在只需要编译运行就可以了





美中不足的是,电脑落子都是随机,并不能判断你是否快要赢了,哈哈!


转载:https://blog.csdn.net/qq_43758820/article/details/101032187
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场