生活随笔
收集整理的這篇文章主要介紹了
数据结构与算法课程设计之五子棋(人机)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據結構與算法課程設計之五子棋(人機)
五子棋是全國智力運動會競技項目之一,是一種兩人對弈的純策略型棋類游戲。通常雙方分別使用黑白兩色的棋子,下在棋盤直線與橫線的交叉點上,先形成五子連線者獲勝。
這是我在大二下數據結構與算法課程設計的時候做的經典小游戲。如果僅僅做一個人人對戰的五子棋(不聯機)是非常簡單的。這實際就是一個簡單的界面設計,鼠標事件的響應以及五子連珠的判斷問題。但我做了一個人機對戰的五子棋。你下一子,電腦會下一子,誰先連成五子誰獲得勝利。想要電腦足夠智能(當然沒有用到現在比較熱門的機器學習和深度學習),就需要程序能夠應對各種情況(這里相對比較復雜,需要足夠了解五子棋的規則,有一定技術的人才能讓電腦不至于是一個大傻瓜)。對于一個對弈類的游戲,其實可以使用博弈樹來做,可以讓電腦找到有限步內的最優解,但我當時并不是很了解博弈樹,所以就沒用。完全是分了將近20種情況,電腦遇到不同情況做出不同的選擇。
除了人機對弈做的有模有樣,我的界面也受到了室友們的一致贊同。并且課設老師在驗收的時候就直接給了優(一般需要經過驗收+報告等步驟,最后綜合給分)。下面展示一下我的程序的界面:
C++課程設計之小游戲歡樂小魚
上面是登錄界面,哪個櫻花一直在往下掉落,是動態的。有人人對戰模式和人機對戰模式。掉落的梅花是一個類,類有一個實現往下掉落的函數。
這個界面是游戲的介紹。
人機對戰,界面包含了基本的下棋功能。以及悔棋,重玩,計實等功能。整個棋盤是一個二維的數組。沒棋子的位子為0.,下白棋的位置位為1,下黑棋的位置為2。(可能是反著的,我記得不是很清楚了)
上面一局棋,是人機對弈。我和電腦玩的。我先手執黑棋,電腦執白棋。很容易看出來是我輸了。
最后我會把代碼放在后面,需要的可以運行看看效果。所以我先講講運行環境:
我編譯的環境:windows 10、Visual Studio 2017、Easy_x春分版2018(加載圖形庫)。
我上面已經標好軟件的位置了,你也可以下載最高版本的,是可以運行我的程序的。
由于需要加載easy_x圖形庫,所以在下載好【Easy_x】后,點擊它然后進去找到vs,點一下安裝就可以了(記得重新打開vs)。
這個是程序用到的圖片,可以免費下載:圖片資源(使用的相對路徑,需要把圖片(不是圖片文件夾)放到源代碼同一級目錄下)
代碼:
#include<iostream>
using namespace std
;
#include<graphics.h>
#include<conio.h>
#include<time.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")enum Condition
{NO_CHESSMEN
, BLACK_CHESSMEN
, WHITE_CHESSMEN
, WALL
};
enum Direction
{HORIZONTAL
, VERTICAL
, POSITIVE_BATTER
, NEGATIVE_BATTER
};
class Flowers {
public:Flowers(){loadimage(&img
[0], _T("桃花一朵1.png"));loadimage(&img
[1], _T("桃花一朵2.png"));loadimage(&img
[2], _T("桃花一朵3.png"));loadimage(&img
[3], _T("桃花一朵4.png"));loadimage(&img1
, _T("桃花枝.png"));loadimage(&img3
, _T("棋盤.png"));x
= (rand() % 250) + 20;m
= (rand() % 2) + 1;}void Move(){n
= n
+ m
;if (n
% (m
* 5) == 0) l
++;if (l
>= 4) l
= 0;putimage(x
, 100 + n
, &img
[l
]);putimage(-50, -50, &img1
, SRCPAINT
);putimage(0, 250, &img3
, SRCPAINT
);setlinecolor(BLACK
);line(x
, 100 + n
, x
+ 25, 100 + n
);if (m
== 2)line(x
, 101 + n
, x
+ 25, 101 + n
);if (n
>= 360){n
= 0;x
= (rand() % 250) + 20;m
= (rand() % 2) + 1;}}
private:IMAGE img
[4];IMAGE img1
, img3
;int x
;int n
= 0, l
= 0, m
= 1;};
class Point {
public:Point(){x
= 0;y
= 0;condition
= NO_CHESSMEN
;for (int i
= 0; i
< 4; i
++)W
[i
] = 0;}void Change_Condition(Condition condition1
){condition
= condition1
;}Condition
Get_Condition(){return condition
;}int Get_M_W(){int a
, b
;a
= max(W
[0], W
[1]);b
= max(W
[2], W
[3]);return max(a
, b
);}int x
;int y
;int W
[4];
private:Condition condition
;
};
typedef struct Lnode
{Point point
;int W_mun
;int W_max
;Lnode
* next
;
}Lnode
;typedef int Status
;
const int NODE
= 15;
IMAGE img11
, img21
, img12
, img22
;
int ss
= 1;
void Introduce();
Status
Pop(Lnode
* top
);
Status
Push(Lnode
* top
, Point point
);
void Draw_Begin_Face();
void Draw_Chessboard();
Status
Init_List(Lnode
*& H
, Point point
);
Status
Increase_Node(Lnode
*& H
, Lnode
* node
);
void Coordinate(Point point
[NODE
+ 2][NODE
+ 2]);
void PVP(int l
, int p
, int q
, Point point
[NODE
+ 2][NODE
+ 2]);
void PVC(int l
, int p
, int q
, Point point
[NODE
+ 2][NODE
+ 2]);
int Work(int front
, int back
, Condition condition1
, Condition condition2
);
int Get_Weight(Point point
[NODE
+ 2][NODE
+ 2], int i
, int j
, Condition condition
, Direction direction
);
void Charge(Point point
[NODE
+ 2][NODE
+ 2], int i
, int j
, Condition condition
);
int main()
{int h
= 0;MOUSEMSG m
; int l
= 0;int q
= 0, p
= 0;Flowers flower
[6];srand((unsigned)time(NULL));loadimage(&img11
, _T("悔1.jpg"));loadimage(&img21
, _T("重1.jpg"));loadimage(&img12
, _T("悔2.jpg"));loadimage(&img22
, _T("重2.jpg"));initgraph(560, 460);HWND hWnd
= GetHWnd();SetWindowText(hWnd
, _T("五子棋"));Draw_Begin_Face();while (true){Point point
[NODE
+ 2][NODE
+ 2];Coordinate(point
);while (MouseHit()){m
= GetMouseMsg();switch (m
.uMsg
){case WM_LBUTTONDOWN
:if (m
.x
>= 400 && m
.x
<= 504){closegraph();if (m
.y
>= 330 && m
.y
<= 355) { Draw_Chessboard(); PVP(l
, p
, q
, point
); }if (m
.y
>= 365 && m
.y
<= 390) { Draw_Chessboard(); PVC(l
, p
, q
, point
); }if (m
.y
>= 400 && m
.y
<= 425) { Introduce(); }if (m
.y
>= 435 && m
.y
<= 460) { return 0; }setfillcolor(BLACK
);cleardevice();Draw_Begin_Face();}}}h
++;flower
[0].Move();if (h
>= 100)flower
[1].Move();if (h
>= 140)flower
[2].Move();if (h
>= 180)flower
[3].Move();if (h
>= 220)flower
[4].Move();if (h
>= 270)flower
[5].Move();Sleep(50);}closegraph();return 0;
}void Introduce()
{initgraph(560, 460);IMAGE img1
;loadimage(&img1
, _T("桃花枝.png"));IMAGE img3
;loadimage(&img3
, _T("棋盤.png"));putimage(0, 250, &img3
);putimage(-50, -50, &img1
);settextcolor(GREEN
);outtextxy(300, 150, _T("游戲名稱:簡易五子棋"));outtextxy(300, 170, _T("游戲玩法:人人對戰、人機對戰"));outtextxy(300, 200, _T("人人對戰:兩人用鼠標點擊下棋,"));outtextxy(300, 220, _T(" 先下的玩家執黑旗,后下"));outtextxy(300, 240, _T(" 的執白棋。"));outtextxy(300, 270, _T("人機對戰:玩家和電腦對抗,玩家"));outtextxy(300, 290, _T(" 可以選擇先手,也可以當"));outtextxy(300, 310, _T(" 后手,可悔棋,可重玩。"));int j
= 0;FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE
));while (!_kbhit()){outtextxy(j
, 440, _T("按任意鍵退出"));setfillcolor(BLACK
);solidrectangle(j
- 1, 440, j
, 460);Sleep(20);j
++;if (j
>= 570)j
= 0;}
}
void Draw_Begin_Face()
{IMAGE img1
;loadimage(&img1
, _T("桃花枝.png"));putimage(-50, -50, &img1
);IMAGE img4
[3];loadimage(&img4
[0], _T("五.png"));putimage(325, 0, &img4
[0]);loadimage(&img4
[1], _T("子.png"));putimage(335, 100, &img4
[1]);loadimage(&img4
[2], _T("棋.png"));putimage(395, 140, &img4
[2]);IMAGE img
[4];loadimage(&img
[0], _T("桃花一朵1.png"));loadimage(&img
[1], _T("桃花一朵2.png"));loadimage(&img
[2], _T("桃花一朵3.png"));loadimage(&img
[3], _T("桃花一朵4.png"));IMAGE img3
;loadimage(&img3
, _T("棋盤.png"));putimage(0, 250, &img3
);IMAGE img6
[4];loadimage(&img6
[0], _T("人人.png"));loadimage(&img6
[1], _T("人機.png"));loadimage(&img6
[2], _T("介紹.png"));loadimage(&img6
[3], _T("退出.png"));putimage(400, 330, &img6
[0]);putimage(400, 365, &img6
[1]);putimage(400, 400, &img6
[2]);putimage(400, 435, &img6
[3]);
}
void Draw_Chessboard()
{initgraph(560, 460);setlinecolor(RGB(230, 200, 150));setlinestyle(PS_SOLID
| PS_JOIN_BEVEL
, 1);line(320, 0, 320, 320);IMAGE img
;loadimage(&img
, _T("木頭4.jpg"));putimage(0, 0, &img
);for (int i
= 2; i
< NODE
; i
++){line(20, (i
- 1) * 30 + 20, 440, (i
- 1) * 30 + 20);line((i
- 1) * 30 + 20, 20, (i
- 1) * 30 + 20, 440);}setlinestyle(PS_SOLID
| PS_JOIN_BEVEL
, 3);line(20, (1 - 1) * 30 + 20, 440, (1 - 1) * 30 + 20);line(20, (NODE
- 1) * 30 + 20, 440, (NODE
- 1) * 30 + 20);line((1 - 1) * 30 + 20, 20, (1 - 1) * 30 + 20, 440);line((NODE
- 1) * 30 + 20, 20, (NODE
- 1) * 30 + 20, 440);setfillcolor(BLACK
);solidcircle(110, 110, 3);solidcircle(350, 110, 3);solidcircle(110, 350, 3);solidcircle(350, 350, 3);solidcircle(230, 230, 3);
}
Status
Pop(Lnode
* top
)
{if (top
->next
== NULL) return false;Lnode
* q
;q
= top
->next
;top
->next
= q
->next
;free(q
);return true;
}
Status
Push(Lnode
* top
, Point point
)
{Lnode
* p
= new Lnode
;if (!p
) return false;p
->point
= point
;p
->next
= top
->next
;top
->next
= p
;return true;
}
Status
Init_List(Lnode
*& H
, Point point
)
{Lnode
* p
= new Lnode
;if (!p
)return false;p
->W_mun
= 0;p
->W_max
= 0;p
->point
= point
;p
->next
= NULL;Increase_Node(H
, p
);return 1;
}
Status
Increase_Node(Lnode
*& H
, Lnode
* node
)
{node
->next
= H
->next
;H
->next
= node
;return 1;}
void Coordinate(Point point
[NODE
+ 2][NODE
+ 2])
{for (int i
= 0; i
< NODE
+ 2; i
++)for (int j
= 0; j
< NODE
+ 2; j
++){point
[i
][j
].x
= (i
- 1) * 30 + 20;point
[i
][j
].y
= (j
- 1) * 30 + 20;}for (int i
= 0; i
< NODE
+ 2; i
++){point
[i
][0].Change_Condition(WALL
);point
[i
][16].Change_Condition(WALL
);point
[0][i
].Change_Condition(WALL
);point
[16][i
].Change_Condition(WALL
);}
}
void PVP(int l
, int p
, int q
, Point point
[NODE
+ 2][NODE
+ 2])
{IMAGE IM
, Im
;int out
= 1;int minute
= 0, secend
= 0;TCHAR s1
[5], s2
[5];putimage(470, 80, &img11
);putimage(470, 180, &img21
);settextcolor(RED
);settextstyle(30, 0, _T("宋體"));outtextxy(470, 350, _T("退出"));outtextxy(470, 400, _T("返回"));settextstyle(0, 0, _T("宋體"));getimage(&Im
, 470, 80, 60, 60);getimage(&IM
, 40, 40, 21, 21);solidrectangle(470, 20, 530, 45);_stprintf_s(s2
, _T("%d"), secend
);outtextxy(510, 25, s2
);_stprintf_s(s1
, _T("%d"), minute
);outtextxy(480, 25, s1
);outtextxy(495, 25, _T(":"));SYSTEMTIME t1
, t2
; GetLocalTime(&t1
);Lnode
* top
= new Lnode
;if (!top
)exit(0);top
->W_max
= 0; top
->W_mun
= 0; top
->next
= NULL; top
->point
= point
[0][0];HWND wnd
= GetHWnd();MOUSEMSG m
;while (out
){GetLocalTime(&t2
);if (t2
.wSecond
!= t1
.wSecond
){secend
++;_stprintf_s(s1
, _T("%d"), secend
);setfillcolor(BLACK
);solidrectangle(500, 20, 530, 45);outtextxy(510, 25, s1
);t1
= t2
;}if (secend
>= 60){secend
= 0; minute
++;_stprintf_s(s2
, _T("%d"), minute
);outtextxy(480, 25, s2
);}while (MouseHit()){m
= GetMouseMsg();switch (m
.uMsg
){case WM_LBUTTONDOWN
:for (int i
= 1; i
<= NODE
; i
++)for (int j
= 1; j
<= NODE
; j
++){if (ss
== 1 && (point
[i
][j
].x
- m
.x
) * (point
[i
][j
].x
- m
.x
) + (point
[i
][j
].y
- m
.y
) * (point
[i
][j
].y
- m
.y
) <= 100 && point
[i
][j
].Get_Condition() == NO_CHESSMEN
){if (l
== 0){setfillcolor(WHITE
);solidcircle(point
[q
][p
].x
, point
[q
][p
].y
, 10);setfillcolor(BLACK
);solidcircle(point
[i
][j
].x
, point
[i
][j
].y
, 10);setfillcolor(RED
);solidcircle(point
[i
][j
].x
, point
[i
][j
].y
, 3);point
[i
][j
].Change_Condition(BLACK_CHESSMEN
);Push(top
, point
[i
][j
]);Charge(point
, i
, j
, BLACK_CHESSMEN
);l
= 1;q
= i
; p
= j
;}else{setfillcolor(BLACK
);solidcircle(point
[q
][p
].x
, point
[q
][p
].y
, 10);setfillcolor(WHITE
);solidcircle(point
[i
][j
].x
, point
[i
][j
].y
, 10);setfillcolor(RED
);solidcircle(point
[i
][j
].x
, point
[i
][j
].y
, 3);point
[i
][j
].Change_Condition(WHITE_CHESSMEN
);Push(top
, point
[i
][j
]);Charge(point
, i
, j
, WHITE_CHESSMEN
);q
= i
; p
= j
;l
= 0;}}}if ((m
.x
- 500) * (m
.x
- 500) + (m
.y
- 110) * (m
.y
- 110) <= 30 * 30 && top
->next
!= NULL){ss
= 1;putimage(470, 80, &Im
);putimage(477, 87, &img12
);Sleep(50);putimage(470, 80, &img11
);int ii
= (top
->next
->point
.x
- 20) / 30 + 1;int jj
= (top
->next
->point
.y
- 20) / 30 + 1;point
[ii
][jj
].Change_Condition(NO_CHESSMEN
);putimage(point
[ii
][jj
].x
- 10, point
[ii
][jj
].y
- 10, &IM
);Pop(top
);q
= 0, p
= 0;}if ((m
.x
- 500) * (m
.x
- 500) + (m
.y
- 210) * (m
.y
- 210) <= 30 * 30 && top
->next
!= NULL){ss
= 1;putimage(470, 180, &Im
);putimage(477, 187, &img22
);Sleep(50);putimage(470, 180, &img21
);for (Lnode
* p
= top
; top
->next
!= NULL;){int ii
= (top
->next
->point
.x
- 20) / 30 + 1;int jj
= (top
->next
->point
.y
- 20) / 30 + 1;point
[ii
][jj
].Change_Condition(NO_CHESSMEN
);putimage(point
[ii
][jj
].x
- 10, point
[ii
][jj
].y
- 10, &IM
);Pop(top
);}q
= 0, p
= 0, l
= 0, secend
= 0, minute
= 0;}if (m
.x
>= 470 && m
.x
<= 530 && m
.y
>= 350 && m
.y
<= 380){HWND wnd
= GetHWnd();if (MessageBox(wnd
, _T("是否退出游戲?"), _T("退出"), MB_YESNO
| MB_ICONQUESTION
) == IDYES
)exit(1);}if (m
.x
>= 470 && m
.x
<= 530 && m
.y
>= 400 && m
.y
<= 430){HWND wnd
= GetHWnd();if (MessageBox(wnd
, _T("是否返回上一界面?"), _T("返回"), MB_YESNO
| MB_ICONQUESTION
) == IDYES
)out
= 0;}}}}
}
void PVC(int l
, int p
, int q
, Point point
[NODE
+ 2][NODE
+ 2])
{IMAGE IM
, Im
;int choose
;int out
= 1;TCHAR s1
[5], s2
[5];int minute
= 0, secend
= 0;SYSTEMTIME t1
, t2
;GetLocalTime(&t1
);putimage(470, 80, &img11
);putimage(470, 180, &img21
);settextcolor(RED
);settextstyle(30, 0, _T("宋體"));outtextxy(470, 350, _T("退出"));outtextxy(470, 400, _T("返回"));settextstyle(0, 0, _T("宋體"));getimage(&Im
, 470, 80, 60, 60);getimage(&IM
, 40, 40, 21, 21);solidrectangle(470, 20, 530, 45);_stprintf_s(s2
, _T("%d"), secend
);outtextxy(510, 25, s2
);_stprintf_s(s1
, _T("%d"), minute
);outtextxy(480, 25, s1
);outtextxy(495, 25, _T(":"));Lnode
* top
= new Lnode
;if (!top
)exit(0);top
->W_max
= 0; top
->W_mun
= 0; top
->next
= NULL; top
->point
= point
[0][0];Sleep(1000);HWND wnd
= GetHWnd();if (MessageBox(wnd
, _T("是否先手?"), _T("選擇"), MB_YESNO
| MB_ICONQUESTION
) == IDYES
)choose
= 1;else choose
= 0;MOUSEMSG m
;while (out
){if (l
== choose
){int number
= 0;Lnode
* H
= new Lnode
;if (!H
)exit(0);H
->W_max
= 0; H
->W_mun
= 0; H
->next
= NULL; H
->point
= point
[0][0];for (int i
= NODE
; i
> 0; i
--)for (int j
= NODE
; j
> 0; j
--){if (point
[i
][j
].Get_Condition() == NO_CHESSMEN
) { Init_List(H
, point
[i
][j
]); number
++; }}for (Lnode
* p
= H
; p
->next
!= NULL; ){int i
= (p
->next
->point
.x
- 20) / 30 + 1;int j
= (p
->next
->point
.y
- 20) / 30 + 1;p
->next
->point
.W
[0] = Get_Weight(point
, i
, j
, WHITE_CHESSMEN
, HORIZONTAL
) + Get_Weight(point
, i
, j
, BLACK_CHESSMEN
, HORIZONTAL
);p
->next
->point
.W
[1] = Get_Weight(point
, i
, j
, WHITE_CHESSMEN
, VERTICAL
) + Get_Weight(point
, i
, j
, BLACK_CHESSMEN
, VERTICAL
);p
->next
->point
.W
[2] = Get_Weight(point
, i
, j
, WHITE_CHESSMEN
, POSITIVE_BATTER
) + Get_Weight(point
, i
, j
, BLACK_CHESSMEN
, POSITIVE_BATTER
);p
->next
->point
.W
[3] = Get_Weight(point
, i
, j
, WHITE_CHESSMEN
, NEGATIVE_BATTER
) + Get_Weight(point
, i
, j
, BLACK_CHESSMEN
, NEGATIVE_BATTER
);p
->next
->W_max
= p
->next
->point
.Get_M_W();p
->next
->W_mun
= p
->next
->point
.W
[0] + p
->next
->point
.W
[1] + p
->next
->point
.W
[2] + p
->next
->point
.W
[3];if (p
->next
->W_max
>= H
->W_max
){H
->W_max
= p
->next
->W_max
;p
= p
->next
;}else{Lnode
* q
= p
->next
;p
->next
= p
->next
->next
;delete(q
);number
--;}}if (H
->next
!= NULL){for (Lnode
* p
= H
; p
->next
!= NULL;){if (p
->next
->W_max
< H
->W_max
){Lnode
* q
= p
->next
;p
->next
= p
->next
->next
;delete(q
);number
--;}else{if (p
->next
->W_mun
> H
->W_mun
) H
->W_mun
= p
->next
->W_mun
;p
= p
->next
;}}for (Lnode
* p
= H
; p
->next
!= NULL;){if (p
->next
->W_mun
< H
->W_mun
){Lnode
* q
= p
->next
;p
->next
= p
->next
->next
;delete(q
);number
--;}else p
= p
->next
;}int w
= rand() % number
;while ((w
- 1) >= 0){H
->next
= H
->next
->next
;w
--;}int i
= (H
->next
->point
.x
- 20) / 30 + 1;int j
= (H
->next
->point
.y
- 20) / 30 + 1;if (choose
== 0){setfillcolor(WHITE
);solidcircle(point
[q
][p
].x
, point
[q
][p
].y
, 10);setfillcolor(BLACK
);solidcircle(point
[i
][j
].x
, point
[i
][j
].y
, 10);setfillcolor(RED
);solidcircle(point
[i
][j
].x
, point
[i
][j
].y
, 3);point
[i
][j
].Change_Condition(BLACK_CHESSMEN
);Charge(point
, i
, j
, BLACK_CHESSMEN
);}else{setfillcolor(BLACK
);solidcircle(point
[q
][p
].x
, point
[q
][p
].y
, 10);setfillcolor(WHITE
);solidcircle(point
[i
][j
].x
, point
[i
][j
].y
, 10);setfillcolor(RED
);solidcircle(point
[i
][j
].x
, point
[i
][j
].y
, 3);point
[i
][j
].Change_Condition(WHITE_CHESSMEN
);Charge(point
, i
, j
, WHITE_CHESSMEN
);}Push(top
, point
[i
][j
]);q
= i
; p
= j
;}else MessageBox(NULL, _T("平局,厲害了!"), _T("游戲結束"), MB_OK
);l
= !choose
;secend
= 0;}else{GetLocalTime(&t2
);if (t2
.wSecond
!= t1
.wSecond
){secend
++;_stprintf_s(s1
, _T("%d"), secend
);setfillcolor(BLACK
);solidrectangle(500, 20, 530, 45);outtextxy(510, 25, s1
);t1
= t2
;}if (secend
>= 60){secend
= 0; minute
++;_stprintf_s(s2
, _T("%d"), minute
);outtextxy(480, 25, s2
);}while (MouseHit()){m
= GetMouseMsg();switch (m
.uMsg
){case WM_LBUTTONDOWN
:{for (int i
= 1; i
<= NODE
; i
++)for (int j
= 1; j
<= NODE
; j
++)if (ss
== 1 && (point
[i
][j
].x
- m
.x
) * (point
[i
][j
].x
- m
.x
) + (point
[i
][j
].y
- m
.y
) * (point
[i
][j
].y
- m
.y
) <= 100 && point
[i
][j
].Get_Condition() == NO_CHESSMEN
){if (choose
== 1){setfillcolor(WHITE
);solidcircle(point
[q
][p
].x
, point
[q
][p
].y
, 10);setfillcolor(BLACK
);solidcircle(point
[i
][j
].x
, point
[i
][j
].y
, 10);setfillcolor(RED
);solidcircle(point
[i
][j
].x
, point
[i
][j
].y
, 3);point
[i
][j
].Change_Condition(BLACK_CHESSMEN
);Charge(point
, i
, j
, BLACK_CHESSMEN
);}else{setfillcolor(BLACK
);solidcircle(point
[q
][p
].x
, point
[q
][p
].y
, 10);setfillcolor(WHITE
);solidcircle(point
[i
][j
].x
, point
[i
][j
].y
, 10);setfillcolor(RED
);solidcircle(point
[i
][j
].x
, point
[i
][j
].y
, 3);point
[i
][j
].Change_Condition(WHITE_CHESSMEN
);Charge(point
, i
, j
, WHITE_CHESSMEN
);}Push(top
, point
[i
][j
]);l
= choose
;q
= i
; p
= j
;}}if ((m
.x
- 500) * (m
.x
- 500) + (m
.y
- 110) * (m
.y
- 110) <= 30 * 30 && top
->next
!= NULL){ss
= 1;putimage(470, 80, &Im
);putimage(477, 87, &img12
);Sleep(50);putimage(470, 80, &img11
);int ii
= (top
->next
->point
.x
- 20) / 30 + 1;int jj
= (top
->next
->point
.y
- 20) / 30 + 1;point
[ii
][jj
].Change_Condition(NO_CHESSMEN
);putimage(point
[ii
][jj
].x
- 10, point
[ii
][jj
].y
- 10, &IM
);Pop(top
);ii
= (top
->next
->point
.x
- 20) / 30 + 1;jj
= (top
->next
->point
.y
- 20) / 30 + 1;point
[ii
][jj
].Change_Condition(NO_CHESSMEN
);putimage(point
[ii
][jj
].x
- 10, point
[ii
][jj
].y
- 10, &IM
);Pop(top
);q
= 0, p
= 0;}if ((m
.x
- 500) * (m
.x
- 500) + (m
.y
- 210) * (m
.y
- 210) <= 30 * 30 && top
->next
!= NULL){ss
= 1;putimage(470, 180, &Im
);putimage(477, 187, &img22
);Sleep(50);putimage(470, 180, &img21
);for (Lnode
* p
= top
; top
->next
!= NULL;){int ii
= (top
->next
->point
.x
- 20) / 30 + 1;int jj
= (top
->next
->point
.y
- 20) / 30 + 1;point
[ii
][jj
].Change_Condition(NO_CHESSMEN
);putimage(point
[ii
][jj
].x
- 10, point
[ii
][jj
].y
- 10, &IM
);Pop(top
);}q
= 0, p
= 0, l
= 0;if (MessageBox(wnd
, _T("是否先手?"), _T("選擇"), MB_YESNO
| MB_ICONQUESTION
) == IDYES
)choose
= 1;else choose
= 0;}if (m
.x
>= 470 && m
.x
<= 530 && m
.y
>= 350 && m
.y
<= 380){HWND wnd
= GetHWnd();if (MessageBox(wnd
, _T("是否退出游戲?"), _T("退出"), MB_YESNO
| MB_ICONQUESTION
) == IDYES
)exit(1);}if (m
.x
>= 470 && m
.x
<= 530 && m
.y
>= 400 && m
.y
<= 430){HWND wnd
= GetHWnd();if (MessageBox(wnd
, _T("是否返回上一界面?"), _T("返回"), MB_YESNO
| MB_ICONQUESTION
) == IDYES
)out
= 0;}}}}}
}
int Work(int front
, int back
, Condition condition1
, Condition condition2
)
{switch (front
+ back
){case 0:return 0; break;case 1: if (condition1
!= NO_CHESSMEN
&& condition2
!= NO_CHESSMEN
)return 0;else if (condition1
== NO_CHESSMEN
&& condition2
== NO_CHESSMEN
) return 10;else return 1; break;case 2: if (condition1
!= NO_CHESSMEN
&& condition2
!= NO_CHESSMEN
)return 0;else if (condition1
== NO_CHESSMEN
&& condition2
== NO_CHESSMEN
) return 100;else return 10; break;case 3: if (condition1
!= NO_CHESSMEN
&& condition2
!= NO_CHESSMEN
)return 0;else if (condition1
== NO_CHESSMEN
&& condition2
== NO_CHESSMEN
) return 1000;else return 100; break;default: return 10000;}
}
int Get_Weight(Point point
[NODE
+ 2][NODE
+ 2], int i
, int j
, Condition condition
, Direction direction
)
{int front
= 0, back
= 0, e
= 0, m
= 0;int h
= i
, l
= j
;Condition condition1
= NO_CHESSMEN
, condition2
= NO_CHESSMEN
;if (direction
== HORIZONTAL
)m
= min(4, h
);else if (direction
== VERTICAL
)m
= min(4, l
);else if (direction
== POSITIVE_BATTER
) { e
= min(h
, l
); m
= min(4, e
); }else { e
= min(16 - h
, l
); m
= min(4, e
); }while (m
> 0){switch (direction
){case HORIZONTAL
: h
--; break;case VERTICAL
:l
--; break;case POSITIVE_BATTER
: h
--; l
--; break;case NEGATIVE_BATTER
:h
++; l
--; break;default:exit(0);}if (point
[h
][l
].Get_Condition() == condition
) front
++;else {condition1
= point
[h
][l
].Get_Condition();break;}m
--;}if (direction
== HORIZONTAL
)m
= min(4, 16 - i
);else if (direction
== VERTICAL
)m
= min(4, 16 - j
);else if (direction
== POSITIVE_BATTER
) { e
= min(16 - i
, 16 - j
); m
= min(4, e
); }else { e
= min(i
, 16 - j
); m
= min(4, e
); }while (m
> 0){switch (direction
){case HORIZONTAL
: i
++; break;case VERTICAL
:j
++; break;case POSITIVE_BATTER
: i
++; j
++; break;case NEGATIVE_BATTER
:i
--; j
++; break;default:exit(0);}if (point
[i
][j
].Get_Condition() == condition
) back
++;else {condition2
= point
[i
][j
].Get_Condition();break;}m
--;}return Work(front
, back
, condition1
, condition2
);
};
void Charge(Point point
[NODE
+ 2][NODE
+ 2], int i
, int j
, Condition condition
)
{int m
, h
= 0;if (condition
== WHITE_CHESSMEN
)m
= 0;else m
= 1;for (int e
= 0, k
= 1; k
<= NODE
; k
++){if (point
[k
][j
].Get_Condition() == condition
) e
++;else e
= 0;if (e
== 5) h
++;}for (int e
= 0, k
= 1; k
< NODE
; k
++){if (point
[i
][k
].Get_Condition() == condition
) e
++;else e
= 0;if (e
== 5)h
++;}if (i
>= j
){for (int e
= 0, k
= 1, l
= (i
- j
) + 1; l
<= NODE
; k
++, l
++){if (point
[l
][k
].Get_Condition() == condition
) e
++;else e
= 0;if (e
== 5) h
++;}}else{for (int e
= 0, k
= 1, l
= (j
- i
) + 1; l
<= NODE
; k
++, l
++){if (point
[k
][l
].Get_Condition() == condition
) e
++;else e
= 0;if (e
== 5) h
++;}}if (i
+ j
<= NODE
){for (int e
= 0, k
= 1, l
= (i
+ j
) - 1; l
> 0; k
++, l
--){if (point
[l
][k
].Get_Condition() == condition
) e
++;else e
= 0;if (e
== 5) h
++;}}else{for (int e
= 0, k
= j
+ i
- NODE
, l
= NODE
; k
<= NODE
; k
++, l
--){if (point
[l
][k
].Get_Condition() == condition
) e
++;else e
= 0;if (e
== 5) h
++;}}if (m
== 0 && h
!= 0) { MessageBox(NULL, _T("執白者獲勝!"), _T("游戲結束"), MB_OK
); ss
= 0; }if (m
== 1 && h
!= 0) { MessageBox(NULL, _T("執黑者獲勝!"), _T("游戲結束"), MB_OK
); ss
= 0; }
}
總結
以上是生活随笔為你收集整理的数据结构与算法课程设计之五子棋(人机)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。