/*=============================================================================\

|=================== ====================|

|=========== ________ ________ ________ ________ __ ________ ===========|

|=========== |___ ___| _____|___ ___| __ | | _____| ===========|

|=========== | | | |_____ | | | |_| | | |_____ ===========|

|=========== | | | _____| | | | _/| |_____ | ===========|

|=========== | | | |_____ | | | |\ \ | |_____| | ===========|

|=========== |__| |________| |__| |__| \__\|__|________| ===========|

|=========== ===========|

|=================== written by: ====================|

\=============================================================================*/

/*

Computer Science 2004-2005

This is the game of tetris which I have recreated to a certain extent. I could

not ever figure out how to get the high scores to work so please excuse that.

There are also a few other glitches which I was not able to get worked out.

However the games works pretty well. Enjoy!

*/

#include <fstream.h>

#include <iostream.h>

#include <conio.h>

#include <graphics.h>

#include "int9.h" //keyboard header

#include "dosgraphics.h" //easier to open graph

#include <dos.h>

#include <stdlib.h>

#include "apstring.h"

#include "apstring.cpp"

#include <string.h>

//global variables

int block [ 3 ] [ 3 ] , nextblockvar [ 3 ] [ 3 ] , blockcolor, nextblockcolor ;

int blockx = 0 , blocky = 0 , score = 0 ;

void background ( ) ; //draws background of game screen

struct highscores //high score structure which never worked

{

int score, sort ;

char * name, * namesort ;

} ;

highscores hs [ 11 ] ;

void main ( )

{ //OPEN

opengraph ( ) ; //dosgraphics.h , opens graph

randomize ( ) ;

for ( int t = 0 ; t < 11 ; t ++ ) //default hs's to 'NoName 0'

{

hs [ t ] . score = 1 + t ;

hs [ t ] . name = "NoName" ;

}

//declaring functions

void intro ( ) , instructions ( ) , viewhs ( ) , initializehs ( ) ;

int choice = 0 , menu ( ) , game ( ) ;

initializehs ( ) ; //obtains high scores if file exists

intro ( ) ; //calls intro function

while ( choice ! = 4 )

{ //open while

cleardevice ( ) ;

choice = menu ( ) ;

cleardevice ( ) ;

switch ( choice )

{

case 1 :

game ( ) ;

break ;

case 2 :

instructions ( ) ;

break ;

case 3 :

viewhs ( ) ;

break ;

case 4 :

return ;

} //close switch

} //close while

} //CLOSE

//----------------------------------------------------------------intro function

void intro ( )

{

//drawing Tetris on the screen

setfillstyle ( SOLID_FILL,RED ) ;

bar3d ( 64 , 96 , 96 , 192 , 16 , 12 ) ; //T

bar3d ( 32 , 72 , 128 , 96 , 16 , 12 ) ;

setfillstyle ( SOLID_FILL,BLUE ) ;

bar3d ( 128 , 96 , 160 , 216 , 16 , 12 ) ; //E::up/down

bar3d ( 160 , 96 , 224 , 120 , 16 , 12 ) ; //top bar

bar3d ( 160 , 144 , 224 , 168 , 16 , 12 ) ; //2nd bar

bar3d ( 160 , 192 , 224 , 216 , 16 , 12 ) ; //bottom bar

setfillstyle ( SOLID_FILL,GREEN ) ;

bar3d ( 256 , 96 , 288 , 192 , 16 , 12 ) ; //T

bar3d ( 224 , 72 , 320 , 96 , 16 , 12 ) ;

setfillstyle ( SOLID_FILL,MAGENTA ) ;

bar3d ( 320 , 96 , 352 , 216 , 16 , 12 ) ; //R

bar3d ( 384 , 192 , 416 , 216 , 16 , 12 ) ;

bar3d ( 352 , 168 , 384 , 192 , 16 , 12 ) ;

bar3d ( 352 , 144 , 384 , 168 , 16 , 12 ) ;

bar3d ( 352 , 96 , 384 , 120 , 16 , 12 ) ;

bar3d ( 384 , 96 , 416 , 168 , 16 , 12 ) ;

setfillstyle ( SOLID_FILL,YELLOW ) ;

bar3d ( 416 , 72 , 448 , 192 , 16 , 12 ) ; //I

setfillstyle ( SOLID_FILL,CYAN ) ;

bar3d ( 448 , 192 , 544 , 216 , 16 , 12 ) ; //S

bar3d ( 512 , 144 , 544 , 216 , 16 , 12 ) ;

bar3d ( 448 , 96 , 488 , 168 , 16 , 12 ) ;

bar3d ( 448 , 144 , 544 , 168 , 16 , 12 ) ;

bar3d ( 448 , 96 , 544 , 120 , 16 , 12 ) ;

outtextxy ( 192 , 264 , "written by: **********" ) ;

outtextxy ( 200 , 312 , "(Press any key to begin)" ) ;

getch ( ) ;

cleardevice ( ) ;

}

//-----------------------------------------------------------------menu function

int menu ( )

