在linux下ubuntu的五子棋游戏c语言代码

更新时间:2024-04-11 20:29:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

#include #include

//#include #include #include #include

#include #define FB_W 1024 #define FB_H 768

#define CHESSBOARD1 10 #define CHESSBOARD2 10 #define X_OFFSET 400 #define Y_OFFSET 200 #define X_CAPASITY 10 #define Y_CAPASITY 10 #define SPACE 40

#define RED 0x00ff0000 #define BLUE 0x0000ff00 #define WHITE 0xffffffff #define BLACK 0xff000000 #define NOKEY 0 #define RADIO 19 #define C_H 17 #define C_W 10

#define T___ 0x00ffffff #define X___ 0xff00ffff #define BORD 0xeeff0000 #define u32_t unsigned int

int array[CHESSBOARD1][CHESSBOARD2][1]; int board[CHESSBOARD1][CHESSBOARD2]; int play1[CHESSBOARD1][CHESSBOARD2][4]; int com[CHESSBOARD1][CHESSBOARD2][4]; int cursor_store[C_H][C_W]; struct fb_info{ int w; int h; int bpp; int *mem; }fb;

static u32_t cursor_pixel[C_H][C_W]= { {BORD,T___,T___,T___,T___,T___,T___,T___,T___,T___},

{BORD,BORD,T___,T___,T___,T___,T___,T___,T___,T___}, {BORD,X___,BORD,T___,T___,T___,T___,T___,T___,T___}, {BORD,X___,X___,BORD,T___,T___,T___,T___,T___,T___}, {BORD,X___,X___,X___,BORD,T___,T___,T___,T___,T___}, {BORD,X___,X___,X___,X___,BORD,T___,T___,T___,T___}, {BORD,X___,X___,X___,X___,X___,BORD,T___,T___,T___}, {BORD,X___,X___,X___,X___,X___,X___,BORD,T___,T___}, {BORD,X___,X___,X___,X___,X___,X___,X___,BORD,T___}, {BORD,X___,X___,X___,X___,X___,X___,X___,X___,BORD}, {BORD,X___,X___,X___,X___,X___,BORD,BORD,BORD,BORD}, {BORD,X___,X___,BORD,X___,X___,BORD,T___,T___,T___}, {BORD,X___,BORD,T___,BORD,X___,X___,BORD,T___,T___}, {BORD,BORD,T___,T___,BORD,X___,X___,BORD,T___,T___}, {T___,T___,T___,T___,T___,BORD,X___,X___,BORD,T___}, {T___,T___,T___,T___,T___,BORD,X___,X___,BORD,T___}, {T___,T___,T___,T___,T___,T___,BORD,BORD,T___,T___} };

