c语言五子棋界面函数 c#五子棋界面设计

求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语言程序设计 关于五子棋的

五子棋程序:

#includeiostream

using namespace std;

int Hsheng(char a[][15]);//判断o子是否获胜的函数

int Bsheng(char a[][15]);//判断x子是否获胜的函数

int he(char a[][15]);//判断是否平局(也就是棋盘下满了)的函数

void qipan(char a[15][15])//执行输出棋盘命令

{ for(int i=0;i15;i++) //打印棋盘

{ for(int j=0;j15;j++)

couta[i][j];

coutendl; } }

int main()

{ char a[15][15]; int x,y;

for(int i=0;i15;i++)

for(int j=0;j15;j++)

a[i][j]=' '; qipan(a);

while(1)//用循环语句执行o,x交替下子,这些while语句看起来似乎是个死循环~实际上都会经过break结束

{ int a1=1;

while(1)

{ for(;a1;)

{ cout"请输入o子下的位置:"; //输入o子的位置

cinxy; if(a[x][y]=='o'||a[x][y]=='x')//判断是否已有子

{cout"已有子请重下"",";continue;}

else if(x=15||y=15){cout"输入错误请重输"",";continue;}

else { a[x][y]='o'; a1=0;}

} break;}

qipan(a);//下好o子后将棋盘显示

if(Hsheng(a))//判断o子是否已经获胜

{ cout"o子获胜"endl; break; }

while(1)//下x子

{ cout"请输入x子下的位置:";

cinxy;

if(a[x][y]=='o'||a[x][y]=='x'||x=15||y=15)

{ for( ; a[x][y]=='o'||a[x][y]=='x'; )

{ cout"已有子请重下";

cout"请输入x子下的位置:";

cinxy;continue; }

for ( ; x=15||y=15||x; )

{ cout"输入错误请重输"","; //判断输入棋子位置是否正确

cout"请输入x子下的位置:";

cinxy;continue ;}

a[x][y]='x';break; }

else

{ a[x][y]='x'; break; } }

qipan(a);//再一次输出棋盘

if(Bsheng(a))//判断x子是否已经获胜

{ cout"x子获胜"endl; break; }

if(he(a))//判断是否平局

{ cout"平局"endl; break; } }

return 0; }

int Hsheng(char a[][15])

{ int i,j;//判断横着的5个是否都相等

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

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

if(a[i][j]=='o'a[i][j+1]=='o'a[i][j+2]=='o'a[i][j+3]=='o'a[i][j+4]=='o')

return 1;

for(j=0;j15;j++)//判断竖着的5个是否都相等

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

if(a[i][j]=='o'a[i+1][j]=='o'a[i+2][j]=='o'a[i+3][j]=='o'a[i+4][j]=='o')

return 1;

for(i=0;i15;i++)//判断左斜5个

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

if(a[i][j]=='o'a[i+1][j+1]=='o'a[i+2][j+2]=='o'a[i+3][j+3]=='o'a[i+4][j+4]=='o')

return 1;

for(i=0;i15;i++)//右斜5个

for(j=14;j3;j--)

if(a[i][j]=='H'a[i+1][j-1]=='o'a[i+2][j-2]=='o'a[i+3][j-3]=='o'a[i+4][j-4]=='o')

return 1;

return 0; }

int Bsheng(char a[][15])//同o,只是改字符

{ int i,j;

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

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

if(a[i][j]=='x'a[i][j+1]=='x'a[i][j+2]=='x'a[i][j+3]=='x'a[i][j+4]=='x')

return 1;

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

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

if(a[i][j]=='x'a[i+1][j]=='x'a[i+2][j]=='x'a[i+3][j]=='x'a[i+4][j]=='x')

return 1;

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

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

if(a[i][j]=='x'a[i+1][j+1]=='x'a[i+2][j+2]=='x'a[i+3][j+3]=='x'a[i+4][j+4]=='x')

return 1;

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

for(j=14;j3;j--)

if(a[i][j]=='x'a[i+1][j-1]=='x'a[i+2][j-2]=='x'a[i+3][j-3]=='x'a[i+4][j-4]=='x')

return 1;

return 0; }

int he(char a[][15])