{

settextstyle ( DEFAULT_FONT,HORIZ_DIR, 1 ) ;

setfillstyle ( SOLID_FILL,RED ) ;

setcolor ( WHITE ) ;

bar3d ( 64 , 96 , 96 , 192 , 16 , 12 ) ; //T

bar3d ( 32 , 72 , 128 , 96 , 16 , 12 ) ;

setfillstyle ( SOLID_FILL,BLUE ) ;

bar3d ( 128 , 96 , 160 , 216 , 16 , 12 ) ; //E::up/down

bar3d ( 160 , 96 , 224 , 120 , 16 , 12 ) ; //top bar

bar3d ( 160 , 144 , 224 , 168 , 16 , 12 ) ; //2nd bar

bar3d ( 160 , 192 , 224 , 216 , 16 , 12 ) ; //bottom bar

setfillstyle ( SOLID_FILL,GREEN ) ;

bar3d ( 256 , 96 , 288 , 192 , 16 , 12 ) ; //T

bar3d ( 224 , 72 , 320 , 96 , 16 , 12 ) ;

setfillstyle ( SOLID_FILL,MAGENTA ) ;

bar3d ( 320 , 96 , 352 , 216 , 16 , 12 ) ; //R

bar3d ( 384 , 192 , 416 , 216 , 16 , 12 ) ;

bar3d ( 352 , 168 , 384 , 192 , 16 , 12 ) ;

bar3d ( 352 , 144 , 384 , 168 , 16 , 12 ) ;

bar3d ( 352 , 96 , 384 , 120 , 16 , 12 ) ;

bar3d ( 384 , 96 , 416 , 168 , 16 , 12 ) ;

setfillstyle ( SOLID_FILL,YELLOW ) ;

bar3d ( 416 , 72 , 448 , 192 , 16 , 12 ) ; //I

setfillstyle ( SOLID_FILL,CYAN ) ;

bar3d ( 448 , 192 , 544 , 216 , 16 , 12 ) ; //S

bar3d ( 512 , 144 , 544 , 216 , 16 , 12 ) ;

bar3d ( 448 , 96 , 488 , 168 , 16 , 12 ) ;

bar3d ( 448 , 144 , 544 , 168 , 16 , 12 ) ;

bar3d ( 448 , 96 , 544 , 120 , 16 , 12 ) ;

//--------------------draws tetris on screen

char var = 0 ;

int pos ;

setcolor ( BLUE ) ;

outtextxy ( 256 , 264 , "Start Game" ) ;

setcolor ( WHITE ) ;

outtextxy ( 256 , 288 , "Instructions" ) ;

outtextxy ( 256 , 312 , "High Scores" ) ;

outtextxy ( 256 , 336 , "Exit Game" ) ;

pos = 1 ; //default cursor on Start Game

while ( var ! = ' \r ' ) //until user presses enter key

{ //open enter while

var = getch ( ) ;

if ( var == KEY_UP )

{ //open up if

switch ( pos )

{ //switch up

case 1 :

pos = 4 ;

setcolor ( WHITE ) ;

outtextxy ( 256 , 264 , "Start Game" ) ;

setcolor ( BLUE ) ;

outtextxy ( 256 , 336 , "Exit Game" ) ;

break ;

case 2 :

pos = 1 ;

setcolor ( WHITE ) ;

outtextxy ( 256 , 288 , "Instructions" ) ;

setcolor ( BLUE ) ;

outtextxy ( 256 , 264 , "Start Game" ) ;

break ;

case 3 :

pos = 2 ;

setcolor ( WHITE ) ;

outtextxy ( 256 , 312 , "High Scores" ) ;

setcolor ( BLUE ) ;

outtextxy ( 256 , 288 , "Instructions" ) ;

break ;

case 4 :

pos = 3 ;

setcolor ( WHITE ) ;

outtextxy ( 256 , 336 , "Exit Game" ) ;

setcolor ( BLUE ) ;

outtextxy ( 256 , 312 , "High Scores" ) ;

break ;

} //switch up

} //close up if

if ( var == KEY_DOWN )

{ //open down if

switch ( pos )

{

case 1 :

pos = 2 ;

setcolor ( WHITE ) ;

outtextxy ( 256 , 264 , "Start Game" ) ;

setcolor ( BLUE ) ;

outtextxy ( 256 , 288 , "Instructions" ) ;

break ;

case 2 :

pos = 3 ;

setcolor ( WHITE ) ;

outtextxy ( 256 , 288 , "Instructions" ) ;

setcolor ( BLUE ) ;

outtextxy ( 256 , 312 , "High Scores" ) ;

break ;

case 3 :

pos = 4 ;

setcolor ( WHITE ) ;

outtextxy ( 256 , 312 , "High Scores" ) ;

setcolor ( BLUE ) ;

outtextxy ( 256 , 336 , "Exit Game" ) ;

break ;

case 4 :

pos = 1 ;

setcolor ( WHITE ) ;

outtextxy ( 256 , 336 , "Exit Game" ) ;

setcolor ( BLUE ) ;

outtextxy ( 256 , 264 , "Start Game" ) ;

break ;

}

} //close down if

} //close enter while

return pos ;

} //close menu function

//---------------------------------------------------------instructions function

void instructions ( )

