全是地址:合集:https://blog.csdn.net/ebirth/article/details/95328792
https://blog.csdn.net/ebirth/article/details/89279857
https://blog.csdn.net/ebirth/article/details/89289191
https://blog.csdn.net/ebirth/article/details/89406594
https://blog.csdn.net/ebirth/article/details/89407353
https://blog.csdn.net/ebirth/article/details/89407353
https://blog.csdn.net/ebirth/article/details/89279690
https://blog.csdn.net/ebirth/column/info/37645
https://blog.csdn.net/ebirth/article/details/89280196
https://blog.csdn.net/ebirth/article/details/91345760
在这里再给四部最佳奥斯卡恐怖片:https://zhidao.baidu.com/question/93874786.html?qbpn=1_1&fr=newsearchlist&word=有恐怖片拿过奥斯卡奖的吗?
#include
#include
#include
#include
#include
void InitializeGame(void);
void GetInput(char * input);
bool CheckAnswer(char * input);
bool GiveTips(char * input);
void GetRandom(char * random);
using namespace std;
char answer[5] = "";
char input[10] = "";
int times = 0;
int main(int argc, char** argv) {
char c;
while (true){
cout << "Enter 'S' to start the game, 'Q' to quit." << endl;
c = toupper(getchar());
while(getchar() != '\n');
if (c == 'Q')
break;
else if(c == 'S'){
cout << "Game Start! Enter your answer:" << endl;
times = 0;
InitializeGame();//初始化游戏
// cout << "The answer is: " << answer << endl;
GetInput(input); //输入猜测值
//检查猜测是否正确 不正确则给出提示
while(GiveTips(input) == false){
times++;
GetInput(input);
}
times++;
cout << "Congratulations! You have got it after " << times << " times." << endl;
}else
cout << "Only 'S' and 'Q' are received." << endl;
}
return 0;
}
/******************************************************************************
*函数名称:void InitializeGame(void)
*函数功能:初始化游戏,生成随机数
*入口参数:无
*返 回 值:无
*******************************************************************************/
void InitializeGame(void){
static bool init_rand = false;
if (init_rand == false){
srand ((unsigned) time(NULL)); //如果未初始化则初始化随机数种子
init_rand = true;
}
GetRandom(answer);//生成随机数
// cout << answer << endl;
}
/******************************************************************************
*函数名称:void GetInput(char * input)
*函数功能:读取一个字符串
*入口参数:返回读取的字符串
*返 回 值:无
*******************************************************************************/
void GetInput(char * input){
gets(input);
while(true){
if(strlen(input) != 4){
cout << "Please input a 4-digits number!" << endl;
gets(input);
continue;
}
if(CheckAnswer(input) == false){
cout << "There couldn't be two same character in your answer!" << endl;
gets(input);//不合法则重新输入
continue;
}
break;
}
}
/******************************************************************************
*函数名称:bool checkanswer(char * input)
*函数功能:判断答案是否合法,即是否存在重复数字
*入口参数:input为待判断的答案
*返 回 值:正确则返回真,否则返回假
*******************************************************************************/
bool CheckAnswer(char * input){
char temp[5];
strcpy (temp, input);
for(int i = 0; i < 4; i++){
for(int j = i + 1; j < 4; j++)
if(temp[i] == input[j])
return false;
}
return true;
}
/******************************************************************************
*函数名称:void GiveTips(char * input)
*函数功能:根据输入的答案来给出提示
*入口参数:待判断的答案
*返 回 值:无
*******************************************************************************/
bool GiveTips(char * input){
// cout << "I'm checking." << endl;
int a = 0, b = 0;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
// cout << "i:" << i << "j:" << j << endl;
if (input[i] == answer[j]){
if(i == j)
a++;
else
b++;
continue;
}
}
}
cout << "Tips:" << a << "A" << b << "B\n" << endl;
if (a == 4)
return true;
cout << "Enter another answer:";
return false;
}
/******************************************************************************
*数名称:void GetRandom(char * random)
*函数功能:产生一个各位数不相等的四位随机数
*口参数:random为返回的随机数
*返 回 值:无
*备 注:先生成一个0-9的整数数组,再随机从中取四个数,每取一个将该位置为-1
*******************************************************************************/
void GetRandom(char * random){
int i, j[10], k;
for (i = 0; i < 10; i++){
j[i] = i;
}
for(i = 0; i < 4; i++){
//生成第i个随机数
k = (int)rand() % 10;//k为下标
while (j[k] == -1){
k = (k + 1) % 10;
}
random[i] = '0' + j[k];
j[k] = -1;
}
}
#include
#include
using namespace std;
int main() {
int x=rand() %50;
for(int i=1;i<=5;i++){
int y;
cin>>y;
if(y < x){
cout<<"smaller"<x){
cout<<"bigger"<
#include
#include
#include
#include
#include
using namespace std;
int food[2] = { 9, 9 };//初始食物坐标
int snake[1000][2];//蛇身坐标
int length = 1;//初始蛇长
int headX, headY;//蛇头坐标
int speed = 500;//游戏难度
int score = 0;//分数
int level = 1;//难度等级
string name;//玩家姓名
void gotoxy(short x, short y);//移动光标
int setdirection(int x);//确定方向变量
void changesnake(int x);//改变蛇身坐标
void ifchangefood();//判断蛇是否吃到食物
void makefood();//创造新食物
bool judgelife();//判断蛇是否存活
void drawsnake();//画蛇
void drawfood();//画食物
void drawwall();//画墙
void drawscore();//画数据
void draw();//绘图
int main()
{
SetConsoleTitle("贪吃蛇游戏");
int po = 2;//初始方向变量
snake[0][0] = 7;
snake[0][1] = 7;//初始蛇头坐标
headX = snake[0][0];
headY = snake[0][1];
gotoxy(30, 7);
cout << "欢迎来到贪吃蛇游戏";
gotoxy(30, 9);
cout << "作者:李国良 版本1.0";
gotoxy(30, 11);
cout << "请输入你的姓名:";
cin >> name;
system("cls");
gotoxy(30, 7);
cout << "游戏控制方式:";
gotoxy(30, 9);
cout << "W键:向上 S键:向下";
gotoxy(30, 11);
cout << "A键:向左 D键:向右";
gotoxy(30, 13);
cout << "空格键:暂停";
gotoxy(30, 15);
cout << "将游戏窗口最大化之后";
gotoxy(30, 17);
cout << "按回车键开始游戏...";
cin.get();
cin.get();
system("cls");
while (true)
{
po = setdirection(po);
system("cls");
changesnake(po);
ifchangefood();
if (!judgelife())
break;
draw();
Sleep(speed);
}
gotoxy(30, 10);
cout << "Game Over!!!";
Sleep(2000);
gotoxy(28, 12);
system("pause");
return 0;
}
void gotoxy(short x, short y)
{
COORD position = { x, y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, position);
}
int setdirection(int x)
{
char ch;
if (_kbhit())
{
ch = _getch();
switch (ch)
{
case 'w':
x = 1;
break;
case 's':
x = 2;
break;
case 'a':
x = 3;
break;
case 'd':
x = 4;
break;
case ' ':
gotoxy(37, 16);
cout << "游 戏 暂 停. . .";
gotoxy(37, 18);
system("pause");
break;
default:
break;
}
}
return x;
}
void changesnake(int x)
{
switch (x)
{
case 1:
headY -= 1;
break;
case 2:
headY += 1;
break;
case 3:
headX -= 1;
break;
case 4:
headX += 1;
break;
default:
break;
}
for (int i = length; i > 0; --i)
{
for (int j = 0; j < 2; ++j)
{
snake[i][j] = snake[i - 1][j];
}
}
snake[0][0] = headX;
snake[0][1] = headY;
}
void ifchangefood()
{
if (snake[0][0] == food[0] && snake[0][1] == food[1])
{
length++;
makefood();
++score;
if (length > 5)
{
speed = 450;
level = 2;
}
if (length > 10)
{
speed = 400;
level = 3;
}
if (length > 15)
{
speed = 350;
level = 4;
}
if (length > 20)
{
speed = 300;
level = 5;
}
if (length > 25)
{
speed = 250;
level = 6;
}
if (length > 30)
{
speed = 200;
level = 7;
}
if (length > 35)
{
speed = 150;
level = 8;
}
if (length > 40)
{
speed = 100;
level = 9;
}
if (length > 45)
{
speed = 50;
level = 10;
}
}
}
void makefood()
{
srand((unsigned)time(NULL));
food[0] = rand() % 30 + 2;
food[1] = rand() % 30 + 4;
for (int m = 0; m < length; ++m)
{
if (food[0] == snake[m][0] && food[1] == snake[m][1])
{
makefood();
break;
}
}
}
bool judgelife()
{
for (int x = 1; x < length; ++x)
{
if (headX == snake[x][0] && headY == snake[x][1])
{
return false;
}
}
if (headX < 1 || headY < 3 || headX > 34 || headY > 34)
return false;
else
return true;
}
void drawsnake()
{
gotoxy(snake[0][0], snake[0][1]);
cout << "@";
for (int n = 1; n < length; ++n)
{
gotoxy(snake[n][0], snake[n][1]);
cout << "#";
}
}
void drawfood()
{
gotoxy(food[0], food[1]);
cout << "$";
}
void drawwall()
{
gotoxy(0, 0);
cout << "------------------------------------";
gotoxy(16, 1);
cout << "贪吃蛇";
gotoxy(0, 2);
cout << "++++++++++++++++++++++++++++++++++++";
gotoxy(0, 35);
cout << "------------------------------------";
for (int x = 0; x < 35; ++x)
{
gotoxy(0, x);
cout << "|";
gotoxy(35, x);
cout << "|";
}
}
void drawscore()
{
gotoxy(37, 10);
cout << "分数:" << score;
gotoxy(37, 12);
cout << "等级:" << level;
gotoxy(37, 14);
cout << "玩家姓名:" << name;
}
void draw()
{
drawsnake();
drawfood();
drawwall();
drawscore();
}
//还有另外一个
#include
#include
#include
#include
#include
#include
#include
#define N 22
using namespace std;
int gameover;
int x1, y1; // 随机出米
int x,y;
long start;
//=======================================
//类的实现与应用initialize
//=======================================
//下面定义贪吃蛇的坐标类
class snake_position
{
public:
int x,y; //x表示行,y表示列
snake_position(){};
void initialize(int &);//坐标初始化
};
snake_position position[(N-2)*(N-2)+1]; //定义贪吃蛇坐标类数组,有(N-2)*(N-2)个坐标
void snake_position::initialize(int &j)
{
x = 1;
y = j;
}
//下面定义贪吃蛇的棋盘图
class snake_map
{
private:
char s[N][N];//定义贪吃蛇棋盘,包括墙壁。
int grade, length;
int gamespeed; //前进时间间隔
char direction; // 初始情况下,向右运动
int head,tail;
int score;
bool gameauto;
public:
snake_map(int h=4,int t=1,int l=4,char d=77,int s=0):length(l),direction(d),head(h),tail(t),score(s){}
void initialize(); //初始化函数
void show_game();
int updata_game();
void setpoint();
void getgrade();
void display();
};
//定义初始化函数,将贪吃蛇的棋盘图进行初始化
void snake_map::initialize()
{
int i,j;
for(i=1;i<=3;i++)
s[1][i] = '*';
s[1][4] = '#';
for(i=1;i<=N-2;i++)
for(j=1;j<=N-2;j++)
s[i][j]=' '; // 初始化贪吃蛇棋盘中间空白部分
for(i=0;i<=N-1;i++)
s[0][i] = s[N-1][i] = '-'; //初始化贪吃蛇棋盘上下墙壁
for(i=1;i<=N-2;i++)
s[i][0] = s[i][N-1] = '|'; //初始化贪吃蛇棋盘左右墙壁
}
//============================================
//输出贪吃蛇棋盘信息
void snake_map::show_game()
{
system("cls"); // 清屏
int i,j;
cout << endl;
for(i=0;i>grade;
while( grade>7 || grade<1 )
{
cout << "请输入数字1-7选择等级,输入其他数字无效" << endl;
cin >> grade;
}
switch(grade)
{
case 1: gamespeed = 1000;gameauto = 0;break;
case 2: gamespeed = 800;gameauto = 0;break;
case 3: gamespeed = 600;gameauto = 0;break;
case 4: gamespeed = 400;gameauto = 0;break;
case 5: gamespeed = 200;gameauto = 0;break;
case 6: gamespeed = 100;gameauto = 0;break;
case 7: grade = 1;gamespeed = 1000;gameauto = 1;break;
}
}
//输出等级,得分情况以及称号
void snake_map::display()
{
cout << "\n\t\t\t\t等级:" << grade;
cout << "\n\n\n\t\t\t\t速度:" << gamespeed;
cout << "\n\n\n\t\t\t\t得分:" << score << "分" ;
}
//随机产生米
void snake_map::setpoint()
{
srand(time(0));
do
{
x1 = rand() % (N-2) + 1;
y1 = rand() % (N-2) + 1;
}while(s[x1][y1]!=' ');
s[x1][y1]='*';
}
char key;
int snake_map::updata_game()
{
gameover = 1;
key = direction;
start = clock();
while((gameover=(clock()-start<=gamespeed))&&!kbhit());
//如果有键按下或时间超过自动前进时间间隔则终止循环
if(gameover)
{
getch();
key = getch();
}
if(key == ' ')
{
while(getch()!=' '){};//这里实现的是按空格键暂停,按空格键继续的功能,但不知为何原因,需要按两下空格才能继续。这是个bug。
}
else
direction = key;
switch(direction)
{
case 72: x= position[head].x-1; y= position[head].y;break; // 向上
case 80: x= position[head].x+1; y= position[head].y;break; // 向下
case 75: x= position[head].x; y= position[head].y-1;break; // 向左
case 77: x= position[head].x; y= position[head].y+1; // 向右
}
if(!(direction==72||direction==80||direction==75 ||direction==77)) // 按键非方向键
gameover = 0;
else if(x==0 || x==N-1 ||y==0 || y==N-1) // 碰到墙壁
gameover = 0;
else if(s[x][y]!=' '&&!(x==x1&&y==y1)) // 蛇头碰到蛇身
gameover = 0;
else if(x==x1 && y==y1)
{ // 吃米,长度加1
length ++;
if(length>=8 && gameauto)
{
length -= 8;
grade ++;
if(gamespeed>=200)
gamespeed -= 200; // 改变贪吃蛇前进速度
else
gamespeed = 100;
}
s[x][y]= '#'; //更新蛇头
s[position[head].x][position[head].y] = '*'; //吃米后将原先蛇头变为蛇身
head = (head+1) % ( (N-2)*(N-2) ); //取蛇头坐标
position[head].x = x;
position[head].y = y;
show_game();
gameover = 1;
score += grade*20; //加分
setpoint(); //产生米
}
else
{ // 不吃米
s[position[tail].x][position[tail].y]=' ';//将蛇尾置空
tail= (tail+1) % ( (N-2) * (N-2) );//更新蛇尾坐标
s[position[head].x][position[head].y]='*'; //将蛇头更为蛇身
head= (head+1) % ( (N-2) * (N-2) );
position[head].x = x;
position[head].y = y;
s[position[head].x][position[head].y]='#'; //更新蛇头
gameover = 1;
}
return gameover;
}
//====================================
//主函数部分
//====================================
int main()
{
char ctn = 'y';
int nodead;
cout<<"\n\n\n\n\n\t\t\t 欢迎进入贪吃蛇游戏!"<> ctn;
}
return 0;
}
#include
#include
#include
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
#include
#include
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);}
void appear()
{for(i=3;i<=22;i++) for(j=1;j<=15;j++) if(a[i][j]!=0) {Place(i-2,2*j); printf("■");}}
void disappear()
{for(i=3;i<=22;i++) for(j=1;j<=15;j++) if(a[i][j]==2) {Place(i-2,2*j); printf(" ");}}
int search(int x0,int y0,int x,int y)
{
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;
}
void button(int x)
{
int m=x*10;
if(x<3)
{
Place(22,m); printf("┌┄┄┄┐");
Place(23,m);
{
if(x==0) {if(ok!=0) printf("┆start ┆"); else printf("┆pause ┆");}
if(x==1) printf("┆reset ┆"); if(x==2) printf("┆ exit ┆");
}
Place(24,m); printf("└┄┄┄┘");
}
else
{
Place(9,m-33); printf("┌┄┄┐");
Place(10,m-33);
{
if(x==4) printf("┆yes ┆"); else printf("┆ no ┆");
}
Place(11,m-33); printf("└┄┄┘");
}
}
void menu(int x)
{
int k,l;
if(x==0)
{
if(ok!=0) {ok=0; return;}
else while(1)
{
mouse=GetAsyncKeyState(VK_LBUTTON);
Place(9,13); printf("pause");
if(search(7,360,71,390)==2) break; Sleep(50);
}
Place(9,12); for(j=1;j<=6;j++) printf(" "); //appear();
}
else
{
for(i=1;i<=20;i++) for(j=2;j<=30;j++) {Place(i,j); printf(" ");}
Place(7,2); for(i=1;i<=15;i++) printf("┄"); Place(12,2); for(i=1;i<=15;i++) printf("┄");
Place(8,6); printf("Do you want to "); if(x==1) printf("reset?"); if(x==2) printf("exit?");
button(4); button(5);
while(1)
{
mouse=GetAsyncKeyState(VK_LBUTTON);
for(i=0;i<=1;i++)
{
k=search(63+i*80,150,110+i*80,180);
if(k!=2) {if(k==1) color(15); else color(7); button(i+4);}
else
{for(l=7;l<=12;l++) for(j=2;j<=30;j++) {Place(l,j); printf(" ");}
if(i==0) {if(x==1) ok=2; else exit(0);} return;
}
}
Sleep(50);
}
}
}
void click()
{
for(i=0;i<=2;i++)
{ mouse=GetAsyncKeyState(VK_LBUTTON);
int k=search(i*80+7,360,i*80+71,390);
if(k!=2) {if(k==1) color(15); else color(7); button(i); color(7);}
else{menu(i); return;}
}
Sleep(50);
}
void ss()
{
Place(9,39); printf("%d",score);
if(score>=speed*100)
{
speed++; ti=ti-200;
if(ti<0) ti=0;
else
{
for(i=1;i<=4;i++)
{
Place(9,12); printf("speed up!"); Sleep(70);
Place(9,12); for(j=1;j<=9;j++) printf(" "); Sleep(70);
}Sleep(200);
}
}
if(best=0;i--)
{
turn[j][q]=ta[i][j];
q++;
if(q>2+add) q=0;
}
}
for(i=0;i<=3;i++) for(j=0;j<=3;j++) ta[i][j]=turn[i][j];
}
}
void born()
{
int x,q=0; srand(time(NULL)); bn=b; shape=nextshape; x=rand()%12+1; if(b==5&&x>1) {x--; add=1;}
for(i=0;i<=2+add;i++) for(j=x;j<=x+2+add;j++) {a[i][j]=nex[i][q]; q++; if(q>2+add) q=0;}add=0;
}
void next()
{
srand(time(NULL)); b=rand()%7; nextshape=rand()%4; add=0;
for(i=3;i<=6;i++) for(j=37;j<=45;j++) {Place(i,j); printf(" ");}
for(i=0;i<=3;i++) for(j=0;j<=3;j++) nex[i][j]=turn[i][j]=ta[i][j]=0; ta[1][1]=2;
if(b==0) ta[1][2]=ta[1][0]=ta[0][1]=2; if(b==1) ta[1][2]=ta[1][0]=ta[0][0]=2; if(b==2) ta[1][2]=ta[1][0]=ta[0][2]=2; if(b==3) ta[1][0]=ta[0][1]=ta[0][2]=2;
if(b==4) ta[0][0]=ta[0][1]=ta[1][2]=2; if(b==5) {ta[1][0]=ta[1][2]=ta[1][3]=2; add=1;} if(b==6) ta[0][0]=ta[0][1]=ta[1][0]=2;
if(nextshape>0&&b!=6)
{change(nextshape); for(i=0;i<=3;i++) for(j=0;j<=3;j++) {nex[i][j]=turn[i][j]; if(nex[i][j]==2) {Place(i+3,j*2+37); printf("■");}}}
else {for(i=0;i<=3;i++) for(j=0;j<=3;j++) {nex[i][j]=ta[i][j]; if(nex[i][j]==2) {Place(i+3,j*2+37); printf("■");}}}
}
void clear()
{
int c=0,f=0,l[23],s=0,k;
for(i=3;i<=22;i++)
{
l[i]=0;
for(j=1;j<=15;j++) c=a[i][j]+c;
if(c==15)
{
for(j=1;j<=15;j++) a[i][j]=0;
for(k=i-1;k>=2;k--) for(j=1;j<=15;j++) a[k+1][j]=a[k][j];
f++; l[i]=1; s=5;
}
c=0;
}
score=score+f*10; while(f>1) {score=score+f*5; f--;} f=0;
while(s>0)
{
for(i=22;i>=3;i--) if(l[i]==1) { Place(i-2,2); for(j=1;j<=15;j++) printf("■");}Sleep(70);
for(i=22;i>=3;i--) if(l[i]==1) { Place(i-2,2); for(j=1;j<=30;j++) printf(" ");}Sleep(70);s--;
}
for(i=3;i<=22;i++) for(j=1;j<=15;j++) {Place(i-2,2*j); printf(" ");} appear();
}
void control()
{
int up,down,right,left,c=0,d=0,x,y,no=0,k,l=0,q=0; k=shape; add=0;
up=GetAsyncKeyState(VK_UP); down=GetAsyncKeyState(VK_DOWN); right=GetAsyncKeyState(VK_RIGHT); left=GetAsyncKeyState(VK_LEFT);
if(down!=0) { tim=0;}
if(left!=0||right!=0)
{
Sleep(100);disappear();
for(i=0;i<=22;i++) for(j=1;j<=15;j++)
{
if(a[i][j]==2&&a[i][j-1]!=1) c++; if(a[i][j]==2&&a[i][j+1]!=1) d++;
}
for(i=0;i<=22;i++) for(j=1;j<=15;j++) if(left!=0&&c==4&&a[i][j]==2) {a[i][j-1]=a[i][j]; a[i][j]=0;}
for(i=0;i<=22;i++) for(j=15;j>=1;j--) if(right!=0&&d==4&&a[i][j]==2) {a[i][j+1]=a[i][j];a[i][j]=0;} appear();
}
if(up!=0&&bn!=6)
{
Sleep(150); disappear();
for(i=0;i<=22;i++)
{
for(j=1;j<=15;j++) if(a[i][j]==2) {x=i; no=1; break;}
if(no==1) break;
} no=0;
for(j=1;j<=15;j++)
{
for(i=0;i<=22;i++) if(a[i][j]==2) {y=j; no=1; break;}
if(no==1) break;
} no=0;
if(k==1) y--; if(k==2) x--; add=0;
if(bn==5) {add=1; if(k==0||k==2) x--; if(k==1||k==3) y--;}
for(i=0;i<=3;i++) for(j=0;j<=3;j++) ta[i][j]=a[x+i][y+j];
if(bn>=0&&bn<5)
{
if(k==0) ta[2][0]=0; if(k==1) ta[0][0]=0; if(k==2) ta[0][2]=0; if(k==3) ta[2][2]=0;
}
if(bn==5)
{ if(k==0) ta[0][3]=ta[2][0]=ta[2][1]=ta[3][1]=ta[3][0]=0;
if(k==1) ta[0][0]=ta[1][0]=ta[0][1]=ta[1][1]=ta[3][3]=0;
if(k==2) ta[3][0]=ta[0][2]=ta[0][3]=ta[1][2]=ta[1][3]=0;
if(k==3) ta[0][0]=ta[2][2]=ta[2][3]=ta[3][2]=ta[3][3]=0;
}
for(i=0;i<=2+add;i++) for(j=0;j<=2+add;j++) if(ta[i][j]!=1) l++;
if(l==9+add*7)
{
change(1);
for(i=0;i<=22;i++) for(j=1;j<=15;j++) if(a[i][j]==2) a[i][j]=0;
for(i=0;i<=2+add;i++) for(j=0;j<=2+add;j++) if(turn[i][j]==2) a[x+i][y+j]=turn[i][j]; shape++;
}
if(shape>3) shape=0; appear();
}
}
int main()
{
int k,start,finish,d; printf("┌"); for(i=1;i<=15;i++) printf("┄"); printf("┐\n");
for(i=1;i<=20;i++) printf("┆\n"); printf("└");
for(i=1;i<=15;i++) printf("┄"); printf("┘"); for(i=1;i<=20;i++) {Place(i,32); printf("┆\n");}
Place(2,37); printf("NEXT"); Place(8,37); printf("SCORE"); Place(14,37); printf("BEST"); Place(11,37); printf("SPEED");
for(i=0;i<=2;i++) button(i);
while(ok==1) click();
while(ok!=1)
{
replace(); next();
while(1)
{
born();
next();
while(1)
{
appear();
k=0;
start=clock();
while(1)
{
finish=clock(); d=finish-start;
if(d>=tim) break;
control(); click(); if(ok==2) break;
}
if(ok==2) break; disappear();
for(i=22;i>=0;i--) for(j=1;j<=15;j++) if(a[i][j]==2&&a[i+1][j]!=1) k++;
if(k==4)
{
for(i=22;i>=0;i--) for(j=1;j<=15;j++) if(a[i][j]==2)
{
a[i+1][j]=2;a[i][j]=0;
}
}
else
{
for(i=22;i>=0;i--) for(j=1;j<=15;j++) if(a[i][j]==2) a[i][j]=1;
break;
}
tim=ti; appear();
}
if(ok==2) break;
appear();clear();ss();
for(i=1;i<=15;i++) if(a[2][i]==1) {ok=1; break;}
}
if(ok==2) {ok=0; continue;}
for(i=1;i<=20;i++) for(j=2;j<=30;j++) {Place(i,j); printf(" ");}
Place(9,12); printf("GAME OVER!");
while(ok==1) click();
}
return 0;
}
#include
#include
#include
#include
using namespace std;
struct pos
{
int x;
int y;
} food, head; /*食物蛇头坐标*/
const int mapX = 35;/*地图*/
const int mapY = 35;/*地图*/
int snake[mapX * mapY][2];/*蛇身子*/
int length;/*蛇身子长度*/
int direction;/*方向*/
void info();/*提示信息*/
void init();/*初始化*/
void getInput();/*获取键盘输入*/
void changeSnake();
bool alive();/*没有撞墙&&咬到自己*/
void makeFood();/*制造食物*/
void gotoXY(short, short);/*移动光标到指定位置*/
void drawMap();/*画地图*/
void drawFood();/*画食物*/
void drawSnake();/*画蛇*/
void clearSnake();/*清除蛇*/
void upgrade();/*吃到食物,升级*/
bool getFood();
void run();
void gameOver();
int main()
{
run();
}
void run()
{
cout << "按任意键开始游戏..." << endl;
getch();
srand(int(time(NULL)));
info();/*提示信息*/
init();/*初始化*/
while (true) /*一步的操作*/
{
gotoXY(37, 3);
cout << length;
for (int i = 0; i < length; i++)
{
gotoXY(37, 2 + i);
cout << snake[i][0] << " " << snake[i][1];
}
getInput();/*获取键盘输入*/
clearSnake();/*清除出蛇*/
changeSnake();/*改变蛇身*/
drawSnake();/*画出蛇*/
if (!alive()) /*没有撞墙&&咬到自己*/
{
gameOver();/*结束游戏*/
break;
}
if (getFood()) /*到食物的地方*/
{
upgrade();/*吃到食物,升级*/
makeFood();/*制造食物*/
drawFood();/*画出新的食物*/
}
//getch();
Sleep(100);
}
}
void info()
{
gotoXY(37,9);
cout << "|--------------------|";
gotoXY(37,10);
cout << "|wasd控制上下左右移动|";
gotoXY(37,11);
cout << "|空格键暂停 |";
gotoXY(37,12);
cout << "|--------------------|";
}
void init()
{
length = 1;
head.x = snake[0][0] = 7;
head.y = snake[0][1] = 7;
makeFood();
direction = 3;
drawMap();
drawFood();
drawSnake();
}
void getInput() /*获取输入,返回一个方向1234分别代表上左下右*/
{
char ch;
if (_kbhit())
{
ch = getch();
if ((ch == 'w' && direction == 3) ||
(ch == 'a' && direction == 4) ||
(ch == 's' && direction == 1) ||
(ch == 'd' && direction == 2))
{
return ;
}
switch (ch)
{
case 'w':
direction = 1;
break;
case 'a':
direction = 2;
break;
case 's':
direction = 3;
break;
case 'd':
direction = 4;
break;
case ' ':
{
gotoXY(37,13);
cout << "游戏暂停,按任意键继续游戏...";
getch();
}
break;
default:
break;
}
}
}
void changeSnake() /*根据方向更新蛇身*/
{
/*更新蛇头*/
switch (direction)
{
case 1:
head.y--;
break;
case 2:
head.x--;
break;
case 3:
head.y++;
break;
case 4:
head.x++;
break;
default:
break;
}
/*更新蛇身*/
for (int i = length; i > 0; i--)
{
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
snake[0][0] = head.x;
snake[0][1] = head.y;
}
bool getFood()
{
return (head.x == food.x && head.y == food.y) ? true : false;
}
bool alive()
{
if (head.x == 0 || head.x == (mapX + 1) || head.y == 0 || head.y == (mapY + 1))
{
return false;
}
for (int i = 1; i < length; i++)
{
if (head.x == snake[i][0] && head.y == snake[i][1])
{
return false;
}
}
return true;
}
void makeFood() /*产生食物坐标*/
{
food.x = rand() % mapX + 1;
food.y = rand() % mapY + 1;
/*食物位置不合法的情况:食物food与snake重合*/
for (int i = 0; i < length; i++)
{
if (food.x == snake[i][0] && food.y == snake[i][1])
{
makeFood();
break;
}
}
}
void upgrade()
{
length++;
snake[length - 1][0] = food.x;
snake[length - 1][1] = food.y;
}
void gotoXY(short x, short y)
{
COORD pos = {x, y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void drawMap()/*画地图*/
{
gotoXY(0, 0);
for (int i = 0; i < 37; i++) cout << "-";
gotoXY(0, 36);
for (int i = 0; i < 37; i++) cout << "-";
for (int i = 0; i < 37; i++)
{
gotoXY(0, i);
cout << "|";
gotoXY(36, i);
cout << "|";
}
}
void drawFood()/*画食物*/
{
gotoXY(food.x, food.y);
cout << "&";
}
void drawSnake()/*画蛇*/
{
for (int i = 0; i < length; i++)
{
gotoXY(snake[i][0], snake[i][1]);
cout << "#";
}
}
void clearSnake()/*清除最后一位即可*/
{
gotoXY(snake[length - 1][0], snake[length - 1][1]);
cout << " ";
}
void gameOver()
{
system("cls");
cout << "Game Over";
}
#include
#include
#include
#include
#include
#include
// 2的概率大于4
#define random_2_4 (rand()%5==4 ? 4:2)
#define random_x(x) (rand()%x)
using namespace std;
// 矩阵大小
const int MAX = 4;
const int MAXMAX = MAX*MAX;
// 初始数据个数
const int INIT_SIZE = 2;
// 数据矩阵
int _matrix[MAX][MAX];
// 合并临时数组
int current[MAX];
// 数据计数
int _count;//0的个数
// 按键操作 => 不使用char...防止不必要的失败... => ch[0]即可...
string ch;
// 得分...
int score;
// 打印数组
void print();
// 操作说明
void help();
// 初始操作
void init();
// 随机位置 => 随机数据2/4
bool random_data_one();
// 上左下右
bool b_up();
bool b_left();
bool b_down();
bool b_right();
void _up();
void _left();
void _down();
void _right();
int main()
{
init();
print();
while (b_up() || b_left() || b_down() || b_right())
{
help();
switch (ch[0])
{
case 'w':
_up();
break;
case 'a':
_left();
break;
case 's':
_down();
break;
case 'd':
_right();
break;
default:
cout <<"!!!!!!!!!!!!无效输入..."<< endl << endl;
break;
}
print();
}
cout << " Game Over !! " << endl;
cout << "最终得分Socre => " << score << endl;
system("pause");
system("pause");
system("pause");
return 0;
}
void print()
{
cout<<"http://blog.csdn.net/yc7369"< " << score << endl;
cout << "-------------------------------------" << endl;
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
{
if (_matrix[i][j])
cout << setw(5) << _matrix[i][j] << " |";
else
cout << setw(7) << " |";
}
cout << endl;
}
cout << "-------------------------------------" << endl << endl;;
}
void help()
{
cout << "wasd => 上左下右" << endl;
cout << "请输入: ";
cin >> ch;
}
void init()
{
_count = 0;
score = 0;
srand((int)time(0));
for (int i = 0; i < INIT_SIZE; i++)
random_data_one();
}
bool random_data_one()
{
if (_count == MAXMAX)
return false;
int left = MAXMAX - _count;
int tmp = random_x(left);
int num = random_2_4;
int k = 0;
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
{
if (_matrix[i][j] == 0)
{
if (k++ == tmp)
{
_matrix[i][j] = num;
break;
}
}
}
}
++_count;
return true;
}
bool b_up()
{
for (int k = 0; k < MAX; k++)
{
// 特殊情况...这一列有空元素...
bool flag = false;
for (int i = 0; i < MAX - 1; i++)
{
if (_matrix[i][k] == 0)
flag = true;
else
{
int j = i + 1;
while (j < MAX)
{
if (_matrix[j][k])
{
if (_matrix[i][k] == _matrix[j][k])
return true;
else
break;
}
else{
++j;
}
}
}
}
if (flag)
{
// 空元素上方有元素则为True...
// 使用左右夹击法...
int i = 0, j = MAX - 1;
while (i < MAX)
{
if (_matrix[i][k])
++i;
else
break;
}
while (j >= 0)
{
if (_matrix[j][k] == 0)
--j;
else
break;
}
if (i < j)
return true;
}
}
return false;
}
bool b_left()
{
for (int k = 0; k < MAX; k++)
{
// 特殊情况...这一行有空元素...
bool flag = false;
for (int i = 0; i < MAX - 1; i++)
{
if (_matrix[k][i] == 0)
flag = true;
else
{
int j = i + 1;
while (j < MAX)
{
if (_matrix[k][j])
{
if (_matrix[k][i] == _matrix[k][j])
return true;
else
break;
}
else{
++j;
}
}
}
}
if (flag)
{
// 空元素右边有元素则为True...
// 使用左右夹击法...
int i = 0, j = MAX - 1;
// i 是空元素位置,j是非空元素位置
while (i < MAX)
{
if (_matrix[k][i])
++i;
else
break;
}
while (j >= 0)
{
if (_matrix[k][j] == 0)
--j;
else
break;
}
if (i < j)
return true;
}
}
return false;
}
bool b_down()
{
for (int k = 0; k < MAX; k++)
{
// 特殊情况...这一列有空元素...
bool flag = false;
for (int i = MAX - 1; i > 0; i--)
{
if (_matrix[i][k] == 0)
flag = true;
else
{
int j = i - 1;
while (j >= 0)
{
if (_matrix[j][k])
{
if (_matrix[i][k] == _matrix[j][k])
return true;
else
break;
}
else{
--j;
}
}
}
}
if (flag)
{
// 空元素上方有元素则为True... => 下边第一个空元素在上边第一个非空元素下边即可...
// 使用左右夹击法...
int i = 0, j = MAX - 1;
// i 是非空位置,j是空元素位置
while (i < MAX)
{
if (_matrix[i][k] == 0)
++i;
else
break;
}
while (j >= 0)
{
if (_matrix[j][k])
--j;
else
break;
}
if (i < j)
return true;
}
}
return false;
}
bool b_right()
{
for (int k = 0; k < MAX; k++)
{
// 特殊情况...这一行这一行有空元素...
bool flag = false;
for (int i = MAX - 1; i > 0; i--)
{
if (_matrix[k][i] == 0)
flag = true;
else
{
int j = i - 1;
while (j >= 0)
{
if (_matrix[k][j])
{
if (_matrix[k][i] == _matrix[k][j])
return true;
else
break;
}
else{
--j;
}
}
}
}
if (flag)
{
// 空元素左边有元素则为True... => 右边第一个空元素在左边第一个非空元素右边即可...
// 使用左右夹击法...
int i = 0, j = MAX - 1;
// i 是非空位置,j是空元素位置
while (i < MAX)
{
if (_matrix[k][i] == 0)
++i;
else
break;
}
while (j >= 0)
{
if (_matrix[k][j])
--j;
else
break;
}
if (i < j)
return true;
}
}
return false;
}
void _up()
{
cout << "按下了上键" << endl << endl;
if (b_up())
{
cout << "可以向上合并" << endl;
for (int i = 0; i < MAX; i++)
{
memset(current, 0, sizeof(int)*MAX);
int ii = 0;
for (int j = 0; j < MAX; j++)
{
if (_matrix[j][i])
current[ii++] = _matrix[j][i];
}
for (int k = 0; k < ii - 1; k++)
{
if (current[k] == current[k + 1])
{
current[k] *= 2;
score += current[k];
current[k + 1] = 0;
++k;
--_count;
}
}
ii = 0;
for (int j = 0; j < MAX; j++)
{
if (current[j])
_matrix[ii++][i] = current[j];
}
for (; ii < MAX; ii++)
_matrix[ii][i] = 0;
}
random_data_one();
}
else{
cout << "!!!!!!!!!!!向上无效" << endl << endl;
}
}
void _left()
{
cout << "按下了左键" << endl << endl;
if (b_left())
{
cout << "可以向左合并" << endl << endl;
for (int i = 0; i < MAX; i++)
{
memset(current, 0, sizeof(int)*MAX);
int ii = 0;
for (int j = 0; j < MAX; j++)
{
if (_matrix[i][j])
current[ii++] = _matrix[i][j];
}
for (int k = 0; k < ii - 1; k++)
{
if (current[k] == current[k + 1])
{
current[k] *= 2;
score += current[k];
current[k + 1] = 0;
++k;
--_count;
}
}
ii = 0;
for (int j = 0; j < MAX; j++)
{
if (current[j])
_matrix[i][ii++] = current[j];
}
for (; ii < MAX; ii++)
_matrix[i][ii] = 0;
}
random_data_one();
}
else{
cout << "!!!!!!!!!!!向左无效" << endl << endl;
}
}
void _down()
{
cout << "按下了下键" << endl << endl;
if (b_down())
{
cout << "可以向下合并" << endl;
for (int i = 0; i < MAX; i++)
{
memset(current, 0, sizeof(int)*MAX);
int ii = 0;
for (int j = MAX - 1; j >= 0; j--)
{
if (_matrix[j][i])
current[ii++] = _matrix[j][i];
}
for (int k = 0; k < ii - 1; k++)
{
if (current[k] == current[k + 1])
{
current[k] *= 2;
score += current[k];
current[k + 1] = 0;
++k;
--_count;
}
}
ii = MAX - 1;
for (int j = 0; j < MAX; j++)
{
if (current[j])
_matrix[ii--][i] = current[j];
}
for (; ii >= 0; ii--)
_matrix[ii][i] = 0;
}
random_data_one();
}
else{
cout << "!!!!!!!!!!!向下无效" << endl << endl;
}
}
void _right()
{
cout << "按下了右键" << endl << endl;
if (b_right())
{
cout << "可以向右合并" << endl;
for (int i = 0; i < MAX; i++)
{
memset(current, 0, sizeof(int)*MAX);
int ii = 0;
for (int j = MAX - 1; j >= 0; j--)
{
if (_matrix[i][j])
current[ii++] = _matrix[i][j];
}
for (int k = 0; k < ii - 1; k++)
{
if (current[k] == current[k + 1])
{
current[k] *= 2;
score += current[k];
current[k + 1] = 0;
++k;
--_count;
}
}
ii = MAX - 1;
for (int j = 0; j < MAX; j++)
{
if (current[j])
_matrix[i][ii--] = current[j];
}
for (; ii >= 0; ii--)
_matrix[i][ii] = 0;
}
random_data_one();
}
else{
cout << "!!!!!!!!!!!向右无效" << endl << endl;
}
}
#include
#include
#include
#include
#include
#include
// 由于棋盘格与逻辑雷区格有一定差别,所以为了坐标能够相互映射,设置宏I,J,分别映射逻辑表的i,j。
#define I (i+2)
#define J (2*(j+1)+1)
/*字体颜色处理函数*/
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor){
HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,(ForeColor%16)|(BackGroundColor%16*16));
}
/*游戏主程序*/
void showmine(); //显示所有地雷
void setmine(int x); //布雷
void printmine(); //打印所有地雷
void countmine(int rowno, int colno); //数雷算法
//////////////////////////////////////////////////////构建棋盘////////////////////////////////
char row[16][80]={
{"----------Platform---------------------"},
{"------------------------------------------------------------------------"},
{"01|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
{"02|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
{"03|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
{"04|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
{"05|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
{"06|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
{"07|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
{"08|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
{"09|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
{"10|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
{"11|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
{"12|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
{"13|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
{"14|\x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02 \x02"},
};
////////////////////////////////////////////////构建雷区//////////////////////////////////////////////////
int mines[14][14]={ //雷区模拟图(01二值化,标记地雷)
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
////////////////////////////////////////////////////////////////////////////////////////////
int mines_demo[14][14]={ //雷数统计图(用于显示周围雷总数)
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
//////////////////////////////////////////////////////////////////////////////
int countstep=0; //已挖坑数目(计数器),用于判定玩家获胜
int n=5; //布雷数(待输入)
/////////////////////////////////主函数开始///////////////////////////////////////////
void main()
{
int i=0,j=0;
int flag=1;
int temp=0;
char ch1;
SetColor(3,0);
printf("欢乐扫雷\n\n");
printf("w-上 s-下 a-左 d-右 q-挖雷\n\n");
SetColor(7,0);
printf("作者:PeterZheng\n");
printf("EagleEyes 工作室\n");
printf("EagleEyes.Inc\n\n");
getch();
system("cls");
printf("你想要布几颗雷?(1< mines <196)");
scanf("%d",&n);
system("cls");
setmine(n);
row[I][J]='\x01';
system("cls");
showmine();
printf("\n\n");
//按键消息处理核心代码
while(flag)
{
ch1=getch();
if(ch1=='w'){
if(i==0){
system("cls");
showmine();
continue;
}else{
system("cls");
if(row[I][J]<'9' && row[I][J]>='0'){ //确认移动前的位置属于已被挖雷(在上一次光标移动时已经恢复数字,只是还没有showmine()而已)
i--; //光标移动
if(row[I][J]<'9' && row[I][J]>='0'){ //确认移动后的位置已被挖雷
temp=row[I][J]; //用temp记录该位置原数字
row[I][J]='\x01'; //将该位置标记为选中
showmine(); //显示雷区图
row[I][J]=temp; //将选中位置改回原数字
continue;
}
row[I][J]='\x01'; //如果不会被覆盖,则直接选中即可
showmine(); //显示雷区图
continue;
}
//移动前的位置未被挖雷情况
row[I][J]='\x02';
i--;
if(row[I][J]<'9' && row[I][J]>='0'){
temp=row[I][J];
row[I][J]='\x01';
showmine();
row[I][J]=temp;
continue;
}
row[I][J]='\x01';
showmine();
}
}else if(ch1=='s'){
if(i==13){
system("cls");
showmine();
continue;
}else{
system("cls");
if(row[I][J]<'9' && row[I][J]>='0'){//防止当前位数字覆盖
i++;
if(row[I][J]<'9' && row[I][J]>='0'){ //防止下一位数字覆盖
temp=row[I][J];
row[I][J]='\x01';
showmine();
row[I][J]=temp;
continue;
}
row[I][J]='\x01';
showmine();
continue;
}
row[I][J]='\x02';
i++;
if(row[I][J]<'9' && row[I][J]>='0'){
temp=row[I][J];
row[I][J]='\x01';
showmine();
row[I][J]=temp;
continue;
}
row[I][J]='\x01';
showmine();
}
}else if (ch1=='a'){
if(j==0){
system("cls");
if(row[I][J]<'9' && row[I][J]>='0'){
temp=row[I][J];
row[I][J]='\x01';
showmine();
row[I][J]=temp;
continue;
}
showmine();
continue;
}else{
system("cls");
if(row[I][J]<'9' && row[I][J]>='0'){
j--;
if(row[I][J]<'9' && row[I][J]>='0'){
temp=row[I][J];
row[I][J]='\x01';
showmine();
row[I][J]=temp;
continue;
}
row[I][J]='\x01';
showmine();
continue;
}
row[I][J]='\x02';
j--;
if(row[I][J]<'9' && row[I][J]>='0'){
temp=row[I][J];
row[I][J]='\x01';
showmine();
row[I][J]=temp;
continue;
}
row[I][J]='\x01';
showmine();
}
}else if (ch1=='d'){
if(j==13){
system("cls");
showmine();
continue;
}else{
system("cls");
if(row[I][J]<'9' && row[I][J]>='0'){
j++;
if(row[I][J]<'9' && row[I][J]>='0'){
temp=row[I][J];
row[I][J]='\x01';
showmine();
row[I][J]=temp;
continue;
}
row[I][J]='\x01';
showmine();
continue;
}
row[I][J]='\x02';
j++;
if(row[I][J]<'9' && row[I][J]>='0'){
temp=row[I][J];
row[I][J]='\x01';
showmine();
row[I][J]=temp;
continue;
}
row[I][J]='\x01';
showmine();
}
}else if(ch1=='q'){ //挖雷消息识别&处理
system("cls");
if (mines[i][j]==1) //如果踩雷
{
printmine(); //打印全图雷区,游戏结束...
printf("\n BOOM!\n\n");
flag=0;
getch();
fflush(stdin);
break;
}else{
//如果没有踩雷...标示本位地雷数目(从雷数统计图读取)
if(i>0 && j>0 && row[I][J]=='\x01'){
row[I][J]=(char)(mines_demo[i][j]+48);
countstep++;
}else if(i==0 && j>0 && row[I][J]=='\x01'){
row[I][J]=(char)(mines_demo[i][j]+48);
countstep++;
}else if(i>0 && j==0 && row[I][J]=='\x01'){
row[I][J]=(char)(mines_demo[i][j]+48);
countstep++;
}else if(i==0 && j==0 && row[I][J]=='\x01'){
row[I][J]=(char)(mines_demo[i][j]+48);
countstep++;
}
if(mines_demo[i][j]==0 && i>0 && j>0 && i<13 && j<13){ //无雷地区自动展开
if(mines_demo[i+1][j]==0 && row[I+1][J]=='\x02'){
row[I+1][J]=(char)(mines_demo[i+1][j]+48);
countstep++;
}
if(mines_demo[i-1][j]==0 && row[I-1][J]=='\x02'){
row[I-1][J]=(char)(mines_demo[i-1][j]+48);
countstep++;
}
if(mines_demo[i][j+1]==0 && row[I][2*(j+1+1)+1]=='\x02'){
row[I][2*(j+1+1)+1]=(char)(mines_demo[i][j+1]+48);
countstep++;
}
if(mines_demo[i][j-1]==0 && row[I][2*(j-1+1)+1]=='\x02'){
row[I][2*(j-1+1)+1]=(char)(mines_demo[i][j+1]+48);
countstep++;
}
if(mines_demo[i+1][j+1]==0 && row[I+1][2*(j+1+1)+1]=='\x02'){
row[I+1][2*(j+1+1)+1]=(char)(mines_demo[i+1][j+1]+48);
countstep++;
}
if(mines_demo[i+1][j-1]==0 && row[I+1][2*(j-1+1)+1]=='\x02'){
row[I+1][2*(j-1+1)+1]=(char)(mines_demo[i+1][j+1]+48);
countstep++;
}
if(mines_demo[i-1][j+1]==0 && row[I-1][2*(j+1+1)+1]=='\x02'){
row[I-1][2*(j+1+1)+1]=(char)(mines_demo[i-1][j+1]+48);
countstep++;
}
if(mines_demo[i-1][j-1]==0 && row[I-1][2*(j-1+1)+1]=='\x02'){
row[I-1][2*(j-1+1)+1]=(char)(mines_demo[i-1][j+1]+48);
countstep++;
}
}
system("cls");
showmine();
if(countstep==196-n)
{
printf("\nYou Win!\n\n");
getch();
flag=0;
break;
}
}
}
}
}
////////////////////////////////////////////主函数结束/////////////////////////////////////////////////
//////////////////////////////////////////数雷算法开始//////////////////////////////////////////
void countmine(int rowno, int colno)
{
int count;
int i,j;
i=rowno;
j=colno;
mines_demo[i][j]=mines[i-1][j]+mines[i+1][j]+mines[i][j-1]+mines[i][j+1]+mines[i-1][j-1]+mines[i-1][j+1]+mines[i+1][j-1]+mines[i+1][j+1];
return;
}
/////////////////////////////////////数雷算法结束////////////////////////////////////////////
void DebugMines()
{
int i=0 , j=0;
printf("\n\n");
for (i = 0 ; i < 14 ; i++)
{
for (j=0;j<14;j++)
{
printf("%d ",mines[i][j]);
}
printf("\n");
}
printf("\n\n");
for (i = 0 ; i < 14 ; i++)
{
for (j=0;j<14;j++)
{
printf("%d ",mines_demo[i][j]);
}
printf("\n");
}
}
///////////////////////////////////////棋盘显示///////////////////////////////////////////////
void showmine(){
int i=0,j=0;
for(i=0;i<16;i++){
for(j=0;j<30;j++){
if(row[i][j]=='\xf'){
SetColor(4,0);
printf("%c",row[i][j]);
SetColor(7,0);
continue;
}else if(row[i][j]<='8' && row[i][j]>='0' && j>2){
SetColor(2,0);
printf("%c",row[i][j]);
SetColor(7,0);
continue;
}
printf("%c",row[i][j]);
}
printf("\n");
}
}
/////////////////////////////////////////随机布雷///////////////////////////////////////
void setmine(int x){
int i,j,k;
//初始化棋盘
for(i=0;i<14;i++){
for(j=0;j<14;j++){
mines[i][j]=0;
}
}
//设置随机种子
srand(time(0));
//随机布雷开始
for(k=1;k<=x;){
i=rand()%14;
j=rand()%14;
if(mines[i][j]!=1){
mines[i][j]=1;
k++;
}else{
continue;
}
}
//方格雷数计算开始
for(i=0;i<14;i++){
for(j=0;j<14;j++){
if(i>0 && j>0 && i<13 && j<13){
countmine(i,j);
}else{ //预置方格雷数计算
//if(mines[i][j]!=1){
if(i==0 && j!=0 && j!=13){
mines_demo[0][j]=mines[0][j-1]+mines[1][j]+mines[0][j+1]+mines[1][j-1]+mines[1][j+1];
}else if(j==0 && i!=0 && i!=13){
mines_demo[i][0]=mines[i-1][0]+mines[i+1][0]+mines[i-1][1]+mines[i+1][1]+mines[i][1];
}else if(i==0 && j==0){
mines_demo[0][0]=mines[0][1]+mines[1][1]+mines[1][0];
}else if(i==13 && j==13){
mines_demo[13][13]=mines[12][13]+mines[12][12]+mines[13][12];
}else if(i==0 && j==13){
mines_demo[0][13]=mines[0][12]+mines[1][12]+mines[1][13];
}else if(i==13 && j==0){
mines_demo[13][0]=mines[12][0]+mines[12][1]+mines[13][1];
}else if(i==13 && j!=0 && j!=13){
mines_demo[i][j] = mines[i][j-1]+mines[i][j+1]+mines[i-1][j-1]+mines[i-1][j]+mines[i-1][j+1];
}else if (j==13 && i!=0 && i!=13)
{
mines_demo[i][j] = mines[i+1][j]+mines[i-1][j]+mines[i-1][j-1]+mines[i][j-1]+mines[i+1][j-1];
}
//}
}
}
}
return;
}
/////////////////////////////打印雷区(失败时)///////////////////////////////
void printmine()
{
int i,j;
system("cls");
for(i=0;i<14;i++)
{
for(j=0;j<14;j++){
if( mines[i][j]==1){
row[I][J]='\xf';
}else{
row[I][J]='\x02';
}
}
}
showmine();
return;
}