📄 chess.c
字号:
/****************************************************************************
Copyright (c) 深圳市民德电子科技有限公司 http://www.szminde.cn
文件名称:chess.c
版本号: 1.0
日期: 2005-05-25
说明: 此文件包含一个井字棋游戏的程序。
调用列表:sys_LCD.c
sys_TchScr.c
subfunctions.c
修改历史:--
****************************************************************************/
#include "def.h"
#include "44b.h"
#include "sys_LCD.h"
#include "sys_TchScr.h"
#include "bmp2array.h"
/*延时函数*/
extern void Delay(int time);
extern void Beep(int time);
/****************************************************************************
函数名称:chess()
说明: 井字棋程序。
调用函数:LCD_ChangeMode()
ClearScr()
ShowBmp()
Delay()
DrawLine()
FillCircle()
LCD_Touch()
Beep()
输入参数:无
输出参数:无
****************************************************************************/
void chess(void)
{
int x,y,whowins=0;
BOOLEAN gameover=TRUE,player=FALSE; // player=FALSE相当"O"玩棋
int chessboard[9]={0};
int chesscount=0;
BOOLEAN chessdown=FALSE;
int i;
rINTMSK=BIT_GLOBAL;
LCD_ChangeMode(DspGraMode);
for(;;)
{
if(TRUE==gameover)
{
gameover=FALSE;
player=FALSE;
chesscount=0;
chessdown=FALSE;
whowins=0;
for(i=0;i<9;i++)
chessboard[i]=0;
ClearScr();
ShowBmp(60,70,title,200,100);
Delay(6000);
ClearScr();
DrawRect(40,0,279,239,15);
DrawRect(41,1,278,238,15);
DrawRect(42,2,277,237,15);
DrawLine(119,2,119,237,15);
DrawLine(120,2,120,237,15);
DrawLine(121,2,121,237,15);
DrawLine(198,2,198,237,15);
DrawLine(199,2,199,237,15);
DrawLine(200,2,200,237,15);
DrawLine(42,79,277,79,15);
DrawLine(42,80,277,80,15);
DrawLine(42,81,277,81,15);
DrawLine(42,158,277,158,15);
DrawLine(42,159,277,159,15);
DrawLine(42,160,277,160,15);
ShowBmp(0,0,startagain,36,80);
ShowBmp(0,160,quit,36,80);
}
if(FALSE==player)
ShowBmp(284,80,nexto,36,160);
else ShowBmp(284,80,nextx,36,160);
chessdown=FALSE;
LCD_Touch(&x,&y);
if(y!=0)
{
switch(x/40+(y/80)*8)
{
case 0: // 重新开始游戏
gameover=TRUE;
Beep(500);
break;
case 1:
case 2:
if(0==chessboard[0])
{
if(FALSE==player)
{
chessboard[0]=1; // 1="O"
FillCircle(81,41,36,15);
player=TRUE;
}
else
{
chessboard[0]=2; // 2="X"
DrawLine(44,4,117,77,15);
DrawLine(44,5,116,77,15);
DrawLine(45,4,117,76,15);
DrawLine(117,4,44,77,15);
DrawLine(117,5,45,77,15);
DrawLine(116,4,44,76,15);
player=FALSE;
}
chessdown=TRUE;
chesscount++;
Beep(500);
}
break;
case 3:
case 4:
if(0==chessboard[1])
{
if(FALSE==player)
{
chessboard[1]=1; // 1="O"
FillCircle(160,41,36,15);
player=TRUE;
}
else
{
chessboard[1]=2; // 2="X"
DrawLine(123,4,196,77,15);
DrawLine(123,5,195,77,15);
DrawLine(124,4,196,76,15);
DrawLine(196,4,123,77,15);
DrawLine(196,5,124,77,15);
DrawLine(195,4,123,76,15);
player=FALSE;
}
chessdown=TRUE;
chesscount++;
Beep(500);
}
break;
case 5:
case 6:
if(0==chessboard[2])
{
if(FALSE==player)
{
chessboard[2]=1; // 1="O"
FillCircle(239,41,36,15);
player=TRUE;
}
else
{
chessboard[2]=2; // 2="X"
DrawLine(202,4,275,77,15);
DrawLine(202,5,274,77,15);
DrawLine(203,4,275,76,15);
DrawLine(275,4,202,77,15);
DrawLine(275,5,203,77,15);
DrawLine(274,4,202,76,15);
player=FALSE;
}
chessdown=TRUE;
chesscount++;
Beep(500);
}
break;
case 9:
case 10:
if(0==chessboard[3])
{
if(FALSE==player)
{
chessboard[3]=1; // 1="O"
FillCircle(81,120,36,15);
player=TRUE;
}
else
{
chessboard[3]=2; // 2="X"
DrawLine(44,83,117,156,15);
DrawLine(44,84,116,156,15);
DrawLine(45,83,117,155,15);
DrawLine(117,83,44,156,15);
DrawLine(117,84,45,156,15);
DrawLine(116,83,44,155,15);
player=FALSE;
}
chessdown=TRUE;
chesscount++;
Beep(500);
}
break;
case 11:
case 12:
if(0==chessboard[4])
{
if(FALSE==player)
{
chessboard[4]=1; // 1="O"
FillCircle(160,120,36,15);
player=TRUE;
}
else
{
chessboard[4]=2; // 2="X"
DrawLine(123,83,196,156,15);
DrawLine(123,84,195,156,15);
DrawLine(124,83,196,155,15);
DrawLine(196,83,123,156,15);
DrawLine(196,84,124,156,15);
DrawLine(195,83,123,155,15);
player=FALSE;
}
chessdown=TRUE;
chesscount++;
Beep(500);
}
break;
case 13:
case 14:
if(0==chessboard[5])
{
if(FALSE==player)
{
chessboard[5]=1; // 1="O"
FillCircle(239,120,36,15);
player=TRUE;
}
else
{
chessboard[5]=2; // 2="X"
DrawLine(202,83,275,156,15);
DrawLine(202,84,274,156,15);
DrawLine(203,83,275,155,15);
DrawLine(275,83,202,156,15);
DrawLine(275,84,203,156,15);
DrawLine(274,83,202,155,15);
player=FALSE;
}
chessdown=TRUE;
chesscount++;
Beep(500);
}
break;
case 17:
case 18:
if(0==chessboard[6])
{
if(FALSE==player)
{
chessboard[6]=1; // 1="O"
FillCircle(81,199,36,15);
player=TRUE;
}
else
{
chessboard[6]=2; // 2="X"
DrawLine(44,162,117,235,15);
DrawLine(44,163,116,235,15);
DrawLine(45,162,117,234,15);
DrawLine(117,162,44,235,15);
DrawLine(117,163,45,235,15);
DrawLine(116,162,44,234,15);
player=FALSE;
}
chessdown=TRUE;
chesscount++;
Beep(500);
}
break;
case 19:
case 20:
if(0==chessboard[7])
{
if(FALSE==player)
{
chessboard[7]=1; // 1="O"
FillCircle(160,199,36,15);
player=TRUE;
}
else
{
chessboard[7]=2; // 2="X"
DrawLine(123,162,196,235,15);
DrawLine(123,163,195,235,15);
DrawLine(124,162,196,234,15);
DrawLine(196,162,123,235,15);
DrawLine(196,163,124,235,15);
DrawLine(195,162,123,234,15);
player=FALSE;
}
chessdown=TRUE;
chesscount++;
Beep(500);
}
break;
case 21:
case 22:
if(0==chessboard[8])
{
if(FALSE==player)
{
chessboard[8]=1; // 1="O"
FillCircle(239,199,36,15);
player=TRUE;
}
else
{
chessboard[8]=2; // 2="X"
DrawLine(202,162,275,235,15);
DrawLine(202,163,274,235,15);
DrawLine(203,162,275,234,15);
DrawLine(275,162,202,235,15);
DrawLine(275,163,203,235,15);
DrawLine(274,162,202,234,15);
player=FALSE;
}
chessdown=TRUE;
chesscount++;
Beep(500);
}
break;
case 16:
ClearScr();
LCD_ChangeMode(DspTxtMode);
rINTMSK=~(BIT_GLOBAL|BIT_TICK);
return;
break;
}
}
if(chessdown) // 判断胜负
{
if((chessboard[0]!=0)&&(chessboard[0]==chessboard[1])
&&(chessboard[0]==chessboard[2]))
{
if(1==chessboard[0])
whowins=1; // "O"胜
else if(2==chessboard[0])
whowins=2; // "X"胜
}
else if((chessboard[0]!=0)&&(chessboard[0]==chessboard[4])
&&(chessboard[0]==chessboard[8]))
{
if(1==chessboard[0])
whowins=1; // "O"胜
else if(2==chessboard[0])
whowins=2; // "X"胜
}
else if((chessboard[0]!=0)&&(chessboard[0]==chessboard[3])
&&(chessboard[0]==chessboard[6]))
{
if(1==chessboard[0])
whowins=1; // "O"胜
else if(2==chessboard[0])
whowins=2; // "X"胜
}
else if((chessboard[3]!=0)&&(chessboard[3]==chessboard[4])
&&(chessboard[3]==chessboard[5]))
{
if(1==chessboard[3])
whowins=1; // "O"胜
else if(2==chessboard[3])
whowins=2; // "X"胜
}
else if((chessboard[6]!=0)&&(chessboard[6]==chessboard[4])
&&(chessboard[6]==chessboard[2]))
{
if(1==chessboard[6])
whowins=1; // "O"胜
else if(2==chessboard[6])
whowins=2; // "X"胜
}
else if((chessboard[6]!=0)&&(chessboard[6]==chessboard[7])
&&(chessboard[6]==chessboard[8]))
{
if(1==chessboard[6])
whowins=1; // "O"胜
else if(2==chessboard[6])
whowins=2; // "X"胜
}
else if((chessboard[1]!=0)&&(chessboard[1]==chessboard[4])
&&(chessboard[1]==chessboard[7]))
{
if(1==chessboard[1])
whowins=1; // "O"胜
else if(2==chessboard[1])
whowins=2; // "X"胜
}
else if((chessboard[2]!=0)&&(chessboard[2]==chessboard[5])
&&(chessboard[2]==chessboard[8]))
{
if(1==chessboard[2])
whowins=1; // "O"胜
else if(2==chessboard[2])
whowins=2; // "X"胜
}
}
if(chessdown&&(whowins==0)) // 判断是否和棋
{
if(9==chesscount)
whowins=3; // 和棋
}
if(whowins!=0)
{
if(1==whowins)
ShowBmp(60,70,owins,200,100);
else if(2==whowins)
ShowBmp(60,70,xwins,200,100);
else if(3==whowins)
ShowBmp(60,70,draw,200,100);
for(i=0;i<9;i++)
chessboard[i]=3;
FillRect(284,80,319,239,15);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -