您的位置:

c语言五子棋图形库,c语言五子棋棋盘

本文目录一览:

怎样用C语言编一个简单的五子棋游戏?

#include graphics.h

#include stdio.h

#include MATH.H

IMAGE* IMG;

IMAGE* IMG2;

IMAGE* IMG3;

IMAGE* whole;

bool mark = false;

int x = 0, y = 0;

int flag[15][15];

void show()

{

outtextxy(550, 100, "白方:");

outtextxy(550, 150, " 箭头移动");

outtextxy(550, 200, " 回车键落子");

outtextxy(550, 250, "黑方:");

outtextxy(550, 300, " ADWS移动");

outtextxy(550, 350, " 空格键落子");

}

int success1(int dir1, int dir2)

{

int number = 0;

int temp_x = x, temp_y = y;

while (((temp_x / 35 + dir1) = 0 (temp_x / 35 + dir1) 15) ((temp_y / 35 + dir2) = 0 (temp_y / 35 + dir2) 15) (flag[(temp_x / 35 + dir1)][(temp_y / 35 + dir2)] == 1))

{

temp_x = temp_x + dir1 * 35;

temp_y = temp_y + dir2 * 35;

++number;

}

return number;

}

int success2(int dir1, int dir2)

{

int number = 0;

int temp_x = x, temp_y = y;

while (((temp_x / 35 + dir1) = 0 (temp_x / 35 + dir1) 15) ((temp_y / 35 + dir2) = 0 (temp_y / 35 + dir2) 15) (flag[(temp_x / 35 + dir1)][(temp_y / 35 + dir2)] == 2))

{

temp_x = temp_x + dir1 * 35;

temp_y = temp_y + dir2 * 35;

++number;

}

return number;

}

int success1()

{

int number = 0;

number = success1(0, -1) + success1(0, 1);//上下

if (number 4)

{

number = success1(-1, 0) + success1(1, 0);//左右

if (number 4)

{

number = success1(-1, -1) + success1(1, 1);//左上右下

if (number 4)

{

number = success1(-1, 1) + success1(1, -1);//左下右上

}

}

}

return number;

}

int success2()

{

int number = 0;

number = success2(0, -1) + success2(0, 1);//上下

if (number 4)

{

number = success2(-1, 0) + success2(1, 0);//左右

if (number 4)

{

number = success2(-1, -1) + success2(1, 1);//左上右下

if (number 4)

{

number = success2(-1, 1) + success2(1, -1);//左下右上

}

}

}

return number;

}

void control()

{

char key = 0;

while (key != 27)

{

Sleep(10);

if (kbhit())

{

key = getch();

switch (key)

{

case VK_LEFT:

if (mark)

break;

if (x 0)

x = x - 35;

break;

case 'a':

case 'A':

if (!mark)

break;

if (x 0)

x = x - 35;

break;

case VK_RIGHT:

if (mark)

break;

if (x 490)

x = x + 35;

break;

case 'd':

case 'D':

if (!mark)

break;

if (x 490)

x = x + 35;

break;

case VK_UP:

if (mark)

break;

if (y 0)

y = y - 35;

break;

case 'w':

case 'W':

if (!mark)

break;

if (y 0)

y = y - 35;

break;

case VK_DOWN:

if (mark)

break;

if (y 490)

y = y + 35;

break;

case 's':

case 'S':

if (!mark)

break;

if (y 490)

y = y + 35;

break;

case VK_RETURN:

if (mark)

break;

if (flag[x / 35][y / 35] == 0)

{

putimage(whole, x + 6, y + 6, 31, 32, IMG2, 0, 0);

flag[x / 35][y / 35] = 1;

if (success1() = 4)

{

outtextxy(600, 50, "黑方 胜!");

key = 27;

}

mark = true;

}

break;

case VK_SPACE:

if (!mark)

break;

if (flag[x / 35][y / 35] == 0)

{

putimage(whole, x + 6, y + 6, 31, 31, IMG3, 0, 0);

flag[x / 35][y / 35] = 2;

if (success2() = 4)

{

outtextxy(600, 50, "白方 胜!");

key = 27;

}

mark = false;

}

break;

default:

break;

}

putimage(0, 0, whole);

putimage_transparent(NULL, IMG, x + 20, y + 20, 0x0, 0, 0, 20, 20);

}

}

}