{ for(int i=0;i15;i++)

for(int j=0;j15;j++)

{ if(a[i][j]==' ')//当棋盘全部子都不是' '时才能return 1,即棋盘已下满

return 0;

}

return 1;

}

希望能帮到你!!

c语言五子棋代码,

package day17.gobang;

import java.util.Arrays;

public class GoBangGame {

public static final char BLANK='*';

public static final char BLACK='@';

public static final char WHITE='O';

public static final int MAX = 16;

private static final int COUNT = 5;

//棋盘

private char[][] board;

public GoBangGame() {

}

//开始游戏

public void start() {

board = new char[MAX][MAX];

//把二维数组都填充‘*’

for(char[] ary: board){

Arrays.fill(ary, BLANK);

}

}

public char[][] getChessBoard(){

return board;

}

public void addBlack(int x, int y) throws ChessExistException{

//@

//char blank = '*';

//System.out.println( x +"," + y + ":" + board[y][x] + "," + BLANK);

if(board[y][x] == BLANK){// x, y 位置上必须是空的才可以添棋子

board[y][x] = BLACK;

return;

}

throw new ChessExistException("已经有棋子了!");

}

public void addWhite(int x, int y)

throws ChessExistException{

if(board[y][x] == BLANK){// x, y 位置上必须是空的才可以添棋子

board[y][x] = WHITE;

return;

}

throw new ChessExistException("已经有棋子了!");

}

//chess 棋子:'@'/'O'

public boolean winOnY(char chess, int x, int y){

//先找到y方向第一个不是 blank的棋子

int top = y;

while(true){

if(y==0 || board[y-1][x]!=chess){

//如果y已经是棋盘的边缘, 或者的前一个不是chess

//就不再继续查找了

break;

}

y--;

top = y;

}

//向回统计所有chess的个数,如果是COUNT个就赢了

int count = 0;

y = top;

while(true){

if(y==MAX || board[y][x]!=chess){

//如果找到头 或者 下一个子不是chess 就不再继续统计了

break;

}

count++;

y++;

}

return count==COUNT;

}

//chess 棋子:'@'/'O'

public boolean winOnX(char chess, int x, int y){

//先找到x方向第一个不是 blank的棋子

int top = x;

while(true){

if(x==0 || board[y][x-1]!=chess){

//如果x已经是棋盘的边缘, 或者的前一个不是chess

//就不再继续查找了

break;

}

x--;

top = x;

}

//向回统计所有chess的个数,如果是COUNT个就赢了

int count = 0;

x = top;

while(true){

if(x==MAX || board[y][x]!=chess){

//如果找到头 或者 下一个子不是chess 就不再继续统计了

break;

}

count++;

x++;

}

return count==COUNT;

}

//chess 棋子:'@'/'O'

public boolean winOnXY(char chess, int x, int y){

//先找MAX向第一个不是 blank的棋子

int top = y;

int left = x;

while(true){

if(x==0 || y==0 || board[y-1][x-1]!=chess){

//如果x已经是棋盘的边缘, 或者的前一个不是chess

//就不再继续查找了

break;

}

x--;

y--;

top = y;

left=x;

}

//向回统计所有chess的个数,如果是COUNT个就赢了

int count = 0;

x = left;

y = top;

while(true){

if(x==MAX || y==MAX || board[y][x]!=chess){

//如果找到头 或者 下一个子不是chess 就不再继续统计了

break;

}

count++;

x++;

y++;

}

return count==COUNT;

}

//chess 棋子:'@'/'O'

public boolean winOnYX(char chess, int x, int y){

//先找到x方向第一个不是 blank的棋子

int top = y;

int left = x;

while(true){

if(x==MAX-1 || y==0 || board[y-1][x+1]!=chess){

//如果x已经是棋盘的边缘, 或者的前一个不是chess

//就不再继续查找了

break;

}

x++;

y--;

top = y;

left=x;

}

//向回统计所有chess的个数,如果是COUNT个就赢了

int count = 0;

x = left;

y = top;

while(true){

if(x==0 || y==MAX || board[y][x]!=chess){

//如果找到头 或者 下一个子不是chess 就不再继续统计了

break;

}

count++;

x--;

y++;

}

return count==COUNT;

}

public boolean whiteIsWin(int x, int y) {

//在任何一个方向上赢了,都算赢

return winOnY(WHITE, x, y) ||

winOnX(WHITE, x, y) ||

winOnXY(WHITE, x, y) ||

winOnYX(WHITE, x, y);

}

public boolean blackIsWin(int x, int y) {

return winOnY(BLACK, x, y) ||

winOnX(BLACK, x, y) ||

winOnXY(BLACK, x, y) ||

winOnYX(BLACK, x, y);

}

}

