求c++编写连连看教程
发布网友
发布时间:2022-04-27 06:20
我来回答
共1个回答
热心网友
时间:2022-06-27 13:58
//ASDW控制方向,空格键选择。
#include <iostream>
#include "Cross.h"
using namespace std;
int main()
{
CCross cs;
char ch=0;
char y=0,x=0;
cs.ReDraw();
do
{
switch(ch)
{
case 'a':
case 'A':
y--;
cs.SetPoint(x,y);
break;
case 's':
case 'S':
x++;
cs.SetPoint(x,y);
break;
case 'd':
case 'D':
y++;
cs.SetPoint(x,y);
break;
case 'w':
case 'W':
x--;
cs.SetPoint(x,y);
break;
case ' ':
cs.SetChoose();
break;
default:
continue;
}
cs.ReDraw();
cs.GetPoint(&x,&y);
}while(ch=getch()());
return 0;
}
// Cross.h: interface for the CCross class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CROSS_H__D8866531_AEF2_4461_B3C6_468E71CB8F6E__INCLUDED_)
#define AFX_CROSS_H__D8866531_AEF2_4461_B3C6_468E71CB8F6E__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include<iostream>
#include<conio.h>
#include"windows.h"
#include"time.h"
using namespace std;
#define LEN 10
#define Sort 6
class CCross
{
public:
void GetPoint(char *nX,char *nY);
BOOL GetChoose();
void SetChoose();
void SetPoint(char X,char Y);
int ReDraw(BOOL ck=true);
CCross();
virtual ~CCross();
void SetColor(unsigned int a);
private:
int Rad();
void Drop();
void Check();
unsigned char map[LEN][LEN];
unsigned char x,y;
unsigned count;
unsigned OneceCount;
BOOL choose;
};
#endif // !defined(AFX_CROSS_H__D8866531_AEF2_4461_B3C6_468E71CB8F6E__INCLUDED_)
// Cross.cpp: implementation of the CCross class.
//
//////////////////////////////////////////////////////////////////////
#include "Cross.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCross::CCross()
{
for(int i=0;i<LEN;i++)
for(int j=0;j<LEN;j++)
map[i][j]=Rad();
x=0;
y=0;
count=0;
OneceCount=0;
choose=false;
srand()((unsigned)time(NULL));
}
CCross::~CCross()
{
}
void CCross::SetColor(unsigned int a)//设置文本颜色
{
switch(a)
{
case 0: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE|BACKGROUND_GREEN)
,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);break;//White
case 1: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);break;//Yellow
case 2: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED);break;//Red
case 3:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_GREEN);break;//Green
case 4:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_BLUE);break;//Blue
case 5:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);break;//Pred
case 6:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);break;//Cyan
}
}
int CCross::ReDraw(BOOL ck)
{
if(ck)
Check();
system("cls");
SetColor(0);
cout<<"┏━";
for(int i=0;i<LEN-1;i++)
cout<<"┳━";
cout<<"┓"<<endl;
for(i=0;i<=(LEN-1)*2;i++,putchar('\n'))
{
if(i%2==0)
{
for(int j=0;j<=LEN*2;j++)
{
if(j%2==0)
{
SetColor(0);
cout<<"┃";
}
else if(x==i/2 && y==j/2)
{
SetColor(map[x][y]%10);
choose ? printf("★") : printf("○");
}
else
{
SetColor(map[i/2][j/2]%10);
switch(map[i/2][j/2]/10)
{
case 0:
printf("●");
break;
case 4:
printf("■");
break;
case 5:
printf("◆");
break;
case 6:
printf("▲");
break;
case 7:
printf("▼");
break;
case 8:
printf("▓");
break;
case 9:
printf("※");
break;
case 10:
printf("卍");
break;
case 11:
printf("卐");
break;
}
}
}
}
else
{
SetColor(0);
cout<<"┣━";
for(int k=0;k<LEN-1;k++)
cout<<"╋━";
cout<<"┫";
}
}
cout<<"┗━";
for(i=0;i<LEN-1;i++)
cout<<"┻━";
cout<<"┛"<<endl;
cout<<"x="<<(short)x<<"y="<<(short)y<<endl;
cout<<"count="<<count<<"Onece="<<OneceCount<<endl;
return 0;
}
void CCross::SetPoint(char X,char Y)
{
unsigned char tmp;
char m,n;
m=(X+LEN)%LEN;
n=(Y+LEN)%LEN;
if(choose)
{
if((x-m)*(x-m)+(y-n)*(y-n)>1)
{
SetColor(2);
cout<<"不能移动!"<<endl;
choose=false;
cout<<"x="<<(short)x<<"y="<<(short)y<<endl;
cout<<"m="<<(short)m<<"n="<<(short)n<<endl;
Sleep(2000);
return;
}
tmp=map[x][y];
map[x][y]=map[m][n];
map[m][n]=tmp;
Check();
if(OneceCount==0)
{
SetColor(2);
cout<<"不能移动!"<<endl;
tmp=map[x][y];
map[x][y]=map[m][n];
map[m][n]=tmp;
Sleep(2000);
}
count+=OneceCount;
choose=false;
OneceCount=0;
}
else
{
x=m;
y=n;
ReDraw();
}
}
void CCross::Check()
{
int i,j,k,m,n;
BOOL ck=false;
for(k=LEN;k>2;k--)
for(i=0;i<LEN;i++)
for(j=0;j<LEN-k+1;j++)
{
for(n=1,m=map[i][j]%10;j+n<LEN,m==map[i][j+n]%10;n++)
m=map[i][j+n]%10;
if(n<3)
continue;
else if(n==3)
m=j;
else
{
map[i][j]=n*10+m;
m=j+1;
}
for(;m<j+k;m++)
{
for(n=i;n>0;n--)
map[n][m]=map[n-1][m];
map[0][m]=Rad();
}
ck=true;
OneceCount++;
}
for(k=LEN;k>2;k--)
for(i=0;i<LEN-k+1;i++)
for(j=0;j<LEN;j++)
{
for(n=1,m=map[i][j]%10;i+n<LEN,m==map[i+n][j]%10;n++)
m=map[i+n][j]%10;
if(n<3)
continue;
else if(n==3)
m=i;
else
{
map[i][j]=n*10+m;
m=i+1;
}
for(;m<i+k;m++)
{
for(n=j;n>0;n--)
map[m][n]=map[m][n-1];
map[m][0]=Rad();
}
ck=true;
OneceCount++;
}
if(ck)
Check();
if(!choose)
{
OneceCount=0;
}
}
void CCross::Drop()
{
int i,j,k,p;
for(i=0;i<LEN;i++)
for(j=LEN-1;j>0;j--)
if(map[j][i]==0)
{
for(k=j,p=1;k-p>0;)
{
if(map[k-p][i]==0)
{
p++;
continue;
}
map[k][i]=map[k-p][i];
k--;
}
for(;p>0;p--)
map[p-1][i]=Rad();
}
}
void CCross::SetChoose()
{
choose=!choose;
}
BOOL CCross::GetChoose()
{
return choose;
}
int CCross::Rad()
{
return rand()()%Sort+1;
}
void CCross::GetPoint(char *nX,char *nY)
{
*nX=x+LEN;
*nY=y+LEN;
}追问可不可以给个注释。。这个是用什么做的?MFC?win32控制台?win32窗口?c++win32控制台?
追答c++win32