记一个小游戏 ---- “井子”小游戏

代码分为三部分:
game.h

#ifndef __GAME_H__
#define __GAME_H__

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#pragma warning(disable:4996)

#define ROW 3
#define COL 3

 void InitBoard(char board[ROW][COL], int row, int col);
 void PrintBoard(char board[ROW][COL], int row, int col);
 void PlayMove(char board[ROW][COL], int row, int col);
 void ComputerMove(char board[ROW][COL], int row, int col);
 char CheckWin(char board[ROW][COL], int row, int col);
#endif

game.c

#include"game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
    int i = 0;
    for (; i < row; i++)
    {
        int j = 0;
        for (; j < col; j++)
        {
            board[i][j] = ' ';
        }
    }

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

}

void PlayMove(char board[ROW][COL], int row, int col)
{
    int x = 0;
    int y = 0;
    while (1)
    {
        printf("玩家走:》");
        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 ComputerMove(char board[ROW][COL], int row, int col)
{
    int x = 0;
    int y = 0;
    printf("电脑走\n");
    while (1)
    {
        x = rand() % row;
        y = rand() % col;
        if (board[x][y] == ' ')
        {
            board[x][y] = '0';
            break;
        }
    }

}

static int IsFull(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (; i < row; i++)
    {
        for (; j < col; j++)
        {
            if (board[i][j] == ' ')
            {
                return 0;//棋盘不满返回0
            }
        }
    }
    return 1;//棋盘满返回1
}
char CheckWin(char board[ROW][COL], int row, int col)
{
    int i = 0;
    for (; i < row; i++)//判断行相等
    {
        if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
        {
            return board[i][1];
        }
    }
    for (; i < col; i++)//判断列相等
    {
        if (board[0][i] == board[1][i] && board[1][i] == board[2][i] &&board[0][i] != ' ')
        {
            return board[0][i];
        }
    }
    if(board[0][0] == board[1][1] && board[1][1] == 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[0][2];
    }
    if (IsFull(board,row,col) == 1)//判断棋盘是否满
    {
        return 'Q';//满返回'Q',平局
    }
    return ' ';//不满返回' ',继续游戏
}

test.c

#include "game.h"



void menu()
{
    printf("********************************\n");
    printf("*****1 Play    0 Exit***********\n");
    printf("********************************\n");
}

void game()
{
    char board[ROW][COL] = { 0 };
    char win = 0;
    InitBoard(board, ROW, COL);
    PrintBoard(board, ROW, COL);
    do{
        PlayMove(board, ROW, COL);
        PrintBoard(board, ROW, COL);
        win=CheckWin(board, ROW, COL);
        if (win != ' ')
        {
            break;
        }
        ComputerMove(board, ROW, COL);
        PrintBoard(board, ROW, COL);
        win=CheckWin(board, ROW, COL);
        if (win != ' ')
        {
            break;
        }

    } while (1);
    if (win == 'X')
    {
        printf("玩家赢\n");
    }
    else if (win == '0')
    {
        printf("电脑赢\n");
    }
    else if (win == 'Q')
    {
        printf("平局\n");
    }
    else
    {
        printf("继续游戏\n");
    }

}
void test()
{
    int input = 0;
    srand((unsigned int)time(NULL));
    do{
        menu();
        printf("请选择:\n");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("退出游戏\n");
            break;
        default:
            printf("选择错误,请重新选择:\n");
            break;
        }
    } while (input);
}


int main()
{
    test();
    system("pause");
    return 0;
}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页