C/C++編程筆記:C語言打造中國象棋遊戲,項目源代碼分享!

中國象棋是起源於中國的一種棋,屬於二人對抗性遊戲的一種,在中國有着悠久的歷史。因爲用具簡單,趣味性強,成爲流行極爲普遍的棋藝活動。編程

它是中國棋文化,也是中華民族的文化瑰寶,它源遠流長,趣味濃厚,基本規則簡明易懂。中國象棋在中國的羣衆中基礎遠遠超過圍棋,是普及最廣的棋類項目,中國象棋已流傳到十幾個國家和地區。它使用方形格狀棋盤,圓形棋子共有32個,紅黑二色各有16個棋子,擺放和活動在交叉點上。雙方交替行棋,先把對方的將(帥)「將死」的一方獲勝。(摘取自百度百科)數組


假的效果圖

那麼咱們今天,將用C語言來實現咱們中國象棋遊戲程序的開發,以爲對你有幫助的小夥伴記得先關注一波哦!微信

實現思路

咱們能夠用繪圖技術繪製棋盤,枚舉定義各類棋子,而後結構體數組,用來保存每個點的信息。函數

重點是棋盤的繪製以及判斷棋子是否可移動到目標位置,移動有兩種可能,一是單純移動,二是吃子移動。主要飛將的特殊規則。廢話很少說,貼源碼!學習

源碼分享