void main()

{

setinitmode(0);

initgraph(800, 538);

SetWindowText(GetHWnd(), "五子棋20110327");

setcolor(0xffffff);

setfont(36, 0, "楷体_GB2312");

IMAGE* IMG1 = new IMAGE;

getimage(IMG1, "JPG", MAKEINTRESOURCE(102));//棋盘

putimage(0, 0, IMG1);

IMG2 = new IMAGE;

getimage(IMG2, "JPG", MAKEINTRESOURCE(103));//黑棋

IMG3 = new IMAGE;

getimage(IMG3, "JPG", MAKEINTRESOURCE(104));//白棋

IMG = new IMAGE;

getimage(IMG, "GIF", MAKEINTRESOURCE(101));//手形

whole = new IMAGE;

getimage(whole, 0, 0, 537, 537);

putimage_transparent(NULL, IMG, x + 20, y + 20, 0x0, 0, 0, 20, 20);

show();

control();

delete IMG1;

delete IMG2;

delete IMG3;

delete whole;

getch();

getch();

closegraph();

}

C语言五子棋求助

先求一下采纳

拿到你的代码放入VS进行调试

graphics图形库中每个函数都报出0xC0000005错误:读取0x00000000处发生访问冲突,

其实看到这个错误问题已经很明显了:你的程序操纵了一个空指针

查看graphics的函数发现initgraph 其返回一个窗口句柄(HWND)

在main函数开始时加入函数:

initgraph(640,320);//640为窗口的宽 320为高

ctrl+F5 完美运行

求C语言编写的五子棋程序。

#includestdio.h

#includestdlib.h

#includegraphics.h

#includebios.h

#includeconio.h

#define LEFT 0x4b00

#define RIGHT 0x4d00

#define DOWN 0x5000

#define UP 0x4800

#define ESC 0x011b

#define SPACE 0x3920

#define BILI 20

#define JZ 4

#define JS 3

#define N 19

int box[N][N];

int step_x,step_y ;

int key ;

int flag=1 ;

void draw_box();

void draw_cicle(int x,int y,int color);

void change();

void judgewho(int x,int y);

void judgekey();

int judgeresult(int x,int y);

void attentoin();

void attention()

{

char ch ;

window(1,1,80,25);

textbackground(LIGHTBLUE);

textcolor(YELLOW);

clrscr();

gotoxy(15,2);

printf("游戏操作规则:");

gotoxy(15,4);

printf("Play Rules:");

gotoxy(15,6);

printf("1、按左右上下方向键移动棋子");

gotoxy(15,8);

printf("1. Press Left,Right,Up,Down Key to move Piece");

gotoxy(15,10);

printf("2、按空格确定落棋子");

gotoxy(15,12);

printf("2. Press Space to place the Piece");

gotoxy(15,14);

printf("3、禁止在棋盘外按空格");

gotoxy(15,16);

printf("3. DO NOT press Space outside of the chessboard");

gotoxy(15,18);

printf("你是否接受上述的游戏规则(Y/N)");

gotoxy(15,20);

printf("Do you accept the above Playing Rules? [Y/N]:");

while(1)

{

gotoxy(60,20);

ch=getche();

if(ch=='Y'||ch=='y')

break ;

else if(ch=='N'||ch=='n')

{

window(1,1,80,25);

textbackground(BLACK);

textcolor(LIGHTGRAY);

clrscr();

exit(0);

}

gotoxy(51,12);

printf(" ");

}

}