int fb_open() {

int fd; int *mem;

fd = open(\ if(fd < 0) {

printf(\ return 1; }

struct fb_var_screeninfo fb_var;

ioctl(fd,FBIOGET_VSCREENINFO,&fb_var); fb.w = fb_var.xres; fb.h = fb_var.yres;

fb.bpp = fb_var.bits_per_pixel;

printf(\ h:%d bpp:%d\\n\fb.mem = mmap(NULL,FB_H*FB_W*fb_var.bits_per_pixel/8,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); //printf(\

return 0; }

void fb_pixel(int x,int y,int color) {

*(fb.mem + y*FB_W+x) = color;

}

void fb_drawline(int x0,int x1,int y,int color) { int i;

for(i = x0;i

fb_pixel(i,y,color); } }

void fb_drawretangle(int x0,int y0,int x1,int y1,int color) {

int i,j;

for(i = x0;i

fb_pixel(i,j,color); } }

void fb_drawline2(int x,int y0,int y1,int color) { int i;

for(i = y0;i

void fb_drawcycle(int x,int y,int r,int color) { int i,j;

for(i = -r;i

if(i*i+j*j <= r*r) {

fb_pixel(i+x,j+y,color); } } }

int get_mouse_info(int fd,char *buf) { int n;

n = read(fd,buf,8); return n; }

int save_cursor(int x,int y) { int i,j;

for(i = 0;i < C_H;i++) {

for(j = 0;j

cursor_store[i][j] = *(fb.mem + j + x+(i +y)*FB_W); }

return 0; }

int restore_cursor(int x,int y) {

int i,j;

for(i = 0;i

for(j = 0;j < C_W;j++)

*(fb.mem+j+x+(i+y)*FB_W)=cursor_store[i][j]; }

return 0; }

void drawmouse(int x,int y) { int i,j;

for(i = 0;i

for(j = 0;j

if(cursor_pixel[i][j] != T___)

fb_pixel(x+j,y+i,cursor_pixel[i][j]);

//printf(\} } }

int printboard() { int i;

fb_drawretangle(X_OFFSET - SPACE,Y_OFFSET - SPACE,X_OFFSET + SPACE *Y_CAPASITY,Y_OFFSET + SPACE * X_CAPASITY,BLUE);

for(i = 0;i < Y_CAPASITY;i++) { fb_drawline(X_OFFSET,X_OFFSET+SPACE*(Y_CAPASITY - 1),i * SPACE + Y_OFFSET,RED);

}

for(i = 0;i < X_CAPASITY;i++) {

fb_drawline2(i*SPACE+X_OFFSET,Y_OFFSET,Y_OFFSET+SPACE*(X_CAPASITY-1),RED); } }

void printqizi(int row,int col, int color) {

int x,y;

x = X_OFFSET + SPACE*col; y = Y_OFFSET + SPACE*row; fb_drawcycle(x,y,RADIO,color); }

struct node_t{ int x; int y; int z; };

//print the chessboard

void print(int board[CHESSBOARD1][CHESSBOARD2]) {

int i, j;

printf(\ printf(\ \

for (j = 0; j < CHESSBOARD2; j++) printf(\ printf(\ printf(\

for (j = 0; j < CHESSBOARD2; j++) printf(\ printf(\

for (i = 0; i < CHESSBOARD1; i++) { printf(\

for (j = 0; j < CHESSBOARD2; j++) { printf(\}

printf(\}

printf(\}

// 鍒濆鍖栨鐩?void init(int map[CHESSBOARD1][CHESSBOARD2]) {

int i,j;

for(i=0;i

void init2(int map[CHESSBOARD1][CHESSBOARD2][4]) {

int i,j,k;

for(i=0;i

for(j=0;j

void init3(int map[CHESSBOARD1][CHESSBOARD2][1]) {

int i , j;

for(i = 0;i

int detection(int map[CHESSBOARD1][CHESSBOARD2][4],int x,int y,int player) {

int i,j,k,nx,ny,counter = 0;

int dirx[4] = {0,1,1,1}; int diry[4] = {1,0,1,-1};

if(board[x][y]) {

return 0; }

for(i =0;i<4;i++) {

nx = x; ny = y;

while(board[nx+=dirx[i]][ny+=diry[i]] == player) counter++; nx =x; ny =y;

while(board[nx-=dirx[i]][ny-=diry[i]] == player) counter++;

switch(counter) { case 0: map[x][y][i] = 0;break; case 1: map[x][y][i] = 5;break; case 2: map[x][y][i] = 10;break; case 3: map[x][y][i] = 15;break; case 4: map[x][y][i] = 20;break; }

//printf(\ \ counter = 0; }

printf(\return 0; }

int get_max(int map[CHESSBOARD1][CHESSBOARD2][4]) {

int i,j,k; int max = 0;

for(i = 0;i

if(map[i][j][k] >max) max = map[i][j][k]; }

return max; }

struct node_t get_max2(int map[CHESSBOARD1][CHESSBOARD2][4]) {

int i,j,k;

struct node_t p; int max = 0,cur;

for(i = 0;i

for(j = 0;j

for(k = 0;k<4;k++) {

cur = map[i][j][k]; if(cur >max) {

max = cur; p.x = i; p.y = j; p.z = k; } } } }

return p; }

struct node_t get_max3(int map[CHESSBOARD1][CHESSBOARD2][4]) {

int i,j,k;

struct node_t p; int max = 0,cur;

for(i = 0;i

for(j = 0;j

for(k = 0;k<4;k++) {

cur = map[i][j][k]; if(cur >= max) {

max = cur; p.x = i; p.y = j; p.z = k; } } } }

return p; }

struct node_t get_max4(int map[CHESSBOARD1][CHESSBOARD2][1]) {

int i,j;

struct node_t p; int max = 0,cur;

for(i = 0;i

for(j = 0;j

cur = map[i][j][1]; if(cur >= max) {

max = cur; p.x = i; p.y = j; p.z = 1; } } }

return p; }

//鏌ョ湅鍧愭爣x y鏄惁鑳借蛋 int if_done(int x,int y) {

if(x>=CHESSBOARD1||x<0) return 0;

if(y>=CHESSBOARD2||y<0) return 0; return 1; }

void computer() {

int i,j,k; init2(play1); init2(com);

for(i = 0;i

for(j = 0;j

detection(play1,i,j,1); detection(com,i,j,2);

} } }

//鏌ョ湅妫嬬洏鐨勬瀛愭儏鍐垫湁鍑犱釜杩炰竴璧?int check(int x,int y,int board[CHESSBOARD1][CHESSBOARD2]) { int i = 0,j = 0,nextx,nexty; int counter = 1; int player; int directionx[4] = {0,1,1,1}; int directiony[4] = {1,0,1,-1}; player = board[x][y]; if(player == 0) return 0; for(i = 0;i<4;i++) { nextx = x; nexty = y; counter = 1; for(j = 1;j<5;j++) { nextx += directionx[i]; nexty +=directiony[i]; if(board[nextx][nexty] == player) counter++; } if(counter == 5) { return 1; } } return 0; }

void drawbg(int row,int col) {

int x,y;

x = X_OFFSET + SPACE*col; y = Y_OFFSET + SPACE*row; fb_drawcycle(x,y,RADIO,BLUE);

fb_drawline(x-RADIO,x+RADIO,y,RED); fb_drawline2(x,y-RADIO,y+RADIO,RED); }

int if_gameover(int board[CHESSBOARD1][CHESSBOARD2])

{

int i,j;

for(i = 0;i

if(check(i,j,board)) return 1; } return 0; }

void mouse_doing() {

int fd,step = 1;

int row,col,flag1=1; int max1,max2; char buf[8];

struct node_t p; int mx,my,a,b,c,d,e; mx = FB_W/2-100; my = FB_H/2; int n;

//drawmouse(mx,my);

fd = open(\if(fd < 0) {

printf(\ mouse error!\\n\}

save_cursor(mx,my); drawmouse(mx,my); while(flag1) {

if(read(fd,buf,8) >0) {

e = (buf[0]&0x07); c = 2*buf[1]; d = 2*buf[2]; if(e == NOKEY) {

restore_cursor(mx,my); mx += c; my -= d;

if(mx >= FB_W-17) mx=FB_W-17; if(mx < 0)

mx = 0;

if(my > FB_H-17) my = FB_H-17; if(my < 0) my = 0;

save_cursor(mx,my); drawmouse(mx,my); }

else if(e == 1) {

if(step%2==1) {

// printf(\

col =(int) (mx-X_OFFSET+SPACE/2)/SPACE; row = (int)(my-Y_OFFSET+SPACE/2)/SPACE; if(!(if_done(row,col))||board[row][col]) { //printf(\ !\\n\ col =(int) (mx-X_OFFSET)/SPACE;

row = (int)(my-Y_OFFSET)/SPACE; continue; }

board[row][col] = 1; restore_cursor(mx,my);

printqizi(row,col,BLACK);//鐢婚粦鑹茬殑妫嬪瓙 save_cursor(mx, my); array[row][col][1] = step; step ++;

computer();//鏇存柊浼樺厛绾ц〃 // printf(\ //flag2 = 1;

} else {

// printf(\

max1 = get_max(play1); max2 = get_max(com); if(max1 >= max2) {

p = get_max2(play1); } else{

p = get_max2(com); }

row = p.x; col = p.y; if(!(if_done(row,col))||board[row][col]) { // printf(\ max1 = get_max(play1); max2 = get_max(com); if(max1 >= max2) { p = get_max3(play1); } else{ p = get_max3(com); } row = p.x; col = p.y; continue; }

board[row][col] = 2; computer(board); restore_cursor(mx,my);

printqizi(row,col,WHITE); save_cursor(mx, my); array[row][col][1] = step; step++; //flag2 = 0;

}

if(if_gameover(board)) flag1=0; } else if(e == 2) {

if(step != 1) { p = get_max4(array);

!\\n\ row = p.x; col = p.y; board[row][col] = 0; array[row][col][1] = 0; computer(board); drawbg(row,col); //restore_cursor(mx,my); step--; } } } }

if(board[row][col] ==1)

printf(\ play1 win!\\n\ else

printf(\ }

void mouse_doing2() {

int fd,step = 1;

int row,col,flag1=1; int max1,max2; char buf[8];

struct node_t p; int mx,my,a,b,c,d,e; mx = FB_W/2-100; my = FB_H/2; int n;

//drawmouse(mx,my);

fd = open(\if(fd < 0) {

printf(\ mouse error!\\n\}

save_cursor(mx,my); drawmouse(mx,my); while(flag1) {

if(read(fd,buf,8) >0) {

e = (buf[0]&0x07);

c = 2*buf[1]; d = 2*buf[2]; if(e == NOKEY) {

restore_cursor(mx,my); mx += c; my -= d;

if(mx >= FB_W-17) mx=FB_W-17; if(mx < 0) mx = 0;

if(my > FB_H-17) my = FB_H-17; if(my < 0) my = 0;

save_cursor(mx,my); drawmouse(mx,my); }

else if(e == 1) {

if(step%2==1) {

// printf(\

col =(int) (mx-X_OFFSET+SPACE/2)/SPACE; row = (int)(my-Y_OFFSET+SPACE/2)/SPACE; if(!(if_done(row,col))||board[row][col]) { //printf(\ !\\n\ col =(int) (mx-X_OFFSET)/SPACE;

row = (int)(my-Y_OFFSET)/SPACE; continue; }

board[row][col] = 1; restore_cursor(mx,my);

printqizi(row,col,BLACK);//鐢婚粦鑹茬殑妫嬪瓙 save_cursor(mx, my); array[row][col][1] = step; step ++;

//computer();//鏇存柊浼樺厛绾ц〃 // printf(\

本文来源:https://www.bwwdw.com/article/s7cp.html

Top