当前位置:首页 > 代码 > 正文

c五子棋代码(五子棋c++代码)

admin 发布:2022-12-19 18:25 147


本篇文章给大家谈谈c五子棋代码,以及五子棋c++代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

五子棋的程序用C怎么编?

**********************************************************/

/* 程序中用到的库函数所在头文件应用 #include 命令包含进来 */

#include stdio.h

#include bios.h

#include ctype.h

#include conio.h

#include dos.h

/**********************************************************/

/* 定义符号常量 */

/*定义画棋盘所需的制表符*/

#define CROSSRU 0xbf /*右上角点*/

#define CROSSLU 0xda /*左上角点*/

#define CROSSLD 0xc0 /*左下角点*/

#define CROSSRD 0xd9 /*右下角点*/

#define CROSSL 0xc3 /*左边*/

#define CROSSR 0xb4 /*右边*/

#define CROSSU 0xc2 /*上边*/

#define CROSSD 0xc1 /*下边*/

#define CROSS 0xc5 /*十字交叉点*/

/*定义棋盘左上角点在屏幕上的位置*/

#define MAPXOFT 5

#define MAPYOFT 2

/*定义1号玩家的操作键键码*/

#define PLAY1UP 0x1157/*上移--'W'*/

#define PLAY1DOWN 0x1f53/*下移--'S'*/

#define PLAY1LEFT 0x1e41/*左移--'A'*/

#define PLAY1RIGHT 0x2044/*右移--'D'*/

#define PLAY1DO 0x3920/*落子--空格键*/

/*定义2号玩家的操作键键码*/

#define PLAY2UP 0x4800/*上移--方向键up*/

#define PLAY2DOWN 0x5000/*下移--方向键down*/

#define PLAY2LEFT 0x4b00/*左移--方向键left*/

#define PLAY2RIGHT 0x4d00/*右移--方向键right*/

#define PLAY2DO 0x1c0d/*落子--回车键Enter*/

/*若想在游戏中途退出, 可按 Esc 键*/

#define ESCAPE 0x011b

/*定义棋盘上交叉点的状态, 即该点有无棋子 */

/*若有棋子, 还应能指出是哪个玩家的棋子 */

#define CHESSNULL 0 //没有棋子

#define CHESS1 'O'//一号玩家的棋子

#define CHESS2 'X'//二号玩家的棋子

/*定义按键类别*/

#define KEYEXIT 0/*退出键*/

#define KEYFALLCHESS 1/*落子键*/

#define KEYMOVECURSOR 2/*光标移动键*/

#define KEYINVALID 3/*无效键*/

/*定义符号常量: 真, 假 --- 真为1, 假为0 */

#define TRUE 1

#define FALSE 0

/**********************************************************/

/* 定义数据结构 */

/*棋盘交叉点坐标的数据结构*/

struct point

{

int x,y;

};

/**********************************************************/

/*自定义函数原型说明 */

void Init(void);

int GetKey(void);

int CheckKey(int press);

int ChangeOrder(void);

int ChessGo(int Order,struct point Cursor);

void DoError(void);

void DoOK(void);

void DoWin(int Order);

void MoveCursor(int Order,int press);

void DrawCross(int x,int y);

void DrawMap(void);

int JudgeWin(int Order,struct point Cursor);

int JudgeWinLine(int Order,struct point Cursor,int direction);

void ShowOrderMsg(int Order);

void EndGame(void);

/**********************************************************/

/**********************************************************/

/* 定义全局变量 */

int gPlayOrder; /*指示当前行棋方 */

struct point gCursor; /*光标在棋盘上的位置 */

char gChessBoard[19][19];/*用于记录棋盘上各点的状态*/

/**********************************************************/

/**********************************************************/

/*主函数*/

void main()

{

int press;

int bOutWhile=FALSE;/*退出循环标志*/

Init();/*初始化图象,数据*/

while(1)

{

press=GetKey();/*获取用户的按键值*/

switch(CheckKey(press))/*判断按键类别*/

{

/*是退出键*/

case KEYEXIT:

clrscr();/*清屏*/

bOutWhile = TRUE;

break;

/*是落子键*/

case KEYFALLCHESS:

if(ChessGo(gPlayOrder,gCursor)==FALSE)/*走棋*/

DoError();/*落子错误*/

else

{

DoOK();/*落子正确*/

/*如果当前行棋方赢棋*/

if(JudgeWin(gPlayOrder,gCursor)==TRUE)

{

DoWin(gPlayOrder);

bOutWhile = TRUE;/*退出循环标志置为真*/

}

/*否则*/

else

/*交换行棋方*/

ChangeOrder();

}

break;

/*是光标移动键*/

case KEYMOVECURSOR:

MoveCursor(gPlayOrder,press);

break;

/*是无效键*/

case KEYINVALID:

break;

}

if(bOutWhile==TRUE)

break;

}

/*游戏结束*/

EndGame();

}

/**********************************************************/

/*界面初始化,数据初始化*/

void Init(void)

{

int i,j;

char *Msg[]=

{

"Player1 key:",

" UP----w",

" DOWN--s",

" LEFT--a",

" RIGHT-d",

" DO----space",

"",

"Player2 key:",

" UP----up",

" DOWN--down",

" LEFT--left",

" RIGHT-right",

" DO----ENTER",

"",

"exit game:",

" ESC",

NULL,

};

/*先手方为1号玩家*/

gPlayOrder = CHESS1;

/*棋盘数据清零, 即棋盘上各点开始的时候都没有棋子*/

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

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

gChessBoard[i][j]=CHESSNULL;

/*光标初始位置*/

gCursor.x=gCursor.y=0;

/*画棋盘*/

textmode(C40);

DrawMap();

/*显示操作键说明*/

i=0;

textcolor(BROWN);

while(Msg[i]!=NULL)

{

gotoxy(25,3+i);

cputs(Msg[i]);

i++;

}

/*显示当前行棋方*/

ShowOrderMsg(gPlayOrder);

/*光标移至棋盘的左上角点处*/

gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);

}

/*画棋盘*/

void DrawMap(void)

{

int i,j;

clrscr();

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

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

DrawCross(i,j);

}

/*画棋盘上的交叉点*/

void DrawCross(int x,int y)

{

gotoxy(x+MAPXOFT,y+MAPYOFT);

/*交叉点上是一号玩家的棋子*/

if(gChessBoard[x][y]==CHESS1)

{

textcolor(LIGHTBLUE);

putch(CHESS1);

return;

}

/*交叉点上是二号玩家的棋子*/

if(gChessBoard[x][y]==CHESS2)

{

textcolor(LIGHTBLUE);

putch(CHESS2);

return;

}

textcolor(GREEN);

/*左上角交叉点*/

if(x==0y==0)

{

putch(CROSSLU);

return;

}

/*左下角交叉点*/

if(x==0y==18)

{

putch(CROSSLD);

return;

}

/*右上角交叉点*/

if(x==18y==0)

{

putch(CROSSRU);

return;

}

/*右下角交叉点*/

if(x==18y==18)

{

putch(CROSSRD);

return;

}

/*左边界交叉点*/

if(x==0)

{

putch(CROSSL);

return;

}

/*右边界交叉点*/

if(x==18)

{

putch(CROSSR);

return;

}

/*上边界交叉点*/

if(y==0)

{

putch(CROSSU);

return;

}

/*下边界交叉点*/

if(y==18)

{

putch(CROSSD);

return;

}

/*棋盘中间的交叉点*/

putch(CROSS);

}

/*交换行棋方*/

int ChangeOrder(void)

{

if(gPlayOrder==CHESS1)

gPlayOrder=CHESS2;

else

gPlayOrder=CHESS1;

return(gPlayOrder);

}

/*获取按键值*/

int GetKey(void)

{

char lowbyte;

int press;

while (bioskey(1) == 0)

;/*如果用户没有按键,空循环*/

press=bioskey(0);

lowbyte=press0xff;

press=press0xff00 + toupper(lowbyte);

return(press);

}

/*落子错误处理*/

void DoError(void)

{

sound(1200);

delay(50);

nosound();

}

/*赢棋处理*/

void DoWin(int Order)

{

sound(1500);delay(100);

sound(0); delay(50);

sound(800); delay(100);

sound(0); delay(50);

sound(1500);delay(100);

sound(0); delay(50);

sound(800); delay(100);

sound(0); delay(50);

nosound();

textcolor(RED+BLINK);

gotoxy(25,20);

if(Order==CHESS1)

cputs("PLAYER1 WIN!");

else

cputs("PLAYER2 WIN!");

gotoxy(25,21);

cputs(" \\^+^/");

getch();

}

/*走棋*/

int ChessGo(int Order,struct point Cursor)

{

/*判断交叉点上有无棋子*/

if(gChessBoard[Cursor.x][Cursor.y]==CHESSNULL)

{

/*若没有棋子, 则可以落子*/

gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT);

textcolor(LIGHTBLUE);

putch(Order);

gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT);

gChessBoard[Cursor.x][Cursor.y]=Order;

return TRUE;

}

else

return FALSE;

}

/*判断当前行棋方落子后是否赢棋*/

int JudgeWin(int Order,struct point Cursor)

{

int i;

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

/*判断在指定方向上是否有连续5个行棋方的棋子*/

if(JudgeWinLine(Order,Cursor,i))

return TRUE;

return FALSE;

}

/*判断在指定方向上是否有连续5个行棋方的棋子*/

int JudgeWinLine(int Order,struct point Cursor,int direction)

