From ccea65d2257e088ebde91ca2301817ea5d501f3f Mon Sep 17 00:00:00 2001 From: Carlos Israel Orta Orta Date: Fri, 26 Apr 2024 00:22:05 -0600 Subject: [PATCH] Remove print dependencies and leave only base functions and phantom piece --- a.out | Bin 22616 -> 0 bytes tetris.c | 121 +++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 78 insertions(+), 43 deletions(-) delete mode 100755 a.out diff --git a/a.out b/a.out deleted file mode 100755 index 7d993a56023658de53072c4f130834ecb4d27b9b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 22616 zcmeHPeRNaDl^@AGFa(eZkT|4`m4>9asfEp_V?vPu!K3BF7@LxIQ$YS#jlO|NE+{9^V>ZF`%e|J8f zCB-J}_MHB)M|13V=l&zw-Q_0Le!Z~Nl&A!t*U1SGGyrAMbHct6^?NYB@$}75-j36mW zbx_*&TG;b{sjp7zl~xHmrB!yGQS^|2nWCz`tSYznme@i8+ag{swTnW6Vru>7MrQ703Br`ViJ3S*6&NM6 zdJ^{XU0-X9fUV9=F;M0Ix@t>cp0LHWP zsR?k~1o#Ihz?V*d@16j^4)`o~9XqnsjvQlZ{;@q_U#m9|2{Wg2S8KrU42506u+zyx zVNb{%WNks8KfH(S+2swpo0-?|+2ixGJ?<8-E699)Uzp28!XefcfdD`Va*ItK*Iq_B z7)C6+rQuc=^g}_H-y=*nF%$Sr`Kan3oYmPmnS!gDdju3`;d1HYawSc&o!i^X}VRaTxF|Cq43%^Q%pn4WQJqxeP!e?aRJF@VZS@>O9xFriOWD6cBc%a~c zf(Hs7D0txiuLu5Ux#1l>I$W&Bwezd6tvHYfn==D?^r_;rywjPb&j8Nc{3Bc~Wp?C< zZ>F-;PzLGdGsM%@DmB3Q?-Ng3wp4=i-yxp1SgGTjKSn%ltx`ug|2XlqrAl>k{>#ME zRw~uW`G<(7EmW$F^IssIwoa+LIR81~X)Bnjc-q-iz8`0glPke;2i%m95*?y=nM)GIJ*zS|G z@zht+H9Y$yNstSto+c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i05D1C)~1DWjl)$>csd?7JI z2}+vZ{wJiq^1s?=tAU$K#pxBYq z)p#CP?*&!w|D^b*r1aD{UIa$|Q~K(CdV{M-dTf6yxG{ZY=WP^Z>#OJcq}1qGcopzyMrNXRa&B{}@80BP3wO>6~9H|w8C8|KM%qpoOv|q-b3O5BarB6R| z$xb9oNm2TlJXKN2(Xq?3wIctZ5|l-XRrF?L)Yy>7@vxms-nE7kwpIxW);LFyEN@3% zi1MPNYlTFyG=2kEdM;9aSL-)@K9T1lu6X=ooUqos!o242Wg<_6E52VIg|Em{$Ya_6 E07}C?Z2$lO diff --git a/tetris.c b/tetris.c index c970e13..37dd887 100644 --- a/tetris.c +++ b/tetris.c @@ -3,6 +3,7 @@ #include #include #include +#include #define ROWS 20 // you can change height and width of table with ROWS and COLS #define COLS 10 @@ -10,9 +11,12 @@ #define FALSE 0 #define MAXSHAPES 7 #define MAXNEXTPIECES 6 +#define MAXWIDTH 4 char table[ROWS][COLS] = {0}; char *Table[ROWS] = {0}; +char Buffer[ROWS][COLS] = {0}; +char NextPzBuffer[MAXNEXTPIECES][MAXWIDTH*MAXWIDTH] = {0}; int score = 0; char GameOn = TRUE; suseconds_t timer = 400000; // decrease this to make it faster @@ -20,11 +24,11 @@ int decrease = 1000; int newLines = 0; typedef struct { - char array[4][4]; + char array[MAXWIDTH][MAXWIDTH]; char wasHold; int width, row, col; } Shape; -Shape current, pieceHold, *hold = NULL; +Shape current, pieceHold; Shape nextPiece[MAXNEXTPIECES]; int iNextPiece = 0, jNextPiece = MAXNEXTPIECES - 1; @@ -41,29 +45,49 @@ Shape ShapesArray[MAXSHAPES]= { // you can add any shape like it's done above. Don't be naughty. }; -char CopyMemShape(Shape *dst, Shape *org){ - int i; +void increaseNewLines(int lines){ + newLines += lines; +} +int getNewLines(){ + return newLines; +} +char getGameOn(){ + return GameOn; +} +int getScore(){ + return score; +} +char** getBuffer(){ + return (char**)Buffer; +} +char** getNextPzBuffer(){ + return (char**)NextPzBuffer; +} + +char **getHoldPiece(){ + return (char**)pieceHold.array; +} + +char CopyMem(char *dst, char *org, unsigned int size){ + int i, j; long long *orgL = (long long*)org, *dstL = (long long*)dst; - if (dst == NULL || org == NULL) + if (dst == NULL) return 0; - for (i = 0; i < sizeof(Shape) / sizeof(long long); i++) - dstL[i] = orgL[i]; + for (i = 0; i < size / sizeof(long long); i++) + dstL[i] = org != NULL ? orgL[i] : 0; i = i * sizeof(long long); - for (i = i; i < sizeof(Shape); i++) - dst[i] = org[i]; + for (i = i; i < size; i++) + dst[i] = org != NULL ? org[i] : 0; return 1; } +char CopyMemShape(Shape *dst, Shape *org){ + return CopyMem((char*)dst, (char*)org, sizeof(Shape)); +} + Shape CopyShape(Shape shape){ Shape new_shape = {0}; - int i; - char *org = (char*)&shape, *dst = (char*)&new_shape; - long long *orgL = (long long*)org, *dstL = (long long*)dst; - for (i = 0; i < sizeof(Shape) / sizeof(long long); i++) - dstL[i] = orgL[i]; - i = i * sizeof(long long); - for (i = i; i < sizeof(Shape); i++) - dst[i] = org[i]; + CopyMem((char*)&new_shape, (char*)&shape, sizeof(Shape)); return new_shape; } @@ -140,16 +164,10 @@ void RemoveFullRowsAndUpdateScore(){ count++; int l, k; char *cleanRow = Table[i]; - long long *erase = (long long*)cleanRow; for (k = i; k >= 1; k--) Table[k] = Table[k-1]; Table[0] = cleanRow; - for (k = 0; k < COLS/sizeof(long long); k++) - erase[k] = 0; - k = k * sizeof(long long); - for (k = k; k < COLS; k++) - cleanRow[k] = 0; - timer-=decrease--; + CopyMem(cleanRow, NULL, sizeof(char)*COLS); } } score += 100*count; @@ -174,28 +192,42 @@ void AddPendingLines(){ } } -void PrintTable(){ - char Buffer[ROWS][COLS] = {0}; +void FillOutput(){ + Shape temp = CopyShape(current); + while (CheckPosition(temp)) + temp.row++; + temp.row--; int i, j; - for(i = 0; i < current.width ;i++){ - for(j = 0; j < current.width ; j++){ - if(current.array[i][j]) + CopyMem((char*)Buffer, NULL, sizeof(char)*ROWS*COLS); + for(i = 0; i < ROWS ;i++){ + if (i < MAXNEXTPIECES){ + char *oOutput = (char*)nextPiece[(iNextPiece + i) % MAXNEXTPIECES].array; + CopyMem((char*)NextPzBuffer[i],oOutput,sizeof(char)*MAXWIDTH*MAXWIDTH); + } + for(j = 0; j < COLS ; j++){ + if((i < MAXWIDTH && j < MAXWIDTH) && current.row >=0 && current.array[i][j]) Buffer[current.row+i][current.col+j] = current.array[i][j]; + if((i < MAXWIDTH && j < MAXWIDTH) && temp.row >=0 && temp.row - 1 > current.row && temp.array[i][j]) + Buffer[temp.row+i][temp.col+j] = tolower(temp.array[i][j]); + Buffer[i][j] = (Table[i][j] + Buffer[i][j]) ? (Buffer[i][j]?Buffer[i][j]:Table[i][j]): '.'; } } +} + +void PrintTable(){ + int i, j; clear(); for(i=0; i ", nextPiece[j].array[1][1]); - j = (j + 1) % MAXNEXTPIECES == 0 ? 0 : j + 1; + printw("%c -> ", NextPzBuffer[i][1*MAXWIDTH+1]); } - printw(" HOLD: %c \n", hold == NULL ? ' ' : hold->array[1][1]); + printw(" HOLD: %c \n", pieceHold.wasHold == 0 ? ' ' : pieceHold.array[1][1]); for(i = 0; i < ROWS ;i++){ for(j = 0; j < COLS ; j++){ - printw("%c ", (Table[i][j] + Buffer[i][j])? (Buffer[i][j]?Buffer[i][j]:Table[i][j]): '.'); + printw("%c ", Buffer[i][j]); } printw("\n"); } @@ -243,18 +275,18 @@ void ManipulateCurrent(int action){ case 'r': if (current.wasHold) break; - if (hold == NULL){ - hold = &pieceHold; - CopyMemShape(hold,¤t); + if (pieceHold.wasHold == 0){ + CopyMemShape(&pieceHold,¤t); SetNewRandomShape(); current.wasHold = 1; + pieceHold.wasHold = 1; } else{ + current.wasHold = 1; CopyMemShape(&temp,¤t); - CopyMemShape(¤t,hold); - CopyMemShape(hold,&temp); + CopyMemShape(¤t,&pieceHold); + CopyMemShape(&pieceHold,&temp); current.col = rand()%(COLS-current.width+1); current.row = 0; - current.wasHold = 1; // DeleteShape(current); if(!CheckPosition(current)){ GameOn = FALSE; @@ -273,7 +305,7 @@ void ManipulateCurrent(int action){ break; } // DeleteShape(temp); - PrintTable(); + FillOutput(); } struct timeval before_now, now; @@ -282,7 +314,7 @@ int hasToUpdate(){ } int main() { - srand(time(0)); + srand(time(0)); for (int i = 0; i < ROWS; i++) Table[i] = table[i]; for (int i = 0; i < MAXNEXTPIECES; i++){ @@ -295,18 +327,21 @@ int main() { gettimeofday(&before_now, NULL); timeout(1); SetNewRandomShape(); + FillOutput(); PrintTable(); - int wait = 5; + int wait = 10; while(GameOn){ if ((c = getch()) != ERR) { ManipulateCurrent(c); + PrintTable(); } gettimeofday(&now, NULL); if (hasToUpdate()) { //time difference in microsec accuracy ManipulateCurrent('s'); + PrintTable(); gettimeofday(&before_now, NULL); if (wait == 0){ - wait = 5; + wait = 10; newLines += 1; } else wait--; }