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

三子棋作為一款經(jīng)典的小游戲,對于初學C語言的我們?nèi)绻?可以熟練了解并可以實現(xiàn)其游戲功能,則對于以后更系統(tǒng)的學習C語言有很好幫助。下來我分享一下我在編程時的感悟和代碼

首先介紹一下三子棋規(guī)則:只要將自己的棋連成一條線(行,列,斜線),即為贏
如圖:

三子棋分析與實現(xiàn)——C語言

創(chuàng)新互聯(lián)公司長期為千余家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為遂川企業(yè)提供專業(yè)的成都網(wǎng)站建設、做網(wǎng)站,遂川網(wǎng)站改版等技術服務。擁有十多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

先根據(jù)圖和玩法大致有個思路:

1.我們可以將棋盤看做是一個三行三列的二維數(shù)組,且每行每列用線隔開以便區(qū)分
2.分為電腦下棋和玩家下棋(電腦先下和玩家先下)
3.判斷輸贏(玩家贏,電腦贏和平局)

大致思路即為上面分析,下面開始寫代碼:

創(chuàng)建tset.c,game.c和head.h三個文件

1.在test.c中編寫主函數(shù)并寫出游戲菜單

在這里我分為電腦先下和玩家先下,代碼如下;

void menu()
{
    printf("***************************************\n");
    printf("*      1.play             0.exit      *\n");
    printf("***************************************\n");
}
void first_move()
{
    printf("***************************************\n");
    printf("*  1.computer first   2.player first  *\n");
    printf("***************************************\n");
}
void game()
{
   return 0;
}
int main()
{
    int choice;
    srand((unsigned int)time(NULL));//產(chǎn)生隨機數(shù)
    do
    {
        menu();  
        printf("請選擇:");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            game();
            break;
        case 0:
            break;
        default:
            printf("輸入錯誤請重新輸入.\n");
            break;
        }
    } while (choice);
    return 0;
}

2.下來逐步在game.c中實現(xiàn)所需功能并且在game函數(shù)中調(diào)用

先打印出棋盤,并將棋盤初始化為空格。代碼如下;

void show_board(char board[ROWS][COLS], int rows, int cols)//打印棋盤
{
    int i;
    for (int 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 init_board(char board[ROWS][COLS], int rows, int cols)//將數(shù)組初始化為空格
{
    int i, j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            board[i][j] = ' ';
        }
    }
}

效果如圖
三子棋分析與實現(xiàn)——C語言

3.編寫玩家落子的代碼

玩家數(shù)輸入二維數(shù)組的坐標,這時需要判斷玩家輸入坐標是否合理以及在此坐標上是否有棋子,代碼如下;

void player_move(char board[ROWS][COLS], int rows, int cols)//玩家落子
{
    int x, y;
    printf("玩家落子:\n");
   while (1)
    {
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= rows && y >= 1 && y <= cols)
        {
            if (board[x - 1][y - 1] == ' ')
            {
                board[x - 1][y - 1] = '*';
                break;
            }
            else printf("位置已被占用!請再試一次。\n");
        }
        else printf("輸入錯誤!請重新輸入.\n");
    }
}

4.玩成電腦落子

這里實現(xiàn)的是電腦在為空格的數(shù)組中隨機落子。代碼如下;

void computer_move(char board[ROWS][COLS], int rows, int cols)// 電腦落子
{

    int x, y;
    printf("電腦落子:\n");
   while (1)
    {
        x = rand() % rows;
        y = rand() % cols;
        if (board[x][y] == ' ')
        {
            board[x][y] = '#';
            break;
        }
    }
}

5.最后就只剩判斷輸贏

先討論玩家或者電腦贏的情況;代碼如下;

char check_win(char board[ROWS][COLS], int rows, int cols)//判斷輸贏
{
    int i;
    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];
    else if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
        return board[1][1];
    else if (is_full(board, rows, cols))
        return 'q';
    return 0;
}

這是考慮是平局的情況,平局則為棋盤下滿都沒有輸贏,可以遍歷整個數(shù)組(棋盤)若沒有空格就為平局。代碼如下;

