真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

C語言實現(xiàn)三子棋-創(chuàng)新互聯(lián)

一、創(chuàng)建游戲初始界面
三子棋,也就是井字棋。構(gòu)思游戲大致模塊,再一一實現(xiàn)每個函數(shù),首先創(chuàng)建test.c用于創(chuàng)建初始界面

1.1 進入游戲

創(chuàng)新互聯(lián)主要從事成都網(wǎng)站設計、成都網(wǎng)站建設、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務太平,10年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:028-86922220
int main()
{
    int input = 0;
    do {
        menu();
        printf("請選擇->");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            printf("開始游戲\n");
            game();
            break;
        case 0:printf("退出游戲\n");
            break;
        default:
            printf("選擇錯誤,請重新選擇\n");
            break;
        }
    } while (input);
    return 0;
}

1.2游戲菜單

void menu()
{
    printf("*****************************\n");
    printf("******     1. play     ******\n");
    printf("******     0. exit     ******\n");//0為假,用于終止循環(huán)
    printf("*****************************\n");
}

1.3游戲的實現(xiàn)

void game()
{
    char board[ROW][COL] = { 0 };//創(chuàng)建一個數(shù)組用于存放玩家和電腦下的棋
    InitBoard(board, ROW, COL);//初始化棋盤
    DisplayBoard(board, ROW, COL);//顯示棋盤
    char ret;
    while (1)
    {
        PlayerMove(board, ROW, COL);//玩家下棋
        DisplayBoard(board, ROW, COL);
        ret = is_win(board, ROW, COL);
        if (ret != 'C')//判斷棋盤是否滿
            break;
        ComputerMove(board, ROW, COL);//電腦下棋
        DisplayBoard(board, ROW, COL);
        ret = is_win(board, ROW, COL);
        if (ret != 'C')
            break;
    }
    if (ret == '*')
        printf("玩家贏\n");
    else if (ret == '#')
        printf("電腦贏\n");
    else if (ret == 'Q')
        printf("平局\n");
}
二、創(chuàng)建存放頭文件的文件
創(chuàng)建game.h用于存放頭文件并且聲明自定義函數(shù)
#define ROW 3
#define COL 3
#include#include#include//函數(shù)的聲明
void InitBoard(char 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 ComputerMove(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, int col);
三、游戲主體的實現(xiàn)
創(chuàng)建game.c用于實現(xiàn)游戲主體
3.1 初始化棋盤函數(shù)
void InitBoard(char board[ROW][COL], int row, int col)
{
    for (int i = 0; i< row; i++)
    {
        for (int j = 0; j< col; j++)
        {
            board[i][j] = ' ';
        }
    }
    //或者memset(&board[0][0],' ',row*col*sizeof(board[0][0]));(注意需要包含頭文件string.h)
}
3.2打印棋盤函數(shù)
void DisplayBoard(char board[ROW][COL], int row, int col)
{
    for (int i = 0; i< row; i++)
    {
        for (int j = 0; j< col; j++)
        {
            printf(" %c ", board[i][j]);
            if (j< col - 1)
                printf("|");//打印分割列
        }
        printf("\n");
        if (i< row - 1)
            for (int j = 0; j< col; j++)
            {
                printf("---");//打印分割行
                if (j< col - 1)
                    printf("|");
            }
        printf("\n");
    }
}
3.3 玩家下棋函數(shù)
void PlayerMove(char board[ROW][COL], int row, int col)
{
    int x = 0, y = 0;
    printf("玩家下棋\n");
    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] = '*';
        else
            printf("坐標被占用,請重新輸入\n");
    }
    else
        printf("坐標非法,請重新輸入\n");
}
3.4 電腦下棋函數(shù)
由于電腦下棋是隨機的,因此需要用rand函數(shù)實現(xiàn),而rand函數(shù)和srand配套使用,后面會把srand函數(shù)放置在主函數(shù)中
void ComputerMove(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;
        }
    }
}
3.5 判斷輸贏函數(shù)
char is_win(char board[ROW][COL], int row, int col)
//玩家贏,返回'*'
//電腦贏,返回'#'
//平局,返回'Q'
//繼續(xù),返回'C'
{
    for (int i = 0; i< row; i++)//判斷行
    {
        if (board[i][0] == board[i][1] && board[i][2] == board[i][1] && board[i][0] != ' ')
            return board[i][0];
    }
    for (int i = 0; i< col; i++)//判斷列
    {
        if (board[0][i] == board[1][i] && board[2][i] == board[1][i] && board[0][i] != ' ')
            return board[i][i];
    }
    if ((board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') || (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' '))
        //判斷對角線
        return board[1][1];
    for (int i = 0; i< row; i++)
    {
        for (int j = 0; j< col; j++)
        {
            if (board[i][j] == ' ')
            {
                return 'C';
            }
        }
    }
    return 'Q';
}
四、完整代碼

4.1 test.c文件

#define _CRT_SECURE_NO_WARNINGS 
#include "game.h"
void menu()
{
    printf("*****************************\n");
    printf("******     1. play     ******\n");
    printf("******     0. exit     ******\n");//0為假,用于終止循環(huán)
    printf("*****************************\n");
}
void game()
{
    char board[ROW][COL] = { 0 };
    InitBoard(board, ROW, COL);//初始化棋盤
    DisplayBoard(board, ROW, COL);//顯示棋盤
    char ret;
    while (1)
    {
        PlayerMove(board, ROW, COL);//玩家下棋
        DisplayBoard(board, ROW, COL);
        ret = is_win(board, ROW, COL);
        if (ret != 'C')//判斷棋盤是否滿
            break;
        ComputerMove(board, ROW, COL);//電腦下棋
        DisplayBoard(board, ROW, COL);
        ret = is_win(board, ROW, COL);
        if (ret != 'C')
            break;
    }
    if (ret == '*')
        printf("玩家贏\n");
    else if (ret == '#')
        printf("電腦贏\n");
    else if (ret == 'Q')
        printf("平局\n");
}
int main()
{
    int input = 0;
    srand((unsigned int)time(NULL));//時間戳生成隨機數(shù),srand頭文件stdlib.h,time頭文件time.h
    do {
        menu();
        printf("請選擇->");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            printf("開始游戲\n");
            game();
            break;
        case 0:printf("退出游戲\n");
            break;
        default:
            printf("選擇錯誤,請重新選擇\n");
            break;
        }
    } while (input);
    return 0;
}

4.2 game.h文件

#define _CRT_SECURE_NO_WARNINGS 
#define ROW 3
#define COL 3
#include#include#include//函數(shù)的聲明
void InitBoard(char 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 ComputerMove(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, int col);

4.3 game.c文件

#define _CRT_SECURE_NO_WARNINGS 
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
    for (int i = 0; i< row; i++)
    {
        for (int j = 0; j< col; j++)
        {
            board[i][j] = ' ';
        }
    }
    //或者memset(&board[0][0],' ',row*col*sizeof(board[0][0]));(注意需要包含頭文件string.h)
}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
    for (int i = 0; i< row; i++)
    {
        for (int j = 0; j< col; j++)
        {
            printf(" %c ", board[i][j]);
            if (j< col - 1)
                printf("|");//打印分割列
        }
        printf("\n");
        if (i< row - 1)
            for (int 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");
    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] = '*';
        else
            printf("坐標被占用,請重新輸入\n");
    }
    else
        printf("坐標非法,請重新輸入\n");
}
void ComputerMove(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;
        }
    }
}
char is_win(char board[ROW][COL], int row, int col)
//玩家贏,返回'*'
//電腦贏,返回'#'
//平局,返回'Q'
//繼續(xù),返回'C'
{
    for (int i = 0; i< row; i++)//判斷行
    {
        if (board[i][0] == board[i][1] && board[i][2] == board[i][1] && board[i][0] != ' ')
            return board[i][0];
    }
    for (int i = 0; i< col; i++)//判斷列
    {
        if (board[0][i] == board[1][i] && board[2][i] == board[1][i] && board[0][i] != ' ')
            return board[i][i];
    }
    if ((board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') || (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' '))
        //判斷對角線
        return board[1][1];
    for (int i = 0; i< row; i++)
    {
        for (int j = 0; j< col; j++)
        {
            if (board[i][j] == ' ')
            {
                return 'C';
            }
        }
    }
    return 'Q';
}

你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧


本文標題:C語言實現(xiàn)三子棋-創(chuàng)新互聯(lián)
分享URL:http://weahome.cn/article/codsig.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部