void draw_box()

{

int x1,x2,y1,y2 ;

setbkcolor(LIGHTBLUE);

setcolor(YELLOW);

gotoxy(7,2);

printf("Left, Right, Up, Down KEY to move, Space to put, ESC-quit.");

for(x1=1,y1=1,y2=18;x1=18;x1++)

line((x1+JZ)*BILI,(y1+JS)*BILI,(x1+JZ)*BILI,(y2+JS)*BILI);

for(x1=1,y1=1,x2=18;y1=18;y1++)

line((x1+JZ)*BILI,(y1+JS)*BILI,(x2+JZ)*BILI,(y1+JS)*BILI);

for(x1=1;x1=18;x1++)

for(y1=1;y1=18;y1++)

box[x1][y1]=0 ;

}

void draw_circle(int x,int y,int color)

{

setcolor(color);

setlinestyle(SOLID_LINE,0,1);

x=(x+JZ)*BILI ;

y=(y+JS)*BILI ;

circle(x,y,8);

}

void judgekey()

{

int i ;

int j ;

switch(key)

{

case LEFT :

if(step_x-10)

break ;

else

{

for(i=step_x-1,j=step_y;i=1;i--)

if(box[i][j]==0)

{

draw_circle(step_x,step_y,LIGHTBLUE);

break ;

}

if(i1)break ;

step_x=i ;

judgewho(step_x,step_y);

break ;

}

case RIGHT :

if(step_x+118)

break ;

else

{

for(i=step_x+1,j=step_y;i=18;i++)

if(box[i][j]==0)

{

draw_circle(step_x,step_y,LIGHTBLUE);

break ;

}

if(i18)break ;

step_x=i ;

judgewho(step_x,step_y);

break ;

}

case DOWN :

if((step_y+1)18)

break ;

else

{

for(i=step_x,j=step_y+1;j=18;j++)

if(box[i][j]==0)

{

draw_circle(step_x,step_y,LIGHTBLUE);

break ;

}

if(j18)break ;

step_y=j ;

judgewho(step_x,step_y);

break ;

}

case UP :

if((step_y-1)0)

break ;

else

{

for(i=step_x,j=step_y-1;j=1;j--)

if(box[i][j]==0)

{

draw_circle(step_x,step_y,LIGHTBLUE);

break ;

}

if(j1)break ;

step_y=j ;

judgewho(step_x,step_y);

break ;

}

case ESC :

break ;

case SPACE :

if(step_x=1step_x=18step_y=1step_y=18)

{

if(box[step_x][step_y]==0)

{

box[step_x][step_y]=flag ;

if(judgeresult(step_x,step_y)==1)

{

sound(1000);

delay(1000);

nosound();

gotoxy(30,4);

if(flag==1)

{

setbkcolor(BLUE);

cleardevice();

setviewport(100,100,540,380,1);

/*定义一个图形窗口*/

setfillstyle(1,2);

/*绿色以实填充*/

setcolor(YELLOW);

rectangle(0,0,439,279);

floodfill(50,50,14);

setcolor(12);

settextstyle(1,0,5);

/*三重笔划字体, 水平放?5倍*/

outtextxy(20,20,"The White Win !");

setcolor(15);

settextstyle(3,0,5);

/*无衬笔划字体, 水平放大5倍*/

outtextxy(120,120,"The White Win !");

setcolor(14);

settextstyle(2,0,8);

getch();

closegraph();

exit(0);

}

if(flag==2)

{

setbkcolor(BLUE);

cleardevice();

setviewport(100,100,540,380,1);

/*定义一个图形窗口*/

setfillstyle(1,2);

/*绿色以实填充*/

setcolor(YELLOW);

rectangle(0,0,439,279);

floodfill(50,50,14);

setcolor(12);

settextstyle(1,0,8);

/*三重笔划字体, 水平放大8倍*/

outtextxy(20,20,"The Red Win !");

setcolor(15);

settextstyle(3,0,5);

/*无衬笔划字体, 水平放大5倍*/

outtextxy(120,120,"The Red Win !");

setcolor(14);

settextstyle(2,0,8);

getch();

closegraph();

exit(0);

}

}

change();

break ;

}

}

else

break ;

}

}