{

cleardevice ( ) ;

setcolor ( BLUE ) ;

background ( ) ;

//==========================draw blocks

setfillstyle ( SOLID_FILL,BLUE ) ;

setcolor ( WHITE ) ;

bar3d ( 416 , 432 , 448 , 456 , 0 , 0 ) ;

bar3d ( 448 , 432 , 480 , 456 , 0 , 0 ) ;

bar3d ( 448 , 408 , 480 , 432 , 0 , 0 ) ;

bar3d ( 448 , 384 , 480 , 408 , 0 , 0 ) ;

setfillstyle ( SOLID_FILL,GREEN ) ;

bar3d ( 384 , 432 , 416 , 456 , 0 , 0 ) ;

bar3d ( 352 , 432 , 384 , 456 , 0 , 0 ) ;

bar3d ( 320 , 432 , 352 , 456 , 0 , 0 ) ;

bar3d ( 352 , 408 , 384 , 432 , 0 , 0 ) ;

setfillstyle ( SOLID_FILL,RED ) ; //falling block

bar3d ( 256 , 240 , 288 , 264 , 0 , 0 ) ;

bar3d ( 256 , 264 , 288 , 288 , 0 , 0 ) ;

bar3d ( 256 , 288 , 288 , 312 , 0 , 0 ) ;

bar3d ( 288 , 288 , 320 , 312 , 0 , 0 ) ;

setcolor ( WHITE ) ;

setfillstyle ( SOLID_FILL,LIGHTGRAY ) ;

bar3d ( 192 , 96 , 224 , 120 , 6 , 6 ) ; //up

line ( 208 , 101 , 208 , 115 ) ;

line ( 208 , 101 , 200 , 110 ) ;

line ( 208 , 101 , 216 , 110 ) ;

bar3d ( 192 , 127 , 224 , 151 , 6 , 6 ) ; //down

line ( 208 , 132 , 208 , 146 ) ;

line ( 208 , 146 , 200 , 136 ) ;

line ( 208 , 146 , 216 , 136 ) ;

bar3d ( 154 , 127 , 186 , 151 , 6 , 6 ) ; //left

line ( 159 , 139 , 181 , 139 ) ;

line ( 159 , 139 , 176 , 129 ) ;

line ( 159 , 139 , 176 , 149 ) ;

bar3d ( 230 , 127 , 262 , 151 , 6 , 6 ) ; //right

line ( 235 , 139 , 257 , 139 ) ;

line ( 240 , 129 , 257 , 139 ) ;

line ( 240 , 149 , 257 , 139 ) ;

setfillstyle ( SOLID_FILL,BLUE ) ;

bar3d ( 155 , 2 , 485 , 70 , 0 , 0 ) ;

bar3d ( 275 , 93 , 489 , 148 , 0 , 0 ) ;

outtextxy ( 160 , 15 , "This is an example of the game screen." ) ;

outtextxy ( 160 , 30 , "Here is where you blocks will be falling." ) ;

outtextxy ( 160 , 45 , "You will use the arrow keys to navigate" ) ;

outtextxy ( 160 , 60 , "the falling blocks." ) ;

outtextxy ( 280 , 95 , "UP:Rotate block clock-wise" ) ;

outtextxy ( 280 , 110 , "DOWN:Let block fall" ) ;

outtextxy ( 280 , 125 , "RIGHT:Move block right" ) ;

outtextxy ( 280 , 140 , "LEFT:Move block left" ) ;

outtextxy ( 200 , 180 , "Press any key to continue." ) ;

getch ( ) ;

background ( ) ;

//==========================draw blocks

setfillstyle ( SOLID_FILL,BLUE ) ;

setcolor ( WHITE ) ;

bar3d ( 416 , 432 , 448 , 456 , 0 , 0 ) ;

bar3d ( 448 , 432 , 480 , 456 , 0 , 0 ) ;

bar3d ( 448 , 408 , 480 , 432 , 0 , 0 ) ;

bar3d ( 448 , 384 , 480 , 408 , 0 , 0 ) ;

setfillstyle ( SOLID_FILL,GREEN ) ;

bar3d ( 384 , 432 , 416 , 456 , 0 , 0 ) ;

bar3d ( 352 , 432 , 384 , 456 , 0 , 0 ) ;

bar3d ( 320 , 432 , 352 , 456 , 0 , 0 ) ;

bar3d ( 352 , 408 , 384 , 432 , 0 , 0 ) ;

setfillstyle ( SOLID_FILL,RED ) ; //falling block

bar3d ( 256 , 240 , 288 , 264 , 0 , 0 ) ;

bar3d ( 256 , 264 , 288 , 288 , 0 , 0 ) ;

bar3d ( 256 , 288 , 288 , 312 , 0 , 0 ) ;

bar3d ( 288 , 288 , 320 , 312 , 0 , 0 ) ;

setfillstyle ( SOLID_FILL,BLUE ) ;

bar3d ( 155 , 230 , 580 , 278 , 0 , 0 ) ;

bar3d ( 155 , 86 , 544 , 110 , 0 , 0 ) ;

outtextxy ( 160 , 96 , "The NEXT box will display the next block to fall" ) ;

outtextxy ( 160 , 240 , "The ROWS will display how many rows you have cleared." ) ;

outtextxy ( 160 , 255 , "To clear a row, you must fill all blocks on a" ) ;

outtextxy ( 160 , 270 , "horizontal row." ) ;

outtextxy ( 200 , 180 , "Press any key to continue." ) ;

getch ( ) ;

background ( ) ;

moveto ( 10 , 5 ) ;

setfillstyle ( SOLID_FILL,BLUE ) ;

bar3d ( 1 , 1 , 639 , 46 , 0 , 0 ) ;

outtext ( "The object of the game is to complete as many horizontal lines as "

"possible." ) ;

outtextxy ( 10 , 20 , "The more lines you clear, the faster the blocks will fall. "

"Once the blocks" ) ;

outtextxy ( 10 , 35 , "reach the top of the screen, you lose. Good luck and enjoy!" ) ;

getch ( ) ;

}

//------------------------------------------------initialize high score function

void initializehs ( )

{

fstream infile ;

int lcv ;

infile. open ( "highscores.txt" ,ios :: in ) ;

if ( ! infile ) //if file doesn't exist

return ;

else //if file does exist

for ( lcv = 0 ; lcv < 10 ; lcv ++ )

infile >> hs [ lcv ] . name >> hs [ lcv ] . score ; //cin high scores and names from file

}

//-------------------------------------------------------view highscore function

void viewhs ( )

