C++各种棋类游戏,种类齐全,十分好玩(海战棋,井字棋,三子棋,象棋,围棋,五子棋)
C++各种棋类游戏,种类齐全,十分好玩(海战棋,井字棋,三子棋,象棋,围棋,五子棋)
·
五子棋
#include <cstdio>
#include <windows.h>
#include <cstdlib>
#include <conio.h>
#include <iostream>
#include <cstring>
using namespace std;
#define Forij(x) for(int i=1;i<=x;i++)for(int j=1;j<=x;j++)
#define N 25
typedef long long LL;
LL fx[4][2]={{1,1},{1,0},{0,1},{1,-1}};
LL Q,GG;
string C[20]={"●","○","﹢","═","║","╔","╚","╗","╝","?"};//╋
void color(LL a){//颜色函数
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void gotoxy(LL x,LL y){
COORD pos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
struct Gomoku{
LL m[50][50],nx,ny;
void reset(){
system("cls");
memset(m,-1,sizeof(m));
color(7);
for (LL i=1; i<=N; i++){
gotoxy(0,i);cout<<C[4]; gotoxy(N+1,i);cout<<C[4];
gotoxy(i,0);cout<<C[3]; gotoxy(i,N+1);cout<<C[3];
}
gotoxy(0,0);cout<<C[5]; gotoxy(0,N+1);cout<<C[6];
gotoxy(N+1,0);cout<<C[7]; gotoxy(N+1,N+1);cout<<C[8];
color(3);
Forij(N){
gotoxy(i,j); cout<<C[2];
}
nx=ny=N/2+1; gotoxy(nx,ny);
}
void _drop(LL x,LL i,LL j){
m[i][j]=x;
gotoxy(i,j);
color(15); cout<<C[x];
}
LL check(){
Forij(N){
for (LL Fx=0,tmp,lst,xx,yy; Fx<4; Fx++) if(m[i][j]!=-1){
xx=i,yy=j,tmp=0,lst=m[i][j];
for (LL k=1; k<=5; k++){
if (xx>N || yy>N) break;
if (m[xx][yy]==(lst^1)){break;}
if (m[xx][yy]==lst) tmp++;
xx+=fx[Fx][0],yy+=fx[Fx][1];
}
if (tmp==5){
return lst;
}
}
}
return -1;
}
LL arnd(LL x,LL y){
LL cnt=0;
for (LL i=x-1; i<=x+1; i++) if (i>0 && i<=N)
for (LL j=y-1; j<=y+1; j++) if (j>0 && j<=N)
if (m[i][j]>-1) cnt++;
return cnt;
}
void get_val(LL x,LL y,LL &val){
val=0;
Forij(N){
for (LL Fx=0,tmp,tk,xx,yy; Fx<4; Fx++){
xx=i,yy=j,tmp=tk=0;
for (LL k=1; k<=5; k++){
if (xx>N || yy>N){tmp=0; break;}
if (m[xx][yy]==(x^1)){tmp=0; break;}
if (m[xx][yy]==x) tmp++,tk+=(1<<(k-1));
xx+=fx[Fx][0],yy+=fx[Fx][1];
}
switch(tmp){
case 5:
val+=8000000000; break;
case 4:
val+=1000+350*y; break;
case 3:
val+=(tk==14)?(300+600*y):(300+200*y); break;
case 2:
val+=3+2*y; break;
case 1:
val+=1+y; break;
}
}
}
}
void AI(LL x){
LL best,brnd,bi,bj,v1,v2,kkk;
best=-2147483647;
brnd=-2147483647;
Forij(N) if (m[i][j]==-1){
m[i][j]=x;
get_val(x,10,v1); //gotoxy(N+5,N/2);printf("%d ",v1);
get_val(x^1,80,v2); //gotoxy(N+5,N/2+1);printf("%d ",v2);
if (v1-v2>best) bi=i,bj=j,best=v1-v2;
if (v1-v2==best)
if ((kkk=arnd(i,j))>brnd)
brnd=kkk,bi=i,bj=j;
m[i][j]=-1;
}
_drop(x,bi,bj);
}
void HM(LL x){
char ch=getch();
for (;;ch=getch()){
if (ch=='w') {if (ny>1) ny--;}
else if (ch=='s') {if (ny<N) ny++;}
else if (ch=='a') {if (nx>1) nx--;}
else if (ch=='d') {if (nx<N)nx++;}
else if (m[nx][ny]==-1){_drop(x,nx,ny); return;}
gotoxy(nx,ny);
}
}
} A;
int main(){
for (;;){
A.reset();
for (GG=-1;;){
gotoxy(A.nx,A.ny);
A.HM(0); GG=A.check(); if (GG>-1) break;
A.AI(1); GG=A.check(); if (GG>-1) break;
}
gotoxy(5,N+3);
if (GG==0) printf("AI_1 win!");
if (GG==1) printf("AI_2 wins!");
while (kbhit()) getch();
Sleep(500);
gotoxy(5,N+3);
printf("Press anything to continue.");
getch();
}
}
围棋
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
const int n=19,rangzi=7;//棋盘大小,让子子数
int x,y,turn,a[101][101],vis[101][101],vist[101][101],ans,p;char g;bool flag;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
bool dfs1(int x,int y,int u){
if(x==0||y==0||x==n+1||y==n+1||a[x][y]==3-u)return true;
if(a[x][y]==0)return false;
vis[x][y]=true;
for(int i=0;i<=3;++i)if(!vis[x+dx[i]][y+dy[i]]&&!dfs1(x+dx[i],y+dy[i],u))return false;
return true;
}
void dfs2(int x,int y,int u){
if(x==0||y==0||x==n+1||y==n+1||a[x][y]!=u)return;
a[x][y]=0;for(int i=0;i<=3;++i)dfs2(x+dx[i],y+dy[i],u);
}
bool dfs3(int x,int y){
if(x==0||y==0||x==n+1||y==n+1||a[x][y]==1)return true;
if(a[x][y]==2)return false;
vis[x][y]=true;
for(int i=0;i<=3;++i)if(!vis[x+dx[i]][y+dy[i]]&&!dfs3(x+dx[i],y+dy[i]))return false;
return true;
}
void dfs4(int x,int y){
if(x==0||y==0||x==n+1||y==n+1||vist[x][y]||a[x][y]==2)return;
ans++;vist[x][y]=1;
if(a[x][y]==0){a[x][y]=3;for(int i=0;i<=3;++i)dfs4(x+dx[i],y+dy[i]);}
}
void putchess(){
system("cls");
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
putchar(i==x&&j==y?'>':' ');
putchar(a[i][j]==3?',':a[i][j]==0?((i==4||i==n-3||2*i==n+1)&&(j==4||j==n-3||2*j==n+1))?'*':'.':a[i][j]==1?'@':'O');
}
putchar('\n');
}
}
bool control(char g){
x=(g=='w'?x+n-2:g=='s'?x:x-1)%n+1;
y=(g=='a'?y+n-2:g=='d'?y:y-1)%n+1;
return g==' ';
}
int main(){
x=y=(n+1)/2;turn=1;
while(true){
putchess();puts("按三下P结束游戏。");
g=_getch();
if(g=='p'){
p++;if(p==3)break;continue;
}else p=0;
if(control(g)){
if(a[x][y]!=0)continue;
a[x][y]=turn;flag=false;
for(int i=0;i<=3;++i){
memset(vis,0,sizeof(vis));
if(a[x+dx[i]][y+dy[i]]==3-turn&&dfs1(x+dx[i],y+dy[i],3-turn))flag=true,dfs2(x+dx[i],y+dy[i],3-turn);
}
if(!flag&&dfs1(x,y,turn)){a[x][y]=0;continue;}
turn=3-turn;
}
}
p=0;
while(true){
putchess();puts("请清除所有死子;按三下p结束清除。");
g=_getch();
if(g=='p'){
p++;if(p==3)break;continue;
}else p=0;
if(control(g)){a[x][y]=0;continue;}
}
for(int i=1;i<=n;++i)for(int j=1;j<=n;++j){
memset(vis,0,sizeof(vis));
if(dfs3(i,j))dfs4(i,j);
}
putchess();printf("黑棋%d目,白棋%d目,%s赢了!",ans,n*n-ans,ans*2-rangzi>n*n?"黑棋":"白棋");
Sleep(3000);
}
象棋
#include <iostream>
#include <memory.h>
#include <cmath>
using namespace std;//存储结构:chess类是基类,派生类是各种棋子,在chessboard类中用chess的指针调用各个棋子
class chessboard;
class chess{
private:
int id;//等级
public:
chess(int i):id(i){}
int get(){return id;}
virtual bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy)=0;
virtual ~chess(){};//虚析构
};
class chessboard{
private:
chess *c[10][9];
char chessword[15][4]={"帅","相","炮","士","車","馬","兵","","卒","马","车","仕","砲","象","将"};//名字
public:
chessboard(){memset(c, NULL, sizeof(c));};//把指针初始化为零指针
void init();
chess* get(int x,int y){return c[x][y];}
int getid(int x,int y){ if(c[x][y]!=NULL) return c[x][y]->get();return 0;}
void show();
void play();
bool move(int startx,int starty,int aimx,int aimy);
~chessboard();//析构函数
static bool end;//判断结束
static int player;
};
bool chessboard::end=true;
int chessboard::player=-1;
bool chessboard::move(int startx,int starty,int aimx,int aimy){
if(startx>=0&&startx<10&&starty>=0&&starty<9//初步判断传入的点是否符合规则
&&aimx>=0&&aimx<10&&aimy>=0&&aimy<9
&&getid(startx,starty)&&getid(startx,starty)*player>0
&&c[startx][starty]->judge_move(*this,startx,starty,aimx,aimy)){
if(c[aimx][aimy]!=NULL) delete c[aimx][aimy];//吃子
c[aimx][aimy]=c[startx][starty];
c[startx][starty]=NULL;
player*=-1;
return true;
}
cout<<"走法错误,不符合规则"<<endl;
return false;
}
class horse : public chess{//马的实现
public:
horse(int i) : chess((i==0?-2:2)){}
bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){
int tempx=aimx-startx,tempy=aimy-starty;
int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy);
if(sid*aid<=0&&(tempx*tempx+tempy*tempy==5)&&!cb.get(startx+tempx/2,starty+tempy/2))
return true;
return false;
}
};
class soldier : public chess{//兵(卒)的实现
public:
soldier(int c) : chess((c==0?-1:1)){}
bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){
int tempx=aimx-startx,tempy=aimy-starty;
int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy);
if(sid*aid<=0&&sid*tempx<=0){
if(abs(tempx)==1&&tempy==0) return true;
if(abs(tempy)==1&&tempx==0)
if((startx/5==0&&sid>0)||(startx/5==1&&sid<0)) return true;
return false;
}
return false;
}
};
class general : public chess{//帅(将)的实现
public:
general(int c) : chess((c==0?-7:7)){}
bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){
int tempx=aimx-startx,tempy=aimy-starty;
int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy);
if(sid*aid<=0&&tempy*tempy+tempx*tempx==1&&aimx%7>=0&&aimx%7<=2&&aimy>=3&&aimy<=5)
return true;
return false;
}
~general(){chessboard::end=false;}
};
class elephant : public chess{//象(相)的实现
public:
elephant(int c) : chess((c==0?-6:6)){}
bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){
int tempx=aimx-startx,tempy=aimy-starty;
int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy);
if(sid*aid<=0&&tempy*tempy+tempx*tempx==8&&startx/5==aimx/5&&!cb.get(startx+tempx/2,starty+tempy/2))
return true;
return false;
}
};
class cannon : public chess{//炮的实现
public:
cannon(int c) : chess((c==0?-5:5)){}
bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){
int tempx=aimx-startx,tempy=aimy-starty;
int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy);
if(sid*aid<=0&&!(tempx&&tempy)&&(tempx+tempy)){
int tot=0;
if(tempx!=0){
int sign=tempx>0?1:-1;
for(int i=1;i<abs(tempx);i++)
if(cb.get(startx+sign*i,starty)) tot++;
}
else{
int sign=tempy>0?1:-1;
for(int i=1;i<abs(tempy);i++)
if(cb.get(startx,starty+sign*i)) tot++;
}
if(!aid)
{if(!tot) return true;}
else
{if(tot==1) return true;}
}
return false;
}
};
class guard: public chess{//士(仕)的实现
public:
guard(int c) : chess((c==0?-4:4)){}
bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){
int tempx=aimx-startx,tempy=aimy-starty;
int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy);
if(sid*aid<=0&&tempy*tempy+tempx*tempx==2&&aimx%7>=0&&aimx%7<=2&&aimy>=3&&aimy<=5)
return true;
return false;
}
};
class rook : public chess{//车的实现
public:
rook(int c) : chess((c==0?-3:3)){}
bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){
int tempx=aimx-startx,tempy=aimy-starty;
int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy);
if(sid*aid<=0&&!(tempx&&tempy)&&(tempx+tempy)){
if(tempx!=0){
int sign=tempx>0?1:-1;
for(int i=1;i<abs(tempx);i++)
if(cb.get(startx+sign*i,starty)) return false;
}
else{
int sign=tempy>0?1:-1;
for(int i=1;i<abs(tempy);i++)
if(cb.get(startx,starty+sign*i)) return false;
}
return true;
}
return false;
}
};
chessboard :: ~chessboard(){
for(int i=0;i<10;i++)
for(int j=0;j<9;j++)
if(c[i][j]!=NULL){
delete c[i][j];
c[i][j]=NULL;
}
}
void chessboard :: init(){//初始化,棋子的生成
c[0][0]=new rook(0); c[0][8]=new rook(0);
c[0][1]=new horse(0); c[0][7]=new horse(0);
c[0][2]=new elephant(0); c[0][6]=new elephant(0);
c[0][3]=new guard(0); c[0][5]=new guard(0);
c[0][4]=new general(0); c[9][4]=new general(1);
c[2][1]=new cannon(0); c[2][7]=new cannon(0);
c[3][0]=new soldier(0); c[3][2]=new soldier(0);
c[3][4]=new soldier(0); c[3][6]=new soldier(0);
c[3][8]=new soldier(0); c[6][8]=new soldier(1);
c[6][0]=new soldier(1); c[6][2]=new soldier(1);
c[6][4]=new soldier(1); c[6][6]=new soldier(1);
c[7][1]=new cannon(1); c[7][7]=new cannon(1);
c[9][0]=new rook(1); c[9][8]=new rook(1);
c[9][1]=new horse(1); c[9][7]=new horse(1);
c[9][2]=new elephant(1); c[9][6]=new elephant(1);
c[9][3]=new guard(1); c[9][5]=new guard(1);
}
void chessboard :: show(){
cout<<"吴 零一二三四五六七八"<<endl<<endl;
char num[10][4]={"零","一","二","三","四","五","六","七","八","九"};
for(int i=0;i<10;i++){
if(i==5) cout<<" ——楚 河 汉 界———"<<endl;
cout<<num[i]<<" ";
for(int j=0;j<9;j++){
if(c[i][j]!=NULL)
cout<<chessword[c[i][j]->get()+7];
else if((i==1&&j==4)||(i==8&&j==4))
cout<<"米";
else
cout<<"十";
}
cout<<endl;
}
}
void chessboard::play(){
this->init();
this->show();
do{
int startx,starty,aimx,aimy;
int sid,aid;
do{
sid=aid=0;
cout<<"请输入起始棋子位置与目标位置的坐标:"<<endl;
cin>>startx>>starty>>aimx>>aimy;
}while(!this->move(startx,starty,aimx,aimy));
this->show();
}while(chessboard::end);
cout<<"结束,赢家是Player"<<(chessboard::player==1?1:2)<<endl;
}
int main(){
chessboard C;
C.play();
}
三子棋
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int Speed=3,pianhao=1;
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME)&0x8000)?1:0)
#define color(COLOR) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),COLOR);
void putss(string a)
{
while(KEY_DOWN('\r')||KEY_DOWN(' '));
bool flag=true;int len=a.length();
for(int i=0;i<len-1;++i)
{
if(KEY_DOWN('\r')||KEY_DOWN(' '))flag=false;
putchar(a[i]);
if(flag)Sleep(Speed*10);
}
putchar(a[len-1]);
while(KEY_DOWN('\r')||KEY_DOWN(' '));
}
int win1,win2,AIturn;bool flag,p;
int game(int turn);
void hint();
int Setting()
{
system("cls");
puts("速度调节——s\n");
puts("键盘偏好——k\n");
puts("退出——e\n");
while(true)
{
if(KEY_DOWN('E')){while(KEY_DOWN('E'));return 0;}
if(KEY_DOWN('S'))
{
while(true)
{
system("cls");
color(112);for(int i=1;i<=6-Speed;++i)puts(" ");
color(7);for(int i=6-Speed+1;i<=5;++i)puts("");
printf("当前速度:%d\n",6-Speed);
puts("按方向上下键进行设置,E键退出\n");
while(true)
{
if(KEY_DOWN(VK_UP))
{
while(KEY_DOWN(VK_UP));
Speed++;
if(Speed==6)Speed=5;
break;
}
if(KEY_DOWN(VK_DOWN))
{
while(KEY_DOWN(VK_DOWN));
Speed--;
if(Speed==0)Speed=1;
break;
}
if(KEY_DOWN('E')){while(KEY_DOWN('E'));return 1;}
}
}
}
if(KEY_DOWN('K'))
{
while(true)
{
system("cls");
puts("按左右键进行设置,E键退出\n");
puts(pianhao==0?"当前偏好:wsad控制方向,空格键放置棋子":"当前偏好:方向键控制方向,Enter键放置棋子");
puts("\n注:本设置仅在单人游戏时有效");
while(true)
{
if(KEY_DOWN(VK_LEFT)||KEY_DOWN(VK_RIGHT))
{
pianhao=1-pianhao;
while(KEY_DOWN(VK_LEFT)||KEY_DOWN(VK_RIGHT));
break;
}
if(KEY_DOWN('E')){while(KEY_DOWN('E'));return 1;}
}
}
}
}
}
int home()
{
system("cls");
puts("主菜单\n");
puts("开始游戏 —— s\n");
puts("游戏说明 —— h\n");
puts("设置 —— p\n");
puts("退出 —— e\n");
puts("制作人:SqrtSecond\n");
puts("(P.S.按Enter或空格键可快速跳过剧情)");
while(true)
{
if(KEY_DOWN('E'))
{
while(true)
{
system("cls");
puts("确定要离开吗?\n");
puts("是 —— y\n");
puts("否 —— n\n");
while(true)
{
if(KEY_DOWN('N'))return 1;
if(KEY_DOWN('Y'))
{
system("cls");
puts("qwq");
return 0;
}
}
}
}
if(KEY_DOWN('H'))
{
hint();
return 1;
}
if(KEY_DOWN('P'))
{
while(Setting());
return 1;
}
if(KEY_DOWN('S'))
{
system("cls");
putss("那么,开始游戏吧!\n");system("pause");
system("cls");
puts("你要玩什么模式?\n");
puts("单人,人机先走——a\n");
puts("单人,人机后走——b\n");
puts("双人——c\n");
win1=win2=0;p=0;
while(true)
{
if(KEY_DOWN('A'))AIturn=1,p=1;
if(KEY_DOWN('B'))AIturn=-1,p=1;
if(KEY_DOWN('C'))AIturn=0,p=1;
if(p)
{
putss("好的,开始吧!");system("pause");
int r=1;
while(true)
{r=-game(r);if(r==0)break;}
return 1;
}
}
}
}
}
void hint()
{
system("cls");
putss("首先,这是一个双人游戏。\n");system("pause");
putss("基本规则和标准的三子棋相同。\n");system("pause");
putss("双方需要每人轮流下一手。\n");system("pause");
putss("每人下完一手后,都会触发一个随机的事件。\n");system("pause");
putss("有可能会消除场上已有的棋子……\n");system("pause");
putss("但也有可能帮你多下一手棋!\n");system("pause");
putss("这正是这个游戏的好玩之处。\n");system("pause");
putss("也很靠运气。\n");system("pause");
putss("另外……\n");system("pause");
putss("别触发5次神秘事件,否则后果自负……\n");system("pause");
putss("准备好了就开始吧!\n");system("pause");
return;
}
int mp[5][5],X,Y;
void putchess()
{
system("cls");
printf("比分:%d:%d\n\n",win1,win2);
puts("◤ ◥");
for(int i=1;i<=3;++i)
{
printf(" ");
for(int j=1;j<=3;++j)
{
if(X==i&&Y==j)color(112);
printf(mp[i][j]==1?"×":mp[i][j]==-1?"○":" ");
if(X==i&&Y==j)color(7);
}
puts(" ");
}
puts("◣ ◢");
}
int l;
int win()
{
l=0;
for(int i=1;i<=3;++i)
{
if(mp[i][1]+mp[i][2]+mp[i][3]==3)l=1;
if(mp[i][1]+mp[i][2]+mp[i][3]==-3)l=-1;
if(mp[1][i]+mp[2][i]+mp[3][i]==3)l=1;
if(mp[1][i]+mp[2][i]+mp[3][i]==-3)l=-1;
}
if(mp[1][1]+mp[2][2]+mp[3][3]==3)l=1;
if(mp[1][1]+mp[2][2]+mp[3][3]==-3)l=-1;
if(mp[1][3]+mp[2][2]+mp[3][1]==3)l=1;
if(mp[1][3]+mp[2][2]+mp[3][1]==-3)l=-1;
if(l==0)
{
l=2;
for(int i=1;i<=3;++i)for(int j=1;j<=3;++j)if(mp[i][j]==0)l=0;
}
if(l==2)
{
putchess();
printf("恭喜双方打成平局!\n");system("pause");
return true;
}
if(l!=0)
{
putchess();
printf("%s获得了胜利!\n",l==1?(AIturn==1?"人机":AIturn==-1?"玩家":"玩家1"):(AIturn==-1?"人机":AIturn==1?"玩家":"玩家2"));system("pause");
l==1?win1++:win2++;
return true;
}
return false;
}
int xx,yy,h,boom,rx,ry,rz;
void effect(int playyer)
{
rx=rand()%12+1;putchess();
puts("事件发生!\n");system("pause");
if(rx<=4)
{
ry=rand()%3+1;rz=rand()%3+1;mp[ry][rz]=0;
putchess();printf("消除格子(%d,%d)!\n",ry,rz);
}
else if(rx==5)
{
ry=rand()%3+1;mp[ry][1]=mp[ry][2]=mp[ry][3]=0;
putchess();printf("消除第%d行!\n",ry);
}
else if(rx==6)
{
ry=rand()%3+1;mp[1][ry]=mp[2][ry]=mp[3][ry]=0;
putchess();printf("消除第%d列!\n",ry);
}
else if(rx==7)
{
mp[1][1]=mp[2][2]=mp[3][3]=0;
putchess();printf("消除左上到右下的对角线!\n");
}
else if(rx==8)
{
mp[1][3]=mp[2][2]=mp[3][1]=0;
putchess();printf("消除右上到左下的对角线!\n");
}
else if(rx==9)
{
do
{
ry=rand()%3+1;
rz=rand()%3+1;
}while(mp[ry][rz]);
mp[ry][rz]=h;
putchess();printf("帮你下一手(%d,%d)!\n",ry,rz);
}
else if(rx==10)
{
ry=rand()%2;
if(ry)
{
for(int i=1;i<=3;++i)for(int j=1;j<=3;++j)mp[i][j]=-mp[i][j];
putchess();printf("反转局势!!!");h=-h;
}
else puts("无事发生!(偷笑)");
}
else
{
boom++;
if(boom==5)
{
system("color 4F");printf("神秘事件×%d!!!\n",boom);
boom=0;system("pause");
putss("5次神秘事件凑齐了……\n");Sleep(Speed*1000);
putss("Windows的数据遭受了毁灭性的打击!!!\n");Sleep(Speed*1000);
for(int i=1;i<=50;++i){system("color 2C");system("color AE");system("color 7A");system("color E4");system("color 58");system("color B6");}
system("color 4F");
for(int i=1;i<=3;++i)for(int j=1;j<=3;++j)mp[i][j]=0;
putchess();
MessageBox(NULL,"您的Windows可能出现了点问题。","Windows系统自动提示",MB_OK+48);
MessageBox(NULL,"Windows正在尝试修复您的系统中……","Windows系统自动提示",MB_OK+64);
MessageBox(NULL,"系统内部存储器已被破坏!","Windows系统自动提示",MB_OK+48);
putss("数据已被破坏……请重新开始……\n");Sleep(Speed*1000);h=-1;
}
else printf("神秘事件×%d!!!\n",boom);
}
system("pause");
if(rx>=11)system("color 07");
}
/*
x=1,2,3,4(1/3):随机消除一个格子
x=5(1/12):随机消除一行
x=6(1/12):随机消除一列
x=7(1/12):消除左上到右下的对角线
x=8(1/12):消除左上到右下的对角线
x=9(1/12):帮自己下一手
x=10(1/12):反转局势或无事发生
x=11,12(1/6):神秘事件
*/
bool player1(){return (((h==1&&AIturn!=-1)||(h==-1&&AIturn==-1))&&pianhao==1)||(((h==1&&AIturn!=1)||(h==-1&&AIturn==1))&&pianhao==0);}
void puthint(){printf("现在轮到%s(执%s)了,你要下在哪一行,哪一列?%s\n",h==1?(AIturn==1?"人机":AIturn==-1?"玩家":"玩家1"):(AIturn==-1?"人机":AIturn==1?"玩家":"玩家2"),h==1?"×":"○",player1()?"(用wsad控制方向,空格键放置棋子)":"(用方向键控制方向,Enter键放置棋子)");}
int AI(int t)
{
for(int i=1;i<=3;++i)
{
if(mp[i][1]+mp[i][2]+mp[i][3]==2*t)return mp[i][1]==0?i*3+1:mp[i][2]==0?i*3+2:i*3+3;
if(mp[1][i]+mp[2][i]+mp[3][i]==2*t)return mp[1][i]==0?1*3+i:mp[2][i]==0?2*3+i:3*3+i;
}
if(mp[1][1]+mp[2][2]+mp[3][3]==2*t)return mp[1][1]==0?4:mp[2][2]==0?8:12;
if(mp[1][3]+mp[2][2]+mp[3][1]==2*t)return mp[1][3]==0?6:mp[2][2]==0?8:10;//判断己方二子连珠
for(int i=1;i<=3;++i)
{
if(mp[i][1]+mp[i][2]+mp[i][3]==-2*t)return mp[i][1]==0?i*3+1:mp[i][2]==0?i*3+2:i*3+3;
if(mp[1][i]+mp[2][i]+mp[3][i]==-2*t)return mp[1][i]==0?1*3+i:mp[2][i]==0?2*3+i:3*3+i;
}
if(mp[1][1]+mp[2][2]+mp[3][3]==-2*t)return mp[1][1]==0?4:mp[2][2]==0?8:12;
if(mp[1][3]+mp[2][2]+mp[3][1]==-2*t)return mp[1][3]==0?6:mp[2][2]==0?8:10;//判断对方二子连珠
if(mp[2][2]==0)return 8;//判中心
if(mp[1][1]+mp[2][2]+mp[3][3]==t&&(mp[1][1]==0||mp[2][2]==0))return mp[2][2]==0?8:mp[1][1]==0?4:12;
if(mp[1][3]+mp[2][2]+mp[3][1]==t&&(mp[1][3]==0||mp[2][2]==0))return mp[2][2]==0?8:mp[1][3]==0?6:10;
for(int i=1;i<=3;++i)
{
if(mp[i][1]+mp[i][2]+mp[i][3]==t&&(mp[i][1]==0||mp[i][2]==0))return mp[i][1]==0?i*3+1:mp[i][3]==0?i*3+3:i*3+2;
if(mp[1][i]+mp[2][i]+mp[3][i]==t&&(mp[1][i]==0||mp[2][i]==0))return mp[1][i]==0?1*3+i:mp[3][i]==0?3*3+i:2*3+i;//判断己方一子连珠
}//判己方一子连珠
return mp[1][1]==0?4:mp[1][3]==0?6:mp[3][1]==0?10:mp[3][3]==0?12:mp[1][2]==0?5:mp[2][1]==0?7:mp[2][3]==0?9:11;
}
int game(int turn)
{
boom=0;h=turn;memset(mp,0,sizeof(mp));
while(true)
{
X=Y=2;putchess();
if(h!=AIturn)
{
puthint();
while(((KEY_DOWN('W')||KEY_DOWN('S')||KEY_DOWN('A')||KEY_DOWN('D')||KEY_DOWN(' '))&&player1())||((KEY_DOWN(VK_UP)||KEY_DOWN(VK_DOWN)||KEY_DOWN(VK_LEFT)||KEY_DOWN(VK_RIGHT)||KEY_DOWN('\r'))&&!player1()));
while(true)
{
if((KEY_DOWN('W')&&player1())||(KEY_DOWN(VK_UP)&&!player1()))
{
X=(X+1)%3+1;
putchess();puthint();
while((KEY_DOWN('W')&&player1())||(KEY_DOWN(VK_UP)&&!player1()));
}
if((KEY_DOWN('A')&&player1())||(KEY_DOWN(VK_LEFT)&&!player1()))
{
Y=(Y+1)%3+1;
putchess();puthint();
while((KEY_DOWN('A')&&player1())||(KEY_DOWN(VK_LEFT)&&!player1()));
}
if((KEY_DOWN('S')&&player1())||(KEY_DOWN(VK_DOWN)&&!player1()))
{
X=(X%3)+1;
putchess();puthint();
while((KEY_DOWN('S')&&player1())||(KEY_DOWN(VK_DOWN)&&!player1()));
}
if((KEY_DOWN('D')&&player1())||(KEY_DOWN(VK_RIGHT)&&!player1()))
{
Y=(Y%3)+1;
putchess();puthint();
while((KEY_DOWN('D')&&player1())||(KEY_DOWN(VK_RIGHT)&&!player1()));
}
if((KEY_DOWN(' ')&&player1())||(KEY_DOWN('\r')&&!player1()))
{
while((KEY_DOWN(' ')&&player1())||(KEY_DOWN('\r')&&!player1()));
if(!mp[X][Y])break;
}
}
}
else
{
puts("人机计算中……");
int m=AI(AIturn),y=(m+2)%3+1,x=(m-y)/3;
if(x==y&&x==2)Sleep(Speed*100);
else if(x+y==3||x+y==5)
{
Sleep(Speed*50);
X=x;Y=y;putchess();puts("人机计算中……");Sleep(Speed*50);
}
else
{
Sleep(Speed*33.33);
X=x;putchess();puts("人机计算中……");Sleep(Speed*33.33);
Y=y;putchess();puts("人机计算中……");Sleep(Speed*33.33);
}
}
mp[X][Y]=h;X=Y=4;
if(win())break;
effect(h);
if(win())break;
h=-h;
}
while(true)
{
system("cls");
puts("要再玩一局吗?\n");
puts("是 —— y\n");
puts("否 —— n\n");
while(true)
{
if(KEY_DOWN('N'))
{
system("cls");
putss("正在计算最终得分……\n");
Sleep(Speed*666.66);
if(win1!=win2)
{
putss("最终的胜利者是:\n");Sleep(Speed*333.33);
printf("%s!\n",win1>win2?(AIturn==1?"人机":AIturn==-1?"玩家":"玩家1"):(AIturn==-1?"人机":AIturn==1?"玩家":"玩家2"));system("pause");
}
else
{
putss("最终结果是……\n");Sleep(Speed*333.33);
puts("平局!");system("pause");
}
printf("你们的比分为:%d:%d。\n",win1,win2);system("pause");
putss("欢迎下次再玩!\n");system("pause");
return 0;
}
if(KEY_DOWN('Y'))return h;
}
}
}
int main()
{
srand(time(NULL));
while(home());
return 0;
}
井字棋
include<cstdio>
#include<windows.h>
#include<ctime>
int a[4][4],i,j,xc,yc,mouse=0,ok=0,record[2][9],foot=9,winner,first=0,who=0;
void Place(const int x, const int y)
{ COORD PlaceCursorHere;PlaceCursorHere.X = y;PlaceCursorHere.Y = x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), PlaceCursorHere);
return;
}
void color(int x)
{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x);}
int search(int x0,int y0,int x,int y)
{
mouse=GetAsyncKeyState(VK_LBUTTON);
POINT pt; HWND h=GetForegroundWindow(); GetCursorPos(&pt); ScreenToClient(h,&pt);
if(pt.x>=x0&&pt.y>=y0&&pt.x<=x&&pt.y<=y)
{
if(mouse!=0) {Sleep(100); return 2;} else return 1;
}
else return 0;
}
bool check(int x)
{
int b=0,c=0,d=0,e=0;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++) {b+=a[i][j]; c+=a[j][i];} d+=a[i][i]; e+=a[i][4-i];
if(b==x||c==x||d==x||e==x) return true; b=0; c=0;
}
return false;
}
void pcmove()
{
int x=10,b=0,c=0,d=0,e=0; xc=0; yc=0;
while(x>0)
{
for(i=1;i<=3;i++)
{
b=0; c=0; for(j=1;j<=3;j++) {b+=a[i][j]; c+=a[j][i];} d+=a[i][i]; e+=a[i][4-i];
if(b==x) {for(j=1;j<=3;j++) if(a[i][j]==0) {xc=i; yc=j; return;}} if(c==x) {for(j=1;j<=3;j++) if(a[j][i]==0) {xc=j; yc=i; return;}}
if(d==x) {for(j=1;j<=3;j++) if(a[j][j]==0) {xc=yc=j; return;}} if(e==x) {for(j=1;j<=3;j++) if(a[j][4-j]==0) {xc=j; yc=4-j; return;}}
} x-=8;
}
if(a[2][2]==0) {xc=yc=2; return;} x=2;
while(x>0) {for(i=1;i<=3;i+=x) for(j=1;j<=3;j+=x) if(a[i][j]==0) {xc=i; yc=j; return;} x--;}
}
void show()
{
int x=foot;
for(i=1;i<=3;i++) for(j=1;j<=3;j++) if(a[i][j]!=0) {if(a[i][j]==1) color(15); else color(11); Place(2*i-1,4*j-2); printf("●");}
color(15); Place(7,0);
if(who==0) {if(foot%2==1) printf("1号"); else printf("2号");} else printf("电脑");
if(who==0||first==1) x--;
printf("下子:(%d,%d)",record[0][8-x],record[1][8-x]);
}
void button(int x)
{
if(x<3)
{
Place(x*3,15); printf("┌┄┄┐");
Place(x*3+1,17); if(x==0){if(ok==0) printf("开始"); else printf("悔棋");} if(x==1) printf("重来"); if(x==2) printf("退出");
Place(x*3+2,15); printf("└┄┄┘");
}
if(x>2&&x<6)
{
Place(10,(x-4)*7); printf("┌┄┐");
Place(11,(x-4)*7+2); if(x==4) printf("是"); else printf("否");
Place(12,(x-4)*7); printf("└┄┘");
}
if(x>5&&x<8)
{
Place(10,(x-6)*12); printf("┌┄┄┄┄┐");
Place(11,(x-6)*12+2); if(x==6) printf("人机对战"); else printf("人人对战");
Place(12,(x-6)*12); printf("└┄┄┄┄┘");
}
if(x>7)
{
Place(10,(x-8)*12); printf("┌┄┄┄┄┐");
Place(11,(x-8)*12+2); if(x==8) printf(" 我先手 "); else printf("电脑先手");
Place(12,(x-8)*12); printf("└┄┄┄┄┘");
}
}
void menu(int x)
{
int k,l;
if(x==0)
{
if(ok==0) {ok=1; return;}
else if(9-foot>1)
{
foot+=2; for(i=9-foot;i<=11-foot;i++) {a[record[0][i]][record[1][i]]=0; record[0][i]=record[1][i]=0;}
for(i=1;i<=3;i++) for(j=1;j<=3;j++) {Place(2*i-1,4*j-2); printf(" ");} show();
}
}
if(x>0&&x<3)
{
for(l=10;l<=12;l++) for(j=1;j<=22;j++) {Place(l,j); printf(" ");}
Place(9,1); printf("你想"); if(x==1) printf("重来吗?"); if(x==2) printf("退出吗?");
button(4); button(5);
while(1)
{
mouse=GetAsyncKeyState(VK_LBUTTON);
for(i=0;i<=1;i++)
{
k=search(7+i*55,165,40+i*55,200);
if(k!=2) {if(k==1) color(15); else color(7); button(i+4);}
else
{for(l=9;l<=12;l++) for(j=1;j<=12;j++) {Place(l,j); printf(" ");}
if(i==0) {if(x==1) ok=2; else exit(0);} return;
}
}
Sleep(50);
}
}
if(x>2&&x<5) {if(x==3) {who=1; return;} else {who=0; return;}}
if(x>4) {if(x==5) {first=0; return;} else {first=1; return;}}
}
void click()
{
for(i=0;i<=2;i++)
{ mouse=GetAsyncKeyState(VK_LBUTTON);
int k=search(125,i*50+5,175,i*50+40);
if(k!=2) {if(k==1) color(15); else color(7); button(i); color(7);}
else{menu(i); return;}
}
Sleep(50);
}
void humove()
{
while(1)
{
for(i=1;i<=3;i++) for(j=1;j<=3;j++) if(search(7+(i-1)*32,7+(j-1)*32,7+i*32,7+j*32)==2&&a[j][i]==0)
{if(who==0&&foot%2!=1) a[j][i]=5; else a[j][i]=1; record[0][9-foot]=j; record[1][9-foot]=i; return;} click(); if(ok==2) return;
}
}
void replace()
{
int k,l,p,q;
Place(7,0); for(i=1;i<=15;i++) printf(" "); for(i=1;i<=3;i++) for(j=1;j<=3;j++) {Place(2*i-1,4*j-2); printf(" "); a[i][j]=0;}
for(i=0;i<2;i++) for(j=0;j<9;j++) record[i][j]=0; foot=9; winner=0; first=0; who=0; ok=1; button(6); button(7);
while(1)
{
for(i=0;i<=1;i++)
{
mouse=GetAsyncKeyState(VK_LBUTTON); k=search(i*80+7,165,i*80+103,200); q=search(125,105,175,140);
if(k!=2) {if(k==1) color(15); else color(7); button(i+6); color(7);}
else
{
menu(i+3); for(l=10;l<=12;l++) for(j=1;j<=22;j++) {Place(l,j); printf(" ");}
if(who==1)
{
button(7); button(8);
while(1)
{
for(j=0;j<=1;j++)
{
mouse=GetAsyncKeyState(VK_LBUTTON); k=search(j*80+7,165,j*80+103,200); q=search(125,105,175,140);
if(k!=2) {if(k==1) color(15); else color(7); button(j+8); color(7);}
else{menu(j+5); for(l=10;l<=12;l++) for(p=1;p<=22;p++) {Place(l,p); printf(" ");} return;}
if(q!=2) {if(q==1) color(15); else color(7); button(2); color(7);} else menu(2);
} Sleep(50);
}
}
return;
}
if(q!=2) {if(q==1) color(15); else color(7); button(2); color(7);} else menu(2);
}
Sleep(50);
}
}
int main()
{
printf("┌─┬─┬─┐\n\n├─┼─┼─┤\n\n├─┼─┼─┤\n\n└─┴─┴─┘\n\n");
for(i=0;i<3;i++) button(i);
while(ok==0) click();
while(1)
{
replace();
while(foot>0&&ok!=0)
{
if(first!=1) {humove(); show(); if(check(3)) {winner=1; ok=0; break;} foot--; if(foot<=0||ok==0||ok==2) break;}
if(who==0&&check(15)) {winner=2;ok=0;break;}
if(who!=0) {pcmove(); record[0][9-foot]=xc; record[1][9-foot]=yc; a[xc][yc]=5; show(); if(check(15)) {winner=2;ok=0;} foot--; first=0;}
}
if(ok==2) {ok=1; continue;} Place(7,0); for(i=1;i<=15;i++) printf(" ");
Place(7,4); color(15); if(winner==0) printf("平局!");
if(who!=0) {if(winner==1) printf("你赢了!"); if(winner==2) printf("你输了!"); Sleep(50);}
else {if(winner==1) printf("1号赢了!"); if(winner==2) printf("2号赢了!"); Sleep(50);}
ok=0; while(ok==0) click();
}
}
海战棋
#include <cstdio>
#include <ctime> //rand()%(x) 0~x-1 int
#include <windows.h> //停顿:Sleep();
#include <cstdlib> //清屏:system("cls");
#include <conio.h> //getch(); char
#include <iostream>
#include <cstring> //未知 :□; 打中 :◎; 未打中:○ 船:★;
using namespace std;
int rest[3][5],r1,r2; //rest[1]:玩家的海域; rest[2]:电脑的海域 r1:玩家还剩船数; r2:电脑还剩船数
int b1[12][12],b2[12][12]; //0:空海; 1:船只; 2:打中; 3:边界 4:未打中 5:沉船
int c1[12][12],c2[12][12]; //c1:玩家海域的颜色 c2:电脑海域颜色
int fx[8][2]={{0,1},{1,0},{0,-1},{-1,0},{1,1},{-1,-1},{1,-1},{-1,1}};
int now[2][2]; //now[左/右][x/y] 光标
string a1[12][12],a2[12][12];
int fd [500][2],head=0,tail=0;
const long long wdmm=0;
long long cjzt=0,wdm;
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void gotoxy(int x,int y)//位置函数(整个界面)(行为x 列为y)
{
COORD pos;
pos.X=2*(y);
pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void gotoxy1(int x,int y)//位置函数(左边棋盘)(行为x 列为y)
{
COORD pos;
pos.X=2*(y+5);
pos.Y=x+1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void gotoxy2(int x,int y)//位置函数(右边棋盘)(行为x 列为y)
{
COORD pos;
pos.X=2*(y+18);
pos.Y=x+1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void check2(int x,int y){
int k=0,kx,ky,f=1;
for (int i=0; i<=3; i++){
if (b2[x+fx[i][0]][y+fx[i][1]]==2) k=i;
if (b2[x+fx[i][0]][y+fx[i][1]]==1) f=0;
}
for (kx=x,ky=y; b2[kx][ky]==2; kx+=fx[k][0],ky+=fx[k][1]);
if (b2[kx][ky]==1) f=0;
if (f){
int w=0;
color(12);
for (kx=x,ky=y; b2[kx][ky]==2; kx+=fx[k][0],ky+=fx[k][1]){
gotoxy2(kx,ky);
a2[kx][ky]="※";
c2[kx][ky]=12;
cout <<"※";
w++;
}
for (kx=x+fx[(k+2)%4][0],ky=y+fx[(k+2)%4][1]; b2[kx][ky]==2; kx+=fx[(k+2)%4][0],ky+=fx[(k+2)%4][1]){
gotoxy2(kx,ky);
a2[kx][ky]="※";
c2[kx][ky]=12;
cout <<"※";
w++;
}
if (w>0){
rest[2][w]--;
r2--;
if (rest[2][w]>0) color(14); else color(11);
gotoxy2(5-w,16); printf("*%d",rest[2][w]);
}
}
}
int move1(){
if (r1*r2==0) return(1);
color(5); gotoxy1(14,4); printf("电脑开炮");
color(13); gotoxy2(14,4); printf("玩家开炮");
int kx=now[1][0],ky=now[1][1],lastx,lasty,f=1;
char ch;
gotoxy2(kx,ky); color(11); if (a2[kx][ky]!=" ")cout <<a2[kx][ky]; else cout <<"▂"; gotoxy2(kx,ky);
while (f){
ch=getch();
if (ch=='1' || ch=='a'){kx=now[1][0]+fx[2][0]; ky=now[1][1]+fx[2][1];}
if (ch=='2' || ch=='s'){kx=now[1][0]+fx[1][0]; ky=now[1][1]+fx[1][1];}
if (ch=='3' || ch=='d'){kx=now[1][0]+fx[0][0]; ky=now[1][1]+fx[0][1];}
if (ch=='5' || ch=='w'){kx=now[1][0]+fx[3][0]; ky=now[1][1]+fx[3][1];}
if (kx>0 && kx<=10 && ky>0 && ky<=10){
gotoxy2(now[1][0],now[1][1]); color(c2[now[1][0]][now[1][1]]); cout <<a2[now[1][0]][now[1][1]];
gotoxy2(kx,ky); color(11); if (a2[kx][ky]!=" ")cout <<a2[kx][ky]; else cout <<"▂"; gotoxy2(kx,ky);
now[1][0]=kx; now[1][1]=ky;
}
if ((ch=='0' || ch==' ')&& b2[kx][ky]<=1){
if (b2[kx][ky]==1){b2[kx][ky]=2; a2[kx][ky]="◎"; c2[kx][ky]=4;}
if (b2[kx][ky]==0){b2[kx][ky]=4; a2[kx][ky]=" ";}
gotoxy2(kx,ky); color(c2[kx][ky]); cout <<a2[kx][ky];
f=0;
check2(kx,ky);
color (7); gotoxy2(12,4); cout <<"("; color(6); cout <<ky; color(7); cout <<","; color(2); cout <<kx;color(7); cout<<") ";
if (b2[kx][ky]==2) move1();
}
if (ch=='8' || ch=='g')
{
if(cjzt=1023577928566)
{
for (int i=1; i<=10; i++) for (int j=1; j<=10; j++)
if (b2[i][j]==1){
gotoxy2(i,j);
color(10);
printf("Ω");
}
}
char ccc=getch();
for (; ccc!='8' && ccc!='g'; ccc=getch());
for (int i=1; i<=10; i++)for (int j=1; j<=10; j++){
gotoxy2(i,j);
color(c2[i][j]);
cout <<a2[i][j];
}
gotoxy2(kx,ky); color(11); if (a2[kx][ky]!=" ")cout <<a2[kx][ky]; else cout <<"▂"; gotoxy2(kx,ky);
}
if (ch=='4' || ch=='q') return(0);
}
return(1);
}
int ok(int x,int y){
int nnn=0;
if (b1[x][y]==2 || b1[x][y]==4 || b1[x][y]==5) return(0);
for (int i=0; i<=7; i++){
if (b1[x+fx[i][0]][y+fx[i][1]]==2) nnn++;
if (b1[x+fx[i][0]][y+fx[i][1]]==5) return(0);
}
if (nnn>1) return(0);
return(1);
}
void check1(int x,int y) {
int k=0,kx,ky,f=1;
for (int i=0; i<=3; i++){
if (b1[x+fx[i][0]][y+fx[i][1]]==2) k=i;
if (b1[x+fx[i][0]][y+fx[i][1]]==1) f=0;
}
for (kx=x,ky=y; b1[kx][ky]==2; kx+=fx[k][0],ky+=fx[k][1]);
if (b1[kx][ky]==1) f=0;
if (f){
int w=0;
color(12);
for (kx=x,ky=y; b1[kx][ky]==2; kx+=fx[k][0],ky+=fx[k][1]){
gotoxy1(kx,ky);
b1[kx][ky]=5;
a1[kx][ky]="※";
c1[kx][ky]=12;
cout <<"※";
w++;
}
for (kx=x+fx[(k+2)%4][0],ky=y+fx[(k+2)%4][1]; b1[kx][ky]==2; kx+=fx[(k+2)%4][0],ky+=fx[(k+2)%4][1]){
gotoxy1(kx,ky);
b1[kx][ky]=5;
a1[kx][ky]="※";
c1[kx][ky]=12;
cout <<"※";
w++;
}
if (w>0){
rest[1][w]--;
r1--;
if (rest[1][w]>0) color(14); else color(11);
gotoxy1(5-w,-5);
printf("%d*",rest[1][w]);
}
}
}
void move2(){
if (r1*r2==0) return;
color(13); gotoxy1(14,4); printf("电脑开炮");
color(5); gotoxy2(14,4); printf("玩家开炮");
Sleep(750);
int kx=0,ky=0,over=0;
while (tail>head){
head++;
kx=fd[head][0]; ky=fd[head][1];
if (ok(kx,ky)){over=1; break;}
}
while (!over){
kx=rand()%(10)+1;
ky=rand()%(10)+1;
if (ok(kx,ky)) over=1;
}
if (b1[kx][ky]==1){b1[kx][ky]=2; a1[kx][ky]="◎"; c1[kx][ky]=4;}
if (b1[kx][ky]==0){b1[kx][ky]=4; a1[kx][ky]=" ";}
gotoxy1(kx,ky); color(11); printf("⊕"); Sleep(600);
gotoxy1(kx,ky); color(c1[kx][ky]); cout <<a1[kx][ky];
color (7); gotoxy1(12,4); cout <<"("; color(6); cout <<ky; color(7); cout <<","; color(2); cout <<kx;color(7); cout<<") ";
check1(kx,ky);
if ((b1[kx][ky]==2 || b1[kx][ky]==5)&& r1*r2>0){
int i=rand()%(4);
for (int ii=0; ii<=3; ii++){
int px=kx+fx[i][0],py=ky+fx[i][1];
if (px>0 && px<=10 && py>0 && py<=10){
tail++;
fd[tail][0]=px;
fd[tail][1]=py;
}
i=(i+1)%4;
}
move2();
}
}
void put(){
int k=4;
while (k--){
for (int i=1; i<=4-k; i++){
int f1=0,f2=0;
int dir1,dir2;
dir1=rand()%(2);
dir2=rand()%(2);
while (!f1){
f1=1;
int lx=rand()%(10)+1;
int ly=rand()%(10)+1;
for(int nx=lx-1; nx<=lx+fx[dir1][0]*k+1; nx++)
for (int ny=ly-1; ny<=ly+fx[dir1][1]*k+1; ny++)
if(b1[nx][ny]==1){f1=0; break;}
for (int nx=lx; nx<=lx+fx[dir1][0]*k; nx++)
for (int ny=ly; ny<=ly+fx[dir1][1]*k; ny++)
if (b1[nx][ny]==3){f1=0; break;}
if (f1){
for (int jx=lx,jy=ly; jx<=lx+fx[dir1][0]*k && jy<=ly+fx[dir1][1]*k; jx+=fx[dir1][0],jy+=fx[dir1][1]){
b1[jx][jy]=1;
c1[jx][jy]=15;
color(15);
gotoxy1(jx,jy); printf("□");
}
}
}
while (!f2){
f2=1;
int lx=rand()%(10)+1;
int ly=rand()%(10)+1;
for(int nx=lx-1; nx<=lx+fx[dir2][0]*k+1; nx++)
for (int ny=ly-1; ny<=ly+fx[dir2][1]*k+1; ny++)
if(b2[nx][ny]==1){f2=0; break;}
for (int nx=lx; nx<=lx+fx[dir2][0]*k; nx++)
for (int ny=ly; ny<=ly+fx[dir2][1]*k; ny++)
if (b2[nx][ny]==3){f2=0; break;}
if (f2){
for (int jx=lx,jy=ly; jx<=lx+fx[dir2][0]*k && jy<=ly+fx[dir2][1]*k; jx+=fx[dir2][0],jy+=fx[dir2][1])
b2[jx][jy]=1;
}
}
int a=1;
}
}
}
void reset(){
system("cls");
color(15);gotoxy(18,10); printf("按 0 重排战船; 按任意键开始与电脑对战");
color(9);
gotoxy(0,9 ); printf("玩家海域");
gotoxy(0,22); printf("电脑海域");
for (int i=1; i<=4; i++) rest[1][i]=rest[2][i]=5-i;
for (int i=1; i<=10; i++){
b1[0][i]=b1[i][0]=b2[0][i]=b2[i][0]=3;
b1[11][i]=b1[i][11]=b2[11][i]=b2[i][11]=3;
}
color(8);
for (int i=1; i<=10; i++)for (int j=1; j<=10; j++) c1[i][j]=c2[i][j]=8;
for (int i=1; i<=10; i++)for (int j=1; j<=10; j++){
b1[i][j]=b2[i][j]=0;
a1[i][j]="□"; gotoxy1(i,j); cout <<a1[i][j];
a2[i][j]="□"; gotoxy2(i,j); cout <<a2[i][j];
}
color(14);
gotoxy1(1,-5); printf("%d*□□□□",rest[1][4]);
gotoxy1(2,-5); printf("%d*□□□ ",rest[1][3]);
gotoxy1(3,-5); printf("%d*□□ ",rest[1][2]);
gotoxy1(4,-5); printf("%d*□ ",rest[1][1]);
gotoxy2(4,12); printf(" □*%d",rest[2][1]);
gotoxy2(3,12); printf(" □□*%d",rest[2][2]);
gotoxy2(2,12); printf(" □□□*%d",rest[2][3]);
gotoxy2(1,12); printf("□□□□*%d",rest[2][4]);
color(2); for (int i=1; i<=10; i++){gotoxy1(i,11); cout <<i; gotoxy2(i,11); cout <<i;}
color(6); for (int i=1; i<=10; i++){gotoxy1(11,i); cout <<i; gotoxy2(11,i); cout <<i;}
color(7); gotoxy1(12,4); printf("( , )"); gotoxy2(12,4); printf("( , )");
put();
now[0][0]=now[0][1]=now[1][0]=now[1][1]=1;
r1=r2=10;
char res=getch(); if (res=='0') reset();
}
int main(){
int gameover=1;
cout<<"请输入密码,暂无密码请按“0”"<<endl;
cin>>wdm;
if(wdm==wdmm)
{
cjzt=1023577928566;
cout<<"请稍后 .";
Sleep(100);
system("cls");
cout<<"请稍后 ..";
Sleep(100);
system("cls");
cout<<"请稍后 ...";
Sleep(100);
system("cls");
cout<<"请稍后 ... .";
Sleep(100);
system("cls");
cout<<"请稍后 ... ..";
Sleep(100);
system("cls");
cout<<"请稍后 ... ...";
Sleep(100);
cout<<"请稍后 .";
Sleep(100);
system("cls");
cout<<"请稍后 ..";
Sleep(100);
system("cls");
cout<<"请稍后 ...";
Sleep(100);
system("cls");
cout<<"请稍后 ... .";
Sleep(100);
system("cls");
cout<<"请稍后 ... ..";
Sleep(100);
system("cls");
cout<<"请稍后 ... ...";
system("cls");
}
else
{
cout<<"请稍后 .";
Sleep(100);
system("cls");
cout<<"请稍后 ..";
Sleep(100);
system("cls");
cout<<"请稍后 ...";
Sleep(100);
system("cls");
cout<<"请稍后 ... .";
Sleep(100);
system("cls");
cout<<"请稍后 ... ..";
Sleep(100);
system("cls");
cout<<"请稍后 ... ...";
Sleep(100);
cout<<"请稍后 .";
Sleep(100);
system("cls");
cout<<"请稍后 ..";
Sleep(100);
system("cls");
cout<<"请稍后 ...";
Sleep(100);
system("cls");
cout<<"请稍后 ... .";
Sleep(100);
system("cls");
cout<<"请稍后 ... ..";
Sleep(100);
system("cls");
cout<<"请稍后 ... ...";
system("cls");
cout<<"请获取密码后重试";
return 0;
}
while (gameover){
srand(time(NULL));
reset();
gotoxy(18,10); printf(" ");
int haha=0;
while (r1*r2){
if (!move1()){haha=1; break;} //玩家(haha==1说明中途退出)
move2(); //电脑
}
gotoxy(18,0);
if (haha) printf("怎么中途退出了...\n\n");
else if (r1==0) printf("很遗憾,你输了...\n\n");
else if (r2==0) printf("恭喜你,你赢了!!!\n\n");
printf("按1退出; 按其它键继续\n>>");
if (getch()=='1') gameover=0;
}
}
更多推荐
所有评论(0)