{

int i;

struct point pos,dpos;

const int testnum = 5;

int count;

switch(direction)

{

case 0:/*在水平方向*/

pos.x=Cursor.x-(testnum-1);

pos.y=Cursor.y;

dpos.x=1;

dpos.y=0;

break;

case 1:/*在垂直方向*/

pos.x=Cursor.x;

pos.y=Cursor.y-(testnum-1);

dpos.x=0;

dpos.y=1;

break;

case 2:/*在左下至右上的斜方向*/

pos.x=Cursor.x-(testnum-1);

pos.y=Cursor.y+(testnum-1);

dpos.x=1;

dpos.y=-1;

break;

case 3:/*在左上至右下的斜方向*/

pos.x=Cursor.x-(testnum-1);

pos.y=Cursor.y-(testnum-1);

dpos.x=1;

dpos.y=1;

break;

}

count=0;

for(i=0;itestnum*2+1;i++)

{

if(pos.x=0pos.x=18pos.y=0pos.y=18)

{

if(gChessBoard[pos.x][pos.y]==Order)

{

count++;

if(count=testnum)

return TRUE;

}

else

count=0;

}

pos.x+=dpos.x;

pos.y+=dpos.y;

}

return FALSE;

}

/*移动光标*/

void MoveCursor(int Order,int press)

{

switch(press)

{

case PLAY1UP:

if(Order==CHESS1gCursor.y0)

gCursor.y--;

break;

case PLAY1DOWN:

if(Order==CHESS1gCursor.y18)

gCursor.y++;

break;

case PLAY1LEFT:

if(Order==CHESS1gCursor.x0)

gCursor.x--;

break;

case PLAY1RIGHT:

if(Order==CHESS1gCursor.x18)

gCursor.x++;

break;

case PLAY2UP:

if(Order==CHESS2gCursor.y0)

gCursor.y--;

break;

case PLAY2DOWN:

if(Order==CHESS2gCursor.y18)

gCursor.y++;

break;

case PLAY2LEFT:

if(Order==CHESS2gCursor.x0)

gCursor.x--;

break;

case PLAY2RIGHT:

if(Order==CHESS2gCursor.x18)

gCursor.x++;

break;

}

gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);

}

/*游戏结束处理*/

void EndGame(void)

{

textmode(C80);

}

/*显示当前行棋方*/

void ShowOrderMsg(int Order)

{

gotoxy(6,MAPYOFT+20);

textcolor(LIGHTRED);

if(Order==CHESS1)

cputs("Player1 go!");

else

cputs("Player2 go!");

gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);

}

/*落子正确处理*/

void DoOK(void)

{

sound(500);

delay(70);

sound(600);

delay(50);

sound(1000);

delay(100);

nosound();

}

/*检查用户的按键类别*/

int CheckKey(int press)

{

if(press==ESCAPE)

return KEYEXIT;/*是退出键*/

else

if

( ( press==PLAY1DO gPlayOrder==CHESS1) ||

( press==PLAY2DO gPlayOrder==CHESS2)

)

return KEYFALLCHESS;/*是落子键*/

else

if

( press==PLAY1UP || press==PLAY1DOWN ||

press==PLAY1LEFT || press==PLAY1RIGHT ||

press==PLAY2UP || press==PLAY2DOWN ||

press==PLAY2LEFT || press==PLAY2RIGHT

)

return KEYMOVECURSOR;/*是光标移动键*/

else

return KEYINVALID;/*按键无效*/

}

用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语言五子棋代码,

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语言五子棋

我浏览了一下你的代码,你对胜利的判断采用的是检查整个棋盘的方式,这样做的最大弊端自然是低效,而且在编写斜方向判断的时候比较复杂。我建议你采用下子后判断的方式,即玩家1下了一个字后,像该子周围8个方向检查是否存在5子的情况,只需要一个判断方法,而且较为简单。

void chooseONE()

{

printf("第一玩家请选择下棋位置\n");

printf("第几个直的\n");

scanf("%d",co);

printf("第几个横的\n");

scanf("%d",ro);

while(arr[ro][co]==1||arr[ro][co]==2)

{

printf("重复了!!!\n");

printf("第几个直的\n");

scanf("%d",co);

printf("第几个横的\n");

scanf("%d",ro);

}

arr[ro][co]=1;

//这里加判断代码,建议用一个判断函数,我给你写个吧

}

//我这里这个方法写了个大概,你看一下和你的代码结合一下就可以了,我用i和j作为for循环的变量,你用来做最大的长宽值,这些你都要改一下。

flag作为判断是否胜利的标志,playerNum是玩家的标志,分1和2,这个函数只写了左和左上的判断,其他方向楼主仿照着写

int win(int row, int col, int playerNum){

int i = 0,j = 0;

int flag = 1;

if(row - 4 = 0){

flag = 1;

for(i = row; i = 0; i--){

if(arr[i][j] != playerNum){

flag = 0;

break;

}

}

if(flag == 1){

return 1;

}

}

if(row - 4 = 0 col - 4 = 0){

flag = 1;

for(i = row ,j = col ; i = 0j=0; i--,j--){

if(arr[i][j] != playerNum){

flag = 0;

break;

}

}

if(flag == 1){

return 1;

}

}

}

求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五子棋代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/17419.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载