在linux下ubuntu的五子棋游戏c语言代码
更新时间:2024-04-11 20:29:01 阅读量: 综合文库 文档下载
- Linux命令推荐度:
- 相关推荐
#include
//#include
#include
#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(\
正在阅读:
梦游青春作文800字06-25
点滴小事,从我做起作文500字06-29
人教版高中地理必修一教学设计-《4.1营造地表形态的力量》教案12-25
2018下学期班级期末复习 2013-2014学年第二学期宝安区期末调研测试卷02-01
河南省人民政府办公厅关于印发河南省城市集中式饮用水源保护区划04-03
心力衰竭患者的运动康复08-11
宽容作文800字07-15
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 五子棋
- 语言
- 代码
- ubuntu
- linux
- 游戏
- DELL R430服务器U盘安装操作系统指南
- 新五年级上册数学选择题
- 重庆市地面水域适用功能类别划分规定
- DT-1电梯检验员在线考试答案
- 非线性方程求解算法的程序设计及比对
- 武汉大学出版社科技分社书目2014秋(最终版)
- 工业设计史历年真题(93-2004)
- 大学桌游社团申请书
- 2017年湖南省株洲市中考物理试卷
- 关于新疆做好兵团重点营运车辆GPS联网联控工作
- 关于大学生养老观念及其更新调查报告
- 党政工团联席会议制度
- 医学统计学 练习题
- 华三交换机命令详解
- allegro焊盘制作
- 威斯敏斯特小要理问答
- 水电线管后开槽的修补施工方案
- 新南杨家哈大铁路立交特大桥上跨既有铁路(15#~16#墩) - 图文
- 沈阳工程学院-数据结构课设报告
- 西交《社区护理学》在线作业答案