{

int lcv ;

setcolor ( WHITE ) ;

outtextxy ( 45 , 66 , "1." ) ;

outtextxy ( 45 , 82 , "2." ) ;

outtextxy ( 45 , 98 , "3." ) ;

outtextxy ( 45 , 114 , "4." ) ;

outtextxy ( 45 , 130 , "5." ) ;

outtextxy ( 45 , 146 , "6." ) ;

outtextxy ( 45 , 163 , "7." ) ;

outtextxy ( 45 , 178 , "8." ) ;

outtextxy ( 45 , 194 , "9." ) ;

outtextxy ( 45 , 210 , "10." ) ;

for ( lcv = 0 ; lcv < 10 ; lcv ++ )

{ //display high scores and names

outtextxy ( 65 , 66 + 16 * lcv,hs [ lcv ] . name ) ;

window ( 18 , 5 + lcv, 21 , 8 + lcv ) ;

cout << hs [ lcv ] . score ;

}

getch ( ) ;

}

//==============================================================================

//-----------------------------------------------------------------game function

//==============================================================================

int game ( )

{

void drawblock ( ) ; //draws the block

void eraseblock ( ) ; //erases the block

void rotateblock ( ) ; //rotates block 90 degrees clockwise

void blockleft ( ) ; //moves block 32 pixels left

void blockright ( ) ; //moves block 32 pixels right

void shiftdown ( ) ; //if clearline=true, moves all blocks down one row

void clearline ( ) ; //if line is full, erase it

bool stopblock ( ) ; //stops the block when it hits another block or barrier

void nextblock ( ) ; //picks and displays the next block in the "next" box

void nextnow ( ) ; //changes next block to the falling block

void highscore ( ) ; //displays high scores on ingame screen

void newhs ( ) ; //if top 10 score, cin then sort and display

score = 0 ; //initializes # of rows cleared to zero. also the score.

int speed = 400 , x = 0 ;

char button = 0 ;

bool stop = false ;

background ( ) ; //draws background

nextblock ( ) ;

highscore ( ) ;

nextnow ( ) ;

while ( button ! = 'q' ) //hit 'q' to quit game

{

nextnow ( ) ;

nextblock ( ) ;

nextblock ( ) ;

blocky = - 72 ; //re-initiates blocky and blockx to starting positions

blockx = 0 ; //" "

speed = 400 - ( 8 * score ) ; //speeds up as more rows are cleared

do { //open stop do while

stop = false ;

while ( ! kbhit ( ) && ! stop ) //loops block falling while the user

{ //open while does not press a key

delay ( 1 ) ;

x ++ ;

if ( x == speed || speed == 0 )

{ //open if

eraseblock ( ) ;

blocky = blocky + 24 ;

drawblock ( ) ;

stop = stopblock ( ) ;

x = 0 ;

} //close if

} //end while

clearline ( ) ;

if ( ! stop )

button = getch ( ) ;

if ( stop )

button = NULL ;

if ( button == KEY_LEFT )

{

eraseblock ( ) ;

blockleft ( ) ;

drawblock ( ) ;

}

if ( button == KEY_RIGHT )

{

eraseblock ( ) ;

blockright ( ) ;

drawblock ( ) ;

}

if ( button == KEY_UP )

{

eraseblock ( ) ;

if ( blocky >= - 24 )

rotateblock ( ) ;

drawblock ( ) ;

}

if ( button == KEY_DOWN )

speed = 0 ;

if ( button == 'q' || button == 'Q' )

return ( score ) ;

if ( button == 'r' || button == 'R' )

getch ( ) ;

if ( blocky == - 48 && stop )

{

setcolor ( WHITE ) ;

setfillstyle ( SOLID_FILL,BLUE ) ;

bar3d ( 160 , 168 , 480 , 216 , 0 , 0 ) ;

outtextxy ( 161 , 181 , "Game Over, Your score was:" ) ;

window ( 52 , 12 , 55 , 15 ) ;

cout << score ;

outtextxy ( 230 , 200 , "Press any key to continue" ) ;

getch ( ) ;

if ( score > hs [ 9 ] . score )

newhs ( ) ;

return ( score ) ;

}

stop = stopblock ( ) ;

} while ( ! stop ) ; //stop do while

speed = 400 ;

} //quit while

getch ( ) ;

return ( score ) ;

} //close game

//==============================================================================

//==============================================================================

//-----------------------------------------------------------background function

void background ( )

{

setcolor ( WHITE ) ;

setfillstyle ( CLOSE_DOT_FILL,BLUE ) ;

//draw border

bar ( 0 , 0 , 159 , 479 ) ;

bar ( 159 , 457 , 481 , 479 ) ;

bar ( 481 , 0 , 639 , 479 ) ;

moveto ( 159 , 0 ) ;

lineto ( 159 , 457 ) ;

lineto ( 481 , 457 ) ;

lineto ( 481 , 0 ) ;

//next box

setcolor ( WHITE ) ;

setfillstyle ( SOLID_FILL,LIGHTGRAY ) ;

bar3d ( 32 , 72 , 128 , 144 , 0 , 0 ) ;

setcolor ( WHITE ) ;

outtextxy ( 60 , 60 , "Next:" ) ;

//rows and time box

setcolor ( WHITE ) ;

bar3d ( 32 , 192 , 128 , 288 , 0 , 0 ) ;

setcolor ( WHITE ) ;

outtextxy ( 60 , 200 , "Rows:" ) ;

//high score - may take out if i cant figure it out :p

outtextxy ( 514 , 60 , "High Scores" ) ;

setcolor ( WHITE ) ;

bar3d ( 512 , 72 , 608 , 384 , 0 , 0 ) ;

//block area

setfillstyle ( SOLID_FILL,DARKGRAY ) ;

bar ( 160 , 0 , 480 , 456 ) ;

} //close background function