void change()

{

if(flag==1)

flag=2 ;

else

flag=1 ;

}

void judgewho(int x,int y)

{

if(flag==1)

draw_circle(x,y,15);

if(flag==2)

draw_circle(x,y,4);

}

int judgeresult(int x,int y)

{

int j,k,n1,n2 ;

while(1)

{

n1=0 ;

n2=0 ;

/*水平向左数*/

for(j=x,k=y;j=1;j--)

{

if(box[j][k]==flag)

n1++;

else

break ;

}

/*水平向右数*/

for(j=x,k=y;j=18;j++)

{

if(box[j][k]==flag)

n2++;

else

break ;

}

if(n1+n2-1=5)

{

return(1);

break ;

}

/*垂直向上数*/

n1=0 ;

n2=0 ;

for(j=x,k=y;k=1;k--)

{

if(box[j][k]==flag)

n1++;

else

break ;

}

/*垂直向下数*/

for(j=x,k=y;k=18;k++)

{

if(box[j][k]==flag)

n2++;

else

break ;

}

if(n1+n2-1=5)

{

return(1);

break ;

}

/*向左上方数*/

n1=0 ;

n2=0 ;

for(j=x,k=y;j=1,k=1;j--,k--)

{

if(box[j][k]==flag)

n1++;

else

break ;

}

/*向右下方数*/

for(j=x,k=y;j=18,k=18;j++,k++)

{

if(box[j][k]==flag)

n2++;

else

break ;

}

if(n1+n2-1=5)

{

return(1);

break ;

}

/*向右上方数*/

n1=0 ;

n2=0 ;

for(j=x,k=y;j=18,k=1;j++,k--)

{

if(box[j][k]==flag)

n1++;

else

break ;

}

/*向左下方数*/

for(j=x,k=y;j=1,k=18;j--,k++)

{

if(box[j][k]==flag)

n2++;

else

break ;

}

if(n1+n2-1=5)

{

return(1);

break ;

}

return(0);

break ;

}

}

void main()

{

int gdriver=VGA,gmode=VGAHI;

clrscr();

attention();

initgraph(gdriver,gmode,"c:\\tc");

/* setwritemode(XOR_PUT);*/

flag=1 ;

draw_box();

do

{

step_x=0 ;

step_y=0 ;

/*draw_circle(step_x,step_y,8); */

judgewho(step_x-1,step_y-1);

do

{

while(bioskey(1)==0);

key=bioskey(0);

judgekey();

}

while(key!=SPACEkey!=ESC);

}

while(key!=ESC);

closegraph();

}

怎样用C语言画五子棋棋盘

************************************************************** C++语言五子棋源程序 ************************************************************* #include graphics.h

#include stdio.h

#include stdlib.h

#include dos.h

#define backcolor CYAN

#define defaultcolor BLACK

#define linecolor MAGENTA

#define player1_color RED

#define player2_color WHITE

#define error_color RED

#define winner_color RED

const int left=40;

const int top=390;

const int d=30;

const int line_num=9;

const int turn=0;

const int r=d/3;

const int j=10;

int x,y,k=1,step=(line_num+1)*(line_num+1);

union REGS regs1,regs2;

class player1;

class player2;

class qipan{

public:

qipan();

~qipan(){};

void init_qipan();

friend void fall(player1 num1,player2 num2,qipan num);

friend void input(player1 num1,player2 num2,qipan num);

private:

int point[line_num+1][line_num+1];

};

