您的位置:

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语言算法 主要是电脑不知道怎么下棋,设计的是玩家先手

objectMainextendsApp{

varreverse_pairs = 0//逆序数

defmsort[T](cmp:(T, T) = Boolean)(l:List[T]):List[T] = {

defmerge(l1:List[T], l2:List[T]):List[T]=(l1, l2)match{

case(Nil, _) = l2

case(_, Nil) = l1

case(x::left1, y::left2) =

if(cmp(x, y))

x::merge(left1, l2)

else{

reverse_pairs += l1.length

y::merge(l1, left2)

}

}

valn = l.length / 2

if(n == 0)

return l

else{

val(l1, l2) = l.splitAt(n)

merge(msort(cmp)(l1), msort(cmp)(l2))

}

}

println(msort((x:Int, y:Int) = xy)(List(5, 4, 3, 2, 7,6 )))

println(reverse_pairs)

}

请用所学的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语言AI算法(原创思路)

我有个简单的思路: 先定义一条线上棋子的各种布局,比如初步定义长度为五个子 ◎◎◎◎● ◎◎●◎× ◎●◎×× ◎×◎×◎ 等等。白圈是自己的子,黑圈是对方的子,叉子是未走的格子。 程序里有个布局表,再定义各个布局的分数,比如连五最99分,连三30分等等。 ...

求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语言五子棋人机对战的代码

首先设2维数组模拟棋盘,棋盘每个位置设权值,代表当前适合下的一个价值

然后根据下面的表,给值,每下一步判断一次

棋型名称 棋型模式 估值

活四 ?AAAA? 300000

死四A AAAA? 2500

死四B AAA?A 3000

死四C AA?AA 2600

活三 ??AAA?? 3000

死三A AAA?? 500

死三B ?A?AA? 800

死三C A??AA 600

死三D A?A?A 550

活二 ???AA??? 650

死二A AA??? 150

死二B ??A?A?? 250

死二C ?A??A? 200

代码挺长的,没什么看的意义,自己琢磨一下这个就写了

不懂得继续问