怎样用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语言实现一个命令行下的五子棋游戏。要求有棋盘界面,并实现人人,人机机人的三种对弈模式。

;tn=baiduPostBrowsersc=4335990148z=430611008pn=0rn=30lm=0word=c%2B%2B#4335990148

百度贴吧里面的,我试过可以运行,有人机对战功能~

#include iostream.h

#include process.h

#include conio.h

class five

{

int x;

int y;

int m;

int n;

int num_xy;

int num_mn;

char qipan[20][20];

public:

five(int X = 0,int Y =0,int M = 0,int N = 0,int Num_xy = 0,int Num_mn = 0)

{

x = X;

y = Y;

m = M;

n = N;

num_xy = Num_xy;

num_mn = Num_mn;

for(int i = 0 ; i 20 ; i ++)

{

for(int j = 0 ;j 20 ; j ++)

qipan[i][j] = '.';

}

}

int getm()

{

return m;

}

int getn()

{

return n;

}

void getpointxy();

void getpointmn();

void machinemn();

void showqipan();

void print();

bool IsxyWin();

bool IsmnWin();

int dangerlevel(int a,int b);

void attack();

};

void five::getpointxy()

{

cout"请分别输入第"num_xy+1"步白棋('o')的横坐标和纵坐标:";

cin x y;

if(x = 0 || y = 0 || x 20 || y 20 )

{

cout "出界,请重新输入:"endl ;

getpointxy();

}

else if(qipan[y-1][x-1] != '.' )

{

cout "请所下的位置已经有棋子,请重新输入:" endl;

getpointxy();

}

else if(num_xy+num_mn = 400)

{

cout "棋盘已满,平局!" endl;

exit(0);

}

else

{

qipan[y-1][x-1] = 'o';

num_xy++;

}

}

void five::getpointmn()

{

cout"请分别输入第"num_mn+1"步黑棋('x')的横坐标和纵坐标:";

cin m n;

if(m = 0 || n = 0 || m 20 || n 20 )

{

cout "出界,请重新输入:"endl ;

getpointmn();

}

else if(qipan[n-1][m-1]!='.')

{

cout "请所下的位置已经有棋子,请重新输入:" endl;

getpointmn();

}

else if(num_xy+num_mn = 400)

{

cout "棋盘已满,平局!" endl;

exit(0);

}

else

{

qipan[n-1][m-1] = 'x';

num_mn++;

}

}

int five::dangerlevel(int a, int b)

{

int level = 0;

for(int i = 1 ; a+i=19; i ++)

{

if(qipan[a+i][b] == '.' a + i = 19)

{

i = i+1;

break;

}

if(qipan[a +i][b] == 'x' a + i = 19)

break;

}

level += (i-1);

for(i = 1 ; a-i=0; i ++)

{

if(qipan[a-i][b] == '.' (a-i) = 0)

{

i = i+1;

break;

}

if(qipan[a-i][b] == 'x' (a-i) = 0)

break;

}

level += (i-1);

for(i = 1 ; b+i=19 ; i ++)

{

if(qipan[a][b + i] == '.' b + i = 19)

{

i = i+1;

break;

}

if(qipan[a][b + i] == 'x' b + i = 19)

break;

}

level += (i-1);

for( i = 1 ; b-i=0 ; i ++)

{

if(qipan[a][b - i] == '.' (b-i) = 0)

{

i = i+1;

break;

}

if(qipan[a][b - i] == 'x' (b-i) = 0)

break;

}

for( i = 1 ; a+i=19 b+i=19; i ++)

{

if(qipan[a + i][b + i] == '.' b+i = 19 a+i = 19)

{

i = i+1;

break;

}

if(qipan[a + i][b + i] == 'x' b+i = 19 a+i = 19)

break;

}

level += (i-1);

for( i = 1 ; a-i= 0 b-i = 0; i ++)

{

if(qipan[a - i][b - i] == '.' a-i= 0 b-i = 0)

{

i = i+1;

break;

}

if(qipan[a - i][b - i] == 'x' a-i= 0 b-i = 0)

break;

}

level += (i-1);

for( i = 1 ; a-i= 0 b+i = 19 ; i ++)

{

if(qipan[a - i][b + i] == '.' a-i= 0 b+i = 19)

{

i = i+1;

break;

}

if(qipan[a - i][b + i] == 'x' a-i= 0 b+i = 19)

break;

}

level += (i-1);

for( i = 1 ; a + i= 19 b-i = 0 ; i ++)

{

if(qipan[a + i][b - i] == '.' a + i= 19 b-i = 0)

{

i = i+1;

break;

}

if(qipan[a + i][b - i] == 'x' a + i= 19 b-i = 0)

break;

}

level += (i-1);

return level;

}