//------------------------------------------------------------drawblock function

void drawblock ( )

{

//using a 3x3 array and if the value is 1, draw a block in that square

int x, y ;

setcolor ( WHITE ) ;

setfillstyle ( SOLID_FILL,blockcolor ) ;

for ( x = 0 ; x < 3 ; x ++ )

{

for ( y = 0 ; y < 3 ; y ++ )

{ //open for

if ( block [ x ] [ y ] == 1 )

{

bar3d ( ( 256 + ( 32 * y ) + blockx ) , ( 0 + ( 24 * x ) + blocky ) ,

( 288 + ( 32 * y ) + blockx ) , ( 24 + ( 24 * x ) + blocky ) , 0 , 0 ) ;

}

} //close for

} //close main for

} //close drawblock

//----------------------------------------------------------erase block function

void eraseblock ( )

{

int x, y ;

setcolor ( 8 ) ; //8=darkgray

setfillstyle ( SOLID_FILL, 8 ) ;

for ( x = 0 ; x < 3 ; x ++ )

{

for ( y = 0 ; y < 3 ; y ++ )

{ //open for

if ( block [ x ] [ y ] == 1 )

{

bar3d ( ( 256 + ( 32 * y ) + blockx ) , ( 0 + ( 24 * x ) + blocky ) ,

( 288 + ( 32 * y ) + blockx ) , ( 24 + ( 24 * x ) + blocky ) , 0 , 0 ) ;

}

} //close for

} //close main for

} //close drawblock

//---------------------------------------------------------rotate block function

void rotateblock ( )

{

if ( blockx < ( - 96 ) || blockx > 136 )

return ; //if block is on far left or right, don't rotate

int fblock [ 3 ] [ 3 ] = { { 0 , 0 , 0 } ,

{ 0 , 0 , 0 } ,

{ 0 , 0 , 0 } } ;

//rotates block clockwise 90 degrees

fblock [ 0 ] [ 0 ] = block [ 2 ] [ 0 ] ;

fblock [ 0 ] [ 1 ] = block [ 1 ] [ 0 ] ;

fblock [ 0 ] [ 2 ] = block [ 0 ] [ 0 ] ;

fblock [ 1 ] [ 0 ] = block [ 2 ] [ 1 ] ;

fblock [ 1 ] [ 1 ] = block [ 1 ] [ 1 ] ;

fblock [ 1 ] [ 2 ] = block [ 0 ] [ 1 ] ;

fblock [ 2 ] [ 0 ] = block [ 2 ] [ 2 ] ;

fblock [ 2 ] [ 1 ] = block [ 1 ] [ 2 ] ;

fblock [ 2 ] [ 2 ] = block [ 0 ] [ 2 ] ;

int lcv, lcv2 ;

for ( lcv = 0 ; lcv < 3 ; lcv ++ )

{

for ( lcv2 = 0 ; lcv2 < 3 ; lcv2 ++ )

block [ lcv ] [ lcv2 ] = fblock [ lcv ] [ lcv2 ] ;

}

} //close rotate function

//-----------------------------------------------------------block left function

void blockleft ( )

{

if ( blocky <= - 24 )

{

if ( blockx > - 96 )

blockx = blockx - 32 ;

else if ( blockx == - 96 && block [ 0 ] [ 0 ] == 0 && block [ 1 ] [ 0 ] == 0 &&

block [ 2 ] [ 0 ] == 0 )

blockx = blockx - 32 ;

return ;

}

int x [ 3 ] = { 0 , 0 , 0 } , y [ 3 ] = { 0 , 0 , 0 } , lcv = 0 ;

bool fstop = false ;

//checks for blocks in each row and assigns values appropriately

//top row

if ( block [ 0 ] [ 2 ] == 1 )

{ x [ 0 ] = 319 + blockx ;

y [ 0 ] = 12 + blocky ; }

if ( block [ 0 ] [ 1 ] == 1 )

{ x [ 0 ] = 287 + blockx ;

y [ 0 ] = 12 + blocky ; }

if ( block [ 0 ] [ 0 ] == 1 )

{ x [ 0 ] = 255 + blockx ;

y [ 0 ] = 12 + blocky ; }

//middle row

if ( block [ 1 ] [ 2 ] == 1 )

{ x [ 1 ] = 319 + blockx ;

y [ 1 ] = 36 + blocky ; }

if ( block [ 1 ] [ 1 ] == 1 )

{ x [ 1 ] = 287 + blockx ;

y [ 1 ] = 36 + blocky ; }

if ( block [ 1 ] [ 0 ] == 1 )

{ x [ 1 ] = 255 + blockx ;

y [ 1 ] = 36 + blocky ; }

//bottom row

if ( block [ 2 ] [ 2 ] == 1 )

{ x [ 2 ] = 319 + blockx ;

y [ 2 ] = 60 + blocky ; }

if ( block [ 2 ] [ 1 ] == 1 )

{ x [ 2 ] = 287 + blockx ;

y [ 2 ] = 60 + blocky ; }

if ( block [ 2 ] [ 0 ] == 1 )

{ x [ 2 ] = 255 + blockx ;

y [ 2 ] = 60 + blocky ; }

for ( lcv = 0 ; lcv < 3 ; lcv ++ )

{

if ( block [ lcv ] [ 0 ] == 0 && block [ lcv ] [ 1 ] == 0 && block [ lcv ] [ 2 ] == 0 )

{

x [ lcv ] = 0 ;

y [ lcv ] = 0 ;

}

}

lcv = 0 ;

do

{

if ( x [ lcv ] == 0 && y [ lcv ] == 0 )

fstop = false ;

else if ( getpixel ( x [ lcv ] - 2 ,y [ lcv ] ) ! = 8 )

fstop = true ;

//cout<<x[lcv]<<" "<<y[lcv]<<endl; getch();

lcv ++ ;

} while ( lcv < 3 && ! fstop ) ; //close stopblock

if ( ! fstop )

blockx = blockx - 32 ;

}

