#include <stdio.h>
#include <easyx.h>
#include <stdlib.h>
#define length 15
#define Wide 700
double size;
int chess[length][length];
bool is_red;
bool is_gameover;
bool is_init;
int s = 0;
int h = 0;
int i = 0, j = 0;
void Init()
{
if (!is_init)
{
initgraph(Wide, Wide, EW_SHOWCONSOLE);
}
is_init = true;
memset(chess,0,sizeof(chess));
size =(float) Wide / (length+1);
is_red = true;//紅棋先下
is_gameover = false;
}
void draw_background()
{
int i;
setbkcolor(RGB(0x0000AA, 0x00AA00, 0x0000AA));
setlinecolor(0);
cleardevice();
for (i = 1; i <= length; i++)
{
line(size, size * i, Wide - size, size * i);//橫線條
line(size * i, size,size*i,Wide-size);//豎線條
TCHAR nums[3];
wsprintf(nums, _T("%d"), i);
outtextxy(size - size / 2, size * i - size / 8, nums);
outtextxy(size * i - size / 8, size - size / 2, nums);
}
setfillcolor(0x00);
int m = 3;
fillcircle(size * m, size * m, size / 8);//1 2 是坐標,3是半徑
fillcircle(size * m, size * (length-m+1), size / 8);
fillcircle(size * (length - m + 1) ,size * m, size / 8);
fillcircle(size * (length - m + 1), size * (length - m + 1), size / 8);
fillcircle(size * ((length)/2+1), size * ((length) / 2 + 1), size / 8);
}
void draw_chess()
{
int i, j;
for (i = 0; i < length; i++)
{
for (j = 0; j < length; j++)
{
if (chess[i][j] == 0)
{
continue;
}
if (chess[i][j] == 1)
{
setfillcolor(0x00AA00);
}
if (chess[i][j] == 2)
{
setfillcolor(0xAA0000);
}
solidcircle(size * (j+ 1), size * (i+ 1), size / 4);
}
}
}
void draw_chess()
{
int i, j;
for (i = 0; i < length; i++)
{
for (j = 0; j < length; j++)
{
if (chess[i][j] == 0)
{
continue;
}
if (chess[i][j] == 1)
{
setfillcolor(0x00AA00);
}
if (chess[i][j] == 2)
{
setfillcolor(0xAA0000);
}
solidcircle(size * (j+ 1), size * (i+ 1), size / 4);
}
}
}
void draw_Init()//畫圖
{
BeginBatchDraw();
draw_background();
draw_chess();
EndBatchDraw();
}
bool is_win(int x,int y)//判斷游戲是否結束
{
if (chess[y][x] == 0)
{
return false;
}
int dir[][2] = { {1,0},{0,1} ,{1,1} ,{1,-1} };
for (int i = 0; i < 4; i++)
{
int sum = 1;
int count = 1;
bool f1 = true, f2 = true;
for (int j = 1; j <=5; j++)
{
int dx = x + dir[i][1] * j;
int dy = y + dir[i][0] * j;
int rx = x - dir[i][1] * j;
int ry = y - dir[i][0] * j;
if (chess[y][x] == 1)
{
if (dx >= 0 && dx < length && dy >= 0 && dy < length && chess[y][x] == chess[dy][dx] && f1)//如果相等證明他們是相同顏色的棋子
{
sum++;
}
else
{
f1 = false;
}
if (rx >= 0 && rx < length && ry >= 0 && ry < length && chess[y][x] == chess[ry][rx] && f2)//如果相等證明他們是相同顏色的棋子
{
sum++;
}
else
{
f2 = false;
}
}
else if (chess[y][x] == 2)
{
if (dx >= 0 && dx < length && dy >= 0 && dy < length && chess[y][x] == chess[dy][dx] && f1)//如果相等證明他們是相同顏色的棋子
{
count++;
}
else
{
f1 = false;
}
if (rx >= 0 && rx < length && ry >= 0 && ry < length && chess[y][x] == chess[ry][rx] && f2)//如果相等證明他們是相同顏色的棋子
{
count++;
}
else
{
f2 = false;
}
}
}
if (sum >= 5)
{
s++;
return true;
}
if (count >= 5)
{
h++;
return true;
}
}
return false;
}
void play()//玩家互動
{
MOUSEMSG msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN)
{
int x = (msg.x -size/2)/ size;
int y = (msg.y-size/2) / size;
if (chess[y][x] == 0&&x>=0&&x<length&&y>=0&&y<length)
{
chess[y][x] = is_red ? 1 : 2;
if (is_win(x, y))
{
is_gameover = true;
return;
}
is_red = !is_red;
}
}
if (msg.uMsg == WM_RBUTTONDOWN)
{
UINT nRset = MessageBox(NULL, _T("確定要重新開始嗎"), _T("提示"), MB_YESNO);
if (nRset==IDNO)
{
MessageBox(GetForegroundWindow(), _T(" "), _T("游戲繼續"), 0);
}
if (nRset==IDYES)
{
Init();
}
}
}
void start_game()//開始游戲
{
Init();
while (true)
{
play();
draw_Init();
if (is_gameover)
{
if (s == 1)
{
MessageBox(GetForegroundWindow(), _T(" "), _T(" 游戲結束,綠方勝利"), 0);
s = 0;
i++;
}
if (h== 1)
{
MessageBox(GetForegroundWindow(), _T(" "), _T(" 游戲結束,藍方勝利"), 0);
h = 0;
j++;
}
Init();
printf("綠方:藍方 %d:%d\n", i,j);
}
}
}
int main()
{
start_game();
getchar();
return 0;
}
uj5u.com熱心網友回復:
And樓主是有什么問題嗎
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/81099.html
標籤:C++ 語言
上一篇:學生管理系統