void five::attack()

{

int num1=1,num2=1,num3=1,num4=1;

int A = 20 , B = 20 ,max = 0;

for(int a = 0 ; a 19 ; a ++)

{

for(int b = 0 ; b 19 ; b ++)

{

if(qipan[a][b] == '.')

{

int level = 0;

for(int i = 1 ; a+i=19; i ++)

{

if(qipan[a+i][b] == '.' a + i = 19)

{

i = i+1;

break;

}

if(qipan[a +i][b] == 'o' a + i = 19)

break;

}

level += (i-1);

for(i = 1 ; a-i=0; i ++)

{

if(qipan[a-i][b] == '.' (a-i) = 0)

{

i = i+1;

break;

}

if(qipan[a-i][b] == 'o' (a-i) = 0)

break;

}

level += (i-1);

for(i = 1 ; b+i=19 ; i ++)

{

if(qipan[a][b + i] == '.' b + i = 19)

{

i = i+1;

break;

}

if(qipan[a][b + i] == 'o' b + i = 19)

break;

}

level += (i-1);

for( i = 1 ; b-i=0 ; i ++)

{

if(qipan[a][b - i] == '.' (b-i) = 0)

{

i = i+1;

break;

}

if(qipan[a][b - i] == 'o' (b-i) = 0)

break;

}

for( i = 1 ; a+i=19 b+i=19; i ++)

{

if(qipan[a + i][b + i] == '.' b+i = 19 a+i = 19)

{

i = i+1;

break;

}

if(qipan[a + i][b + i] == 'o' b+i = 19 a+i = 19)

break;

}

level += (i-1);

for( i = 1 ; a-i= 0 b-i = 0; i ++)

{

if(qipan[a - i][b - i] == '.' a-i= 0 b-i = 0)

{

i = i+1;

break;

}

if(qipan[a - i][b - i] == 'o' a-i= 0 b-i = 0)

break;

}

level += (i-1);

for( i = 1 ; a-i= 0 b+i = 19 ; i ++)

{

if(qipan[a - i][b + i] == '.' a-i= 0 b+i = 19)

{

i = i+1;

break;

}

if(qipan[a - i][b + i] == 'o' a-i= 0 b+i = 19)

break;

}

level += (i-1);

for( i = 1 ; a + i= 19 b-i = 0 ; i ++)

{

if(qipan[a + i][b - i] == '.' a + i= 19 b-i = 0)

{

i = i+1;

break;

}

if(qipan[a + i][b - i] == 'o' a + i= 19 b-i = 0)

break;

}

level += (i-1);

if(level = max)

{

n = a + 1;

m = b + 1;

max = level;

}

}

else

continue;

}

}

qipan[n-1][m-1] = 'x';

}

void five::machinemn()

