這是一篇C語言編寫的一個三子棋小遊戲程序

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "標頭.h"
#pragma once
#define ROW 3
#define COL 3
#include<stdlib.h>
#include<time.h>
///函數聲明
void InitBoard(int board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col);
void Playermove(char board[ROW][COL], int row, int col);
void ComputereMove(char board[ROW][COL], int row, int col);
char Iswin(char board[ROW][COL], int row, int col);
int Isfull(char board[ROW][COL], int row, int col);

void menu()
{
    printf("****************************\n");
    printf("****1.遊戲開始***2.結束*****\n");
    printf("****************************\n");
}
void InitBoard(char board[ROW][COL], int row, int col)
{
    int i, j;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            board[i][j] = ' ';

        }
    }
}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
    int i = 0;
    for (i = 0; i < row; i++)
    {
        int j = 0;
        for (j = 0; j < col; j++)
        {//打印一行的數據
            printf(" %c ", board[i][j]);
            if (j < col - 1)
                printf("|");
        }
        printf("\n");
        打印分割行
            if (i < row - 1)
            {
                for (j = 0; j < col; j++)
                {
                    printf("---");
                    if (j < col - 1)
                        printf("|");

                }
                printf("\n");
        }

    }

}
void Playermove(char board[ROW][COL], int row, int col)
{
    int x=0, y=0;
    printf("玩家走:\n");
    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] = '*';
                break;
            }
            else
                printf("該座標被佔用");

        }
        else
            printf("座標非法,請從新輸入\n");
    }
}
void ComputereMove(char board[ROW][COL], int row, int col)
{
    int x = 0, y = 0;
    printf("電腦走\n");
    while (1)
    {
        x = rand() % row;
        y = rand() % col;
        if (board[x][y] == ' ')
        {
            board[x][y] = '#';
            break;
        }
    }
}
int Isfull(char board[ROW][COL], int row, int col)
{
    int i, j;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (board[i][j] == ' ')
                return 0;
        }
    }
    return 1;

}
char Iswin(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[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
        return board[1][1];
        判斷是否平局
    if (1 == Isfull(board, ROW, COL))
    {
        return 'Q';
    }
    else
        return 'C';

}
void game()
{
    char ret = 0;
    char board[ROW][COL] = { 0 };
    InitBoard(board, ROW, COL);
    DisplayBoard(board, ROW, COL); 
    while (1)
    {
        Playermove(board, ROW, COL);
        DisplayBoard(board, ROW, COL);
        ret = Iswin(board, ROW, COL);
        if (ret != 'C')
        {
            break;
        }

        ComputereMove(board, ROW, COL);
        DisplayBoard(board, ROW, COL);
        ret = Iswin(board, ROW, COL);
        if (ret != 'C')
        {
            break;
        }

    }
    if (ret == '*')
    {
        printf("玩家贏\n");
    }
    else if (ret == '#')
    {
        printf("電腦贏\n");

    }
    else
    {
        printf("平局\n");
    }

}
int main()
{
    int input=0;
    srand((unsigned int)time(NULL));
    do
    {
        menu();
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            printf("開始遊戲");
            break;
        case 2:
            printf("退出遊戲");
            break;
        default:
            break;
        }
    }
    while(input);

}
相關文章
相關標籤/搜索