static int is_full(char board[ROWS][COLS], int rows, int cols)//檢測是否為平局
{
    int i, j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            if (board[i][j] == ' ')
                return 0;
        }
    }
    return 1;
}

初步測試結果如下

三子棋分析與實現(xiàn)——C語言

在圖中我們可以看出電腦可以有贏的機會卻在隨機下子,這樣的游戲是沒有挑戰(zhàn)的。因此應該修改下電腦下子的代碼,使其智能化。我們可在電腦落子時判斷如果有在同行,同列或者斜線上有相同棋子的,電腦則優(yōu)先落子在其未成一條線的空格處,這樣實現(xiàn)簡單智能化

代碼如下;

void computer_move(char board[ROWS][COLS], int rows, int cols)//智能化電腦落子
{
    int x, y, i;
    printf("電腦落子:\n");
   while (1)
    {
        x = rand() % rows;
        y = rand() % cols;
        for (i = 0; i < rows; i++)
        {
            if (board[i][0] == board[i][1] && board[i][0] == '#' && board[i][2] == ' ')
            {
                board[i][2] = '#';
                goto flag1;
            }
            else if (board[i][1] == board[i][2] && board[i][1] == '#' && board[i][0] == ' ')
            {
                board[i][0] = '#';
                goto flag1;
            }
            else if (board[i][0] == board[i][2] && board[i][2] == '#' && board[i][1] == ' ')
            {
                board[i][1] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[1][i] && board[0][i] == '#' && board[2][i] == ' ')
            {
                board[2][i] = '#';
                goto flag1;
            }
            else if (board[1][i] == board[2][i] && board[1][i] == '#' && board[0][i] == ' ')
            {
                board[0][i] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[2][i] && board[2][i] == '#' && board[1][i] == ' ')
            {
                board[1][i] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[1][1] && board[0][0] == '#' && board[2][2] == ' ')
            {
                board[2][2] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][2] && board[1][1] == '#' && board[0][0] == ' ')
            {
                board[0][0] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[2][2] && board[0][0] == '#' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
            else if (board[0][2] == board[1][1] && board[1][1] == '#' && board[2][0] == ' ')
            {
                board[2][0] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][0] && board[1][1] == '#' && board[0][2] == ' ')
            {
                board[0][2] = '#';
                goto flag1;
            }
            else if (board[2][0] == board[0][2] && board[2][0] == '#' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
        }
        for (i = 0; i < rows; i++)
        {
            if (board[i][0] == board[i][1] && board[i][0] == '*' && board[i][2] == ' ')
            {
                board[i][2] = '#';
                goto flag1;
            }
            else if (board[i][1] == board[i][2] && board[i][1] == '*' && board[i][0] == ' ')
            {
                board[i][0] = '#';
                goto flag1;
            }
            else if (board[i][0] == board[i][2] && board[i][2] == '*' && board[i][1] == ' ')
            {
                board[i][1] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[1][i] && board[0][i] == '*' && board[2][i] == ' ')
            {
                board[2][i] = '#';
                goto flag1;
            }
            else if (board[1][i] == board[2][i] && board[1][i] == '*' && board[0][i] == ' ')
            {
                board[0][i] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[2][i] && board[2][i] == '*' && board[1][i] == ' ')
            {
                board[1][i] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[1][1] && board[0][0] == '*' && board[2][2] == ' ')
            {
                board[2][2] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][2] && board[1][1] == '*' && board[0][0] == ' ')
            {
                board[0][0] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[2][2] && board[0][0] == '*' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
            else if (board[0][2] == board[1][1] && board[1][1] == '*' && board[2][0] == ' ')
            {
                board[2][0] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][0] && board[1][1] == '*' && board[0][2] == ' ')
            {
                board[0][2] = '#';
                goto flag1;
            }
            else if (board[2][0] == board[0][2] && board[2][0] == '*' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
        }
        if (board[x][y] == ' ')
        {
            board[x][y] = '#';
            goto flag1;
        }
    }
flag1:;
}