{

int Num1=1,Num2=1,Num3=1,Num4=1,A11 = 21,A12 = 21,A21 = 21,A22 = 21,A31 = 21,A32 = 21,A41 = 21,A42 = 21;

for(int i = x ; i x+5 x+520 ; i ++)

{

if(qipan[y-1][i] == '.')

{

A11=i;

break;

}

if(qipan[y-1][i] == 'x')

break;

if(qipan[y-1][i] == 'o')

Num1++;

}

for(i = x-2; i = 0 ; i --)

{

if(qipan[y-1][i] == '.')

{

A12 = i;

break;

}

if(qipan[y-1][i] == 'x')

{

break;

}

if(qipan[y-1][i] == 'o')

Num1++;

}

////

for( i = y ; i y+5 y+5 20 ; i ++)

{

if(qipan[i][x-1] == '.')

{

A21 = i;

break;

}

if(qipan[i][x-1] == 'x')

{

break;

}

if(qipan[i][x-1] == 'o')

Num2++;

}

for(i = y-2; i = 0 ; i --)

{

if(qipan[i][x-1] == '.')

{

A22 = i;

break;

}

if(qipan[i][x-1] == 'x')

{

break;

}

if(qipan[i][x-1] == 'o')

Num2++;

}

////

for(i = 0 ; i 5 x+i20 y+i20; i++ )

{

if(qipan[y+i][x+i] == '.')

{

A31 = i;

break;

}

if(qipan[y+i][x+i] == 'x')

{

break;

}

if(qipan[y+i][x+i] =='o')

Num3++;

}

for(i = 0 ; x - i -2=0 y-i-2=0 i 5;i++ )

{

if(qipan[y-i-2][x-i-2] == '.')

{

A32 = i;

break;

}

if(qipan[y-i-2][x-i-2] == 'x')

break;

if(qipan[y-i-2][x-i-2] == 'o')

Num3++;

}

////

for(i = 0 ; x + i 20 y - i - 2 = 0 i 5;i ++)

{

if(qipan[y-i-2][x+i] == '.')

{

A41 = i;

break;

}

if(qipan[y-i-2][x+i] == 'x')

{

break;

}

if(qipan[y-i-2][x+i] == 'o')

Num4++;

}

for(i = 0 ; y + i 20 x - i - 2 = 0 i 5;i ++)

{

if(qipan[y+i][x-i-2] == '.')

{

A42 = i;

break;

}

if(qipan[y+i][x-i-2] == 'x')

{

break;

}

if(qipan[y+i][x-i-2] == 'o')

Num4++;

}

////

if(num_xy == 0 num_xy == 0)

{

m = n = 10;

qipan[m-1][n-1] = 'x';

}

else if(num_xy == 1)

attack();

else if(num_xy == 1)

{

m = x+1;

n = x+1;

qipan[m-1][n-1] = 'x';

}

else if(Num1 == 4 A11 == 21 A12 != 21) //一排四个右边一头堵住

{

n =1 + y-1;

m = 1 + A12;

qipan[y-1][A12] = 'x';

}

else if(Num1 == 4 A12 == 21 A11 != 21) //一排四个左边边一头堵住

{

n =1 + y-1;

m = 1 + A11;

qipan[y-1][A11] = 'x';

}

else if(Num2 == 4 A21 == 21 A22 != 21) //一列四个下边一头堵住

{

n =1 + A22;

m = 1 + x-1;

qipan[A22][x-1] = 'x';

}

else if(Num2 == 4 A22 == 21 A21 != 21) //一列四个上边一头堵住

{

n =1 + A21;

m = 1 + x-1;

qipan[A21][x-1] = 'x';

}

else if(Num3 == 4 A31 == 21 A32 != 21) //一左上右下斜列四个右边一头堵住

{

n =1 + y - A32 - 2;

m = 1 + x - A32 - 2;

qipan[y-A32-2][x-A32-2] = 'x';

}

else if(Num3 == 4 A32 == 21 A31 != 21) //一左上右下斜列四个左边一头堵住

{

n =1 + y + A31;

m = 1 + x + A31;

qipan[y+A31][x+A31] = 'x';

}

else if(Num4 == 4 A41 == 21 A42 != 21) //一左下右上斜列四个右边一头堵住

{

n =1 + y + A42;

m = 1 + x - A42 - 2;

qipan[y+A42][x-A42-2] = 'x';

}

else if(Num4 == 4 A42 == 21 A41 != 21) //一左下右上斜列四个左边一头堵住

{

n =1 + y - A41 - 2;

m = 1 + x + A41;

qipan[y-A41-2][x+A41] = 'x';

}

else if(Num1==3 A11 != 21 A12 != 21) //一排3 个两头没堵

{

n =1 + y-1;

m = 1 + A11;

if(dangerlevel(y-1,A11) = dangerlevel(y-1,A12))

qipan[y-1][A11] = 'x';

else

qipan[y-1][A12] = 'x';

}

else if(Num2==3 A21 != 21 A22 != 21) //一列 3 个两头没堵

{

n =1 + A21;

m = 1 + x-1;

if(dangerlevel(A21,x-1) = dangerlevel(A22,x-1))

qipan[A21][x-1] = 'x';

else

qipan[A22][x-1] = 'x';

}

else if(Num3 == 3 A31 != 21 A32 != 21) //一左上右下斜列3 个两头没堵

{

n =1 + y + A31;

m = 1 + x + A31;

if(dangerlevel(y+A31,x+A31) = dangerlevel(y-A32 - 2,y - A32 -2))

qipan[y+A31][x+A31] = 'x';

else

qipan[y-A32-2][x-A32 -2] = 'x';

}

else if(Num4==3 A41 != 21 A42 != 21) //一左下右上斜列3 个两头没堵

{

n =1 + y - A41 - 2;

m = 1 + x + A41;

if(dangerlevel(y-A41-2,x+A41) = dangerlevel(y-A42-2,x+A42))

qipan[y-A41-2][x+A41] = 'x';

else

qipan[y-A42-2][x+A42] = 'x';

}

else if(Num1 == 2 A11 != 21 A12 != 21) //一排2个两头没堵

{

n =1 + y-1;

m = 1 + A11;

qipan[y-1][A11] = 'x';

}

else if( Num2 == 2 A21 != 21 A22 != 21) //一列2个两头没堵

{

n =1 + A21;

m = 1 + x-1;

qipan[A21][x-1] = 'x';

}

else if(Num3 == 2 A31 != 21 A32 != 21) //一左上右下斜列2 个两头没堵

{

n =1 + y + A31;

m = 1 + x + A31;

qipan[y+A31][x+A31] = 'x';

}

else if(Num4==2 A41 != 21 A42 != 21) //一左下右上斜列2个两头没堵

{

n =1 + y - A41 - 2;

m = 1 + x + A41;

qipan[y-A41-2][x+A41] = 'x';

}

else

attack();

}

