三子棋简单实现

1.说到三子棋的实现,首先我们要构思并打印棋盘,以及初始化棋盘

2.打印一个菜单并附上选择模式

3.实现游戏,人玩游戏、电脑玩各写一个函数

4.判断棋盘是否已满并判断输赢


头文件game.h

#ifndef __GAME_H__
#define __GAME_H__

#include 
#include        //随机数函数头文件
#include 

#define ROWS 3
#define COLS 3

void DisplayBoard(char board[ROWS][COLS],int rows,int cols);
void InitBoard(char board[ROWS][COLS], int rows, int cols);
void ComputerMove(char board[ROWS][COLS], int rows, int cols);
void PlayerMove(char board[ROWS][COLS], int rows, int cols);
char CheckWin(char board[ROWS][COLS], int rows, int cols);

#endif //__GAME_H__

game.c

#include "game.h"

//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int rows, int cols)    
{
    int i = 0;
    for (i = 0; i < rows; i++)
    {
        printf(" %c | %c | %c \n", board[i][0],board[i][1],board[i][2]);
        if (i != rows-1)
            printf("---|---|---\n");
    }
    printf("\n");
}

//初始化棋盘
void InitBoard(char board[ROWS][COLS], 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] = ' ';
        }
    }
}

//实现电脑走函数
void ComputerMove(char board[ROWS][COLS], int rows, int cols)
{
    int x = 0;
    int y = 0;
    printf("电脑走:>\n");
    while (1)
    {
        x = rand() % rows;        //rand为产生随机数函数
        y = rand() % cols;
        if (board[x][y] == ' ')
        {
            board[x][y] = 'X';
            break;
        }
    }
}

//实现玩家走函数
void PlayerMove(char board[ROWS][COLS], int rows, int cols)
{
    int x = 0;
    int y = 0;
    printf("玩家走:>\n");
    while (1)
    {
        printf("请输入坐标:(x y)>");
        scanf("%d%d", &x, &y);
        if (x >= 1 && x <= rows && y >= 1 && y <= cols)
        {
            if (board[x - 1][y - 1] == ' ')
            {
                board[x - 1][y - 1] = '*';
                break;
            }
            else
            {
                printf("该位置已被占用!\n");
            }
        }
        else
        {
            printf("此坐标为非法坐标,请重新输入!\n");
        }
    }
}

//判断棋盘是否已满
static int IsFull(char board[ROWS][COLS], int rows, int cols)     //只在这一个函数中使用
{
    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 CheckWin(char board[ROWS][COLS], int rows, int cols)
{
    int i = 0;
    for (i = 0; i < rows; i++)
    {
        if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
            return board[i][0];
    }
    for (i = 0; i < cols; i++)
    {
        if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
            return board[0][i];
    }
    if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
    {
        return board[1][1];
    }
    if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
    {
        return board[1][1];
    }
    if (IsFull(board, rows, cols))
    {
        return 'q';           //代表平局
    }
    return ' ';
}

test.c

#include "game.h"

void game()
{
    char ret = 0;
    char arr[ROWS][COLS] = { 0 };
    InitBoard(arr, ROWS, COLS);
    //DisplayBoard(arr, ROWS, COLS);    //打印棋盘
    while (1)
    {
        ComputerMove(arr, ROWS, COLS);      //电脑走
        DisplayBoard(arr, ROWS, COLS);
        ret = CheckWin(arr, ROWS, COLS);
        if (ret != ' ')
        {
            break;
        }
        PlayerMove(arr, ROWS, COLS);        //玩家走
        DisplayBoard(arr, ROWS, COLS);
        ret = CheckWin(arr, ROWS, COLS);
        if (ret != ' ')
        {
            break;
        }
    } 
    if (ret == 'X')
    {
        printf("电脑赢!\n");
    }
    else if (ret == '*')
    {
        printf("玩家赢!\n");
    }
    else if (ret == 'q')
    {
        printf("平局!\n");
    }
}
void menu()
{
    printf("****************************************\n");
    printf("********** 欢迎进入三子棋游戏 **********\n");
    printf("***********1.play      0.exit***********\n");
    printf("****************************************\n");
}
void test()
{
    int input = 0;
    srand((unsigned int)time(NULL));     //初始化随机数,设置随机数种子
    do
    {
        menu();
        printf("请选择:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            break;
        default:
            printf("选择错误,请重新选择!\n");
            break;
        }
    } while (input);
}
int main()
{
    test();
    return 0;
}

程序实现

1.菜单部分

三子棋简单实现_第1张图片

2.选择1,开始玩游戏

三子棋简单实现_第2张图片

3.实现输赢

三子棋简单实现_第3张图片

4.退出游戏

三子棋简单实现_第4张图片

此游戏可实现多次反复游戏!

你可能感兴趣的:(项目)