效果如圖;
三子棋分析與實現(xiàn)——C語言

檢測所有結果

電腦贏
三子棋分析與實現(xiàn)——C語言
玩家贏
三子棋分析與實現(xiàn)——C語言
平局
三子棋分析與實現(xiàn)——C語言

全部代碼

tese.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"head.h"
void menu()
{
    printf("***************************************\n");
    printf("*      1.play             0.exit      *\n");
    printf("***************************************\n");
}
void first_move()
{
    printf("***************************************\n");
    printf("*  1.computer first   2.player first  *\n");
    printf("***************************************\n");
}
void game()
{
    int choice, win;
    char board[ROWS][COLS];
    init_board(board, ROWS, COLS);
    first_move();
flag:
    printf("請選擇");
    scanf("%d", &choice);
    switch (choice)
    {
    case 1: //電腦先落子
        do
        {
            computer_move(board, ROWS, COLS);   //電腦落子
            show_board(board, ROWS, COLS);      //打印棋盤
           win = check_win(board, ROWS, COLS); 
            if (win != 0)  //沒贏
                break;
            player_move(board, ROWS, COLS);     //玩家落子
            show_board(board, ROWS, COLS);      // 打印棋盤
           win = check_win(board, ROWS, COLS);
        } while (win == 0);
        if (win == '#')
            printf("很遺憾,你輸了!\n");
        if (win == '*')
            printf("恭喜,你贏了!\n");
        if (win == 'q')
            printf("平局\n");
        break;
    case 2: //玩家先落子
        show_board(board, ROWS, COLS);
        do
        {
            player_move(board, ROWS, COLS);
            show_board(board, ROWS, COLS);
           win = check_win(board, ROWS, COLS);
            if (win != 0)
                break;
            computer_move(board, ROWS, COLS);
            show_board(board, ROWS, COLS);
           win = check_win(board, ROWS, COLS);
        } while (win == 0);
        if (win == '#')
            printf("很遺憾,你輸了!\n");
        if (win == '*')
            printf("恭喜,你贏了!\n");
        if (win == 'q')
            printf("平局\n");
        break;
    default:
        printf("輸入錯誤,請重新輸入\n");
        goto flag;
    }
}
int main()
{
    int choice;
    srand((unsigned int)time(NULL));//產(chǎn)生隨機數(shù)
    do
    {
        menu();  
        printf("請選擇:");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            game();
            break;
        case 0:
            break;
        default:
            printf("輸入錯誤請重新輸入.\n");
            break;
        }
    } while (choice);
    return 0;
}

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"head.h"

void show_board(char board[ROWS][COLS], int rows, int cols)//打印棋盤
{
    int i;
    for (int 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 init_board(char board[ROWS][COLS], int rows, int cols)//將數(shù)組初始化為空格
{
    int i, j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            board[i][j] = ' ';
        }
    }
}