void five::showqipan()

{

int p=0,q=0;

for(int i = 0 ; i = 20 ; i++)

{

if(p10)

{

cout p ' ';

}

else

cout p ;

p++;

}

cout endl;

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

{

if(i=0)

{

q++;

if(q10)

cout q ' ';

else

cout q;

}

for(int j = 0 ; j 20 ; j ++)

cout qipan[i][j]' ' ;

cout endl;

}

}

void five::print()

{

int p=0,q=0;

for(int i = 0 ; i = 20 ; i++)

{

if(p10)

{

cout p ' ';

}

else

cout p ;

p++;

}

cout endl;

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

{

if(i=0)

{

q++;

if(q10)

cout q ' ';

else

cout q;

}

for(int j = 0 ; j 20 ; j ++)

{

cout qipan[i][j]' ' ;

}

cout endl;

}

}

bool five::IsxyWin()

{

int num1=1,num2=1,num3=1,num4=1;

for(int i = x ; i x+5 x+520 ; i ++)

{

if(qipan[y-1][i] != 'o')

break;

if(qipan[y-1][i] == 'o')

num1++;

}

for(i = x-2; i = 0 ; i --)

{

if(qipan[y-1][i] != 'o')

break;

if(qipan[y-1][i] == 'o')

num1++;

}

for( i = y ; i y+5 ; i ++)

{

if(qipan[i][x-1] != 'o')

break;

if(qipan[i][x-1] == 'o')

num2++;

}

for(i = y-2; i = 0 ; i --)

{

if(qipan[i][x-1] != 'o')

break;

if(qipan[i][x-1] == 'o')

num2++;

}

for(i = 0 ; i 5 x+i20 y+i20; i++ )

{

if(qipan[y+i][x+i] != 'o')

break;

if(qipan[y+i][x+i] =='o')

num3++;

}

for(i = 0 ; x - i -2=0 y-i-2=0 i 5;i++ )

{

if(qipan[y-i-2][x-i-2] != 'o')

break;

if(qipan[y-i-2][x-i-2] == 'o')

num3++;

}

for(i = 0 ; x + i 20 y - i - 2 = 0 i 5;i ++)

{

if(qipan[y-i-2][x+i] != 'o')

break;

if(qipan[y-i-2][x+i] == 'o')

num4++;

}

for(i = 0 ; y + i 20 x - i - 2 = 0 i 5;i ++)

{

if(qipan[y+i][x-i-2] != 'o')

break;

if(qipan[y+i][x-i-2] == 'o')

num4++;

}

if(num1=5||num2=5||num3=5||num4=5)

return 1;

else

return 0;

}

