1.c?语言棋源码c语言程源代?????????Դ??
2.五子棋C语言代码
3.求助高手,求一个C语言五子棋的序设轻聊im源码bug代码 带流程图的!
4.C语言五子棋胜利判断式
c?计棋E4A自写课程表源码?????????Դ??
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#define TEXTS 7
#define CURSOR
#define CHESSBOARD
#define WHITECHESS
#define SELECTEDWHITE
#define BLACKCHESS
#define SELECTEDBLACK
#define qx1_num
#define qx2_num
typedef struct node
{
int step;
int color;
} NODE;
typedef struct point
{
int x;
int y;
}_POINT;
typedef struct qixing
{
char qx[8];
int value;
}QIXING;
HANDLE hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
_POINT cursor;
int direction[8][2]={ { 0,-1},{ 0,1},{ -1,0},{ 1,0},{ -1,-1},{ 1,1},{ -1,1},{ 1,-1}};
QIXING qx1[qx1_num]={ { "x",},{ "1x",},{ "x",},{ "0x",},{ "x",},{ "x",},
{ "0x",},{ "x",},{ "0x",},{ "0x",},{ "x",},
{ "x1",},{ "1x",},{ "x",},{ "x",},{ "1x",},
{ "x",},{ "0x",},{ "x",},{ "0x",},{ "0x",},
{ "0x",},{ "x",},{ "x2",},{ "1x",},{ "0x",},{ "0x",5}};
QIXING qx2[qx2_num]={ { "x",},{ "1x",},{ "x",},{ "0x",},{ "x",},{ "x",},
{ "0x",},{ "x",},{ "0x",},{ "0x",},{ "x",},
{ "x1",},{ "1x",},{ "x",},{ "1x",},{ "x",},
{ "0x",},{ "x",},{ "0x",},{ "0x",},
{ "0x",},{ "x",},{ "x2",},{ "1x",},{ "0x",},{ "0x",}};
//------------------------------------------------------------------------------------------------------
void textcolor(int color)
{
SetConsoleTextAttribute(hOutput, color);
}
void gotoxy(int x, int y)
{
COORD coordScreen={ 0,0};
coordScreen.X=x;
coordScreen.Y=y;
SetConsoleCursorPosition(hOutput,coordScreen);
}
void printnode(NODE chessboard[][], int x, int y)
{
textcolor(CHESSBOARD);
if(chessboard[x][y].step==0)
{
if(x==cursor.x&&y==cursor.y)
textcolor(CURSOR);
switch(x)
{
case 0:
if(y==0)printf("┏");
else if(y==)printf("┓");
else printf("┳");
break;
case 3:
if(y==0)printf("┣");
else if(y==3||y==)printf("╬");
else if(y==)printf("┫");
else printf("╋");
break;
case 7:
if(y==0)printf("┣");
else if(y==7)printf("╬");
else if(y==)printf("┫");
else printf("╋");
break;
case :
if(y==0)printf("┣");
else if(y==3||y==)printf("╬");
else if(y==)printf("┫");
else printf("╋");
break;
case :
if(y==0)printf("┗");
else if(y==)printf("┛");
else printf("┻");
break;
default:
if(y==0)printf("┣");
else if(y==)printf("┫");
else printf("╋");
}
}
else if(chessboard[x][y].color==0)
{
if(x==cursor.x&&y==cursor.y)
textcolor(SELECTEDWHITE);
else textcolor(WHITECHESS);
printf("●");
}
else
{
if(x==cursor.x&&y==cursor.y)
textcolor(SELECTEDBLACK);
else textcolor(BLACKCHESS);
printf("●");
}
}
void printchessboard(NODE chessboard[][])
{
int i,j;
char letter[]={ " A B C D E F G H I J K L M N O\n"};
for(i=0;i<;i++)
{
textcolor(TEXTS);
printf("%2d",-i);
for(j=0;j<;j++)
printnode(chessboard,i,j);
textcolor(TEXTS);
printf("\n");
}
textcolor(TEXTS);
printf("%s",letter);
printf("移动:方向键 下棋:ENTER 悔棋:U 退出:F");
}
void renew(NODE chessboard[][],int x,int y)
{
COORD coordScreen;
CONSOLE_SCREEN_BUFFER_INFO csbi;
if(x<0||x>||y<0||y>)
return;
if(!GetConsoleScreenBufferInfo(hOutput,&csbi))
return;
coordScreen=csbi.dwCursorPosition;
gotoxy((y-1)*2+4,x+1);
printnode(chessboard,x,y);
SetConsoleCursorPosition(hOutput,coordScreen);
}
void showmenu()
{
textcolor(TEXTS);
system("cls");
printf("1.人机对战\n2.双人对战\n3.退出\n\n请选择[1~3]:");
}
void showsubmenu()
{
textcolor(TEXTS);
system("cls");
printf("1.你先手\n2.电脑先手\n3.返回上级菜单\n\n请选择[1~3]:");
}
int getchoose(int min, int max)
{
int choose;
do
{
choose=getch()-;
}while(choose<min||choose>max);
printf("%d",choose);
return choose;
}
//-------------------------------------------------------------------------------------------------
bool quit;
bool regret;
bool getmove(NODE chessboard[][])
{
char c;
for(;;)
{
c=getch();
if(c==-)
switch(getch())
{
case :
cursor.x--;
if(cursor.x<0)cursor.x=0;
renew(chessboard,cursor.x+1,cursor.y);
renew(chessboard,cursor.x,cursor.y);
break;
case :
cursor.x++;
if(cursor.x>)
cursor.x=;
renew(chessboard,cursor.x-1,cursor.y);
renew(chessboard,cursor.x,cursor.y);
break;
case :
cursor.y--;
if(cursor.y<0)cursor.y=0;
renew(chessboard,cursor.x,cursor.y+1);
renew(chessboard,cursor.x,cursor.y);
break;
case :
cursor.y++;
if(cursor.y>)cursor.y=;
renew(chessboard,cursor.x,cursor.y-1);
renew(chessboard,cursor.x,cursor.y);
break;
case :
quit=true;
return true;
}
else if(c==&&chessboard[cursor.x][cursor.y].step==0)
return true;
else if(c=='U'||c=='u')
{
regret=true;
return true;
}
}
}
void beback(NODE chessboard[][], int step)
{
int i,j,tempx,tempy;
if(step==1)return;
if(step>2)
{
for(i=0;i<;i++)
for(j=0;j<;j++)
{
if(chessboard[i][j].step==step-1)
{
chessboard[i][j].step=0;
renew(chessboard,i,j);
}
else if(chessboard[i][j].step==step-2)
{
chessboard[i][j].step=0;
tempx=cursor.x;
tempy=cursor.y;
cursor.x=i;
cursor.y=j;
renew(chessboard,i,j);
renew(chessboard,tempx,tempy);
}
}
}
else if(step==2)
{
for(i=0;i<;i++)
for(j=0;j<;j++)
if(chessboard[i][j].step==step-1)
{
chessboard[i][j].step=0;
renew(chessboard,i,j);
}
tempx=cursor.x;
tempy=cursor.y;
cursor.x=7;
cursor.y=7;
renew(chessboard,i,j);
renew(chessboard,tempx,tempy);
}
}
//-----------------------------------------------------------------------------------------
bool inside(int x,int y)
{
if(x<0||x>||y<0||y>)return false;
return true;
}
int line(NODE chessboard[][],int dirt,int x,int y,int color)
{
int i;
for(i=0;chessboard[x+direction[dirt][0]][y+direction[dirt][1]].step>0&&
chessboard[x+direction[dirt][0]][y+direction[dirt][1]].color==color;i++)
{
x=x+direction[dirt][0];
y=y+direction[dirt][1];
if(!inside(x,y))return i;
}
return i;
}
bool win(NODE chessboard[][],int x,int y,int color)
{
if(line(chessboard,0,x,y,color)+line(chessboard,1,x,y,color)>3)
return true;
if(line(chessboard,2,x,y,color)+line(chessboard,3,x,y,color)>3)
return true;
if(line(chessboard,4,x,y,color)+line(chessboard,5,x,y,color)>3)
return true;
if(line(chessboard,6,x,y,color)+line(chessboard,7,x,y,color)>3)
return true;
return false;
}
//----------------------------------------------------------------------------------------------
int attacktrend,defenttrend;
bool macth1(NODE chessboard[][],int x,int y,int dirt,int kind,int color)
{
int k;
char c;
char *p;
p=strchr(qx1[kind].qx,'x');
for(k=0;k<=p-qx1[kind].qx;k++)
{
x-=direction[dirt][0];
y-=direction[dirt][1];
}
for(k=0;(unsigned)k<strlen(qx1[kind].qx);k++)
{
x+=direction[dirt][0];
y+=direction[dirt][1];
if(!inside(x,y))return(false);
if(chessboard[x][y].step>0&&chessboard[x][y].color==color)c='2';
else if(chessboard[x][y].step>0)c='1';
else c='0';
if(c=='0'&&qx1[kind].qx[k]=='x')continue;
if(c!=qx1[kind].qx[k])return(false);
}
return true;
}
int value_qx1(NODE chessboard[][],int x,int y,int dirt, int color)
{
int i;
for(i=0;i<qx1_num;i++)
if(macth1(chessboard,x,y,dirt,i,color))
return qx1[i].value;
return 0;
}
bool macth2(NODE chessboard[][],int x,int y,int dirt, int kind,int color)
{
int k;
char c;
char *p;
p=strchr(qx2[kind].qx,'x');
for(k=0;k<=p-qx2[kind].qx;k++)
{
x-=direction[dirt][0];
y-=direction[dirt][1];
}
for(k=0;(unsigned)k<strlen(qx2[kind].qx);k++)
{
x+=direction[dirt][0];
y+=direction[dirt][1];
if(!inside(x,y))return false;
if(chessboard[x][y].step>0&&chessboard[x][y].color==color)c='2';
else if(chessboard[x][y].step>0)c='1';
else c='0';
if(c=='0'&&qx2[kind].qx[k]=='x')continue;
if(c!=qx2[kind].qx[k])return(false);
}
return true;
}
int value_qx2(NODE chessboard[][],int x,int y,int dirt, int color)
{
int i;
for(i=0;i<qx2_num;i++)
if(macth2(chessboard,x,y,dirt,i,color))
return qx2[i].value;
return 0;
}
void AI(NODE chessboard[][], int *x,int *y,int color)
{
int max=0;
int maxi,maxj;
int i,j,k;
int probability=1;
int value[][]={ 0};
int valueattack[][]={ { 0}};
int valuedefent[][]={ { 0}};
for(i=0;i<;i++)
for(j=0;j<;j++)
{
if(chessboard[i][j].step>0)continue;
for(k=0;k<8;k++)
valuedefent[i][j]+=value_qx1(chessboard,i,j,k,color);
if(maxi<valuedefent[i][j])
maxi=valuedefent[i][j];
}
for(i=0;i<;i++)
for(j=0;j<;j++)
{
if(chessboard[i][j].step>0)continue;
for(k=0;k<8;k++)
valueattack[i][j]+=value_qx2(chessboard,i,j,k,color);
if(maxj<valuedefent[i][j])
maxj=valuedefent[i][j];
}
if(rand()%(maxi+maxj+1)>maxi)
{
attacktrend=1;
defenttrend=1;
}
else
{
attacktrend=1;
defenttrend=2;
}
for(i=0;i<;i++)
for(j=0;j<;j++)
{
value[i][j]=valuedefent[i][j]*defenttrend+valueattack[i][j]*attacktrend;
if(max<value[i][j])
{
max=value[i][j];
maxi=i;
maxj=j;
probability=1;
}
else if(max==value[i][j])
{
if(rand()%(probability+1)<probability)
probability++;
else
{
probability=1;
max=value[i][j];
maxi=i;
maxj=j;
}
}
}
*x=maxi;
*y=maxj;
}
//-------------------------------------------------------------------------------------------------
bool vshuman;
void Vs(bool human)
{
int i,j;
int color=1;
int lastx,lasty;
int computer;
NODE chessboard[][]={ { 0,0}};
if(!human)
{
showsubmenu();
switch(getchoose(1,3))
{
case 1:
computer=0;
attacktrend=1;
defenttrend=1;
break;
case 2:
computer=1;
attacktrend=1;
defenttrend=2;
break;
case 3:return;
}
}
for(i=0;i<;i++)
for(j=0;j<;j++)
chessboard[i][j].step=0;
cursor.x=7;
cursor.y=7;
quit=false;
system("cls");
printf("\n");
printchessboard(chessboard);
for(i=1;i<=;)
{
gotoxy(0,0);
textcolor(TEXTS);
printf(" 第%d手, ",i);
if(color==1)printf("黑棋下");
else printf("白棋下");
regret=false;
if(i>1)
{
if(color!=computer||human)getmove(chessboard);
else
{
lastx=cursor.x;
lasty=cursor.y;
AI(chessboard,&cursor.x,&cursor.y,color);
renew(chessboard,lastx,lasty);
}
}
if(quit)return;
if(regret)
{
beback(chessboard,i);
if(i>2)i-=2;
else if(i==2)
{
i=1; color=(color+1)%2;
}
}
else
{
chessboard[cursor.x][cursor.y].step=i++;
chessboard[cursor.x][cursor.y].color=color;
renew(chessboard,cursor.x,cursor.y);
color=(color+1)%2;
}
if(win(chessboard,cursor.x,cursor.y,(color+1)%2)&&!regret)
{
textcolor(TEXTS);
gotoxy(0,0);
printf(" ");
gotoxy(0,0);
if(color==1)printf(" 白棋赢了!");
else printf(" 黑棋赢了!");
getch();
return;
}
}
gotoxy(0,0);
printf(" ");
gotoxy(0,0);
printf(" 平局!");
}
int main()
{
srand((unsigned)time(NULL));
for(;;)
{
showmenu();
switch(getchoose(1,3))
{
case 1:Vs(false);break;
case 2:Vs(true);break;
case 3:printf("\n");return 0;
}
}
return 0;
}
五子棋C语言代码
五子棋C语言代码如下:
#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 0x/*上移--'W'*/
#define PLAY1DOWN 0x1f/*下移--'S'*/
#define PLAY1LEFT 0x1e/*左移--'A'*/
#define PLAY1RIGHT 0x/*右移--'D'*/
#define PLAY1DO 0x/*落子--空格键*/
/*定义2号玩家的操作键键码*/
#define PLAY2UP 0x/*上移--方向键up*/
#define PLAY2DOWN 0x/*下移--方向键down*/
#define PLAY2LEFT 0x4b/*左移--方向键left*/
#define PLAY2RIGHT 0x4d/*右移--方向键right*/
#define PLAY2DO 0x1c0d/*落子--回车键Enter*/
/*若想在游戏中途退出, 可按 Esc 键*/
#define ESCAPE 0xb
/*定义棋盘上交叉点的状态, 即该点有无棋子 */
/*若有棋子, 还应能指出是哪个玩家的棋子 */
#define CHESSNULL 0 /*没有棋子*/
#define CHESS1 'O'/*一号玩家的棋子*/
#define CHESS2 'X'/*二号玩家的棋子*/
/*定义按键类别*/
#define KEYEXv 0/*退出键*/
#define KEYFALLCHESS 1/*落子键*/
#define KEYMOVECURSOR 2/*光标移动键*/
#define KEYINVALID 3/*无效键*/
/*定义符号常量: 真, 假 --- 真为1, 假为0 */
#define TRUE 1
#define FALSE 0
/