//----------------------------------------------------------block right function

void blockright ( )

{

if ( blocky <= - 24 )

{

if ( blockx <= 96 )

blockx = blockx + 32 ;

else if ( blockx == 128 && block [ 0 ] [ 2 ] == 0 && block [ 1 ] [ 2 ] == 0 &&

block [ 2 ] [ 2 ] == 0 )

blockx = blockx + 32 ;

return ;

}

int x [ 3 ] = { 0 , 0 , 0 } , y [ 3 ] = { 0 , 0 , 0 } , lcv = 0 ;

bool fstop = false ;

//checks for blocks in each row and assigns values appropriately

//top row

if ( block [ 0 ] [ 0 ] == 1 )

{ x [ 0 ] = 289 + blockx ;

y [ 0 ] = 12 + blocky ; }

if ( block [ 0 ] [ 1 ] == 1 )

{ x [ 0 ] = 321 + blockx ;

y [ 0 ] = 12 + blocky ; }

if ( block [ 0 ] [ 2 ] == 1 )

{ x [ 0 ] = 353 + blockx ;

y [ 0 ] = 12 + blocky ; }

//middle row

if ( block [ 1 ] [ 0 ] == 1 )

{ x [ 1 ] = 289 + blockx ;

y [ 1 ] = 36 + blocky ; }

if ( block [ 1 ] [ 1 ] == 1 )

{ x [ 1 ] = 321 + blockx ;

y [ 1 ] = 36 + blocky ; }

if ( block [ 1 ] [ 2 ] == 1 )

{ x [ 1 ] = 353 + blockx ;

y [ 1 ] = 36 + blocky ; }

//bottom row

if ( block [ 2 ] [ 0 ] == 1 )

{ x [ 2 ] = 289 + blockx ;

y [ 2 ] = 60 + blocky ; }

if ( block [ 2 ] [ 1 ] == 1 )

{ x [ 2 ] = 321 + blockx ;

y [ 2 ] = 60 + blocky ; }

if ( block [ 2 ] [ 2 ] == 1 )

{ x [ 2 ] = 353 + blockx ;

y [ 2 ] = 60 + blocky ; }

for ( lcv = 0 ; lcv < 3 ; lcv ++ )

{

if ( block [ lcv ] [ 0 ] == 0 && block [ lcv ] [ 1 ] == 0 && block [ lcv ] [ 2 ] == 0 )

{

x [ lcv ] = 0 ;

y [ lcv ] = 0 ;

}

}

lcv = 0 ;

do

{

if ( x [ lcv ] == 0 && y [ lcv ] == 0 )

fstop = false ;

else if ( getpixel ( x [ lcv ] + 2 ,y [ lcv ] ) ! = 8 )

fstop = true ;

//cout<<x[lcv]<<" "<<y[lcv]<<endl; getch();

lcv ++ ;

} while ( lcv < 3 && ! fstop ) ; //close stopblock

if ( ! fstop )

blockx = blockx + 32 ;

}

//-----------------------------------------------------------stop block function

bool stopblock ( )

{

int x [ 3 ] = { 0 , 0 , 0 } , y [ 3 ] = { 0 , 0 , 0 } , lcv = 0 ;

bool fstop = false ;

//left column

if ( block [ 0 ] [ 0 ] == 1 )

{ x [ 0 ] = 272 + blockx ;

y [ 0 ] = 25 + blocky ; }

if ( block [ 1 ] [ 0 ] == 1 )

{ x [ 0 ] = 272 + blockx ;

y [ 0 ] = 49 + blocky ; }

if ( block [ 2 ] [ 0 ] == 1 )

{ x [ 0 ] = 272 + blockx ;

y [ 0 ] = 73 + blocky ; }

//middle column

if ( block [ 0 ] [ 1 ] == 1 )

{ x [ 1 ] = 304 + blockx ;

y [ 1 ] = 25 + blocky ; }

if ( block [ 1 ] [ 1 ] == 1 )

{ x [ 1 ] = 304 + blockx ;

y [ 1 ] = 49 + blocky ; }

if ( block [ 2 ] [ 1 ] == 1 )

{ x [ 1 ] = 304 + blockx ;

y [ 1 ] = 73 + blocky ; }

//right column

if ( block [ 0 ] [ 2 ] == 1 )

{ x [ 2 ] = 336 + blockx ;

y [ 2 ] = 25 + blocky ; }

if ( block [ 1 ] [ 2 ] == 1 )

{ x [ 2 ] = 336 + blockx ;

y [ 2 ] = 49 + blocky ; }

if ( block [ 2 ] [ 2 ] == 1 )

{ x [ 2 ] = 336 + blockx ;

y [ 2 ] = 73 + blocky ; }

for ( lcv = 0 ; lcv < 3 ; lcv ++ )

{

if ( block [ 0 ] [ lcv ] == 0 && block [ 1 ] [ lcv ] == 0 && block [ 2 ] [ lcv ] == 0 )

{

x [ lcv ] = 0 ;

y [ lcv ] = 0 ;

}

}

lcv = 0 ;

do

{

if ( x [ lcv ] == 0 && y [ lcv ] == 0 )

fstop = false ;

else if ( getpixel ( x [ lcv ] ,y [ lcv ] + 2 ) ! = 8 )

fstop = true ;

else fstop = false ;

lcv ++ ;

} while ( lcv < 3 && ! fstop ) ; //close stopblock

return ( fstop ) ;

}

