闲来无事,同学们想下五子棋,就写了一个代码,现在分享出来,让颓废时的我们学习之余的我们多一些乐趣。
#define NONE "\e[0m"
#define BLACK "\e[0;30m"
#define L_BLACK "\e[1;30m"
#define RED "\e[0;31m"
#define L_RED "\e[1;31m"
#define GREEN "\e[0;32m"
#define L_GREEN "\e[1;32m"
#define BROWN "\e[0;33m"
#define YELLOW "\e[1;33m"
#define BLUE "\e[0;34m"
#define L_BLUE "\e[1;34m"
#define PURPLE "\e[0;35m"
#define L_PURPLE "\e[1;35m"
#define CYAN "\e[0;36m"
#define L_CYAN "\e[1;36m"
#define GRAY "\e[0;37m"
#define WHITE "\e[1;37m"
#define BOLD "\e[1m"
#define UNDERLINE "\e[4m"
#define BLINK "\e[5m"
#define REVERSE "\e[7m"
#define HIDE "\e[8m"
#define CLEAR "\e[2J"
#define CLRLINE "\r\e[K" //or "\e[1K\r"
#include<bits/stdc++.h>
using namespace std;
const int N=1000;
char nam[10];
char namea[3],nameb[3];
int a[N][N];
int n=17;
int b;
bool check(int x,int y,int val)
{
int cnt=1,l=x,r=y;l=x-1,r=y-1;
while(a[l][y]==val&&l>=2) cnt++,l--;l=x+1;
while(a[l][y]==val&&l<=17) cnt++,l++;l=x-1;
if(cnt>=5) return 1;cnt=1;
while(a[x][r]==val&&r>=2) cnt++,r--;r=y+1;
while(a[x][r]==val&&r<=17) cnt++,r++;r=y-1;
if(cnt>=5) return 1;cnt=1;
while(a[l][r]==val&&l>=2&&r>=2) cnt++,l--,r--;l=x+1,r=y+1;
while(a[l][r]==val&&l<=17&&r<=17) cnt++,l++,r++;l=x-1,r=y+1;
if(cnt>=5) return 1;cnt=1;
while(a[l][r]==val&&l>=2&&r<=17) cnt++,l--,r++;l=x+1,r=y-1;
while(a[l][r]==val&&l<=17&&r>=2) cnt++,l++,r--;l=x,r=y;
if(cnt>=5) return 1;
return 0;
}
void cou(){
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
if(i==1&&j==1){
printf(REVERSE WHITE " ");
continue;
}
if(!a[i][j]){
int c=(i-1)*16+j;
c-=17;
printf(REVERSE L_BLACK "|%3d|",c);
}
else {
if(a[i][j]==2&&i!=1&&j!=1){
printf(YELLOW "|");
printf(YELLOW "%c%c%c",nameb[0],nameb[1],nameb[2]);
// cout<<nameb;
printf(YELLOW "|");
}
else if(a[i][j]==3&&i!=1&&j!=1){
printf(L_CYAN "|");
printf(L_CYAN "%c%c%c",namea[0],namea[1],namea[2]);
// cout<<namea;
printf(L_CYAN "|");
}
else{
printf(WHITE" %2d ",a[i][j]);
}
}
}
cout<<endl;
for(int j=1;j<=n;++j){
if(i==1&&j==1){
printf(REVERSE WHITE " ");
continue;
}
if(!a[i][j]){
printf(L_BLACK "|___|");
}
else {
if(a[i][j]==2&&i!=1&&j!=1){
printf(YELLOW "|___|");
}
else if(a[i][j]==3&&i!=1&&j!=1){
printf(L_CYAN "|___|");
// printf(REVERSE "X ");
}
else{
printf(WHITE" ");
}
}
}
cout<<endl;
}
}
bool again(){
cout<<WHITE "Do you want to play again?"<<endl;
char qwe;
while(qwe!='y'&&qwe!='Y'&&qwe!='N'&&qwe!='n'){
qwe=getchar();
}
char sbwy;
while((sbwy>'9'||sbwy<'0')&&sbwy!='\n'){
sbwy=getchar();
}
if(qwe=='y'||qwe=='Y'){
return true;
}
else{
return false;
}
}
bool asd(){
int xc,yc;
b=2;
memset(a,0,sizeof(a));
for(int i=1;i<=n;++i){
a[1][i+1]=i;
a[i+1][1]=i;
}
cou();
while(1){
int xxc=1;
scanf("%d",&xxc);
if(xxc==0){
return again();
}
xc=(xxc-1)/16+1;
yc=xxc-(xc-1)*(16);
++xc;++yc;
while(a[xc][yc]||xxc>256){
cout<<"It has been token\n";
scanf("%d",&xxc);
if(xxc==0){
return again();
}
xc=(xxc-1)/16+1;
yc=xxc-(xc-1)*(16);
++xc;++yc;
}
b^=1;
a[xc][yc]=b;
cou();
if(check(xc,yc,b)){
if(b==2){
printf("%3s",nameb);
cout<<"win"<<endl;
}
else{
printf("%3s",namea);
cout<<"win"<<endl;
}
return again();
}
}
}
void wd(){
printf(REVERSE "created by SDFZ \n");
printf("ZJJ\n blog https://www.cnblogs.com/zjjjj/ \n");
printf("WCR\n blog https://www.cnblogs.com/skyline-oi/ \n");
printf("ZKX\n blog https://www.cnblogs.com/SpadeA261/ \n");
cout<<endl;
printf("Thank you for playing my game. \n");
}
int main(){
cout<<BOLD WHITE"Welcome to gomoku"<<endl;
getchar();
printf("it's better to set your font to what you like and font size to 9\npress 0 during the game can shut it down\n");
printf("Player1 will go first\nplease type the number of the block to put stone\n");
printf("Please type in your name\n");
printf("Player1 name:\n");
cin>>nam;
namea[0]=nam[0];
namea[1]=nam[1];
namea[2]=nam[2];
getchar();
printf("Player2 name:\n");
cin>>nam;
nameb[0]=nam[0];
nameb[1]=nam[1];
nameb[2]=nam[2];
getchar();
printf("enter");
getchar();
while(1){
if(!asd()){
wd();
break;
}
}
return 0;
}