//void computer_move(char board[ROWS][COLS], int rows, int cols)// 電腦落子
//{
//
//  int x, y;
//  printf("電腦落子:\n");
// while (1)
//  {
//      x = rand() % rows;
//      y = rand() % cols;
//      if (board[x][y] == ' ')
//      {
//          board[x][y] = '#';
//          break;
//      }
//  }
//}
void computer_move(char board[ROWS][COLS], int rows, int cols)//智能化電腦落子
{
    int x, y, i;
    printf("電腦落子:\n");
   while (1)
    {
        x = rand() % rows;
        y = rand() % cols;
        for (i = 0; i < rows; i++)
        {
            if (board[i][0] == board[i][1] && board[i][0] == '#' && board[i][2] == ' ')
            {
                board[i][2] = '#';
                goto flag1;
            }
            else if (board[i][1] == board[i][2] && board[i][1] == '#' && board[i][0] == ' ')
            {
                board[i][0] = '#';
                goto flag1;
            }
            else if (board[i][0] == board[i][2] && board[i][2] == '#' && board[i][1] == ' ')
            {
                board[i][1] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[1][i] && board[0][i] == '#' && board[2][i] == ' ')
            {
                board[2][i] = '#';
                goto flag1;
            }
            else if (board[1][i] == board[2][i] && board[1][i] == '#' && board[0][i] == ' ')
            {
                board[0][i] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[2][i] && board[2][i] == '#' && board[1][i] == ' ')
            {
                board[1][i] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[1][1] && board[0][0] == '#' && board[2][2] == ' ')
            {
                board[2][2] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][2] && board[1][1] == '#' && board[0][0] == ' ')
            {
                board[0][0] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[2][2] && board[0][0] == '#' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
            else if (board[0][2] == board[1][1] && board[1][1] == '#' && board[2][0] == ' ')
            {
                board[2][0] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][0] && board[1][1] == '#' && board[0][2] == ' ')
            {
                board[0][2] = '#';
                goto flag1;
            }
            else if (board[2][0] == board[0][2] && board[2][0] == '#' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
        }
        for (i = 0; i < rows; i++)
        {
            if (board[i][0] == board[i][1] && board[i][0] == '*' && board[i][2] == ' ')
            {
                board[i][2] = '#';
                goto flag1;
            }
            else if (board[i][1] == board[i][2] && board[i][1] == '*' && board[i][0] == ' ')
            {
                board[i][0] = '#';
                goto flag1;
            }
            else if (board[i][0] == board[i][2] && board[i][2] == '*' && board[i][1] == ' ')
            {
                board[i][1] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[1][i] && board[0][i] == '*' && board[2][i] == ' ')
            {
                board[2][i] = '#';
                goto flag1;
            }
            else if (board[1][i] == board[2][i] && board[1][i] == '*' && board[0][i] == ' ')
            {
                board[0][i] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[2][i] && board[2][i] == '*' && board[1][i] == ' ')
            {
                board[1][i] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[1][1] && board[0][0] == '*' && board[2][2] == ' ')
            {
                board[2][2] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][2] && board[1][1] == '*' && board[0][0] == ' ')
            {
                board[0][0] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[2][2] && board[0][0] == '*' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
            else if (board[0][2] == board[1][1] && board[1][1] == '*' && board[2][0] == ' ')
            {
                board[2][0] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][0] && board[1][1] == '*' && board[0][2] == ' ')
            {
                board[0][2] = '#';
                goto flag1;
            }
            else if (board[2][0] == board[0][2] && board[2][0] == '*' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
        }
        if (board[x][y] == ' ')
        {
            board[x][y] = '#';
            goto flag1;
        }
    }
flag1:;
}

void player_move(char board[ROWS][COLS], int rows, int cols)//玩家落子
{
    int x, y;
    printf("玩家落子:\n");
   while (1)
    {
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= rows && y >= 1 && y <= cols)
        {
            if (board[x - 1][y - 1] == ' ')
            {
                board[x - 1][y - 1] = '*';
                break;
            }
            else printf("位置已被占用!請再試一次。\n");
        }
        else printf("輸入錯誤!請重新輸入.\n");
    }
}

static int is_full(char board[ROWS][COLS], int rows, int cols)//檢測是否為平局
{
    int i, j;
    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;
    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];
    else if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
        return board[1][1];
    else if (is_full(board, rows, cols))
        return 'q';
    return 0;
}

head.h

#include<stdio.h>
#include<time.h>
#include<Windows.h>

#define ROWS 3
#define COLS 3

void show_board(char board[ROWS][COLS], int rows, int cols);//打印棋盤
void init_board(char board[ROWS][COLS], int rows, int cols);//初始化棋盤為空格
void computer_move(char board[ROWS][COLS], int rows, int cols);//電腦落子
void player_move(char board[ROWS][COLS], int rows, int cols);//玩家落子
char check_win(char board[ROWS][COLS], int rows, int cols);//判斷輸贏

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

名稱欄目:三子棋分析與實現(xiàn)——C語言-創(chuàng)新互聯(lián)
文章來源:http://muchs.cn/article14/dcdede.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、定制開發(fā)、網(wǎng)頁設計公司、網(wǎng)站建設網(wǎng)站維護、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營