class player1{

public:

player1();

~player1(){};

friend void fall(player1 num1,player2 num2,qipan num);

friend void input(player1 num1,player2 num2);

friend int judge_winner(player1 num1,player2 num2);

private:

int point1[line_num+1][line_num+1];

};

class player2{

public:

player2();

~player2(){};

friend void fall(player1 num1,player2 num2,qipan num);

friend void input(player1 num1,player2 num2,qipan num);

friend int judge_winner(player1 num1,player2 num2);

private:

int point2[line_num+1][line_num+1];

};

void input(player1 num1,player2 num2);

void fall(player1 num1,player2 num2,qipan num);

int judge_winner(qipan num,player1 num1,player2 num2);

void inputerror();

void display_winner(int);

void main()

{

int driver=DETECT,mode;

initgraph(driver,mode,"e:\tc30\bgi");

qipan num;

player1 num1;

player2 num2;

while(step--)

{

input(num1,num2,num);

fall(num1,num2,num);

if(judge_winner(num1,num2))

{

display_winner(k);

}

}

// getchar();

}

qipan::qipan(void)

{ int j,i;

char ch[2]="0";

setbkcolor(backcolor);

setcolor(linecolor);

for(i=0;i=line_num;i++)

{

line(left,top-i*d,left+line_num*d,top-i*d);

}

for(i=0;i=line_num;i++)

{

line(left+i*d,top,left+i*d,top-line_num*d);

}

for(i=0;i=line_num;i++)

{ if(*ch=='9'+1) *ch='a';

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left+i*d-2,top+r+3,ch);

(*ch)=(*ch)+1;

}

*ch='0';

for(i=0;i=line_num;i++)

{if(*ch=='9'+1) *ch='a';

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left-r-10,top-d*i-3,ch);

(*ch)=(*ch)+1;

}

setcolor(defaultcolor);

for(i=0;i=line_num;i++)

{

for(j=0;j=line_num;j++)

point[i][j]=0;

}

}

void fall(player1 num1,player2 num2,qipan num)

{

int flag=k%2;

if(flag)

{ setcolor(player2_color);

num2.point2[x][y]=1;

num.point[x][y]=2;

circle(left+d*x,top-d*y,r);

setfillstyle(1,player2_color);

floodfill(left+d*x,top-d*y,player2_color);

}

else

{ num1.point1[x][y]=1;

num.point[x][y]=1;

setcolor(player1_color);

circle(left+d*x,top-d*y,r);

setfillstyle(1,player1_color);

floodfill(left+d*x,top-d*y,player1_color);

}

setcolor(defaultcolor);

}

void input(player1 num1,player2 num2,qipan num)

{ char xx,yy;

k++;

while(1)

{

regs1.h.ah=0;

xx=int86(22,®s1,®s1)-'0';

if(xx==('q'-'0')||xx==('Q'-'0'))

{ step=0;

return;

}

regs1.h.ah=0;

yy=int86(22,®s1,®s1)-'0';

if(yy==('q'-'0')||yy==('Q'-'0'))

{

step=0;

return ;

}

if(xx0||xxline_num)

{ inputerror();

continue;

}

if(yy0||yyline_num)

{inputerror();

continue;

}

if(num.point[xx][yy]==0)

{

break;

}

else

{

inputerror();

continue;

}

}

x=(int)xx;

y=(int)yy;

setcolor(backcolor);

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left+d*line_num/3,top+d*2,"Input error");

setcolor(defaultcolor);

}

player1::player1()

{

int i,j;

for(i=0;i=line_num;i++)

{

for(j=0;j=line_num;j++)

point1[i][j]=0;

}

}

player2::player2()

{ int i,j;

for(i=0;i=line_num;i++)

{

for(j=0;j=line_num;j++)

point2[i][j]=0;

}

}

void inputerror(void)

{ setcolor(error_color);

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left+d*line_num/3,top+d*2,"Input error");

setcolor(defaultcolor);

}

