一只程序猿的养成日记 第一章 第七节 三子棋的实现 有错求指导

game.h

#ifndef __GAME_H__  
#define __GMME_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

#define _CRT_SECURE_NO_WARNINGS 1  
#include"game.h"  
#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
#include<time.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("请落子(坐标以空格隔开):>");  
        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] = 'X';  
                break;  
            }  
            else  
            {  
                printf("请落子:>\n");  
            }  
        }  
    }  
}  
void computer_move(char board[ROWS][COLS], int rows, int  cols)  
{  
    while (1)  
    {  
        int x = rand() % 3;  
        int y = rand() % 3;  
        if (board[x][y] == ' ')  
        {  
            board[x][y] = '0';  
            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][1] != ' '))  
        {  
            return board[i][1];  
        }  
    }  
    for (i = 0; i < cols; i++)  
    {  
        if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[1][i] != ' '))  
        {  
            return board[1][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[2][2] != ' '))  
    {  
        return board[1][1];  
    }  
    if (is_full(board, rows, cols))  
    {  
        return 'q';//棋盘满了  
    }  
    return ' ';  
}  


test12-07.c

#define _CRT_SECURE_NO_WARNINGS 1  
#include<stdio.h>  
#include<stdlib.h>  
#include"game.h"  
  
void menu()  //打印菜单  
{  
printf("***************************\n");
    printf("*********  1.开始  ********\n");  
    printf("*********          ********\n");  
    printf("*********  0.离开  ********\n");
printf("***************************\n");
}  
void game()  
{  
    int ret = 0;  
    char board[ROWS][COLS] = { 0 };  
    init_board(board, ROWS, COLS);  
    display_board(board, ROWS, COLS);  
    while (1)  
    {  
        printf("玩家落子:\n");  
        player_move(board, ROWS, COLS);  
        display_board(board, ROWS, COLS);  
        ret = check_win(board, ROWS, COLS);  
        if (ret != ' ')  
            break;  
        printf("电脑落子:\n");  
        computer_move(board, ROWS, COLS);  
        display_board(board, ROWS, COLS);  
        check_win(board, ROWS, COLS);  
        if (ret != ' ')  
            break;  
    }  
    if (ret == 'X')    
    {  
        printf("恭喜您!您赢了本局游戏!\n");  
    }  
        else if (ret == '0')  
    {  
        printf("太遗憾了!你输掉了本局游戏!\n");  
    }  
    else  
    {  
        printf("平局!还要再来一局吗?\n");  
    }  
}  
enum  Option  
{  
    EXIT,//0  
    PLAY //1  
};  
int main()  
{  
    int input = 0;  
    srand((unsigned int)time(NULL));  
    do  
    {  
        menu();  
        printf("请选择:>");  
        scanf("%d", &input);  
        switch(input)  
        {  
        case PLAY:  
            game();  
            break;  
        case EXIT:  
            break;  
        default:  
            printf("选择错误!请重新选择:\n");  
            break;  
        }  
    } while (input);  
    system("pause\n");  
    return 0;  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值