//-----------------------------------------------------------clear line function

void clearline ( )

{

int x, y, lcv, lcv2, store [ 10 ] ;

void eraseline ( int ) ;

bool erase = true ;

y = 444 ;

while ( y >= 12 )

{

erase = true ;

for ( lcv = 0 ; lcv < 10 ; lcv ++ )

{

x = 176 + ( 32 * lcv ) ;

store [ lcv ] = getpixel ( x,y ) ;

}

lcv2 = 0 ;

while ( erase && lcv2 < 10 )

{

if ( store [ lcv2 ] == 8 )

erase = false ;

else

erase = true ;

lcv2 ++ ;

} //close erase while

if ( erase )

{

eraseline ( y ) ; //if line full, erase it, drop blocks and recheck same line

setcolor ( DARKGRAY ) ;

setfillstyle ( SOLID_FILL,LIGHTGRAY ) ;

bar ( 33 , 212 , 127 , 236 ) ;

//cout score

window ( 10 , 14 , 45 , 16 ) ;

score + = 1 ;

cout << score ;

}

else

y = y - 24 ; //if line not full, check next line

}

}

//-----------------------------------------------------------erase line function

void eraseline ( int y )

{

int x, y2, color ;

//erases the bar that is full

setcolor ( 8 ) ; //DARKGRAY

setfillstyle ( SOLID_FILL, 8 ) ;

bar ( 160 ,y - 12 , 480 ,y + 12 ) ;

for ( y2 = y - 12 ; y2 >= 24 ; y2 = y2 - 24 )

{

for ( x = 160 ; x < 480 ; x = x + 32 )

{

color = getpixel ( x + 16 ,y2 - 12 ) ; //gets color of block above (x,y2)

if ( color == 8 )

{

setcolor ( color ) ;

setfillstyle ( SOLID_FILL,color ) ;

bar ( x,y2,x + 32 ,y2 + 23 ) ;

}

else

{

setcolor ( WHITE ) ;

setfillstyle ( SOLID_FILL,color ) ;

bar3d ( x,y2,x + 32 ,y2 + 24 , 0 , 0 ) ;

}

} //x for

} //y for

}

//-----------------------------------------------------------next block function

void nextblock ( )

{

setcolor ( WHITE ) ;

setfillstyle ( SOLID_FILL,LIGHTGRAY ) ;

bar3d ( 32 , 72 , 128 , 144 , 0 , 0 ) ; //redraws over old block

void pickblock ( ) ; //picks which block to use and assigns values to block[3][3]

int x, y ;

pickblock ( ) ;

setfillstyle ( SOLID_FILL,nextblockcolor ) ;

for ( x = 0 ; x < 3 ; x ++ ) //draws block inside of box

{

for ( y = 0 ; y < 3 ; y ++ )

{ //open for

if ( nextblockvar [ x ] [ y ] == 1 )

bar3d ( ( 32 + ( 32 * y ) ) , ( 48 + ( 24 * x ) ) , ( 64 + ( 32 * y ) ) , ( 72 + ( 24 * x ) )

, 0 , 0 ) ;

} //close for

} //close main for

} //close function

//------------------------------------------------------------pickblock function

void pickblock ( )

