Question: So I have the code for tetris.cpp and tetris.h. I need the code that will make the code I have executable. Basically need all the

So I have the code for "tetris.cpp" and "tetris.h". I need the code that will make the code I have executable. Basically need all the code that would go in the main function.

My Code:

tetris.cpp #include "tetris.h" Tetrad::Tetrad() { LoadGraphics(); } Tetrad::~Tetrad() { SDL_FreeSurface(tetrisbits); } void Tetrad::Init(int newtype) { //piece[0] is the "pivot" block during rotations type=newtype; switch(type) { case SHAPE_Z: piece[0]=5; piece[1]=4; piece[2]=15; piece[3]=16; break; case SHAPE_S: piece[0]=15; piece[1]=6; piece[2]=14; piece[3]=5; break; case SHAPE_L: piece[0]=5; piece[1]=4; piece[2]=6; piece[3]=14; break; case SHAPE_J: piece[0]=5; piece[1]=4; piece[2]=6; piece[3]=16; break; case SHAPE_O: piece[0]=4; piece[1]=5; piece[2]=14; piece[3]=15; break; case SHAPE_T: piece[0]=5; piece[1]=4; piece[2]=6; piece[3]=15; break; case SHAPE_I: default: piece[0]=4; piece[1]=3; piece[2]=5; piece[3]=6; break; } pieces=4; //color=rand()%6+1; color = type; moves=0; haslanded=false; tetradrect.x=color*16; tetradrect.y=0; tetradrect.w=16; tetradrect.h=16; shadowrect.x=color*16; shadowrect.y=32; shadowrect.w=16; shadowrect.h=16; return; } void Tetrad::Theme(const char* theme) { SDL_Surface *temp; char* filename = new char[80]; sprintf(filename, "media/pieces%s.png", theme); temp=IMG_Load(filename); if(!temp) { printf("Failed to load tetrad graphics %s ", filename); } else { SDL_FreeSurface(tetrisbits); tetrisbits=SDL_DisplayFormatAlpha(temp); SDL_FreeSurface(temp); } delete[] filename; return; } int Tetrad::GetType() { return type; } void Tetrad::LoadGraphics() { SDL_Surface* temp; temp=IMG_Load("media/pieces.png"); if(!temp) { printf("Failed to load tetrad graphics media/pieces.png "); } tetrisbits=SDL_DisplayFormatAlpha(temp); SDL_FreeSurface(temp); } bool Tetrad::MoveLeft(Bucket* bucket) { int i; bool move=!haslanded; for(i=0; iIsEmpty(piece[i]-1, true) || piece[i]%10==0) { move=false; } } if(move) { for(i=0; iIsEmpty(piece[i]+1, true) || piece[i]%10==9) { move=false; } } if(move) { for(i=0; iIsEmpty(piece[i]+10, true) || piece[i]/10==19) { move=false; } } if(move) { for(i=0; iAddBlock(piece[i], color); } haslanded=true; } return move; } bool Tetrad::MoveUp(Bucket* bucket) { int i; bool move=true; for(i=0; iIsEmpty(piece[i]+10, true) || piece[i]/10==0) { move=false; } } if(move) { for(i=0; i199 || !bucket->IsEmpty(piece[i], true) || (leftedge && rightedge) ) { rotate=false; } if(piece[i]%10max) { max=piece[i]%10; } } if(max-min>pieces) { rotate=false; } if(rotate) { break; } else { for(i=0; iloc||loc>199) { return false; } for(i=0; iIsEmpty(j, true)) { shadoff=(j/10)-(piece[i]/10); } else { break; } } shadmin = (shadmin>shadoff) ? shadoff : shadmin; if(SDL_BlitSurface(tetrisbits, &tetradrect, surf, &destrect)) { LoadGraphics(); SDL_BlitSurface(tetrisbits, &tetradrect, surf, &destrect); } } //draw the shadow for(i=0; i199) { return false; } else { return (area[block]==EMPTY || (break_is_empty && area[block]&BREAK)); } } void Bucket::AddBlock(int block, int color) { int i; bool fullrow=true; if(0>block||block>199) { return; } area[block]=STACK | color; for(i=(block/10)*10; i<(block/10)*10+10; i++) { if(IsEmpty(i, false)) { fullrow=false; break; } } if(fullrow) { for(i=(block/10)*10; i<(block/10)*10+10; i++) { area[i]|=BREAK; } if(block/10breakrowmax) { breakrowmax=block/10; } } return; } void Bucket::DelBlock(int block) { if(0>block||block>200) { return; } if(!IsEmpty(block, false)) { area[block]|=BREAK; } return; } int Bucket::Drop() { int i, j; int droppingto=breakrowmax; int lines=0; if(breakrowmin<=breakrowmax) { for(i=breakrowmax; i>=0; i--) { //each row if(!(area[i*10]&BREAK)) { //if it's not breaking for(j=0; j<10; j++) { //each block area[droppingto*10+j]=area[i*10+j]; area[i]=EMPTY; } droppingto--; } else { lines++; } } } else { for(i=0; i<200; i++) { if(area[i]&BREAK) { area[i]=EMPTY; } } } breakrowmin=19; breakrowmax=0; return lines; } int Bucket::Draw(SDL_Surface* surf) { int i; int lines=0; //TODO: could be faster if went up from bottom? // if an entire blank row is found, we're done, right? if(breakstart && SDL_GetTicks()-breakstart>BREAK_TIME) { lines=Drop(); breakstart=0; } for(i=0; i<200; i++) { destrect.x=(i%10)*16+OFFSET_X; destrect.y=(i/10)*16+OFFSET_Y; if(area[i]&BREAK) { if(!breakstart) { //start breaking! stackrect.x=0; stackrect.y=0; if(SDL_BlitSurface(tetrisbits, &stackrect, surf, &destrect)) { LoadGraphics(); SDL_BlitSurface(tetrisbits, &stackrect, surf, &destrect); } breakstart=SDL_GetTicks(); } else { //continue breaking! //TODO: funky visual tricks using breakstart and BREAK_TIME stackrect.x=(rand()%7)*16; stackrect.y=(rand()%2)*16; if(((float)(i%10)/10.0)+0.4 <= 1.0-(((float)SDL_GetTicks()-(float)breakstart)/ (float)BREAK_TIME/2) || 1.0-((float)(i%10)/10.0)+0.4 <= 1.0-(((float)SDL_GetTicks()-(float)breakstart)/ (float)BREAK_TIME/2)) { if(SDL_BlitSurface(tetrisbits, &stackrect, surf, &destrect)) { LoadGraphics(); SDL_BlitSurface(tetrisbits, &stackrect, surf, &destrect); } } } } else if(area[i]!=EMPTY) { stackrect.x=(area[i] & COLORMASK) *16; stackrect.y=16; if(SDL_BlitSurface(tetrisbits, &stackrect, surf, &destrect)) { LoadGraphics(); SDL_BlitSurface(tetrisbits, &stackrect, surf, &destrect); } } } return lines; } 

