#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include "fb_draw.h"
static int x, y;
int crr[10][10]={ //显示一个囧字
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,1,0,0,1},
{1,0,1,0,0,0,0,1,0,1},
{1,1,0,0,0,0,0,0,1,1},
{1,0,1,1,1,1,1,1,0,1},
{1,0,1,0,0,0,0,1,0,1},
{1,0,1,0,0,0,0,1,0,1},
{1,0,1,1,1,1,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
int arr[1][1]={1};
int brr[10][10]={0};
int drr[10][10]={0};
void fb_draw_element(int a, int b, int color)
{
// int w = W / WC;
// int h = H / HC;
int xs = xres() / 2 - W / 2 + a * 40;
int ys = yres() / 2 - H / 2 + b * 40;
int i, j;
for (j = ys; j <= ys + 40; j++) {
for (i = xs; i <= xs + 40; i++) {
if(j==ys||j==ys+40||i==xs||i==xs+40)
fb_draw_point(i, j, color);
}
}
}
void draw_shape(int color)
{
int i, j;
for (j = 0; j < 1; j++){
for (i = 0; i < 1; i++) {
if (arr[j][i])
fb_draw_element(i + x, j + y, color);
}
}
}
void fb_draw_element1(int a,int b,int color)
{
int w = W / WC;
int h = H / HC;
int xs = xres() / 2 - W / 2 + a * 40;
int ys = yres() / 2 - H / 2 + b * 40;
int i, j;
for (j = ys+10; j <= ys + 30; j++) {
for (i = xs+10; i <= xs + 30; i++) {
fb_draw_point(i, j, color);
}
}
}
void fb_draw_element2(int a,int b,int color)
{
int w = W / WC;
int h = H / HC;
int xs = xres() / 2 - W / 2 + a * 40;
int ys = yres() / 2 - H / 2 + b * 40;
int i, j;
for (j = ys+10; j <= ys + 30; j++) {
for (i = xs+10; i <= xs + 30; i++) {
fb_draw_point(i, j, color);
}
}
}
void draw_shape1(int color)
{
int i, j;
for (j = 0; j < 10; j++){
for (i = 0; i < 10; i++) {
if (brr[j][i])
fb_draw_element1(i + x, j + y, color);
}
}
}
void draw_are()
{
int w = W / WC;
int h = H / HC;
int xs = xres() / 2 - W / 2 ;
int ys = yres() / 2 - H / 2 ;
int i, j;
for (j = ys-h; j <= ys + H +h; j++) {//画大边框
for (i = xs-w; i <= xs + W +w; i++) {
if((j>=ys-h&&j<=ys-1)||(j>=ys+H+1&&j<=ys+H+h))
fb_draw_point(i, j,FC );
if((i>=xs-w&&i<=xs-1)||(i>=xs+W+1&&i<=xs+W+w))
fb_draw_point(i,j,FC);
}
}
for(j=ys+20;j<=ys+H;j+=40)//画横行直线
{
for(i=xs+20;i<=xs+W-20;i+=1)
{
fb_draw_point(i,j,FC);
}
}
for(j=ys+20;j<=ys+H-20;j+=1)//画竖行直线
{
for(i=xs+20;i<=xs+W;i+=40)
{
fb_draw_point(i,j,FC);
}
}
}
int left_break() //zuo bian jie yue jie pan duan
{
int tmp=5;
int i,j;
for(j=0;j<1;j++)
{
for(i=0;i<1;i++)
{
if(arr[j][i]==1)
{
if(tmp>i)//qu man zu zhe yi tiao jian de zui xiao zhi
tmp=i;
}
}
}
if(tmp+x>0)
return 1;
return 0;
}
int right_break() //you bian jie yue jie
{
int tmp=0;
int i,j;
for(j=0;j<1;j++)
{
for(i=0;i<1;i++)
{
if(arr[j][i]==1)
{
if(tmp<i)//qu man zu zhe yi tiao jian de zui da zhi
tmp=i;
}
}
}
if(tmp+x<9)
return 1;
return 0;
}
int low_break()//di xia bian jie pan duan
{
int tmp=0;
int i,j;
for(j=0;j<1;j++)
{
for(i=0;i<1;i++)
{
if(arr[j][i]==1)
{
if(tmp<j)
tmp=j;
}
}
}
if(tmp+y<=8)
return 1;
return 0;
}
int top_break()
{
int tmp=0;
int i,j;
for(j=0;j<1;j++)
{
for(i=0;i<1;i++)
{
if(arr[j][i]==1)
{
if(tmp>j)
tmp=j;
}
}
}
if(tmp+y>0)
return 1;
return 0;
}
int have()
{
if(brr[x][y]==1||drr[x][y]==1)
{
return 0;
}
return 1;
}
int judge()
{
int i=0;
int j=0;
int flag_1=0;
int flag_2=0;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(brr[i][j]==1&&brr[i][j+1]==1&&brr[i][j+2]==1&&brr[i][j+3]==1&&brr[i][j+4]==1)
{
return 1;
}
if(brr[j][i]==1&&brr[j+1][i]==1&&brr[j+2][i]==1&&brr[j+3][i]==1&&brr[j+4][i]==1)
{
return 1;
}
if(brr[i][j]==1&&brr[i+1][j+1]==1&&brr[i+2][j+2]==1&&brr[i+3][j+3]==1&&brr[i+4][j+4]==1)
{
return 1;
}
if(brr[i][j]==1&&brr[i+1][j-1]==1&&brr[i+2][j-2]==1&&brr[i+3][j-3]==1&&brr[i+4][j-4]==1)
{
return 1;
}
if(drr[i][j]==1&&drr[i][j+1]==1&&drr[i][j+2]==1&&drr[i][j+3]==1&&drr[i][j+4]==1)
{
return 2;
}
if(drr[j][i]==1&&drr[j+1][i]==1&&drr[j+2][i]==1&&drr[j+3][i]==1&&drr[j+4][i]==1)
{
return 2;
}
if(drr[i][j]==1&&drr[i+1][j+1]==1&&drr[i+2][j+2]==1&&drr[i+3][j+3]==1&&drr[i+4][j+4]==1)
{
return 2;
}
if(drr[i][j]==1&&drr[i+1][j-1]==1&&drr[i+2][j-2]==1&&drr[i+3][j-3]==1&&drr[i+4][j-4]==1)
{
return 2;
}
}
}
}
int main(void)
{
int color_flag=0;
int fd;
char ch;
int f_1;
system("clear");
fd = fb_open();
init_keyboard();
while(1)
{
draw_are();
draw_shape(BC);
ch=getchar();
if(ch=='\n'&&color_flag==0&&have())
{
brr[x][y]=1;
fb_draw_element1(x,y,FC);
f_1=judge();
if(f_1==1)
{
printf("blue win!\n");
fb_close();//关闭打开的文件
recover_keyboard();//将键盘设置成阻塞
return 0;
}
if(f_1==2)
{
printf("red win!\n");
fb_close();//关闭打开的文件
recover_keyboard();//将键盘设置成阻塞
return 0;
}
color_flag=(color_flag+1)%2;
ch='k';
}
if(ch=='\n'&&color_flag==1&&have())
{
drr[x][y]=1;
fb_draw_element2(x,y,BC);
f_1=judge();
if(f_1==1)
{
printf("blue win!\n");
fb_close();//关闭打开的文件
recover_keyboard();//将键盘设置成阻塞
return 0;
}
if(f_1==2)
{
printf("red win!\n");
fb_close();//关闭打开的文件
recover_keyboard();//将键盘设置成阻塞
return 0;
}
color_flag=(color_flag+1)%2;
ch='k';
}
if(ch=='a')//左移
{
if(left_break())
{
draw_shape(0x00);
x--;
draw_shape(BC);
}
}
if(ch=='d')//右移
{
if(right_break())
{
draw_shape(0x00);
x++;
draw_shape(BC);
}
}
if(ch=='s')//下移
{
if(low_break())
{
draw_shape(0x00);
y++;
draw_shape(BC);
}
}
if(ch=='w')//下移
{
if(top_break())
{
draw_shape(0x00);
y--;
draw_shape(BC);
}
}
if(ch=='q')
{
fb_close();//关闭打开的文件
recover_keyboard();//将键盘设置成阻塞
return -1;
}
}
fb_close();//关闭打开的文件
recover_keyboard();//将键盘设置成阻塞
return 0;
}