自己写的俄罗斯方块 欢迎提出建议
#include<iostream>
#include<cstring>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
#include<conio.h>
#define maxn 20
#define maxm 10
using namespace std;
int square[maxn+5][maxm+5];//▇
int difficult,x,y,button;//中心在x行,y列;
int ch[4][4];
int swap_cube[4][4];
int sq[maxn+5][maxm+5];
int ch_extra[4][4];
int tt,swap_tt;//第tt个cube
char move;
int score=0;
clock_t start,end;
int cube1[4][4]= {1,1,1,1,
1,0,1,1,
0,0,0,1,
1,1,1,1
};
int cube2[4][4]= {1,1,1,1,
1,0,0,1,
0,0,1,1,
1,1,1,1
};
int cube3[4][4]= {1,1,1,1,
0,1,1,1,
0,0,0,1,
1,1,1,1
};
int cube4[4][4]= {1,1,1,1,
0,0,1,1,
0,0,1,1,
1,1,1,1
};
int cube5[4][4]= {1,1,1,1,
0,0,1,1,
1,0,0,1,
1,1,1,1
};
int cube6[4][4]= {1,1,1,1,
1,1,0,1,
0,0,0,1,
1,1,1,1
};
int cube7[4][4]= {1,1,1,1,
0,0,0,0,
1,1,1,1,
1,1,1,1
};
void copy(int x,int y) {
for(int i=-1; i<=2; i++)
for(int j=-1; j<=2; j++) {
if (ch[i+1][j+1]==0)
sq[x+i][y+j]=ch[i+1][j+1];
}
}
void outt() {
memcpy(sq,square,sizeof(square));
copy(x,y);
printf("╔════════════════════╗ ");
cout<<endl;
for(int i=1; i<=maxn; i++) {
cout<<"║";
for(int j=1; j<=maxm; j++)
if (sq[i][j]) cout<<" ";
else cout<<"▇";
cout<<"║ ";
if(i>=1&&i<=3)
{
for(int j=0;j<4;j++)
if(swap_cube[i-1][j]==1) cout<<" ";else cout<<"▇";
cout<<endl;
}else cout<<endl;
}
printf("╚════════════════════╝ \n");
printf("The difficulty you choosed is %d \n",difficult);
printf("Your score is %d \n",score);
}
void intt() {
memset(square,0,sizeof(square));
for(int i=1; i<=maxn; i++)
for(int j=1; j<=maxm; j++)
square[i][j]=1;
memcpy(swap_cube,cube1,sizeof(cube1));
swap_tt=1;
srand(time(NULL));
cout<<"Please Keydown the difficulty you like(1~9):"<<endl;
cin>>difficult;
system("cls");
//cout<<"Input the degree of difficulty (0~9):";
//cin>>difficult;
}
void add_cube() {
tt=swap_tt;
swap_tt=rand()%7+1;
// tt=7;
memcpy(ch,swap_cube,sizeof(swap_cube));
switch(swap_tt) {
case 1:
memcpy(swap_cube,cube1,sizeof(cube1));
break;
case 2:
memcpy(swap_cube,cube2,sizeof(cube2));
break;
case 3:
memcpy(swap_cube,cube3,sizeof(cube3));
break;
case 4:
memcpy(swap_cube,cube4,sizeof(cube4));
break;
case 5:
memcpy(swap_cube,cube5,sizeof(cube5));
break;
case 6:
memcpy(swap_cube,cube6,sizeof(cube6));
break;
case 7:
memcpy(swap_cube,cube7,sizeof(cube7));
break;
default:break;
}
x=1;
y=6;
}
bool check_move(int move_xx,int move_yy) {
for(int i=0; i<=3; i++)
for(int j=0; j<=3; j++)
if(sq[move_xx+i-1][move_yy+j-1]==0&&ch[i][j]==0) return false;
return true;
}
void run(int move_xx,int move_yy) {
move_xx+=x;
move_yy+=y;
memcpy(sq,square,sizeof(square));
if(check_move(move_xx,move_yy)) {
x=move_xx;
y=move_yy;
}
}
bool check_rotate(int x,int y) {
for(int i=0; i<=3; i++)
for(int j=0; j<=3; j++)
if(sq[x+i-1][y+j-1]==0&&ch_extra[i][j]==0) return false;
return true;
}
void rotate() {
if(tt==4) return;
memset(ch_extra,1,sizeof(ch_extra));
if(tt!=7)
for(int i=0; i<=3; i++)
for(int j=0; j<=3; j++)
ch_extra[2-j][i]=ch[i][j];
if(tt==7) {
if (ch[1][0]==0)
for(int i=0; i<=3; i++)
ch_extra[i][1]=0;
else
for(int i=0; i<=3; i++)
ch_extra[1][i]=0;
}
memcpy(sq,square,sizeof(square));
if(check_rotate(x,y)) {
memcpy(ch,ch_extra,sizeof(ch_extra));
}
}
bool check_move_down() {
memcpy(sq,square,sizeof(square));
for(int i=0; i<=3; i++)
for(int j=0; j<=3; j++)
if(sq[x+i][y+j-1]==0&&ch[i][j]==0) return false;
return true;
}
void save_square() {
memcpy(sq,square,sizeof(square));
copy(x,y);
memcpy(square,sq,sizeof(sq));
}
void clean(){
for(int i=1;i<=maxn;i++)
{
int flag=1;
for(int j=1;j<=maxm;j++)
if(square[i][j]==1){
flag=0;
break;
}
if(flag==1){
for(int k=1;k<=maxm;k++)
{
for(int j=i;j>=2;j--)
square[j][k]=square[j-1][k];
square[1][k]=1;
}
score+=difficult;
}
}
}
void KeyBoard_hit(){
int tt;
move=0;
tt=getch();
switch(tt) {
case 0xE0:
switch(tt=getch()) {
case 72:
move='u';
break;
case 80:
move='d';
break;
case 75:
move='l';
break;
case 77:
move='r';
break;
default:
break;
}
break;
default:
break;
}
switch(move) {
case 'u':
rotate();
break;
case 'l':
run(0,-1);
break;
case 'r':
run(0,1);
break;
case 'd':
run(+1,0);
break;
}
}
int main() {
while(true){
intt();
button=1;
add_cube();
start=clock();
outt();
while(button) {
end=clock();
if(end-start>=(10-difficult)*100) {
start=clock();
run(+1,0);
system("cls");
outt();
if (check_move_down()==false) {
while(clock()-start<=(10-difficult)*100)
{
if(kbhit())
{
KeyBoard_hit();
system("cls");
outt();
}
}
if(check_move_down()) continue;
save_square();
clean();
add_cube();}
}
if(kbhit())
{
KeyBoard_hit();
system("cls");
outt();
if (check_move_down()==false) {
while(clock()-start<=(10-difficult)*100)
{
if(kbhit())
{
KeyBoard_hit();
system("cls");
outt();
}
}
save_square();
clean();
add_cube();
}
}
bool flag=true;
for(int i=1;i<=maxm;i++)
if(square[1][i]==0) {
flag=false;
break;
}
if (!flag) button=false;
}
system("cls");
cout<<"GAMEOVER!!!"<<endl;
system("pause");
}
return 0;
}
本人只是一名蒟蒻,大佬不喜勿喷?