tetris.h:

#include "tetris.h" Tetrad::Tetrad() { LoadGraphics(); } Tetrad::~Tetrad() { SDL_FreeSurface(tetrisbits); } void Tetrad::Init(int newtype) { //piece[0] is the "pivot" block during rotations type=newtype; switch(type) { case SHAPE_Z: piece[0]=5; piece[1]=4; piece[2]=15; piece[3]=16; break; case SHAPE_S: piece[0]=15; piece[1]=6; piece[2]=14; piece[3]=5; break; case SHAPE_L: piece[0]=5; piece[1]=4; piece[2]=6; piece[3]=14; break; case SHAPE_J: piece[0]=5; piece[1]=4; piece[2]=6; piece[3]=16; break; case SHAPE_O: piece[0]=4; piece[1]=5; piece[2]=14; piece[3]=15; break; case SHAPE_T: piece[0]=5; piece[1]=4; piece[2]=6; piece[3]=15; break; case SHAPE_I: default: piece[0]=4; piece[1]=3; piece[2]=5; piece[3]=6; break; } pieces=4; //color=rand()%6+1; color = type; moves=0; haslanded=false; tetradrect.x=color*16; tetradrect.y=0; tetradrect.w=16; tetradrect.h=16; shadowrect.x=color*16; shadowrect.y=32; shadowrect.w=16; shadowrect.h=16; return; } void Tetrad::Theme(const char* theme) { SDL_Surface *temp; char* filename = new char[80]; sprintf(filename, "media/pieces%s.png", theme); temp=IMG_Load(filename); if(!temp) { printf("Failed to load tetrad graphics %s ", filename); } else { SDL_FreeSurface(tetrisbits); tetrisbits=SDL_DisplayFormatAlpha(temp); SDL_FreeSurface(temp); } delete[] filename; return; } int Tetrad::GetType() { return type; } void Tetrad::LoadGraphics() { SDL_Surface* temp; temp=IMG_Load("media/pieces.png"); if(!temp) { printf("Failed to load tetrad graphics media/pieces.png "); } tetrisbits=SDL_DisplayFormatAlpha(temp); SDL_FreeSurface(temp); } bool Tetrad::MoveLeft(Bucket* bucket) { int i; bool move=!haslanded; for(i=0; iIsEmpty(piece[i]-1, true) || piece[i]%10==0) { move=false; } } if(move) { for(i=0; iIsEmpty(piece[i]+1, true) || piece[i]%10==9) { move=false; } } if(move) { for(i=0; iIsEmpty(piece[i]+10, true) || piece[i]/10==19) { move=false; } } if(move) { for(i=0; iAddBlock(piece[i], color); } haslanded=true; } return move; } bool Tetrad::MoveUp(Bucket* bucket) { int i; bool move=true; for(i=0; iIsEmpty(piece[i]+10, true) || piece[i]/10==0) { move=false; } } if(move) { for(i=0; i199 || !bucket->IsEmpty(piece[i], true) || (leftedge && rightedge) ) { rotate=false; } if(piece[i]%10max) { max=piece[i]%10; } } if(max-min>pieces) { rotate=false; } if(rotate) { break; } else { for(i=0; iloc||loc>199) { return false; } for(i=0; iIsEmpty(j, true)) { shadoff=(j/10)-(piece[i]/10); } else { break; } } shadmin = (shadmin>shadoff) ? shadoff : shadmin; if(SDL_BlitSurface(tetrisbits, &tetradrect, surf, &destrect)) { LoadGraphics(); SDL_BlitSurface(tetrisbits, &tetradrect, surf, &destrect); } } //draw the shadow for(i=0; i199) { return false; } else { return (area[block]==EMPTY || (break_is_empty && area[block]&BREAK)); } } void Bucket::AddBlock(int block, int color) { int i; bool fullrow=true; if(0>block||block>199) { return; } area[block]=STACK | color; for(i=(block/10)*10; i<(block/10)*10+10; i++) { if(IsEmpty(i, false)) { fullrow=false; break; } } if(fullrow) { for(i=(block/10)*10; i<(block/10)*10+10; i++) { area[i]|=BREAK; } if(block/10breakrowmax) { breakrowmax=block/10; } } return; } void Bucket::DelBlock(int block) { if(0>block||block>200) { return; } if(!IsEmpty(block, false)) { area[block]|=BREAK; } return; } int Bucket::Drop() { int i, j; int droppingto=breakrowmax; int lines=0; if(breakrowmin<=breakrowmax) { for(i=breakrowmax; i>=0; i--) { //each row if(!(area[i*10]&BREAK)) { //if it's not breaking for(j=0; j<10; j++) { //each block area[droppingto*10+j]=area[i*10+j]; area[i]=EMPTY; } droppingto--; } else { lines++; } } } else { for(i=0; i<200; i++) { if(area[i]&BREAK) { area[i]=EMPTY; } } } breakrowmin=19; breakrowmax=0; return lines; } int Bucket::Draw(SDL_Surface* surf) { int i; int lines=0; //TODO: could be faster if went up from bottom? // if an entire blank row is found, we're done, right? if(breakstart && SDL_GetTicks()-breakstart>BREAK_TIME) { lines=Drop(); breakstart=0; } for(i=0; i<200; i++) { destrect.x=(i%10)*16+OFFSET_X; destrect.y=(i/10)*16+OFFSET_Y; if(area[i]&BREAK) { if(!breakstart) { //start breaking! stackrect.x=0; stackrect.y=0; if(SDL_BlitSurface(tetrisbits, &stackrect, surf, &destrect)) { LoadGraphics(); SDL_BlitSurface(tetrisbits, &stackrect, surf, &destrect); } breakstart=SDL_GetTicks(); } else { //continue breaking! //TODO: funky visual tricks using breakstart and BREAK_TIME stackrect.x=(rand()%7)*16; stackrect.y=(rand()%2)*16; if(((float)(i%10)/10.0)+0.4 <= 1.0-(((float)SDL_GetTicks()-(float)breakstart)/ (float)BREAK_TIME/2) || 1.0-((float)(i%10)/10.0)+0.4 <= 1.0-(((float)SDL_GetTicks()-(float)breakstart)/ (float)BREAK_TIME/2)) { if(SDL_BlitSurface(tetrisbits, &stackrect, surf, &destrect)) { LoadGraphics(); SDL_BlitSurface(tetrisbits, &stackrect, surf, &destrect); } } } } else if(area[i]!=EMPTY) { stackrect.x=(area[i] & COLORMASK) *16; stackrect.y=16; if(SDL_BlitSurface(tetrisbits, &stackrect, surf, &destrect)) { LoadGraphics(); SDL_BlitSurface(tetrisbits, &stackrect, surf, &destrect); } } } return lines; } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!