【三子棋】C语言小程序三子棋

首先创建一个项目,建立一个头文件game.h,两个源文件game.c和test.c
game.h代码片:

#ifndef __GAME_H__
#define __GAME_H__

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define COLS 3
#define ROWS 3

void init_borad(int arr[ROWS][COLS], int rows, int cols);
void display_borad(int arr[ROWS][COLS], int rows, int cols);
void computer_move(int arr[ROWS][COLS], int rows, int cols);
void player_move(int arr[ROWS][COLS], int rows, int cols);
char check_win(int arr[ROWS][COLS], int rows, int cols);

#endif

test.c代码片:

#include"game.h"
#include<time.h>
#define NUM 40
void menu()
{
    int i = 0;
    int j = 0;
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < NUM; j++)
        {
            printf("%c", 3);
        }
        printf("\n");
        if (i == 1)
            printf(" 1.Play 0.Exit \n");
    }
}
void game()
{
    int arr[ROWS][COLS] = { 0 };
    int ret = 0;
    init_borad(arr,ROWS, COLS);
    display_borad(arr, ROWS, COLS);
    srand((unsigned int)time(NULL));
    while (1)
    {
        player_move(arr, ROWS, COLS);
        ret = check_win(arr, ROWS, COLS);
        if (ret != ' ')
            break;
        display_borad(arr, ROWS, COLS);
        computer_move(arr, ROWS, COLS);
        ret = check_win(arr, ROWS, COLS);
        display_borad(arr, ROWS, COLS);
        if (ret != ' ')
            break;

    }
    if (ret == 'q')
    {
        printf("平局\n");
    }
    if (ret == '*')
    {
        printf("恭喜你赢了%c\n", 1);
    }
    if (ret == 'o')
    {
        printf("你输了,不要灰心哦%c\n", 1);
    }

}
int main()
{
    int input = 0;
    do
    {
        menu();
        printf("请选择>:");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            break;
        default:
            printf("选择有误,请重新选择:>");
            break;
        }
    }while (input);
    return 0;
}

game.c代码片:

#include"game.h"

void init_borad(int arr[ROWS][COLS], int rows, int cols)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
            arr[i][j] = ' ';
    }
}


void display_borad(int arr[ROWS][COLS], int rows, int cols)
{
    int i = 0;
    int j = 0;
    printf(" ___ ___ ___ \n");
    for (i = 0; i < rows; i++)
    {
        printf("| %c | %c | %c |\n", arr[i][0], arr[i][1], arr[i][2]);
            printf("|___|___|___|\n");
    }
}


void player_move(int arr[ROWS][COLS], int rows, int cols)
{
    int x = 0;
    int y = 0;
    while (1)
    {
        printf("玩家下,请选择>:");
        scanf("%d%d", &x,&y);
        x--;
        y--;
        if ((x >= 0) && (x <= 2) && (y >= 0) && (y <= 2))
        {
            if (arr[x][y] == ' ')
            {
                arr[x][y] = '*';
                break;
            }
            else
            {
                printf("输入有误,请重新输入%c", 1);
            }
        }
        else
        {
            printf("输入有误,请重新输入%c", 1);
        }
    }
}


void computer_move(int arr[ROWS][COLS], int rows, int cols)
{
    printf("电脑下\n");
    while (1)
    {
        int x = 0;
        int y = 0;
        x = rand() % 3;
        y = rand() % 3;
        if (arr[x][y] == ' ')
        {
            arr[x][y] = 'o';
            break;
        }
    }
}


static int is_full(int arr[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 (arr[i][j] == ' ')
            {
                return 0;
            }
        }
    }
    return 1;
}


char check_win(int arr[ROWS][COLS], int rows, int cols)
{
    int i = 0;
    for (i = 0; i < rows; i++)
    {
        if ((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2]) && (arr[i][0] != ' '))
        {
            return arr[i][0];
        }
    }
    for (i = 0; i < rows; i++)
    {
        if ((arr[0][i] == arr[1][i]) && (arr[1][i] == arr[2][i]) && (arr[0][i] != ' '))
        {
            return arr[0][i];
        }
    }
    if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) && (arr[0][0] != ' '))
    {
        return arr[1][1];
    }
    if ((arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0]) && (arr[0][0] != ' '))
    {
        return arr[1][1];
    }
    if (is_full(arr, rows, cols))
    {
        return 'q';
    }
    return ' ';
}
    原文作者:Rae8023
    原文地址: https://blog.csdn.net/Rae8023/article/details/54318573
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