C实现三子棋

写的一个小游戏有不足的地方敬请指出来,十分感谢提错

看你怎样打出平局大笑

game.h

#ifndef __GAME_H__
#define __GAME_H__
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

#define   ROWS     3
#define   COLS     3

void init_board(char board[ROWS][COLS], int rows, int cols);    //建立一个棋盘
void display_board(char board[ROWS][COLS], int rows, int cols); //调试用的棋盘
void player_move(char board[ROWS][COLS], int rows, int cols);   //玩家移动
void computer_move(char board[ROWS][COLS], int rows, int cols); //电脑移动
char check_win(char board[ROWS][COLS], int rows, int cols);     //检查游戏是否结束


#endif     __GAME_H__


game.c

#include "game.h"

void init_board(char board[ROWS][COLS], int rows, int cols)
{
    memset (board, ' ',sizeof(char)*rows*cols);
}

void display_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]);
        if(i != rows-1)
            printf("---|---|---\n");
    }
}
void player_move(char board[ROWS][COLS], int rows, int cols)
{
    int x = 0;
    int y = 0;
    while(1)
    {
        printf("请选择你想要下的位置\n(例:第二行第二个按:2空格2) :\n");
        scanf("%d%d", &x, &y);
        x--;
        y--;
        if ( (x>=0) && (x<=rows-1) && (y>=0) && (y<=cols-1) )    //判断落子位置是否合法
        {
            if (board[x][y] == ' ')                              //判断落子位置是否为空
            {
                board[x][y] = 1;
                break;
            }
            else
            {
                printf("输入错误,请重新输入\n");
            }
        }
        else
        {
            printf("输入错误,请重新输入\n");
        }
    }
}
void computer_move(char board[ROWS][COLS], int rows, int cols)
{
    printf("‖‖‖‖\n");
    while(1)
    {
        int x = rand() % 3;                 //rand函数产生一个随机值
        int y = rand() % 3;                 //产生一个0~2的随机数
        if (board[x][y] == ' ')             //判断落子位置是否为空
        {
            board[x][y] = 2;
            break;
        }
    }
}
static int is_full(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 check_win(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];
        }
        if ( (board[0][i] == board[1][i]) && (board[0][i] == board[2][i]) && (board[i][0] != ' ') )
        {
            return board[0][i];
        }
    }

    if ( (board[0][0] == board[1][1]) && (board[0][0] == 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[1][1];
    }
    if (is_full(board, ROWS,COLS))
    {
        return 'q';
    }
    return ' ';
}



test.c

#include "game.h"

void menu()
{
    printf("按【1】『开始』\n");
    printf("按【0】『结束』\n");
}

enum Option
{
    EXIT,
    PLAY
};

void game()
{
    char ret = 0;
    char board[ ROWS ][ COLS ] = {0};
    init_board(board, ROWS, COLS);
    display_board(board, ROWS, COLS);

    while(1)
    {
        player_move(board, ROWS, COLS); 
        display_board(board, ROWS, COLS);     
        ret = check_win(board, ROWS, COLS);
        if (ret !=' ')
        {
            break;
        }

        computer_move(board, ROWS, COLS);
        display_board(board, ROWS, COLS);
        ret = check_win(board, ROWS, COLS);
        if (ret !=' ')
        {
            break;
        }
    }
    if (ret == 1)
    {
        printf("玩家赢\n");
    }
    else if(ret == 2)
    {
        printf("你输了\n");
    }
    else
    {
        printf("平局\n");
    }
}

int main()
{
    int input = 0;
    srand((unsigned int)time(NULL));
    do {
        menu();
        printf("请选择>>\n");
        scanf("%d",&input);
        switch (input)
        {
        case PLAY:
                game();
                break;
        case EXIT:
                break;
        default :
            {
                printf("错误指令,请重新选择>>.\n");
                break;
            }
        }
    }while (input);

    return 0;
}

如果有疑问和建议发送邮件到blbagony@163.com
欢迎纠错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值