{

int pick ;

pick = 1 + rand ( ) % 7 ; //picks random number between 1-7 to pick block

//assigns values of the 3v3 block array to create each of the 7 blocks

switch ( pick )

{

case 1 :

nextblockvar [ 0 ] [ 0 ] = 0 ; nextblockvar [ 0 ] [ 1 ] = 0 ; nextblockvar [ 0 ] [ 2 ] = 0 ;

nextblockvar [ 1 ] [ 0 ] = 1 ; nextblockvar [ 1 ] [ 1 ] = 1 ; nextblockvar [ 1 ] [ 2 ] = 1 ;

nextblockvar [ 2 ] [ 0 ] = 0 ; nextblockvar [ 2 ] [ 1 ] = 0 ; nextblockvar [ 2 ] [ 2 ] = 0 ;

nextblockcolor = 5 ;

break ;

case 2 :

nextblockvar [ 0 ] [ 0 ] = 0 ; nextblockvar [ 0 ] [ 1 ] = 0 ; nextblockvar [ 0 ] [ 2 ] = 0 ;

nextblockvar [ 1 ] [ 0 ] = 0 ; nextblockvar [ 1 ] [ 1 ] = 1 ; nextblockvar [ 1 ] [ 2 ] = 1 ;

nextblockvar [ 2 ] [ 0 ] = 1 ; nextblockvar [ 2 ] [ 1 ] = 1 ; nextblockvar [ 2 ] [ 2 ] = 0 ;

nextblockcolor = 14 ;

break ;

case 3 :

nextblockvar [ 0 ] [ 0 ] = 0 ; nextblockvar [ 0 ] [ 1 ] = 0 ; nextblockvar [ 0 ] [ 2 ] = 0 ;

nextblockvar [ 1 ] [ 0 ] = 0 ; nextblockvar [ 1 ] [ 1 ] = 1 ; nextblockvar [ 1 ] [ 2 ] = 0 ;

nextblockvar [ 2 ] [ 0 ] = 1 ; nextblockvar [ 2 ] [ 1 ] = 1 ; nextblockvar [ 2 ] [ 2 ] = 1 ;

nextblockcolor = 2 ;

break ;

case 4 :

nextblockvar [ 0 ] [ 0 ] = 0 ; nextblockvar [ 0 ] [ 1 ] = 0 ; nextblockvar [ 0 ] [ 2 ] = 0 ;

nextblockvar [ 1 ] [ 0 ] = 0 ; nextblockvar [ 1 ] [ 1 ] = 1 ; nextblockvar [ 1 ] [ 2 ] = 1 ;

nextblockvar [ 2 ] [ 0 ] = 0 ; nextblockvar [ 2 ] [ 1 ] = 1 ; nextblockvar [ 2 ] [ 2 ] = 1 ;

nextblockcolor = 1 ;

break ;

case 5 :

nextblockvar [ 0 ] [ 0 ] = 0 ; nextblockvar [ 0 ] [ 1 ] = 0 ; nextblockvar [ 0 ] [ 2 ] = 0 ;

nextblockvar [ 1 ] [ 0 ] = 0 ; nextblockvar [ 1 ] [ 1 ] = 0 ; nextblockvar [ 1 ] [ 2 ] = 1 ;

nextblockvar [ 2 ] [ 0 ] = 1 ; nextblockvar [ 2 ] [ 1 ] = 1 ; nextblockvar [ 2 ] [ 2 ] = 1 ;

nextblockcolor = 4 ;

break ;

case 6 :

nextblockvar [ 0 ] [ 0 ] = 0 ; nextblockvar [ 0 ] [ 1 ] = 0 ; nextblockvar [ 0 ] [ 2 ] = 0 ;

nextblockvar [ 1 ] [ 0 ] = 1 ; nextblockvar [ 1 ] [ 1 ] = 0 ; nextblockvar [ 1 ] [ 2 ] = 0 ;

nextblockvar [ 2 ] [ 0 ] = 1 ; nextblockvar [ 2 ] [ 1 ] = 1 ; nextblockvar [ 2 ] [ 2 ] = 1 ;

nextblockcolor = 3 ;

break ;

case 7 :

nextblockvar [ 0 ] [ 0 ] = 0 ; nextblockvar [ 0 ] [ 1 ] = 0 ; nextblockvar [ 0 ] [ 2 ] = 0 ;

nextblockvar [ 1 ] [ 0 ] = 1 ; nextblockvar [ 1 ] [ 1 ] = 1 ; nextblockvar [ 1 ] [ 2 ] = 0 ;

nextblockvar [ 2 ] [ 0 ] = 0 ; nextblockvar [ 2 ] [ 1 ] = 1 ; nextblockvar [ 2 ] [ 2 ] = 1 ;

nextblockcolor = 11 ;

break ;

} //close pick switch

} //close pickblock

//-------------------------------------------------------------next now function

void nextnow ( )

{

int lcv, lcv2 ;

for ( lcv = 0 ; lcv < 3 ; lcv ++ )

{

for ( lcv2 = 0 ; lcv2 < 3 ; lcv2 ++ )

block [ lcv ] [ lcv2 ] = nextblockvar [ lcv ] [ lcv2 ] ;

}

blockcolor = nextblockcolor ;

} //end function

//==============================================================================

//------------------------------------------------------------highscore function

void highscore ( )

{

fstream in_file ; // ("highscores.txt");

in_file. open ( "highscore.txt" ,ios :: in ) ;

if ( ! in_file )

{

cout << "File could not be opened.

" ;

return ;

}

setcolor ( BLUE ) ;

outtextxy ( 512 , 96 , "1." ) ;

outtextxy ( 512 , 120 , "2." ) ;

outtextxy ( 512 , 144 , "3." ) ;

outtextxy ( 512 , 168 , "4." ) ;

outtextxy ( 512 , 192 , "5." ) ;

outtextxy ( 512 , 216 , "2." ) ;

outtextxy ( 512 , 240 , "7." ) ;

outtextxy ( 512 , 264 , "8." ) ;

outtextxy ( 512 , 288 , "9." ) ;

outtextxy ( 512 , 312 , "10." ) ;

int lcv ;

for ( lcv = 0 ; lcv < 10 ; lcv ++ )

{

outtextxy ( 522 , 96 + ( 24 * lcv ) ,hs [ lcv ] . name ) ;

window ( 75 , 7 + lcv, 78 , 9 + lcv ) ;

cout << hs [ lcv ] . score ;

}

in_file. close ( ) ;

}

//--------------------------------------------------------new highscore function

//I hate files :p (This never worked)

void newhs ( )

{

cleardevice ( ) ;

setcolor ( WHITE ) ;

moveto ( 10 , 50 ) ;

outtext ( "NEW HIGH SCORE! Enter in your name and leave out spaces." ) ;

cin >> hs [ 10 ] . name ;

hs [ 10 ] . score = score ;

int x, x2, max = 0 , count = 0 ;

for ( x = 0 ; x < 11 ; x ++ ) //sorts top 10(11 including new score)

{

max = 0 ;

for ( x2 = 0 ; x2 < 11 ; x2 ++ )

{

if ( hs [ x2 ] . score > max )

{

max = hs [ x2 ] . score ;

count = x2 ;

}

}

hs [ x ] . sort = max ;

hs [ count ] . score = 0 ;

hs [ x ] . namesort = hs [ count ] . name ;

}

//re-enter sorted name and score in order to original variables

for ( x = 0 ; x < 11 ; x ++ )

{

hs [ x ] . score = hs [ x ] . sort ;

hs [ x ] . name = hs [ x ] . namesort ;

;

}