三子棋小游戏C语言

设计三子棋思路

  • 首先写main函数,无论游戏开始还是退出,都先要打印一个菜单,所以写一个菜单函数menu();然后根据菜单选择游戏开始和退出了,如果选择玩游戏,那么就执行游戏函数game(),否则就退出。
  • 对于game()函数中,首先要打印一个棋盘,用一个二维数组;第二步就是玩家下棋;第三步电脑下棋;每一步下棋都要判断输赢。然后一直循环,直到有一方赢,或者平局退出。
  • 对于下棋的结果,有3中情况,一种是玩家赢,一种是电脑赢,另一种是平局。对于平局,它的意思就是棋盘是否满了。

test.c中具体代码

#include "game.h"
void game()
{
    char ret = 0;
    char board[ROW][COL] = {' '};
    init_board(board, ROW, COL);
    print_board(board, ROW, COL);
    while(1)
    {
        play_move(board, ROW, COL);
        print_board(board, ROW, COL);
        ret = is_win(board, ROW, COL);
        if (ret != ' ')
        {
            break;
        }
        computer_move(board, ROW, COL);
        print_board(board, ROW, COL);
        ret = is_win(board, ROW, COL);
        if (ret != ' ')
        {
            break;
        }
    }
    if (ret == 'x')
        printf("玩家赢!\n");
    else if (ret == '0')
        printf("电脑赢!\n");
    else 
        printf("平局!\n");
}
void menu()
{
    printf("*************************\n");
    printf("*      1.PLAY           *\n");
    printf("       2.EXIT           *\n");
    printf("*************************\n");

}
int main()
{
    srand((unsigned int)time(NULL));
    int input = 0;
    do
    {
        printf("请选择:> \n");
        scanf("%d", &input);
        menu();
        switch(input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("退出游戏!\n");
            break;
        default:
            break;
        }
    }
    while(input);
    system("pause");
    return 0;
}

game.c中代码

#include "game.h"
int is_full(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (board[i][j] == ' ')
                return 0;
        }
    }
    return 1;
}
char is_win(char board[ROW][COL], int row, int col)
{
    int i = 0;
    for (i = 0; i < row; 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 < col; 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[1][1] != ' ')
        return board[1][1];
    if (is_full() == 1)
        return 'q';
    return ' ';
}

void computer_move(char board[ROW][COL], int row, int col)
{
    int x = rand() % row;
    int y = rand() % col;
    printf("电脑下棋:> \n");
    while(1)
    {
        if (board[x][y] != ' ')
        {
            board[x][y] = '0';
            break;
        }
    }
}

void play_move(char board[ROW][COL], int row, int col)
{
    int x = 0;
    int y = 0;
    while(1)
    {
        printf("玩家下棋:> ");
        scanf("%d%d", &x, &y);
        if ((x >= 1 && x <= row) && (y >= 1 && y <= col))
        {
            if (board[x-1][y-1] != ' ')
            {
                board[x-1][y-1] = 'x';
                break;
            }
            else
            {
                printf("该位置被占用!\n");
            }
        }
        else
        {
            printf("输入位置无效!\n");
        }       
    }
}

void print_board(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for(i = 0; i < row; i++)
    {
        printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
        if( i < row - 1)
        {
            printf("---|---|---\n");
        }   
    }
}
void init_board(char board[ROW][COL], int row, int col)  
{
    memset(board, ' ', row*col*board[0][0]);
}

game.h中声明

#ifndef __GAME_H__
#define __GAME_H__

#include 
#include 
#include 
#include 
#define ROW 3
#define COL 3

void init_board(char board[ROW][COL], int row, int col);  //棋盘初始化
void print_board(char board[ROW][COL], int row, int col);  //打印棋盘
void play_move(char board[ROW][COL], int row, int col);   //玩家下棋
void computer_move(char board[ROW][COL], int row, int col);  //电脑下棋
char is_win(char board[ROW][COL], int row, int col);   //判断输赢

#endif

你可能感兴趣的