public:
Algo(int w,int h);
void nextSec();
void nextShape();
void changeShape();
void showShape();
void showShape2();
void start();
bool inRec(int x,int y);
void left();
void right();
~Algo();
private:
long elapsec;
int current;//0,1,2,3,4
int** space;
int w,h;
int status;
int* cPosX;
int* cPosY;
int cPos;
};
#include "Algo.h"
#include <time.h>
#include <stdio.h>
#include <iostream>
using namespace std;
Algo::Algo(int w,int h){
elapsec=0;
srand( (unsigned)time( NULL ) );
current=0;
this->w=w;
this->h=h;
space=new int*[w];
for(int i=0;i<w;i++){
space[i]=new int[h];
}
start();
}
Algo::~Algo(){
for(int i=0;i<w;i++)
delete space[i];
delete space;
delete cPosX;
delete cPosY;
}
void Algo::nextSec(){
if(status==0){
cout<<"game over"<<endl;
return;
}
//status==1
//down or nextshape
if(inRec(cPosX[0],cPosY[0]) && inRec(cPosX[1],cPosY[1]) && inRec(cPosX[2],cPosY[2])&& inRec(cPosX[3],cPosY[3])){
cPosY[0]=cPosY[0]+1;
cPosY[1]=cPosY[1]+1;
cPosY[2]=cPosY[2]+1;
cPosY[3]=cPosY[3]+1;
}
else{
space[cPosY[0]-1][cPosX[0]]=1;
space[cPosY[1]-1][cPosX[1]]=1;
space[cPosY[2]-1][cPosX[2]]=1;
space[cPosY[3]-1][cPosX[3]]=1;
nextShape();
}
//eliminate
bool elimite;
for(int i=0;i<w;i++){
//elimite line i
elimite=true;
for(int j=0;j<h;j++){
if(space[i][j]==0)
elimite=false;
}
if(elimite){
cout<<"eliminate one line"<<endl;
for(int ii=i;ii>0;ii--){
for(int j=0;j<h;j++){
space[ii][j]=space[ii-1][j];
}
}
}
}
}
void Algo::nextShape(){
current=(rand()%5);
switch(current){
case 0:
//----
cPosX[0]=w/2-2;cPosY[0]=0;
cPosX[1]=w/2-1;cPosY[1]=0;
cPosX[2]=w/2;cPosY[2]=0;
cPosX[3]=w/2+1;cPosY[3]=0;
//cout<<"next 0:"<<cPosX[0]<<","<<cPosY[0]<<":"<<cPosX[1]<<","<<cPosY[1]<<":"<<cPosX[2]<<","<<cPosY[2]<<":"<<cPosX[3]<<","<<cPosY[3]<<":"<<endl;
break;
case 1:
//---
// -
cPosX[0]=w/2-1;cPosY[0]=0;
cPosX[1]=w/2;cPosY[1]=0;
cPosX[2]=w/2+1;cPosY[2]=0;
cPosX[3]=w/2;cPosY[3]=1;
//cout<<"next 1:"<<cPosX[0]<<","<<cPosY[0]<<":"<<cPosX[1]<<","<<cPosY[1]<<":"<<cPosX[2]<<","<<cPosY[2]<<":"<<cPosX[3]<<","<<cPosY[3]<<":"<<endl;
break;
case 2:
//--
// --
cPosX[0]=w/2-1;cPosY[0]=0;
cPosX[1]=w/2;cPosY[1]=0;
cPosX[2]=w/2;cPosY[2]=1;
cPosX[3]=w/2+1;cPosY[3]=1;
//cout<<"next 2:"<<cPosX[0]<<","<<cPosY[0]<<":"<<cPosX[1]<<","<<cPosY[1]<<":"<<cPosX[2]<<","<<cPosY[2]<<":"<<cPosX[3]<<","<<cPosY[3]<<":"<<endl;
break;
case 3:
// --
//--
cPosX[0]=w/2;cPosY[0]=0;
cPosX[1]=w/2+1;cPosY[1]=0;
cPosX[2]=w/2-1;cPosY[2]=1;
cPosX[3]=w/2;cPosY[3]=1;
//cout<<"next 3:"<<cPosX[0]<<","<<cPosY[0]<<":"<<cPosX[1]<<","<<cPosY[1]<<":"<<cPosX[2]<<","<<cPosY[2]<<":"<<cPosX[3]<<","<<cPosY[3]<<":"<<endl;
break;
case 4:
//--
//--
cPosX[0]=w/2-1;cPosY[0]=0;
cPosX[1]=w/2;cPosY[1]=0;
cPosX[2]=w/2-1;cPosY[2]=1;
cPosX[3]=w/2;cPosY[3]=1;
//cout<<"next 4:"<<cPosX[0]<<","<<cPosY[0]<<":"<<cPosX[1]<<","<<cPosY[1]<<":"<<cPosX[2]<<","<<cPosY[2]<<":"<<cPosX[3]<<","<<cPosY[3]<<":"<<endl;
break;
}
if(!inRec(cPosX[0],cPosY[0]) ||!inRec(cPosX[1],cPosY[1]) ||!inRec(cPosX[2],cPosY[2]) ||!inRec(cPosX[3],cPosY[3])){
status=0;
}
}
void Algo::changeShape(){
switch(current){
case 0:
//----
if(cPosY[0]==cPosY[1]){
//----
if(!inRec(cPosX[0]+2,cPosY[0]-2)|| !inRec(cPosX[1]+1,cPosY[1]-1)|| !inRec(cPosX[3]-1,cPosY[3]+1)){
return;
}
cPosX[0]=cPosX[0]+2;cPosY[0]=cPosY[0]-2;
cPosX[1]=cPosX[1]+1;cPosY[1]=cPosY[1]-1;
cPosX[3]=cPosX[3]-1;cPosY[3]=cPosY[3]+1;
}
else{
//-
//-
//-
//-
if(!inRec(cPosX[0]-2,cPosY[0]+2) || !inRec(cPosX[1]-1,cPosY[1]+1)|| !inRec(cPosX[3]+1,cPosY[3]-1)){
return;
}
cPosX[0]=cPosX[0]-2;cPosY[0]=cPosY[0]+2;
cPosX[1]=cPosX[1]-1;cPosY[1]=cPosY[1]+1;
cPosX[3]=cPosX[3]+1;cPosY[3]=cPosY[3]-1;
}
break;
case 1:
//---
// -
if( cPosY[0]==cPosY[1] && cPosY[0]==cPosY[2] && cPosY[3]==cPosY[1]+1){
//---
// -
if(!inRec(cPosX[0]+1,cPosY[0]-1))
return;
cPosX[0]=cPosX[0]+1;cPosY[0]=cPosY[0]-1;
}
else if( cPosX[0]==cPosX[1] && cPosX[0]==cPosX[3] && cPosX[2]==cPosX[1]+1){
//-
//--
//-
if(!inRec(cPosX[0]+1,cPosX[0]) || !inRec(cPosX[3]+2,cPosY[3]-1))
return;
cPosX[0]=cPosX[0]+1;
cPosX[3]=cPosX[3]+2;cPosY[3]=cPosY[3]-1;
}
else if( cPosY[1]==cPosY[2] && cPosY[1]==cPosY[3] && cPosY[2]==cPosY[0]+1){
// -
//---
if(!inRec(cPosX[3]-1,cPosY[3]+1))
return;
cPosX[3]=cPosX[3]-1;cPosY[3]=cPosY[3]+1;
}
else {
// -
//--
// -
if(!inRec(cPosX[0]-2,cPosY[0]+1) || !inRec(cPosX[3]-1,cPosY[3]))
return;
cPosX[0]=cPosX[0]-2;cPosY[0]=cPosY[0]+1;
cPosX[3]=cPosX[3]-1;
}
break;
case 2:
//--
// --
if(cPosY[0]==cPosY[1]){
//--
// --
if(!inRec(cPosX[0]+1,cPosY[0]) || !inRec(cPosX[1]-1,cPosY[1]+1)|| !inRec(cPosX[3]-2,cPosY[3]+1))
return;
cPosX[0]=cPosX[0]+1;
cPosX[1]=cPosX[1]-1;cPosY[1]=cPosY[1]+1;
cPosX[3]=cPosX[3]-2;cPosY[3]=cPosY[3]+1;
}
else{
// -
//--
//-
if(!inRec(cPosX[0]-1,cPosY[0]) || !inRec(cPosX[1]+1,cPosY[1]-1)|| !inRec(cPosX[3]+2,cPosY[3]-1))
return;
cPosX[0]=cPosX[0]-1;
cPosX[1]=cPosX[1]+1;cPosY[1]=cPosY[1]-1;
cPosX[3]=cPosX[3]+2;cPosY[3]=cPosY[3]-1;
}
break;
case 3:
// --
//--
if(cPosY[0]==cPosY[1]){
// --
//--
if(!inRec(cPosX[0]-1,cPosY[0]) || !inRec(cPosX[1]-2,cPosY[1]+1)|| !inRec(cPosX[2]+1,cPosY[2])|| !inRec(cPosY[3]+1,cPosY[3]))
return;
cPosX[0]=cPosX[0]-1;
cPosX[1]=cPosX[1]-2;cPosY[1]=cPosY[1]+1;
cPosX[2]=cPosX[2]+1;
cPosY[3]=cPosY[3]+1;
}
else{
//-
//--
// -
if(!inRec(cPosX[0]+1,cPosY[0]) || !inRec(cPosX[1]+2,cPosY[1]-1)|| !inRec(cPosX[2]-1,cPosY[2])|| !inRec(cPosY[3]-1,cPosY[3]))
return;
cPosX[0]=cPosX[0]+1;
cPosX[1]=cPosX[1]+2;cPosY[1]=cPosY[1]-1;
cPosX[2]=cPosX[2]-1;
cPosY[3]=cPosY[3]-1;
}
break;
case 4:
//--
//--
break;
}
}
void Algo::showShape(){
for(int i=0;i<w;i++){
for(int j=0;j<h;j++){
if(space[i][j]==1 )
cout<<"#";
else if(cPosX[0]==j && cPosY[0]==i || cPosX[1]==j && cPosY[1]==i || cPosX[2]==j && cPosY[2]==i|| cPosX[3]==j && cPosY[3]==i){
cout<<"^";
}
else
cout<<" ";
if(j==h-1){
cout<<"|"<<endl;
}
}
}
for(int i=0;i<h;i++){
cout<<"-";
}
cout<<endl;
}
void Algo::showShape2(){
int xmin=w,ymin=h;
for(int i=0;i<4;i++){
//cout<<cPosX[i]<<","<<cPosY[i]<<endl;
if(cPosX[i]<xmin)
xmin=cPosX[i];
if(cPosY[i]<ymin)
ymin=cPosY[i];
}
int x0=cPosX[0]-xmin,x1=cPosX[1]-xmin,x2=cPosX[2]-xmin,x3=cPosX[3]-xmin;
int y0=cPosY[0]-ymin,y1=cPosY[1]-ymin,y2=cPosY[2]-ymin,y3=cPosY[3]-ymin;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(x0==j&&y0==i ||x1==j&&y1==i ||x2==j&&y2==i ||x3==j&&y3==i){
cout<<"*";
}
else{
cout<<" ";
}
if(j==3)
cout<<endl;
}
}
}
bool Algo::inRec(int x,int y){
if(x>=h || x<0 || y>=h || y<0 ||space[y][x]==1)
return false;
else
return true;
}
void Algo::left(){
if(inRec(cPosX[0]-1,cPosY[0]) && inRec(cPosX[1]-1,cPosY[1]) && inRec(cPosX[2]-1,cPosY[2])&& inRec(cPosX[3]-1,cPosY[3])){
cPosX[0]=cPosX[0]-1;
cPosX[1]=cPosX[1]-1;
cPosX[2]=cPosX[2]-1;
cPosX[3]=cPosX[3]-1;
}
}
void Algo::right(){
if(inRec(cPosX[0]+1,cPosY[0]) && inRec(cPosX[1]+1,cPosY[1]) && inRec(cPosX[2]+1,cPosY[2])&& inRec(cPosX[3]+1,cPosY[3])){
cPosX[0]=cPosX[0]+1;
cPosX[1]=cPosX[1]+1;
cPosX[2]=cPosX[2]+1;
cPosX[3]=cPosX[3]+1;
}
}
void Algo::start(){
for(int i=0;i<w;i++){
for(int j=0;j<h;j++){
space[i][j]=0;
}
}
cPosX=new int[4];
cPosY=new int[4];
cPos=0;
for(int i=0;i<4;i++){
cPosX[i]=0;
cPosY[i]=0;
}
status=1;
nextShape();
//nextSec();
}