功能一:落子提示,十字準星(可以跟蹤滑鼠,提示該位置可以落子)
功能二:左側的落子顏色提示(避免落子太多忘記當前落什麼顏色的棋子)
功能三:悔棋
滑鼠右鍵悔棋
功能四:再來一局(滑鼠右鍵,一方勝利時觸發)
`
判斷輸贏的規則:每落下一顆棋子,以該棋子爲中心,分別朝八個方向尋找相連且同色的棋子計數,如果相反的兩方向之和等於4,則判斷勝利
遊戲背景:13天學完C語言(一本書)後做的遊戲,瞭解圖形庫用了兩天,寫邏輯部分用了一天,每天五小時。
遊戲缺點:1.程式碼比較凌亂,想到什麼寫什麼了,主要是主函數那一塊有些臃腫;2.沒能實現聯網對戰功能;3.命名也應該很不規範,小白作品。
涉及到的知識點:指針,陣列,回圈,函數
後續:現在在學習c++,學完後會再寫一次五子棋的,爭取能聯網對戰。
原始碼:
#include <graphics.h>
#include <conio.h>
#include<stdio.h>
#include <time.h>
#define NUM_B 225 //棋子數量
#define HL 15 //上下
#define X(x) 162+x50
#define Y(y) 30+y50
#define BXY 15 //橫軸/縱軸數量
void board(void);
int get_x(short xx);
int get_y(short yy);
int left_right(int(*arr)[BXY], short x, short y); //左右
int up_down(int(*arr)[BXY], short x, short y); //上下
int lu_rd(int(*arr)[BXY], short x, short y); //左上右下
int ld_ru(int(*arr)[BXY], short x, short y); //左下右上
void win_b(void);
void win_w(void);
void fall_w(void); //落白子
void fall_b(void); //落黑子
int main()
{
int undo_x = 0, undo_y = 0;
int again = 0; //用於勝利後開啓下一局的開關
int cut = 0; //用於勝利後棋盤不可下棋的開關
int FLAG[BXY][BXY]; //棋盤,儲存棋子,0代表空,1代表白子,2代表黑子
int x, y; //座標
int i, j, wb; //i,j儲存滑鼠上一個狀態,wb控制下一局開局的棋子顏色(這個感覺有沒有都一樣,,,寫了就不刪了)
i = j = wb = 0;
int x1[2] = { -2,-2 }, y1[2] = { -2,-2 }; //儲存滑鼠前後狀態,用於消除十字準星
board(); //建立視窗
fall_b();
/* 儲存所有棋子座標,並且把棋子狀態設爲0 */
for (y = 0; y < 15; y++)
for (x = 0; x < 15; x++)
FLAG[x][y] = 0;
MOUSEMSG m; // 定義滑鼠訊息
while (true) //遊戲無限回圈
{
m = GetMouseMsg(); //獲取滑鼠資訊
switch (m.uMsg)
{
case WM_MOUSEMOVE: //滑鼠移動
if (cut == 0 && (get_x(m.x) > -1 && get_x(m.x) < 15) && (get_y(m.y) > -1 && get_y(m.y) < 15) && FLAG[get_x(m.x)][get_y(m.y)] < 1) //十字準星
{
// 建立一個矩形區域
HRGN rgn = CreateRectRgn(162, 30, 862, 730);
// 將該矩形區域設定爲裁剪區
setcliprgn(rgn);
// 不再使用 rgn,清理 rgn 佔用的系統資源
DeleteObject(rgn);
if (i == 0)
{
x1[i] = get_x(m.x);
y1[i] = get_y(m.y);
i = 1;
j = 0;
}
else if (i == 1)
{
x1[i] = get_x(m.x);
y1[i] = get_y(m.y);
i = 0;
j = 1;
}
setfillcolor(LIGHTBLUE);
solidrectangle(X(get_x(m.x)) - 15, Y(get_y(m.y)) - 3, X(get_x(m.x)) + 15, Y(get_y(m.y)) + 3);
solidrectangle(X(get_x(m.x)) - 3, Y(get_y(m.y)) - 15, X(get_x(m.x)) + 3, Y(get_y(m.y)) + 15);
}
break;
case WM_LBUTTONDOWN: //滑鼠左鍵點選
if (cut == 0 && (get_x(m.x) > -1 && get_x(m.x) < 15) && (get_y(m.y) > -1 && get_y(m.y) < 15) && FLAG[get_x(m.x)][get_y(m.y)] < 1)
{
// 取消之前設定的裁剪區
setcliprgn(NULL);
if (wb == 0)
{
setfillcolor(BLACK);
solidcircle(X(get_x(m.x)), Y(get_y(m.y)), 20);
undo_x = get_x(m.x);
undo_y = get_y(m.y);
fall_w();
wb = 1;
FLAG[get_x(m.x)][get_y(m.y)] = 2;
if (left_right(FLAG, m.x, m.y) > 0 || up_down(FLAG, m.x, m.y) > 0 || lu_rd(FLAG, m.x, m.y) > 0 || ld_ru(FLAG, m.x, m.y) > 0)
{
win_b();
again = 1;
cut = 1;
}
}
else if (wb == 1)
{
setfillcolor(WHITE);
solidcircle(X(get_x(m.x)), Y(get_y(m.y)), 20);
undo_x = get_x(m.x);
undo_y = get_y(m.y);
fall_b();
wb = 0;
FLAG[get_x(m.x)][get_y(m.y)] = 1;
if (left_right(FLAG, m.x, m.y) > 0 || up_down(FLAG, m.x, m.y) > 0 || lu_rd(FLAG, m.x, m.y) > 0 || ld_ru(FLAG, m.x, m.y) > 0)
{
win_w();
again = 1;
cut = 1;
}
}
}
break;
case WM_RBUTTONUP: //滑鼠右鍵點選
if (again == 1)
{
for (y = 0; y < 15; y++)
for (x = 0; x < 15; x++)
FLAG[x][y] = 0;
// 取消之前設定的裁剪區
setcliprgn(NULL);
board();
undo_x = undo_y = 0;
again = 0;
cut = 0;
if (wb == 1)
{
wb = 0;
fall_b();
}
else
{
wb = 1;
fall_w();
}
}
else
{
if (FLAG[undo_x][undo_y] == 1)
{
// 取消之前設定的裁剪區
setcliprgn(NULL);
setfillcolor(RGB(209, 186, 116)); //填充背景顏色
solidrectangle(X(undo_x) - 20, Y(undo_y) - 20, X(undo_x) + 20, Y(undo_y) + 20);
line(X(undo_x) - 20, Y(undo_y), X(undo_x) + 20, Y(undo_y));
line(X(undo_x), Y(undo_y) - 20, X(undo_x), Y(undo_y) + 20);
/*9個帶黑點的落子位置*/
if (undo_x != 15 && undo_y != 15 && (undo_x + 1) % 4 == 0 && (undo_y + 1) % 4 == 0)
{
setfillcolor(BLACK);
fillcircle(X(undo_x), Y(undo_y), 5);
}
fall_w();
FLAG[undo_x][undo_y] = 0;
wb = 1;
}
else if (FLAG[undo_x][undo_y] == 2)
{
// 取消之前設定的裁剪區
setcliprgn(NULL);
setfillcolor(RGB(209, 186, 116)); //填充背景顏色
solidrectangle(X(undo_x) - 20, Y(undo_y) - 20, X(undo_x) + 20, Y(undo_y) + 20);
line(X(undo_x) - 20, Y(undo_y), X(undo_x) + 20, Y(undo_y));
line(X(undo_x), Y(undo_y) - 20, X(undo_x), Y(undo_y) + 20);
/*9個帶黑點的落子位置*/
if (undo_x != 15 && undo_y != 15 && (undo_x + 1) % 4 == 0 && (undo_y + 1) % 4 == 0)
{
setfillcolor(BLACK);
fillcircle(X(undo_x), Y(undo_y), 5);
}
fall_b();
FLAG[undo_x][undo_y] = 0;
wb = 0;
}
}
break;
}
if (((get_x(m.x)) != x1[j] || (get_y(m.y) != y1[j])) && FLAG[x1[j]][y1[j]] < 1)
{
setfillcolor(RGB(209, 186, 116)); //填充背景顏色
solidrectangle(X(x1[j]) - 15, Y(y1[j]) - 15, X(x1[j]) + 15, Y(y1[j]) + 15);
line(X(x1[j]) - 15, Y(y1[j]), X(x1[j]) + 15, Y(y1[j]));
line(X(x1[j]), Y(y1[j]) - 15, X(x1[j]), Y(y1[j]) + 15);
/*9個帶黑點的落子位置*/
if (x1[j] != 15 && y1[j] != 15 && (x1[j] + 1) % 4 == 0 && (y1[j] + 1) % 4 == 0)
{
setfillcolor(BLACK);
fillcircle(X(x1[j]), Y(y1[j]), 5);
}
}
}
// 關閉圖形視窗
closegraph();
_getch();
return 0;
}
void board()
{
initgraph(1024, 760); //視窗大小
setbkcolor(RGB(190, 231, 233)); //視窗背景
cleardevice();
setlinecolor(BLACK); //線條顏色
setlinestyle(PS_SOLID | PS_ENDCAP_SQUARE, 2); //棋盤邊框線條樣式
setfillcolor(RGB(209, 186, 116)); //棋盤背景顏色
fillrectangle(132, 0, 892, 760);
fillrectangle(162, 30, 862, 730); //棋盤大小,棋盤規格15X15
setlinestyle(PS_SOLID | PS_ENDCAP_SQUARE, 2); //棋盤線條樣式
for (int i = 80; i <= 730; i += 50) //棋盤橫線
line(162, i, 862, i);
for (int i = 212; i <= 862; i += 50) //棋盤豎線
line(i, 30, i, 730);
setfillcolor(BLACK); //圓點填充顏色爲黑色
for (int i = 3; i <= 12; i += 4) //繪製9個圓點
{
fillcircle(X(i), Y(3), 5);
fillcircle(X(i), Y(7), 5);
fillcircle(X(i), Y(11), 5);
}
setfillcolor(RGB(190, 237, 199)); //棋盤背景顏色
fillrectangle(20, 310, 115, 425);
settextcolor(BLACK);
settextstyle(25, 0, _T(「Consolas」));
wchar_t s[] = L"請落子";
outtextxy(30, 315, s);
fillrectangle(5, 460, 125, 495);
settextstyle(15, 0, _T("Consolas"));
wchar_t s2[] = L"點選滑鼠右鍵悔棋";
outtextxy(10, 470, s2);
settextstyle(25, 0, _T("Consolas"));
wchar_t s1[] = L"yp_lien";
settextcolor(RGB(214, 213, 183));
outtextxy(930, 730, s1);
}
void fall_w()
{
setfillcolor(WHITE);
solidcircle(66, 380, 30);
}
void fall_b()
{
setfillcolor(BLACK);
solidcircle(66, 380, 30);
}
int get_x(short xx)
{
if (((162 - HL) <= xx) && ((162 + HL) >= xx))
return 0;
else if (((212 - HL) <= xx) && ((212 + HL) >= xx))
return 1;
else if (((262 - HL) <= xx) && ((262 + HL) >= xx))
return 2;
else if (((312 - HL) <= xx) && ((312 + HL) >= xx))
return 3;
else if (((362 - HL) <= xx) && ((362 + HL) >= xx))
return 4;
else if (((412 - HL) <= xx) && ((412 + HL) >= xx))
return 5;
else if (((462 - HL) <= xx) && ((462 + HL) >= xx))
return 6;
else if (((512 - HL) <= xx) && ((512 + HL) >= xx))
return 7;
else if (((562 - HL) <= xx) && ((562 + HL) >= xx))
return 8;
else if (((612 - HL) <= xx) && ((612 + HL) >= xx))
return 9;
else if (((662 - HL) <= xx) && ((662 + HL) >= xx))
return 10;
else if (((712 - HL) <= xx) && ((712 + HL) >= xx))
return 11;
else if (((762 - HL) <= xx) && ((762 + HL) >= xx))
return 12;
else if (((812 - HL) <= xx) && ((812 + HL) >= xx))
return 13;
else if (((862 - HL) <= xx) && ((862 + HL) >= xx))
return 14;
else
return -1;
}
int get_y(short yy)
{
if (((30 - HL) <= yy) && ((30 + HL) >= yy))
return 0;
else if (((80 - HL) <= yy) && ((80 + HL) >= yy))
return 1;
else if (((130 - HL) <= yy) && ((130 + HL) >= yy))
return 2;
else if (((180 - HL) <= yy) && ((180 + HL) >= yy))
return 3;
else if (((230 - HL) <= yy) && ((230 + HL) >= yy))
return 4;
else if (((280 - HL) <= yy) && ((280 + HL) >= yy))
return 5;
else if (((330 - HL) <= yy) && ((330 + HL) >= yy))
return 6;
else if (((380 - HL) <= yy) && ((380 + HL) >= yy))
return 7;
else if (((430 - HL) <= yy) && ((430 + HL) >= yy))
return 8;
else if (((480 - HL) <= yy) && ((480 + HL) >= yy))
return 9;
else if (((530 - HL) <= yy) && ((530 + HL) >= yy))
return 10;
else if (((580 - HL) <= yy) && ((580 + HL) >= yy))
return 11;
else if (((630 - HL) <= yy) && ((630 + HL) >= yy))
return 12;
else if (((680 - HL) <= yy) && ((680 + HL) >= yy))
return 13;
else if (((730 - HL) <= yy) && ((730 + HL) >= yy))
return 14;
else
return -1;
}
void win_b()
{
setbkcolor(RGB(214, 213, 183));
settextcolor(RGB(244, 96, 108));
wchar_t s[] = L"黑棋勝利,黑棋優先落子";
wchar_t s1[] = L"點選滑鼠右鍵開始下一局";
settextstyle(30, 0, _T(「Consolas」));
outtextxy(380, 5, s);
outtextxy(380, 35, s1);
}
void win_w()
{
setbkcolor(RGB(214, 213, 183));
settextcolor(RGB(244, 96, 108));
wchar_t s[] = L"白棋勝利,白棋優先落子";
wchar_t s1[] = L"點選滑鼠右鍵開始下一局";
settextstyle(30, 0, _T(「Consolas」));
outtextxy(380, 5, s);
outtextxy(380, 35, s1);
}
int left_right(int(*p)[BXY], short x, short y)
{
int k, i, j;
i = j = 0;
for (k = 1; k < 5; k++)
if (p[get_x(x) - k][get_y(y)] == p[get_x(x)][get_y(y)])
i++;
else
k = 5;
for (k = 1; k < 5; k++)
if (p[get_x(x) + k][get_y(y)] == p[get_x(x)][get_y(y)])
j++;
else
k = 5;
return ((i + j) > 3) ? 1 : -1;
}
int up_down(int(*p)[BXY], short x, short y) //上下
{
int k, i, j;
i = j = 0;
for (k = 1; k < 5; k++)
if (p[get_x(x)][get_y(y) - k] == p[get_x(x)][get_y(y)])
i++;
else
k = 5;
for (k = 1; k < 5; k++)
if (p[get_x(x)][get_y(y) + k] == p[get_x(x)][get_y(y)])
j++;
else
k = 5;
return ((i + j) > 3) ? 1 : -1;
}
int lu_rd(int(*p)[BXY], short x, short y) //左上右下
{
int k, i, j;
i = j = 0;
for (k = 1; k < 5; k++)
if (p[get_x(x) - k][get_y(y) - k] == p[get_x(x)][get_y(y)])
i++;
else
k = 5;
for (k = 1; k < 5; k++)
if (p[get_x(x) + k][get_y(y) + k] == p[get_x(x)][get_y(y)])
j++;
else
k = 5;
return ((i + j) > 3) ? 1 : -1;
}
int ld_ru(int(*p)[BXY], short x, short y) //左下右上
{
int k, i, j;
i = j = 0;
for (k = 1; k < 5; k++)
if (p[get_x(x) - k][get_y(y) + k] == p[get_x(x)][get_y(y)])
i++;
else
k = 5;
for (k = 1; k < 5; k++)
if (p[get_x(x) + k][get_y(y) - k] == p[get_x(x)][get_y(y)])
j++;
else
k = 5;
return ((i + j) > 3) ? 1 : -1;
}