C语言井字棋

C语言小白写的井字棋 只有人机对战 棋盘放到二维数组里来显示 电脑目前只想出来用随机数来差生坐标 
#ifndef SPY_GAME
#define SPY_GAME
#define ROWS 3
#define COLS 3
#endif
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void init_board(char board[ROWS][COLS], int rows, int cols);
void print_board(char board[ROWS][COLS], int rows, int cols);
void play_game(char board[ROWS][COLS]);
char check_win(char board[ROWS][COLS]);
void player_move(char board[ROWS][COLS]);
void com_move(char board[ROWS][COLS]);

void menu()
{
    printf("     1.开始    \n");
    printf("               \n");
    printf("     0.结束    \n");

}
void game()
{
    int input = 1;
    char board[ROWS][COLS] = { 0 };
    init_board(board, ROWS, COLS);
    while (input)
    {
        menu();
        printf("请选择:");
        scanf("%d", &input);
        if (input == 1)
        {
            init_board(board, ROWS, COLS);
            print_board(board, ROWS, COLS);
            play_game(board);
        }

    }
}

int main()
{
    game();
    return 0;
}

void com_move(char board[ROWS][COLS])
{
    int x = 0;
    int y = 0;
    srand((unsigned)time(NULL));
    while (1)
    {
        x = rand() % 3;
        y = rand() % 3;
        if (board[x][y] == ' ')
        {
            board[x][y] = 'O';
            return;
        }
    }

}

void player_move(char board[ROWS][COLS])
{
    int x = 0;
    int y = 0;
    while (1)
    {
        printf("请输入坐标:");
        scanf("%d%d", &x, &y);
        if (board[x-1][y-1] == ' ')
        {
            board[x-1][y-1] = 'X';
            return;
        }
        else
        {
            printf("输入错误");
        }
    }

}

char check_win(char board[ROWS][COLS])
{
    int i = 0;
    for (i = 0; i < 3; i++)
    {
        if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][1] != ' '))
        {
            return 'p';
        }
    }
    for (i = 0; i < 3; i++)
    {
        if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[1][i] != ' '))
        {
            return 'p';
        }
    }
    if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && (board[1][1] != ' '))
    {
        return 'p';
    }
    if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[1][1] != ' '))
    {
        return 'p';
    }
}

void init_board(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 print_board(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]);
        printf("---|---|---\n");
    }
}

void play_game(char board[ROWS][COLS])
{
    char ret;
    int count = 0;
    while (1)
    {
        if (count >= 9)
        {
            printf("这都能平\n");
            break;
        }
        player_move(board);
        count++;
        print_board(board, ROWS, COLS);
        if ((ret = check_win(board)) == 'p')
        {
            printf("赢\n");
            break;
        }
        if (count >=  9)
        {
            printf("这都能平\n");
            break;
        }
        count++;
        printf("电脑走:\n");
        com_move(board);

        print_board(board, ROWS, COLS);
        if ((ret = check_win(board)) == 'p')
        {
            printf("妈的智障\n");
            break;
        }

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值