bool five::IsmnWin()

{

int num1=1,num2=1,num3=1,num4=1;

for(int i = m ; i m+5 m+520 ; i ++)

{

if(qipan[n-1][i] != 'x')

break;

if(qipan[n-1][i] == 'x')

num1++;

}

for(i = m-2; i = 0 ; i --)

{

if(qipan[n-1][i] != 'x')

break;

if(qipan[n-1][i] == 'x')

num1++;

}

for( i = n ; i n+5 ; i ++)

{

if(qipan[i][m-1] != 'x')

break;

if(qipan[i][m-1] == 'x')

num2++;

}

for(i = n-2; i = 0 ; i --)

{

if(qipan[i][m-1] != 'x')

break;

if(qipan[i][m-1] == 'x')

num2++;

}

for(i = 0 ; i 5 m+i20 n+i20; i++ )

{

if(qipan[n+i][m+i] != 'x')

break;

if(qipan[n+i][m+i] =='x')

num3++;

}

for(i = 0 ; m - i -2=0 n-i-2=0 i 5;i++ )

{

if(qipan[n-i-2][m-i-2] != 'x')

break;

if(qipan[n-i-2][m-i-2] == 'x')

num3++;

}

for(i = 0 ; m + i 20 n - i - 2 = 0 i 5;i ++)

{

if(qipan[n-i-2][m+i] != 'x')

break;

if(qipan[n-i-2][m+i] == 'x')

num4++;

}

for(i = 0 ; n + i 20 m - i - 2 = 0 i 5;i ++)

{

if(qipan[n+i][m-i-2] != 'x')

break;

if(qipan[n+i][m-i-2] == 'x')

num4++;

}

if(num1=5||num2=5||num3=5||num4=5)

return 1;

else

return 0;

}

void main()

{

five a;

int temp;

cout "欢迎体验本游戏!这是本游戏的棋盘:" endl;

a.showqipan();

cout "双人游戏请按1,人机游戏请按2,退出请按3:";

cin temp;

if(temp == 3)

exit(1);

if(temp == 1)

{

cout "您选择了双人游戏,下面游戏开始:" endl;

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

{

a.getpointxy();

a.print();

if(a.IsxyWin())

{

cout "白棋胜!" endl;

break;

}

a.getpointmn();

a.print();

if(a.IsmnWin())

{

cout "黑棋胜!" endl;

break;

}

}

}

if(temp == 2)

{

int temp1=0;

cout "您选择了人机游戏,您是白棋('o'),电脑是黑棋('x'),下面游戏开始:" endl;

cout "您先下请按1,电脑先下请按2:";

cin temp1;

if(temp1 == 1)

{

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

{

a.getpointxy();

a.print();

if(a.IsxyWin())

{

cout "你赢了!恭喜!" endl;

break;

}

a.machinemn();

a.print();

cout "电脑走(" a.getm() ',' a.getn() ')' endl;

if(a.IsmnWin())

{

cout "哈哈,你输了!" endl;

break;

}

}

}

if(temp1 == 2)

{

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

{

a.machinemn();

a.print();

if(a.IsmnWin())

{

cout "哈哈,你输了!" endl;

break;

}

a.getpointxy();

a.print();

if(a.IsxyWin())

{

cout "你赢了!恭喜!" endl;

break;

}

}

}

}

cout "Press any key to EXIT\n";

getch();

}


分享题目:c语言五子棋界面函数 c#五子棋界面设计
网址分享:http://scjbc.cn/article/hisise.html

其他资讯