五子棋又叫连五子或五子连,是全国智力运动会竞技项目之一,是一种两人对弈的纯策略型棋类游戏,起源于中国古代,起先是宫廷贵族的游戏,后来流传到了民间。
通常双方分别使用黑白两色的棋子,轮流下在棋盘竖线与横线的交叉点上,先形成五子连线者获胜。在专业五子棋竞赛规则中,另有“交换”、“禁手”等规定,用于限制先行方优势。[1]
五子棋由棋盘、棋子、谱纸、棋钟构成,棋子数量为黑色棋子113枚,白色棋子112枚[1],与围棋相似,属于传统的黑白棋种之一。五子棋的段位制最高段位是一级段位。[2]其主要流行于华人和汉字文化圈的国家,在欧美一些国家也有大量爱好者。[3]五子棋将科学、艺术、竞技、娱乐与教育五者融为一体,以下就是五子棋的代码了,给个免费的赞支持一下小编吧:
//win7版本
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<time.h>
#include<conio.h>
#include<windows.h>
#include<ctime>
using namespace std;
struct coordinate{
int X,Y;
}Coor1[626],Coor2[626];
char Getin=' ';
int Map[25][25],Sum1,Sum2;
int Map1[25][25];
void No(){
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
}
void _Coordinate_(int x,int y){
HANDLE winHandle;//句柄
COORD pos = {x,y};
winHandle = GetStdHandle(STD_OUTPUT_HANDLE);
//设置光标位置
SetConsoleCursorPosition(winHandle,pos);
}
int Hrong(int x,int y,int n){
if(Map[x][y]==n) return 5;
}
void Nturn(){
int x1[10],y1[10];
for(int i=Sum1-1;i>=0;i--){
int x=Coor2[i].X,y=Coor2[i].Y,Emc=0;
for(int j=0;j<10;j++){x1[j]=x;y1[j]=y;}
while(Map[x1[0]][y1[0]]==2){ x1[0]++;y1[0]++;Emc++;}
if(Emc==2&&Map[x-1][y-1]==0&&Map[x1[0]][y1[0]]==0){Map1[x1[0]][y1[0]]+=5+3*Hrong(x1[0]+1,y1[0]+1,2);}
if(Emc==3&&Map[x1[0]][y1[0]]==0){Map1[x1[0]][y1[0]]+=50+90*Hrong(x1[0]+1,y1[0]+1,2);}
if(Emc==4&&Map[x1[0]][y1[0]]==0){Map1[x1[0]][y1[0]]+=500;}
Emc=0;
while(Map[x1[1]][y1[1]]==2){ x1[1]--;y1[1]++;Emc++;}
if(Emc==2&&Map[x+1][y-1]==0&&Map[x1[1]][y1[1]]==0){Map1[x1[1]][y1[1]]+=5+3*Hrong(x1[1]-1,y1[1]+1,2);}
if(Emc==3&&Map[x1[1]][y1[1]]==0){Map1[x1[1]][y1[1]]+=50+90*Hrong(x1[1]-1,y1[1]+1,2);}
if(Emc==4&&Map[x1[1]][y1[1]]==0){Map1[x1[1]][y1[1]]+=500;}
Emc=0;
while(Map[x1[2]][y1[2]]==2){ x1[2]++;y1[2]--;Emc++;}
if(Emc==2&&Map[x-1][y+1]==0&&Map[x1[2]][y1[2]]==0){Map1[x1[2]][y1[2]]+=5+3*Hrong(x1[2]+1,y1[2]-1,2);}
if(Emc==3&&Map[x1[2]][y1[2]]==0){Map1[x1[2]][y1[2]]+=50+90*Hrong(x1[2]+1,y1[2]-1,2);}
if(Emc==4&&Map[x1[2]][y1[2]]==0){Map1[x1[2]][y1[2]]+=500;}
Emc=0;
while(Map[x1[3]][y1[3]]==2){ x1[3]--;y1[3]--;Emc++;}
if(Emc==2&&Map[x+1][y+1]==0&&Map[x1[3]][y1[3]]==0){Map1[x1[3]][y1[3]]+=5+3*Hrong(x1[3]-1,y1[3]-1,2);}
if(Emc==3&&Map[x1[3]][y1[3]]==0){Map1[x1[3]][y1[3]]+=50+90*Hrong(x1[3]-1,y1[3]-1,2);}
if(Emc==4&&Map[x1[3]][y1[3]]==0){Map1[x1[3]][y1[3]]+=500;}
Emc=0;
while(Map[x1[4]][y1[4]]==2){ x1[4]++;Emc++;}
if(Emc==2&&Map[x-1][y]==0&&Map[x1[4]][y1[4]]==0){Map1[x1[4]][y1[4]]+=5+3*Hrong(x1[4]+1,y1[4],2);}
if(Emc==3&&Map[x1[4]][y1[4]]==0){Map1[x1[4]][y1[4]]+=50+90*Hrong(x1[4]+1,y1[4],2);}
if(Emc==4&&Map[x1[4]][y1[4]]==0){Map1[x1[4]][y1[4]]+=500;}
Emc=0;
while(Map[x1[5]][y1[5]]==2){ x1[5]--;Emc++;}
if(Emc==2&&Map[x+1][y]==0&&Map[x1[5]][y1[5]]==0){Map1[x1[5]][y1[5]]+=5+3*Hrong(x1[5]-1,y1[5],2);}
if(Emc==3&&Map[x1[5]][y1[5]]==0){Map1[x1[5]][y1[5]]+=50+90*Hrong(x1[5]-1,y1[5],2);}
if(Emc==4&&Map[x1[5]][y1[5]]==0){Map1[x1[5]][y1[5]]+=500;}
Emc=0;
while(Map[x1[6]][y1[6]]==2){ y1[6]--;Emc++;}
if(Emc==2&&Map[x][y+1]==0&&Map[x1[6]][y1[6]]==0){Map1[x1[6]][y1[6]]+=5+3*Hrong(x1[6],y1[6]-1,2);}
if(Emc==3&&Map[x1[6]][y1[6]]==0){Map1[x1[6]][y1[6]]+=50+90*Hrong(x1[6],y1[6]-1,2);}
if(Emc==4&&Map[x1[6]][y1[6]]==0){Map1[x1[6]][y1[6]]+=500;}
Emc=0;
while(Map[x1[7]][y1[7]]==2){ y1[7]++;Emc++;}
if(Emc==2&&Map[x][y-1]==0&&Map[x1[7]][y1[7]]==0){Map1[x1[7]][y1[7]]+=5+3*Hrong(x1[7],y1[7]+1,2);}
if(Emc==3&&Map[x1[7]][y1[7]]==0){Map1[x1[7]][y1[7]]+=50+90*Hrong(x1[7],y1[7]+1,2);}
if(Emc==4&&Map[x1[7]][y1[7]]==0){Map1[x1[7]][y1[7]]+=500;}
}
}
void Uturn(){
int x1[10],y1[10];
for(int i=Sum1-1;i>=0;i--){
int x=Coor1[i].X,y=Coor1[i].Y,Emc=0;
for(int j=0;j<10;j++){x1[j]=x;y1[j]=y;}
while(Map[x1[0]][y1[0]]==1){ x1[0]++;y1[0]++;Emc++;}
if(Emc==2&&Map[x-1][y-1]==0&&Map[x1[0]][y1[0]]==0){Map1[x1[0]][y1[0]]+=5+4*Hrong(x1[0]+1,y1[0]+1,1);}
if(Emc==3&&Map[x-1][y-1]==0&&Map[x1[0]][y1[0]]==0){Map1[x1[0]][y1[0]]+=45+2*Hrong(x1[0]+1,y1[0]+1,1);}
if(Emc==4&&Map[x-1][y-1]==2&&Map[x1[0]][y1[0]]==0){Map1[x1[0]][y1[0]]+=55;}
Emc=0;
while(Map[x1[1]][y1[1]]==1){ x1[1]--;y1[1]++;Emc++;}
if(Emc==2&&Map[x+1][y-1]==0&&Map[x1[1]][y1[1]]==0){Map1[x1[1]][y1[1]]+=5+4*Hrong(x1[1]-1,y1[1]+1,1);}
if(Emc==3&&Map[x+1][y-1]==0&&Map[x1[1]][y1[1]]==0){Map1[x1[1]][y1[1]]+=45+2*Hrong(x1[1]-1,y1[1]+1,1);}
if(Emc==4&&Map[x+1][y-1]==2&&Map[x1[1]][y1[1]]==0){Map1[x1[1]][y1[1]]+=55;}
Emc=0;
while(Map[x1[2]][y1[2]]==1){ x1[2]++;y1[2]--;Emc++;}
if(Emc==2&&Map[x-1][y+1]==0&&Map[x1[2]][y1[2]]==0){Map1[x1[2]][y1[2]]+=5+4*Hrong(x1[2]+1,y1[2]-1,1);}
if(Emc==3&&Map[x-1][y+1]==0&&Map[x1[2]][y1[2]]==0){Map1[x1[2]][y1[2]]+=45+2*Hrong(x1[2]+1,y1[2]-1,1);}
if(Emc==4&&Map[x-1][y+1]==2&&Map[x1[2]][y1[2]]==0){Map1[x1[2]][y1[2]]+=55;}
Emc=0;
while(Map[x1[3]][y1[3]]==1){ x1[3]--;y1[3]--;Emc++;}
if(Emc==2&&Map[x+1][y+1]==0&&Map[x1[3]][y1[3]]==0){Map1[x1[3]][y1[3]]+=5+4*Hrong(x1[3]-1,y1[3]-1,1);}
if(Emc==3&&Map[x+1][y+1]==0&&Map[x1[3]][y1[3]]==0){Map1[x1[3]][y1[3]]+=45+2*Hrong(x1[3]-1,y1[3]-1,1);}
if(Emc==4&&Map[x+1][y+1]==2&&Map[x1[3]][y1[3]]==0){Map1[x1[3]][y1[3]]+=55;}
Emc=0;
while(Map[x1[4]][y1[4]]==1){ x1[4]++;Emc++;}
if(Emc==2&&Map[x-1][y]==0&&Map[x1[4]][y1[4]]==0){Map1[x1[4]][y1[4]]+=5+4*Hrong(x1[4]+1,y1[4],1);}
if(Emc==3&&Map[x-1][y]==0&&Map[x1[4]][y1[4]]==0){Map1[x1[4]][y1[4]]+=45+2*Hrong(x1[4]+1,y1[4],1);}
if(Emc==4&&Map[x-1][y]==2&&Map[x1[4]][y1[4]]==0){Map1[x1[4]][y1[4]]+=55;}
Emc=0;
while(Map[x1[5]][y1[5]]==1){ x1[5]--;Emc++;}
if(Emc==2&&Map[x+1][y]==0&&Map[x1[5]][y1[5]]==0){Map1[x1[5]][y1[5]]+=5+4*Hrong(x1[5]-1,y1[5],1);}
if(Emc==3&&Map[x+1][y]==0&&Map[x1[5]][y1[5]]==0){Map1[x1[5]][y1[5]]+=45+2*Hrong(x1[5]-1,y1[5],1);}
if(Emc==4&&Map[x+1][y]==2&&Map[x1[5]][y1[5]]==0){Map1[x1[5]][y1[5]]+=55;}
Emc=0;
while(Map[x1[6]][y1[6]]==1){ y1[6]--;Emc++;}
if(Emc==2&&Map[x][y+1]==0&&Map[x1[6]][y1[6]]==0){Map1[x1[6]][y1[6]]+=5+4*Hrong(x1[6],y1[6]-1,1);}
if(Emc==3&&Map[x][y+1]==0&&Map[x1[6]][y1[6]]==0){Map1[x1[6]][y1[6]]+=45+2*Hrong(x1[6],y1[6]-1,1);}
if(Emc==4&&Map[x][y+1]==2&&Map[x1[6]][y1[6]]==0){Map1[x1[6]][y1[6]]+=55;}
Emc=0;
while(Map[x1[7]][y1[7]]==1){ y1[7]++;Emc++;}
if(Emc==2&&Map[x][y-1]==0&&Map[x1[7]][y1[7]]==0){Map1[x1[7]][y1[7]]+=5+4*Hrong(x1[7],y1[7]+1,1);}
if(Emc==3&&Map[x][y-1]==0&&Map[x1[7]][y1[7]]==0){Map1[x1[7]][y1[7]]+=45+2*Hrong(x1[7],y1[7]+1,1);}
if(Emc==4&&Map[x][y-1]==2&&Map[x1[7]][y1[7]]==0){Map1[x1[7]][y1[7]]+=55;}
}
}
void ESET(){
for(int i=2;i<23;i++)
for(int j=2;j<23;j++){
if(Map[i][j]==0&&Map[i+1][j]!=0&&Map[i-1][j]==Map[i+1][j]&&(Map[i+2][j]==0||Map[i+2][j]==Map[i+1][j])&&(Map[i-2][j]==0||Map[i-2][j]==Map[i-1][j]))
Map1[i][j]+=5;
if(Map[i][j]==0&&Map[i][j+1]!=0&&Map[i][j-1]==Map[i][j+1]&&(Map[i][j+2]==0||Map[i][j+2]==Map[i][j+1])&&(Map[i][j-2]==0||Map[i][j-2]==Map[i][j-1]))
Map1[i][j]+=5;
if(Map[i][j]==0&&Map[i+1][j+1]!=0&&Map[i-1][j-1]==Map[i+1][j+1]&&(Map[i+2][j+2]==0||Map[i+2][j+2]==Map[i+1][j+1])&&(Map[i-2][j-2]==0||Map[i-2][j-2]==Map[i-1][j-1]))
Map1[i][j]+=5;
if(Map[i][j]==0&&Map[i-1][j+1]!=0&&Map[i+1][j-1]==Map[i-1][j+1]&&(Map[i-2][j+2]==0||Map[i-2][j+2]==Map[i-1][j+1])&&(Map[i+2][j-2]==0||Map[i+2][j-2]==Map[i+1][j-1]))
Map1[i][j]+=5;
}
}
void AI(){
if(Sum1==0){
Map[12][12]=2;
Coor2[Sum2].X=12;
Coor2[Sum2++].Y=12;
return;
}
for(int i=0;i<25;i++)
for(int j=0;j<25;j++)
Map1[i][j]=0;
int x1[10],y1[10],Ax,Ay,Max=0;
ESET();
Uturn();
Nturn();
for(int i=Sum1-1;i>=0;i--){
int x=Coor1[i].X,y=Coor1[i].Y,Emc[10]={0};
for(int j=0;j<10;j++){x1[j]=x;y1[j]=y;}
while(Map[x1[0]][y1[0]]==1){ x1[0]++;y1[0]++;Emc[0]++;}
while(Map[x1[1]][y1[1]]==1){ x1[1]--;y1[1]++;Emc[1]++;}
while(Map[x1[2]][y1[2]]==1){ x1[2]++;y1[2]--;Emc[2]++;}
while(Map[x1[3]][y1[3]]==1){ x1[3]--;y1[3]--;Emc[3]++;}
while(Map[x1[4]][y1[4]]==1){ x1[4]++;Emc[4]++;}
while(Map[x1[5]][y1[5]]==1){ x1[5]--;Emc[5]++;}
while(Map[x1[6]][y1[6]]==1){ y1[6]--;Emc[6]++;}
while(Map[x1[7]][y1[7]]==1){ y1[7]++;Emc[7]++;}
for(int j=0;j<8;j++)
if(Map[x1[j]][y1[j]]==0) Map1[x1[j]][y1[j]]+=Emc[j];
}
for(int i=Sum2-1;i>=0;i--){
int x=Coor2[i].X,y=Coor2[i].Y,Emc[10]={0};
for(int j=0;j<10;j++){x1[j]=x;y1[j]=y;}
while(Map[x1[0]][y1[0]]==2){ x1[0]++;y1[0]++;Emc[0]++;}
while(Map[x1[1]][y1[1]]==2){ x1[1]--;y1[1]++;Emc[1]++;}
while(Map[x1[2]][y1[2]]==2){ x1[2]++;y1[2]--;Emc[2]++;}
while(Map[x1[3]][y1[3]]==2){ x1[3]--;y1[3]--;Emc[3]++;}
while(Map[x1[4]][y1[4]]==2){ x1[4]++;Emc[4]++;}
while(Map[x1[5]][y1[5]]==2){ x1[5]--;Emc[5]++;}
while(Map[x1[6]][y1[6]]==2){ y1[6]--;Emc[6]++;}
while(Map[x1[7]][y1[7]]==2){ y1[7]++;Emc[7]++;}
for(int j=0;j<8;j++)
if(Map[x1[j]][y1[j]]==0) Map1[x1[j]][y1[j]]+=Emc[j];
}
for(int i=0;i<25;i++)
for(int j=0;j<25;j++)
if(Map1[i][j]>Max){
Max=Map1[i][j];
Ax=i;
Ay=j;
}
Map[Ax][Ay]=2;
Coor2[Sum2].X=Ax;
Coor2[Sum2++].Y=Ay;
return;
}
void Win(){
int x1[10],y1[10],Max1=0,Max2=0;
for(int i=0,e=0;i<Sum1;i++){
int x=Coor1[i].X,y=Coor1[i].Y;
for(int j=0;j<10;j++){x1[j]=x;y1[j]=y;}
while(Map[x1[0]][y1[0]]==1){ x1[0]++;y1[0]++;e++;}
Max1=max(e,Max1);
e=0;
while(Map[x1[1]][y1[1]]==1){ x1[1]--;y1[1]++;e++;}
Max1=max(e,Max1);
e=0;
while(Map[x1[2]][y1[2]]==1){ x1[2]++;y1[2]--;e++;}
Max1=max(e,Max1);
e=0;
while(Map[x1[3]][y1[3]]==1){ x1[3]--;y1[3]--;e++;}
Max1=max(e,Max1);
e=0;
while(Map[x1[4]][y1[4]]==1){ x1[4]++;e++;}
Max1=max(e,Max1);
e=0;
while(Map[x1[5]][y1[5]]==1){ x1[5]--;e++;}
Max1=max(e,Max1);
e=0;
while(Map[x1[6]][y1[6]]==1){ y1[6]--;e++;}
Max1=max(e,Max1);
e=0;
while(Map[x1[7]][y1[7]]==1){ y1[7]++;e++;}
Max1=max(e,Max1);
e=0;
if(Max1>=5){
cout<<"电脑就是给智障,你赢了!!!";
getchar();
exit(0);
}
}
for(int i=0,e=0;i<Sum2;i++){
int x=Coor2[i].X,y=Coor2[i].Y;
for(int j=0;j<10;j++){x1[j]=x;y1[j]=y;}
while(Map[x1[0]][y1[0]]==2){ x1[0]++;y1[0]++;e++;}
Max2=max(e,Max2);
e=0;
while(Map[x1[1]][y1[1]]==2){ x1[1]--;y1[1]++;e++;}
Max2=max(e,Max2);
e=0;
while(Map[x1[2]][y1[2]]==2){ x1[2]++;y1[2]--;e++;}
Max2=max(e,Max2);
e=0;
while(Map[x1[3]][y1[3]]==2){ x1[3]--;y1[3]--;e++;}
Max2=max(e,Max2);
e=0;
while(Map[x1[4]][y1[4]]==2){ x1[4]++;e++;}
Max2=max(e,Max2);
e=0;
while(Map[x1[5]][y1[5]]==2){ x1[5]--;e++;}
Max2=max(e,Max2);
e=0;
while(Map[x1[6]][y1[6]]==2){ y1[6]--;e++;}
Max2=max(e,Max2);
e=0;
while(Map[x1[7]][y1[7]]==2){ y1[7]++;e++;}
Max2=max(e,Max2);
e=0;
if(Max2>=5){
cout<<"你就是给智障,电脑赢了!!!";
getchar();
exit(0);
}
}
}
void start(bool a){
char in=0;
int X=12,Y=12;
bool Flag=1;
system("mode con cols=51 lines=27");
if(a){
while(in!='\n'){
_Coordinate_(0,0);
for(int i=0;i<25;i++){
for(int j=0;j<25;j++){
if(i==X&&j==Y){cout<<"╳";continue;}
if(Map[i][j]==0){cout<<"╋";continue;}
}
cout<<"\n";
}
in=getch();
if(in=='w') X--;
if(in=='s') X++;
if(in=='a') Y--;
if(in=='d') Y++;
if(in==' '){Map[X][Y]=1;Coor1[Sum1].X=X;Coor1[Sum1++].Y=Y;break;}
}
}while(1){
_Coordinate_(0,0);
if(Flag) AI();
for(int i=0;i<25;i++){
for(int j=0;j<25;j++){
if(i==X&&j==Y){cout<<"╳";continue;}
if(Map[i][j]==2){cout<<"○";continue;}
if(Map[i][j]==1){cout<<"●";continue;}
if(Map[i][j]==0){cout<<"╋";continue;}
}
cout<<"\n";
}if(Flag) Win();
in=getch();
Flag=0;
if(in=='w'){X--;Flag=0;}
if(in=='s'){X++;Flag=0;}
if(in=='a'){Y--;Flag=0;}
if(in=='d'){Y++;Flag=0;}
if(in==' '){
if(Map[X][Y]) Flag=0;
else{
Map[X][Y]=1;
Flag=1;
Coor1[Sum1].X=X;
Coor1[Sum1++].Y=Y;
}
}
}
}
void manu1(){
_Coordinate_(0,0);
cout<<"■■■■■■■■■■\n";
cout<<"■ 1.先手 ■\n";
cout<<"■ 2.后手 ■\n";
cout<<"■■■■■■■■■■";
while(1){
char in=getch();
if(in=='1') start(1);
if(in=='2') start(0);
}
}
void manu(){
system("cls");
cout<<"■■■■■■■■■■\n";
cout<<"■ abs五子棋 ■\n";
cout<<"■ 1.开局 ■\n";
cout<<"■■■■■■■■■■";
while(1){
char in=getch();
if(in=='1') manu1();
}
}
int main(){
system("mode con cols=21 lines=5");
No();
manu();
return 0;
}