五子棋人机对战c语言代码(五子棋c语言代码 人机)

今天给各位分享五子人机对战c语言代码的知识,其中也会对五子棋c语言代码 人机进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

1、c语言五子棋人机对战的代码2、找五子棋源代码c++3、用C写一个五子棋程序4、求一个五子棋人机对战c语言算法 主要是电脑不知道怎么下棋,设计的是玩家先手5、求C语言编写的五子棋程序。

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

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

不懂得继续问

五子棋人机对战c语言代码(五子棋c语言代码 人机)

找五子棋源代码c++

#include “iostream”

#include iomanip

using namespace std;

const int M=20;

const int N=20;

int main()

{

char weizhi[M][N];

int k,i,j,x,y,flag=0;

cout”欢迎使用简易双人对战五子棋游戏”endl;

cout”五子棋棋谱如下:”endl;

for(k=0;k=N;k++)

coutsetw(3)setfill(‘ ‘)k;

coutendl;

for(i=1;i=M;i++)

{

coutsetw(3)setfill(‘ ‘)i;

for(j=1;j=N;j++)

{

weizhi[i][j]=’-‘;

coutsetw(3)setfill(‘ ‘)weizhi[i][j];

}

coutendl;

}

while(flag==0)

{

//红方落子

cout”请红方输入落子位置:”endl;

loop1:

cout”请输入落子的行数:”;

cinx;

cout”请输入落子的列数:”;

ciny;

if(weizhi[x][y]==’-‘)

{

weizhi[x][y]=’*’;

for(k=0;k=N;k++)

coutsetw(3)setfill(‘ ‘)k;

coutendl;

for(i=1;i=M;i++)

{

coutsetw(3)setfill(‘ ‘)i;

for(j=1;j=N;j++)

coutsetw(3)setfill(‘ ‘)weizhi[i][j];

coutendl;

}

}

else

{

cout”你不能在这落子,请重新选择落子位置:”endl;

goto loop1;

}

//判断胜利

for(i=1;i=M-4;i++)

{

for(j=1;j=N-4;j++)

{

if(weizhi[i][j]==’*’ weizhi[i][j+1]==’*’ weizhi[i][j+2]==’*’ weizhi[i][j+3]==’*’ weizhi[i][j+4]==’*’)

{

cout”恭喜红方获得简易双人对战五子棋的胜利!耶~~~”endl;

flag=1;

break;

}

if(weizhi[i][j]==’*’ weizhi[i+1][j]==’*’ weizhi[i+2][j]==’*’ weizhi[i+3][j]==’*’ weizhi[i+4][j]==’*’)

{

cout”恭喜红方获得简易双人对战五子棋的胜利!耶~~~”endl;

flag=1;

break;

}

if(weizhi[i][j]==’*’ weizhi[i+1][j+1]==’*’ weizhi[i+2][j+2]==’*’ weizhi[i+3][j+3]==’*’ weizhi[i+4][j+4]==’*’)

{

cout”恭喜红方获得简易双人对战五子棋的胜利!耶~~~”endl;

flag=1;

break;

}

if(flag==1)

break;

}

}

//蓝方落子

cout”请蓝方输入落子位置:”endl;

loop2:

cout”请输入落子的行数:”;

cinx;

cout”请输入落子的列数:”;

ciny;

if(weizhi[x][y]==’-‘)

{

weizhi[x][y]=’#’;

for(k=0;k=N;k++)

coutsetw(3)setfill(‘ ‘)k;

coutendl;

for(i=1;i=M;i++)

{

coutsetw(3)setfill(‘ ‘)i;

for(j=1;j=N;j++)

coutsetw(3)setfill(‘ ‘)weizhi[i][j];

coutendl;

}

}

else

{

cout”你不能在这落子,请重新选择落子位置:”;

goto loop2;

}

//判断胜利

for(i=1;i=M-4;i++)

{

for(j=1;j=N-4;j++)

{

if(weizhi[i][j]==’#’ weizhi[i][j+1]==’#’ weizhi[i][j+2]==’#’ weizhi[i][j+3]==’#’ weizhi[i][j+4]==’#’)

{

cout”恭喜蓝方获得简易双人对战五子棋的胜利!耶~~~”endl;

flag=1;

break;

}

if(weizhi[i][j]==’#’ weizhi[i+1][j]==’#’ weizhi[i+2][j]==’#’ weizhi[i+3][j]==’#’ weizhi[i+4][j]==’#’)

{

cout”恭喜蓝方获得简易双人对战五子棋的胜利!耶~~~”endl;

flag=1;

break;

}

if(weizhi[i][j]==’#’ weizhi[i+1][j+1]==’#’ weizhi[i+2][j+2]==’#’ weizhi[i+3][j+3]==’#’ weizhi[i+4][j+4]==’#’)

{

cout”恭喜蓝方获得简易双人对战五子棋的胜利!耶~~~”endl;

flag=1;

break;

}

if(flag==1)

break;

}

}

}

return 0;

}

我运行过,没有错误.

用C写一个五子棋程序

#includeiostream

#includecstdlib

using namespace std;

class CHESS

{

public:

CHESS();

void setStep(bool ipjudge);//双人对战轮流走棋函数

void setStepC(bool ipjudge);//人机对战走棋函数

void coutChess();//输出棋盘

void coutPW();//输出权值表

bool getTurn(){flag=!flag;return flag;}//轮流走棋控制函数

void flushChess();//刷新棋盘信息函数

void judgeWin();//判断是否赢棋函数

void winer();//赢家输出函数

int getAns(){return result;}//返回结果(赢家判断)

static int count;//走棋步数变量

private:

bool flag;//轮流走棋判断变量

int PW[16][16],tPW[4];//权值变量,最高权值变量

int result,num[2];//结果(赢家判断),玩家输入棋子坐标判断

char inPut[2],temp[2];//玩家输入数据,转换暂存数据

char CBoard[16][16];//棋盘数据

int judgeAWin(int a,int b);//判断是否A为赢家函数

int judgeBWin(int a,int b);//判断是否B为赢家函数

void cSetStep();//电脑走棋函数

void setPower();//初始化权值函数

int adddepth(int depth);//填充权值函数

void judgePw(int x,int y,int direction,int depth,char test);//棋子判断[x坐标,y坐标,方向(顺时针0-无,1-左,2-左上,3-上,4-右上),深度(depth),标识符(A/B)]

void getFinalPw();

//权值判断函数

};

int CHESS::count=0;

void VsComputer(); //人机对战

void VsPlayer(); //双人对战

int main()

{

int choose;

CHESS newP;

do

{

choose=0;

system(“cls”);

cout” 欢乐五子棋”endlendl;

cout”请选择:”endlendl;

cout”1:人机对战模式”endlendl;

cout”2:双人对战模式”endlendl;

cout”3:退出游戏”endlendlendl;

cout”**************”endl;

cout”**************”endlendlendlendl;

cout”请输入你的选择:”;

cinchoose;

if(choose==2)

VsPlayer();

else if(choose==1)

VsComputer();

else if(choose==3)

exit(0);

else

{

cout”输入错误,请重新输入!”endl;

system(“pause”);

}

}while(choose!=3);

return 0;

}

void VsComputer()

{

bool ipjudge;

CHESS newP;

do

{

newP.coutChess();

newP.setStepC(ipjudge);//人机对战函数

if(!ipjudge)

continue;

if(!newP.getTurn())

newP.flushChess();

newP.coutChess();

newP.judgeWin();

CHESS::count++;

}while(newP.getAns()==0CHESS::count256);

newP.winer();

}

void CHESS::setStepC(bool ipjudge)

{

int i;

if(flag)

{

cout”棋手走棋:”;

cininPut;

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

if(inPut[i]’0’||(inPut[i]’9’inPut[i]’O’)||(inPut[i]’F’inPut[i]’O’)||inPut[i]’f’)

{

ipjudge=false;

cout”输入越界,请重新输入!”;

system(“pause”);

break;

}

}

else

cSetStep();//轮到电脑走棋

}

void CHESS::cSetStep()

{

int i,j,depth=0;

setPower();

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

{

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

{

if(CBoard[i][j]==’+’)//优化:排除周围全是+的情况

{

if(CBoard[i-1][j]==’O’||CBoard[i-1][j-1]==’O’||CBoard[i][j-1]==’O’||CBoard[i+1][j-1]==’O’||CBoard[i+1][j]==’O’||CBoard[i+1][j+1]==’O’||CBoard[i][j+1]==’O’||CBoard[i-1][j+1]==’O’)

{

judgePw(i,j,0,depth,’O’);

judgePw(i,j,0,depth,’X’);

}

else if(CBoard[i-1][j]==’X’||CBoard[i-1][j-1]==’X’||CBoard[i][j-1]==’X’||CBoard[i+1][j-1]==’X’||CBoard[i+1][j]==’X’||CBoard[i+1][j+1]==’X’||CBoard[i][j+1]==’X’||CBoard[i-1][j+1]==’X’)

{

judgePw(i,j,0,depth,’O’);

judgePw(i,j,0,depth,’X’);

}

}

}

}

getFinalPw();

//coutPW();

//system(“pause”);

if(tPW[0]0)

CBoard[tPW[1]][tPW[2]]=’X’;

/*else if(tPW[0]0tPW[3]1)

{

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

{

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

{

if(tPW[0]==PW[i][j])

if()

}

}

}*/

else

{

cout”权值函数错误!”;

system(“pause”);

exit(1);

}

}

void CHESS::judgePw(int x,int y,int direction,int depth,char test)

{

if(depth=0depth4)

{

if(direction==1)//左方

{

if(CBoard[x-depth-1][y]==test)

judgePw(x,y,1,depth+1,test);

else

PW[x][y]+=adddepth(depth);

}

else if(direction==2)//左上方

{

if(CBoard[x-depth-1][y-depth-1]==test)

judgePw(x,y,2,depth+1,test);

else

PW[x][y]+=adddepth(depth);

}

else if(direction==3)//正上方

{

if(CBoard[x][y-depth-1]==test)

judgePw(x,y,3,depth+1,test);

else

PW[x][y]+=adddepth(depth);

}

else if(direction==4)//右上方

{

if(CBoard[x+depth+1][y-depth-1]==test)

judgePw(x,y,4,depth+1,test);

else

PW[x][y]+=adddepth(depth);

}

else if(direction==5)//右方

{

if(CBoard[x+depth+1][y]==test)

judgePw(x,y,5,depth+1,test);

else

PW[x][y]+=adddepth(depth);

}

else if(direction==6)//右下方

{

if(CBoard[x+depth+1][y+depth+1]==test)

judgePw(x,y,6,depth+1,test);

else

PW[x][y]+=adddepth(depth);

}

else if(direction==7)//正下方

{

if(CBoard[x][y+depth+1]==test)

judgePw(x,y,7,depth+1,test);

else

PW[x][y]+=adddepth(depth);

}

else if(direction==8)//左下方

{

if(CBoard[x-depth-1][y+depth+1]==test)

judgePw(x,y,6,depth+1,test);

else

PW[x][y]+=adddepth(depth);

}

else if(direction==0)

{

if(CBoard[x-depth-1][y]==test)//左方

judgePw(x,y,1,depth+1,test);

if(CBoard[x-depth-1][y-depth-1]==test)//左上方

judgePw(x,y,2,depth+1,test);

if(CBoard[x][y-depth-1]==test)//正上方

judgePw(x,y,3,depth+1,test);

if(CBoard[x+depth+1][y-depth-1]==test)//右上方

judgePw(x,y,4,depth+1,test);

if(CBoard[x+depth+1][y]==test)//右方

judgePw(x,y,5,depth+1,test);

if(CBoard[x+depth+1][y+depth+1]==test)//右下方

judgePw(x,y,6,depth+1,test);

if(CBoard[x][y+depth+1]==test)//正下方

judgePw(x,y,7,depth+1,test);

if(CBoard[x-depth-1][y+depth+1]==test)//左下方

judgePw(x,y,6,depth+1,test);

}

}

else if(depth==4)

PW[x][y]+=adddepth(depth);

else

{

cout”深度函数出错!”;

system(“pause”);

exit(1);

}

}

int CHESS::adddepth(int depth)

{

switch(depth)

{

case 0:return 0;break;

case 1:return 1;break;

case 2:return 2;break;

case 3:return 4;break;

case 4:return 6;break;

default:

{

cout”深度函数错误!”;

system(“pause”);

exit(1);

}

}

}

void CHESS::getFinalPw()

{

int i,j;

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

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

{

if(PW[i][j]tPW[0])

{

tPW[0]=PW[i][j];

tPW[1]=i;

tPW[2]=j;

tPW[3]=1;

}

else if(PW[i][j]==tPW[0]PW[i][j]!=0)

tPW[3]+=1;

}

}

////////////////////////////////////////////////////////////////////////

//双人对战

////////////////////////////////////////////////////////////////////////

void VsPlayer()

{

bool ipjudge;

CHESS newP;

do

{

newP.coutChess();

newP.setStep(ipjudge);

if(!ipjudge)

continue;

newP.getTurn();

newP.flushChess();

newP.coutChess();

newP.judgeWin();

CHESS::count++;

}while(newP.getAns()==0CHESS::count256);

newP.winer();

}

void CHESS::winer()

{

if(CHESS::count==256)

{

cout”|||||||||平局!本局结束!”endl;

system(“pause”);

exit(1);

}

if(result==1)

cout”|||||||||恭喜!棋手A赢得本局胜利!”endlendl;

else if(result==2)

cout”|||||||||恭喜!棋手B赢得本局胜利!”endlendl;

system(“pause”);

}

//默认构造函数

CHESS::CHESS()

{

int i,j;

count=0;

result=0;

flag=true;

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

{

if(i10)

CBoard[i][0]=i+48;

else

CBoard[i][0]=i+55;

}

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

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

CBoard[i][j]=’+’;

}

//填充权值函数

void CHESS::setPower()

{

int i,j;

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

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

PW[i][j]=0;

tPW[0]=0;

tPW[1]=0;

tPW[2]=0;

tPW[3]=0;

}

//输出棋盘函数

void CHESS::coutChess()

{

int i,j;

system(“cls”);

cout” 欢乐五子棋”endlendl;

cout” 0 1 2 3 4 5 6 7 8 9 A B C D E F”endl;

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

{

if(i=0i10)

couti’ ‘;

else if(i=10)

coutstatic_castchar(i+55)’ ‘;

else

cout”棋盘列序号输出错误!”;

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

coutCBoard[i][j]’ ‘;

coutendl;

}

coutendlendl;

}

//双人对战,棋手轮流走棋函数

void CHESS::setStep(bool ipjudge)

{

if(flag)

cout”棋手A走棋:”;

else

cout”棋手B走棋:”;

cininPut;

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

{

if(inPut[i]’0’||(inPut[i]’9’inPut[i]’O’)||(inPut[i]’F’inPut[i]’O’)||inPut[i]’f’)

{

ipjudge=false;

cout”输入越界,请重新输入!”;

system(“pause”);

break;

}

}

}

//刷新棋盘

void CHESS::flushChess()

{

int i;

temp[0]=inPut[0];

temp[1]=inPut[1];

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

{

if(temp[i]=’0’temp[i]=’9′)

num[i]=static_castint(temp[i]-48);

else if(temp[i]=’O’temp[i]=’F’)

num[i]=static_castint(temp[i]-55);

else if(temp[i]=’O’temp[i]=’f’)

num[i]=static_castint(temp[i]-87);

else

{

cout”用户输入未知错误1,请重新输入!”;

system(“pause”);

exit(1);

}

}

if(CBoard[num[0]][num[1]]==’+’!flag)

CBoard[num[0]][num[1]]=’O’;

else if(CBoard[num[0]][num[1]]==’+’flag)

CBoard[num[0]][num[1]]=’X’;

else

{

flag=!flag;

cout”用户输入错误,请重新输入!”;

system(“pause”);

}

}

//判断胜出新算法

void CHESS::judgeWin()

{

int i=0,j;

do

{

j=0;

do

{

if(CBoard[i][j]==’O’)

result=judgeAWin(i,j);

else if(CBoard[i][j]==’X’)

result=judgeBWin(i,j);

else if(CBoard[i][j]==’+’);

else

{

cout”棋盘[“i”][“j”]位置信息错误!”endl;

system(“pause”);

exit(1);

}

if(result==1||result==2)

break;

j++;

}while(j=15);

if(result==1||result==2)

break;

i++;

}while(i=15);

}

//对棋手A的棋子检测

int CHESS::judgeAWin(int a,int b)

{

if(CBoard[a][b-1]==’O’CBoard[a][b-2]==’O’CBoard[a][b+1]==’O’CBoard[a][b+2]==’O’) //横向五子

return 1;

else if(CBoard[a+1][b]==’O’CBoard[a+2][b]==’O’CBoard[a-1][b]==’O’CBoard[a-2][b]==’O’) //纵向五子

return 1;

else if(CBoard[a+1][b+1]==’O’CBoard[a+2][b+2]==’O’CBoard[a-1][b-1]==’O’CBoard[a-2][b-2]==’O’) //左上右下五子

{

return 1;

}

else if(CBoard[a+1][b-1]==’O’CBoard[a+2][b-2]==’O’CBoard[a-1][b+1]==’O’CBoard[a-2][b+2]==’O’) //左下右上五子

return 1;

else

return 0;

}

//对棋手B的棋子检测

int CHESS::judgeBWin(int a,int b)

{

if(CBoard[a][b-1]==’X’CBoard[a][b-2]==’X’CBoard[a][b+1]==’X’CBoard[a][b+2]==’X’) //横向五子

return 2;

else if(CBoard[a+1][b]==’X’CBoard[a+2][b]==’X’CBoard[a-1][b]==’X’CBoard[a-2][b]==’X’) //纵向五子

return 2;

else if(CBoard[a+1][b+1]==’X’CBoard[a+2][b+2]==’X’CBoard[a-1][b-1]==’X’CBoard[a-2][b-2]==’X’) //左上右下五子

return 2;

else if(CBoard[a+1][b-1]==’X’CBoard[a+2][b-2]==’X’CBoard[a-1][b+1]==’X’CBoard[a-2][b+2]==’X’) //左下右上五子

return 2;

else

return 0;

}

//输出权值表

void CHESS::coutPW()

{

int i,j;

system(“cls”);

cout” 欢乐五子棋(权值表)”endlendl;

cout” 0 1 2 3 4 5 6 7 8 9 A B C D E F”endl;

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

{

if(i=0i10)

couti’ ‘;

else if(i=10)

coutstatic_castchar(i+55)’ ‘;

else

cout”棋盘列序号输出错误!”;

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

coutPW[i][j]’ ‘;

coutendl;

}

coutendlendl;

}

求一个五子棋人机对战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语言编写的五子棋程序。

#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语言代码 人机、五子棋人机对战c语言代码的信息别忘了在本站进行查找喔。

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年4月5日 03:33:37
下一篇 2024年4月5日 03:40:45

相关推荐

  • c语言改写模式,c语言实现修改功能

    c语言程序修改? 1、这个程序有4个错误,我都加粗了,第一个是m没有赋初值,第二个是while表达式中的ch=getchar()需要括号括起来,第三个是m=m*10+ch-0中的0也需要用单引号括起来,第四个是第2个while中为m!=0。 2、define容易造成误会,因为不符合一般的编程习惯,false 0, true 1;scanf放在你的那个地方是达…

    2024年5月23日
    4100
  • c语言控制代码的换码序列,c语言交换代码

    求C语言编程大神解答一下下面这个编程代码? k==5,用5去除125余0,所以r=125%5中r为0。由于!0为1,所以执行while循环体:先打印出5(k的值),再n=n/k==125/5=25;由于251则再打印出*号。这一循环结果输出是5*。 下面是我的代码,三个函数分别对应三个问题。 在实现基本要求的前提下,拓展了可以从键盘输入的功能,以下为各题代码…

    2024年5月23日
    5800
  • c语言扫描io脚状态,c语言端口扫描

    求51单片机的上升沿和下降沿C语言检测程序列子,端口就是普通IO口。 上升沿触发是当信号有上升沿时的开关动作,当电位由低变高而触发输出变化的就叫上升沿触发。也就是当测到的信号电位是从低到高也就是上升时就触发,叫做上升沿触发。 单片机怎么计算1s内下降沿的个数的C语言程序或者计算两个下降沿的时间(检测脉冲频率)计算1s内下降沿的个数方法是,一个定时器设置定时1…

    2024年5月23日
    4500
  • c语言mallloc使用的简单介绍

    C语言中使用malloc必须加#includemallo.h? 1、在C语言中使用malloc函数进行动态内存分配。malloc的全称是memory allocation,中文叫动态内存分配。原型:extern void malloc(unsigned int num_bytes);功能:分配长度为num_bytes字节的内存块。 2、你可以看一下C语言那本…

    2024年5月23日
    4500
  • c语言三位小数,C语言三位小数

    怎样用C++语言输出精确到小数点后三位的数? 1、用C++语言输出精确到小数点后三位的数,可以参考下面给出的代码:coutsetiosflags(ios:fixed)setprecision(3)。其中 setiosflags中set是设置的意思。ios是iostream的缩写,即输入输出流。flags是标志的意思。 2、要精确到小数点后若干位,则数据类型为…

    2024年5月23日
    7500
  • c语言21点游戏,二十一点游戏代码c语言

    如何使用C语言编写简单小游戏? 1、数学知识:长方形的面积S=a*b 长方形周长L=2*(a+b)其中a b分别为长方形的宽和高。算法分析:长方形面积及周长均依赖于宽和高,所以先要输入宽高值,然后根据公式计算,输出结果即可。 2、/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有c语言的编译器(vc++0、turbo…

    2024年5月23日
    6500
  • c语言当中的null,C语言当中的符号

    C/C++中,NULL和null的区别是什么? nul 和 null要看编译器,不同的编译器有所区别。 所以C或者C++中都使用一个特殊定义NULL表示无效值,其本质就是未定义具体数据类型的0值。 null是是什么都没有的意思。在java中表示空对象。 本意是“空的;元素只有零的”意思。计算机中通常表示空值,无结果,或是空集合。\x0d\x0a在ASCII码…

    2024年5月23日
    4700
  • 包含c语言对txt文件命名的词条

    如何在C语言编程里面修改源文件名字 如果你是在WINDOWS的话,简单了,随便用个编辑器,比如记事本,然后写c源程序,保存到你想要保存的位置。如果你在DOS下,可以用edit,写好以后,按alt键,选择文件菜单,然后保存。 用open打开文件,注意操作模式使用“修改”或者“添加” 用write或者fprintf向文件中写入你的内容。 用close关闭文件。 …

    2024年5月23日
    5000
  • 学c语言编程,学c语言编程用什么软件

    编程开发必须要学C语言吗? 1、要学习。编程开发的学习内容主要包括c语言、python和c+语言。C语言作为一种简单灵活的高级编程语言,它是一个面向过程的语言,一般是作为计算机专业的基础入门语言课程。 2、C语言。对于刚接触编程的人来说,先学习C语言是非常重要的。C语言可以说是是计算机编程语言的鼻祖,其他的编程语言几乎全是由C语言变化衍生出来的。 3、不需要…

    2024年5月23日
    3500
  • c语言用string定义字符串,c语言中用string类型来处理字符串类型

    C++怎样定义定义字符串 1、第一是字符数组来表示字符串。用下面的语句声明:char a[10];C语言中字符数组与字符串的唯一区别是字符串末尾有一个结束符\0,而字符数组不需要。 2、在C中定义字符串有下列几种形式:字符串常量,char数组,char指针 字符串常量 即:位于一对双括号中的任何字符。双引号里的字符加上编译器自动提供的结束标志\0字符,作为 …

    2024年5月23日
    4500

发表回复

登录后才能评论



关注微信