int judge_winner(player1 num1,player2 num2)

{

int count=0,m=0,n=0,a=0,b=0,xx0,yy0;

int flag=k%2;

xx0=x; yy0=y;

if(!flag)

{ //left ------- right

while(xx0=1m4) {xx0--;m++;}

while(n9xx0=line_num)

{

if(num1.point1[xx0][y]==1)

{

count++;

if(count==5) return 1;

}

else

{

count=0;

}

n++;

xx0++;

}

//up ------ down

count=0; xx0=x; m=0; n=0;

while(yy0=1m4){yy0--;m++;}

while(n9yy0=line_num)

{

if(num1.point1[x][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

{

count=0;

}

n++;

yy0++;

}

//left up ----- right down

xx0=x;

yy0=y;

m=0;

n=0;

count=0;

while(xx0=1m4){ xx0--; a++; m++;} m=0;

while(yy0=line_numm4){ yy0++; b++; m++;}

if(a=b)

{

xx0=x-a; yy0=y+a;

}

else

{

xx0=x-b; yy0=y+b;

}

while(xx0=line_numyy0=0n9)

{

if(num1.point1[xx0][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

{

count=0;

}

xx0++;

yy0--;

n++;

}

//right up ----- left down

count=0;

a=0;

b=0;

n=0;

m=0;

xx0=x;

yy0=y;

while(xx0line_numm4){ xx0++; m++; a++;} m=0;

while(yy0line_numm4){ yy0--; m++; b++; }

if(a=b)

{

xx0=x+a;

yy0=y+a;

}

else

{

xx0=x+b;

yy0=y+b;

}

while(xx0=0yy0=0n9)

{

if(num1.point1[xx0][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

count=0;

xx0--;

yy0--;

n++;

}

//no winer

return 0;

}

else

{

//left ------- right

while(xx0=1m4) {xx0--;m++;}

while(n9xx0=line_num)

{

if(num1.point1[xx0][y]==1)

{

count++;

if(count==5) return 1;

}

else

{

count=0;

}

n++;

xx0++;

}

//up ------ down

count=0; xx0=x; m=0; n=0;

while(yy0=1m4){yy0--;m++;}

while(n9yy0=line_num)

{

if(num2.point2[x][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

{

count=0;

}

n++;

yy0++;

}

//left up ----- right down

xx0=x;

yy0=y;

m=0;

n=0;

count=0;

while(xx0=1m4){ xx0--; a++; m++;} m=0;

while(yy0=line_numm4){ yy0++; b++; m++;}

if(a=b)

{

xx0=x-a; yy0=y+a;

}

else

{

xx0=x-b; yy0=y+b;

}

while(xx0=line_numyy0=0n9)

{

if(num2.point2[xx0][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

{

count=0;

}

xx0++;

yy0--;

n++;

}

//right up ----- left down

count=0;

a=0;

b=0;

n=0;

m=0;

xx0=x;

yy0=y;

while(xx0line_numm4){ xx0++; m++; a++;} m=0;

while(yy0line_numm4){ yy0--; m++; b++; }

if(a=b)

{

xx0=x+a;

yy0=y+a;

}

else

{

xx0=x+b;

yy0=y+b;

}

while(xx0=0yy0=0n9)

{

if(num2.point2[xx0][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

count=0;

xx0--;

yy0--;

n++;

}

//no winer

return 0;

}

}

void display_winner(int k)

{

int flag=k%2;

if(!flag)

{ setcolor(winner_color);

settextstyle(DEFAULT_FONT,HORIZ_DIR,2);

outtextxy(left+d*2,top+40,"Red is winner");

setcolor(defaultcolor);

step=0;

getchar();

}

else

{ setcolor(winner_color);

settextstyle(DEFAULT_FONT,HORIZ_DIR,2);

outtextxy(left+2*d,top+40,"White is winner");

setcolor(defaultcolor);

step=0;

}

}