Proteus+Keil+STM32+ILI9341+黑白棋设计
B站演示链接:
传送门
相应的资源我也发了,里面有ioc工程,电路图,以及演示视频
资源链接:https://download.csdn.net/download/qq_44624316/13701147
其中我用红色空心圆代表光标,作为下棋子的位置,
有上,下,左,右,确定四个按钮。
黑白棋规则:只能下在能翻转对方棋子的地方,没有棋子可以下那么需要让对方下。
主要还是黑白棋设计的代码部分和显示部分。
我用的是SPI和中断模拟。
显示8*8棋盘的代码:
void showtable()
{
step = 0;
cntb = 0;
cntw = 0;
int i = 0, j = 0;
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
f[i][j] = 0;
}
}
LCD_DrawLine(47, 95, 47, 225);
LCD_DrawLine(47, 95, 177, 95);
LCD_DrawLine(177, 95, 177, 225);
LCD_DrawLine(47, 225, 177, 225);
for (i = 96; i < 240; i += 16)
{
LCD_DrawLine(48, i, 176, i);
}
for (i = 48; i < 192; i += 16)
{
LCD_DrawLine(i, 96, i, 224);
}
gui_circle(104, 152, BLACK, 5, 1);
gui_circle(120, 152, BLACK, 5, 0);
gui_circle(104, 168, BLACK, 5, 0);
gui_circle(120, 168, BLACK, 5, 1);
f[4][4] = 1;
f[5][5] = 1;
f[5][4] = 2;
f[4][5] = 2;
gui_circle(120, 120, RED, 6, 0);
flag = 1;
}
判断是否可以下棋子的代码,可以下的话进行翻转:
int judge()
{
int a[]={
-1,-1,-1,0,0,1,1,1},b[]={
-1,0,1,-1,1,-1,0,1};
int i = 0, j = 0;
int kk = 0;
int isin = 0;
int cnt = 0;
int x;
int y;
cntb = 0;
cntw = 0;
if (flag == 1)
{
kk = 1;
}
else if (flag == 0)
{
kk = 2;
}
for (i = 0; i < 8; i++)
{
cnt = 0;
int flag1 = 0;
x = (startx - 8) / 16 - 2 + a[i];
y = (starty - 8) / 16 - 5 + b[i];
while (x > 0 && x < 9 && y > 0 && y < 9 && f[x][y] != kk && f[x][y] != 0)
{
flag1 = 1;
cnt++;
x += a[i];
y += b[i];
}
if (flag1 == 0)
{
continue;
}
if (x > 0 && x < 9 && y > 0 && y < 9 && f[x][y] == kk)
{
isin = 1;
int tx = (startx - 8) / 16 - 2 + a[i];
int ty = (starty - 8) / 16 - 5 + b[i];
if (kk == 1)
{
gui_circle(startx, starty, BLACK, 5, 1);
}
else if (kk == 2)
{
gui_circle(startx, starty, BLACK, 5, 0);
}
f[(startx - 8) / 16 - 2][(starty - 8) / 16 - 5] = kk;
for (j = 1; j <= cnt; j++)
{
int trux = startx + k * a[i] * j;
int truy = starty + k * b[i] * j;
f[tx][ty] = f[(startx - 8) / 16 - 2][(starty - 8) / 16 - 5];
if (f[tx][ty] == 1)
{
gui_circle(trux, truy, WHITE, 5, 1);
gui_circle(trux, truy, BLACK, 5, 1);
}
else if (f[tx][ty] == 2)
{
gui_circle(trux, truy, WHITE, 5, 1);
gui_circle(trux, truy, BLACK, 5, 0);
}
tx += a[i];
ty += b[i];
}
}
}
if (isin == 1)
{
for (i = 1; i <= 8; i++)
{
for (j = 1; j <= 8; j++)
{
if (f[i][j] == 1)
{
cntb++;
}
else if (f[i][j] == 2)
{
cntw++;
}
}
}
int blg = cntb % 10;
int bls = cntb / 10;
int whg = cntw % 10;
int whs = cntw / 10;
Show_Str(86,280,WHITE,0XFFFF,(u8 *)" ",16,0);
show(bls, 0);
show(blg, 10);
Show_Str(104,280,BLACK,0XFFFF,(u8 *)" : ",16,1);
show(whs, 40);
show(whg, 50);
}
return isin;
}
光标显示的代码:(需要把上一次的光标位置给清除掉
void cleartable()
{
gui_circle(startx, starty, WHITE, 6, 0);
if (f[(startx - 8) / 16 - 2][(starty - 8) / 16 - 5] == 1)
{
gui_circle(startx, starty, BLACK, 5, 1);
}
else if (f[(startx - 8) / 16 - 2][(starty - 8) / 16 - 5] == 2)
{
gui_circle(startx, starty, BLACK, 5, 0);
}
}
用中断实现的代码部分:(按钮按下对应光标移动)
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
/***CONFIRM***/
if(GPIO_Pin == GPIO_PIN_0)
{
if(0 == HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_0))
{
if (flag == 1)
{
//black
if (judge() == 1 || step >= 12)
{
step = 0;
flag = 0;
Show_Str(70,70,WHITE,0XFFFF,(u8 *)"Black go",16,1);
Show_Str(70,70,WHITE,0XFFFF,(u8 *)"White go",16,1);
Show_Str(70,70,BLACK,0XFFFF,(u8 *)"White go",16,1);
}
else if (judge() == 0)
{
step++;
Show_Str(70,240,BLACK,0XFFFF,(u8 *)"ILLEGAL STEP!",16,1);
Show_Str(70,240,WHITE,0XFFFF,(u8 *)"ILLEGAL STEP!",16,1);
}
}
else
{
//white
if (judge() == 1 || step >= 12)
{
step = 0;
flag = 1;
Show_Str(70,70,WHITE,0XFFFF,(u8 *)"Black go",16,1);
Show_Str(70,70,WHITE,0XFFFF,(u8 *)"White go",16,1);
Show_Str(70,70,BLACK,0XFFFF,(u8 *)"Black go",16,1);
}
else if (judge() == 0)
{
step++;
Show_Str(70,240,BLACK,0XFFFF,(u8 *)"ILLEGAL STEP!",16,1);
Show_Str(70,240,WHITE,0XFFFF,(u8 *)"ILLEGAL STEP!",16,1);
}
}
}
}
/***UP***/
if(GPIO_Pin == GPIO_PIN_4)
{
if(0 == HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_4))
{
cleartable();
starty = starty - k;
gui_circle(startx, starty, RED, 6, 0);
}
}
/***LEFT***/
if(GPIO_Pin == GPIO_PIN_5)
{
if(0 == HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_5))
{
cleartable();
startx = startx - k;
gui_circle(startx, starty, RED, 6, 0);
}
}
/***DOWN***/
if(GPIO_Pin == GPIO_PIN_6)
{
if(0 == HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_6))
{
cleartable();
starty = starty + k;
gui_circle(startx, starty, RED, 6, 0);
}
}
/***RIGHT***/
if(GPIO_Pin == GPIO_PIN_8)
{
if(0 == HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_8))
{
cleartable();
startx = startx + k;
gui_circle(startx, starty, RED, 6, 0);
}
}
}