平面四子棋
事先声明:
代码是我自个儿写的,规则不是我发明的~~~~。
我觉得我真是太不务正业了!
规则
在一个7*12的棋盘里,下四子棋。只不过,这个棋盘是竖立着的,它的棋子是从上往下掉的。
比如说:
如果该红棋走了,我可以这么走:
然后红棋就会掉落,落在它下面那颗蓝棋的上面。
下一步就该蓝棋走,然后红棋……以此类推,谁先四个同色的棋子连成一条线,就获胜。
代码
话不多说,详见代码:
#include<cstdio>
#include<windows.h>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<conio.h>
using namespace std;
char map[25][25];
void init_map()
{
for(int j=1;j<=12;j++) map[0][j]=' ';
for(int i=1;i<=7;i++)
for(int j=1;j<=12;j++)
map[i][j]='O';
}
struct Player
{
char o;
}p[3];
void init_player()
{
p[1].o='A'; p[2].o='B';
}
int cnt;
void out_map()
{
system("cls");
for(int i=0;i<=7;i++)
for(int j=1;j<=12;j++)
{
cout<<map[i][j];
if(j==12) cout<<endl;
}
}
int drx[9]={0,1,-1,0,0,1,-1,1,-1};
int dry[9]={0,0,0,1,-1,1,-1,-1,1};
int check()
{
for(int i=1;i<=7;i++)
for(int j=1;j<=12;j++)
if(map[i][j]!='O')
{
int cnt,x,y;
for(int p=1;p<=8;p++)
{
cnt=0;
x=i; y=j;
for(int m=1;m<=4;m++)
{
x+=drx[p],y+=dry[p];
if(map[x][y]==map[i][j]) cnt++;
else break;
}
if(cnt==3)
{
x=i; y=j;
for(int m=1;m<=4;m++)
{
map[x][y]='*';
x+=drx[p],y+=dry[p];
out_map();
Sleep(150);
}
if(map[i][j]=='B') return 1;
else return 2;
}
}
}
return 0;
}
void won(int s)
{
char c;
if(s==1) c='B';
else c='A';
printf("Player %c has won the game !!!!\n",c);
}
int winer,round;
void godown(int y,char o)
{
int x=1;
while(map[x][y]=='O' and x<=7)
{
map[x][y]=o;
out_map();
Sleep(150);
map[x][y]='O';
x++;
}
x--;
map[x][y]=o;
out_map();
}
int bx=0,by=0;
void play(int round)
{
int k=round%2+1;
char c=getch(),o=p[k].o;
map[bx][by]='*';
out_map();
while(1)
{
if(c=='A' or c=='a' or c=='D' or c=='d')
{
map[bx][by]=' ';
if(c=='A' or c=='a') by--;
else by++;
if(by<=0 or by>12)
{
if(c=='A' or c=='a') by++;
else by--;
c=getch();
continue;
}
map[bx][by]='*';
out_map();
}
else
{
godown(by,o);
break;
}
c=getch();
}
}
int main()
{
init_map();
init_player();
out_map();
puts("\nHave fun playing! Made by C20182030.");
puts("Press any key to start :");
char c=getch();
while(1)
{
winer=check();
if(winer)
{
won(winer);
c=getch();
break;
}
for(int j=1;j<=12;j++) map[0][j]=' ';
round++;
play(round);
}
}