#include<stdio.h>
#include<graphics.h>
#define INTERVAL 50  //前面的間隔
#define CHESS_GRID_SIZE 70  //格子寬度
#define ROW  10
#define COL  9
enum PIECES//枚舉棋子
{
    車,馬,象,士,將,砲,卒,
    俥,馬,相,仕,帥,炮,兵,
    SPACE,BEGIN,END
};
//紅黑方棋子、
int redChess[] = { 車, 馬, 象, 士, 將, 砲, 卒};
int blackChess[] = { 俥, 馬, 相, 仕, 帥, 炮, 兵 };
const char*chessName[] = { "", "", "", "", "", "", "", "", "", "", "", "", "", "" };
struct Chess//棋子屬性
{
    int x;
    int y;
    int id;//哪一個棋子
    int type;//是哪一方的棋子 紅,黑?
    bool river;//判斷小兵是否過了河
};
struct Chess  map[ROW][COL];//結構體數組,用來保存每個點的信息
POINT begin = { -1, -1 }, end = { -1, -1 };//保存先後兩次點擊的數組下標
int state = BEGIN;
//初始化遊戲數據
void GameInit()
{
    for (int i = 0; i < ROW; i++)
    {
        for (int k = 0; k < COL; k++)
        {
            int chessname = SPACE;
            int mcolor = BLACK;
            //黑棋
            if (i <= 4)
            {
                mcolor = BLACK;
                //初始化第一行的棋子,
                if (i == 0)
                {
                    if (k <= 4)
                    {
                        chessname = blackChess[k];
                    }
                    else
                    {
                        chessname = blackChess[8-k];
                    }
                }
                //設置炮
                if (i == 2 && (k == 1 || k == 7))
                {
                    chessname = blackChess[砲];
                }
                //設置小兵
                if (i == 3 && k % 2 == 0)
                {
                    chessname = blackChess[卒];
                }

            }
            //紅棋
            else 
            {
                mcolor = RED;
                //初始化第一行的棋子,
                if (i == 9)
                {
                    if (k <= 4)
                    {
                        chessname = redChess[k];
                    }
                    else
                    {
                        chessname = redChess[8 - k];
                    }
                }
                //設置炮
                if (i == 7 && (k == 1 || k == 7))
                {
                    chessname = redChess[炮];
                }
                //設置小兵
                if (i == 6 && k % 2 == 0)
                {
                    chessname = redChess[兵];
                }
            }
            map[i][k].id = chessname;
            map[i][k].river = false;
            map[i][k].type = mcolor;
            map[i][k].x = k*CHESS_GRID_SIZE + INTERVAL;
            map[i][k].y = i*CHESS_GRID_SIZE + INTERVAL;
        }
    }
}
//遊戲的繪製函數
void GameDraw()
{
    //設置背景顏色 red black 
    setbkcolor(RGB(252, 215, 162));
    cleardevice();

    //繪製棋盤
    setlinecolor(BLACK);
    setlinestyle(PS_SOLID, 2);
    for (int i = 0; i < 10; i++)
    {
        //畫橫線
        line(INTERVAL, i*CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, i*CHESS_GRID_SIZE + INTERVAL);
        //畫豎線
        if (i < 9)
        {
            line(i*CHESS_GRID_SIZE + INTERVAL, INTERVAL, i*CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL);
        }
    }
    rectangle(INTERVAL - 5, INTERVAL - 5, 8 * CHESS_GRID_SIZE + INTERVAL+5, 5+9 * CHESS_GRID_SIZE + INTERVAL);
    //楚河漢界顯示
    setfillcolor(RGB(252, 215, 162));
    fillrectangle(INTERVAL, 4 * CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL);
    //顯示文字
    char river[20] = "楚河        漢界";
    settextstyle(50, 0, "楷體");
    settextcolor(BLACK);
    setbkmode(TRANSPARENT);
    outtextxy(INTERVAL+100, 4 * CHESS_GRID_SIZE + INTERVAL+10, river);
    //畫九宮格
        //畫上面
    line(3 * CHESS_GRID_SIZE + INTERVAL, INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 2 * CHESS_GRID_SIZE + INTERVAL);
    line(3 * CHESS_GRID_SIZE + INTERVAL, 2 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, INTERVAL);
       //畫下面
    line(3 * CHESS_GRID_SIZE + INTERVAL, 7 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL);
    line(3 * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE+INTERVAL,5*CHESS_GRID_SIZE+INTERVAL,7*CHESS_GRID_SIZE+INTERVAL);
    //畫棋子
    settextstyle(30, 0, "楷體");
    for (int i = 0; i < ROW; i++)
    {
        for (int k = 0; k < COL; k++)
        {
            if (map[i][k].id != SPACE)
            {
                setlinecolor(map[i][k].type);
                fillcircle(map[i][k].x, map[i][k].y, 30);
                fillcircle(map[i][k].x, map[i][k].y, 25);
                settextcolor(map[i][k].type);
                outtextxy(map[i][k].x-10, map[i][k].y-10, chessName[map[i][k].id]);
            }
            
        }
    }
}
void MouseControl()
{
    if (MouseHit())
    {
        MOUSEMSG msg = GetMouseMsg();
        if (msg.uMsg == WM_LBUTTONDOWN)
        {
            //獲取鼠標點擊的數組的下標
            int row = (msg.y-INTERVAL)/CHESS_GRID_SIZE;
            int col = (msg.x-INTERVAL)/CHESS_GRID_SIZE;
            if (state == BEGIN)
            {
                state = END;
                begin.x = row;
                begin.y = col;
            }
            else if (state == END)
            {
                state = BEGIN;
                begin.x = row;
                begin.y = col;
            }
            printf("%d,%d  %d\n", row, col, state);
        }
    }
}
void chessMove()
{
    if (begin.x != -1 && end.x != -1 && !(begin.x == end.x &&begin.y == end.y))
    {
        map[end.x][end.y].id = map[begin.x][begin.y].id;
        map[end.x][end.y].type = map[begin.x][begin.y].type;
        map[end.x][end.y].river = map[begin.x][begin.y].river;
        map[begin.x][begin.y].id = SPACE;
    }

}
int main555()
{    
    //建立一個圖形窗口
    initgraph(800,800,SHOWCONSOLE);
    GameInit();
    printf("歡迎來到頑石老師課堂,中國象棋");
    BeginBatchDraw();
    while (1)
    {
        GameDraw();
        FlushBatchDraw();
        MouseControl();
        chessMove();
    }
    return 0;
}

以上就是分享的所有內容,但願對你們有所幫助!spa

自學C/C++編程難度很大,不妨和一些志同道合的小夥伴一塊兒學習成長!code

C語言C++編程學習交流圈子,【點擊進入】微信公衆號:C語言編程學習基地blog

有一些源碼和資料分享,歡迎轉行也學習編程的夥伴,和你們一塊兒交流成長會比自遊戲

相關文章
相關標籤/搜索