C语言——LCD触摸屏井字棋

使用C语言编写LCD触摸屏井字棋

效果如图
C语言——LCD触摸屏井字棋_第1张图片
C语言——LCD触摸屏井字棋_第2张图片

全局变量

int player_counter = 1;//*用于记录落子个数
int data[3][3]={{2,2,2},{2,2,2},{2,2,2}};//*棋盘

井字棋主函数

该函数在工程中为LCD触摸屏触摸的回调函数
LCD像素为240*240

//e->pos.x和 e->pos.y为触摸屏坐标 
int ontouch(int e->pos.x,int e->pos.y)//需要传入触摸坐标
{
    extern void game_char(int flag);

    int over_flag = 2;//*游戏结束标志位,0和1代表不同玩家获胜,2游戏继续进行
    int player;
    player_counter++;//*落子计数器
    player = player_counter % 2;//*玩家判断

    if(player_counter == 11)//*重启判定
    {
        game_reset();
        ui_show_main(PAGE_41);//*重载游戏页面
        return 0;
    }
    //*******************第一行落子判断*********************************
    if(e->pos.y > 0 && e->pos.y < 80 )
    {
        if(e->pos.x > 160 && e->pos.x < 240 && data[0][2] == 2)//*第1行第3个
        {
            data[0][2] = player;
            player_draw(player, 200, 40);
            over_flag = game_referee(player,0,2);
        }
        else if(e->pos.x > 80 && e->pos.x < 160 && data[0][1] == 2)//*第1行第2个
        {
            data[0][1] = player;
            player_draw(player, 120, 40);
            over_flag = game_referee(player,0,1);
        }
        else if(e->pos.x > 0 && e->pos.x < 80 && data[0][0] == 2)//*第1行第1个
        {
            data[0][0] = player;
            player_draw(player, 40, 40);
            over_flag = game_referee(player,0,0);
        }
        else
        {
            player_counter--;
        }
    }
    //*****************************第二行落子判断**************************************
    else if(e->pos.y > 80 && e->pos.y < 160 )
    {
        if(e->pos.x > 160 && e->pos.x < 240 && data[1][2] == 2)//*第2行第3个
        {
            data[1][2] = player;
            player_draw(player, 200, 120);
            over_flag = game_referee(player,1,2);
        }
        else if(e->pos.x > 80 && e->pos.x < 160 && data[1][1] == 2)//*第2行第2个
        {
            data[1][1] = player;
            player_draw(player, 120, 120);
            over_flag = game_referee(player,1,1);
        }
        else if(e->pos.x > 0 && e->pos.x < 80 && data[1][0] == 2)//*第2行第1个
        {
            data[1][0] = player;
            player_draw(player, 40, 120);
            over_flag = game_referee(player,1,0);
        }
        else
        {
            player_counter--;
        }
    }
    //****************************第三行落子判断**************************************
    else if(e->pos.y > 160 && e->pos.y < 240)//*
    {
        if(e->pos.x > 160 && e->pos.x < 240 && data[2][2] == 2)//*第3行第3个
        {
            data[2][2] = player;
            player_draw(player, 200, 200);
            over_flag = game_referee(player,2,2);
        }
        else if(e->pos.x > 80 && e->pos.x < 160 && data[2][1] == 2)//*第3行第2个
        {
            data[2][1] = player;
            player_draw(player, 120, 200);
            over_flag = game_referee(player,2,1);
        }
        else if(e->pos.x > 0 && e->pos.x < 80 && data[2][0] == 2)//*第3行第1个
        {
            data[2][0] = player;
            player_draw(player, 40, 200);
            over_flag = game_referee(player,2,0);
        }
        else
        {
            player_counter--;
        }
    }

    //**********************************************************
    if(over_flag == 0)//*玩家x获胜
    {
        game_char(2);//*输出字符
        player_counter = 10;

    }
    else if(over_flag == 1)//*玩家o获胜
    {
        game_char(1);//*输出字符
        player_counter = 10;

    }
    else if(player_counter == 10)//*平局
    {
        game_char(0);//*输出字符
        //player_counter += 1;
    }
}

游戏复位

用于玩家获胜或平局时重置棋盘

void game_reset(void)//*游戏复位
{
    int i,j;
    player_counter = 1;
    for(i=0;i<3;i++)
        {
        for(j=0;j<3;j++)
        {
            data[i][j] = 2;
        }
    }
}

玩家落子

在LCD屏幕上绘制棋子

void player_draw(int player, int x, int y)//*玩家落子
{
    extern int lcd_draw_fork(int x,int y,int r,u16 color);
    extern int lcd_draw_circular(int x,int y,int r,u16 color);
    if(player == 1)
    {
        lcd_draw_fork(x,y,25,0x9999);//*触摸点画叉
    }
    else if(player == 0)
    {
       lcd_draw_circular(x,y,25,0x6666);//*触摸点画圆
    }
}

胜负判断

根据落子坐标判断胜负,返回0和1代表不同玩家获胜,2无人获胜

int game_referee(int player,int x,int y)
{
    int xay = x+y;
    if(data[x][0]==player && data[x][1]==player && data[x][2]==player)//*所在行检测
    {
        return player;
    }
    else if(data[0][y]==player && data[1][y]==player && data[2][y]==player)//*所在列检测
    {
        return player;
    }
    else if(xay!=3 || xay!=5)//*斜检测
    {
        if(data[0][0]==player && data[1][1]==player && data[2][2]==player)
            {
                return player;
            }
        else if(data[0][2]==player && data[1][1]==player && data[2][0]==player)
            {
                return player;
            }
    }
    return 2;

}

你可能感兴趣的:(c语言,单片机,开发语言)