Need Help

This is a discussion on Need Help within the C Programming forums, part of the General Programming Boards category; I'm planning to write a tetris game, in C. I can use conio.h and gotoxy function. But I don't know ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    66

    Need Help

    I'm planning to write a tetris game, in C.
    I can use conio.h and gotoxy function.

    But I don't know how to begin, I can't think of a way to define the blocks.

    Can anyone tell me the main concept of tetris?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by ThLstN View Post
    I can use conio.h and gotoxy function.
    You can, but better not to... conio.h is not standard

    Quote Originally Posted by ThLstN View Post
    But I don't know how to begin, I can't think of a way to define the blocks.

    Can anyone tell me the main concept of tetris?
    For example with the matrix of 0 and 1
    Code:
    0 0 1
    1 1 1
    0 0 0
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ThLstN View Post
    I'm planning to write a tetris game, in C.
    I can use conio.h and gotoxy function.

    But I don't know how to begin, I can't think of a way to define the blocks.

    Can anyone tell me the main concept of tetris?
    First, if you're a beginner, I recommend using conio.h and gotoxy, for your Tetris game, unless you already know the equivalent functions in Windows API, or want to share your code and have it compile on other compilers. Any Windows system should have no trouble playing your Tetris game, from the executable form of the game.

    Tetris games typically use a number of 2D arrays (small), to represent the blocks. The blocks (the array's), are chosen at random, to appear at the top of the game. After a short delay, they are displayed further downward in a new position, simply by incrementing the y value of their old position. They always fall straight downward, unless you change that.

    The user can rotate the blocks by pressing a key (not too challenging to code up), and of course, make them drop quickly with another key press. The user won't see all of any array except for the square block. They only see the parts that are filled with something other than a zero value. (usually just a one)

    It's a good challenge to code one up. I'm sure you'll learn a lot.

    What functions do you think a Tetris game will need, ThLstN?

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    Well... I have been trying to code one up, and it went pretty well.
    So far I have created the functions to drop the blocks and I'm working on the rotate function.

    This is what I've been working on, I'm still testing the functions and the rotate function doesn't work yet.

    Whay does the indentation always got messed up if I pasted the code from Borland C?

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<time.h>
    #include<stdlib.h>
    
    void delay(int n)
    {
    	clock_t sec;
       sec = clock();
       while((clock() - sec) < n);
    }
    
    void rotateblocks(int blocks[6][4][4],int typ)
    {
    	int i,j,temp;
    	for(i=0;i<4;i++)
       {
       	for(j=0;j<4;j++)
          {
          	temp = blocks[typ][i][j];
             blocks[typ][i][j] = blocks[typ][3-j][i];
             blocks[typ][3-j][i] = temp;
          }
       }
    }
    
    void moveblocks(int grid[30][25],int blocks[6][4][4])
    {
    	int i,j,k,l;
       int posx = 1,axs=1;
       k = random(6);
       char key;
       for(i=1;i<=25;i++)
       {
       	clrscr();
          if(kbhit()) key = getch();
          switch(key)
          {
          	case 75:
             {
             	if(posx>1) posx--;
                key = 0;
             }
             break;
             case 77:
             {
             	if(posx<10) posx++;
                key = 0;
             }
             break;
             case 72:
             {
             	rotateblocks(blocks,k);
                key = 0;
             }
          }
       	for(j=0;j<4;j++)
          {
          	for(l=0;l<4;l++)
             {
             	if(blocks[k][j][l]==1) printf("X");
             }
    	   	gotoxy(posx,i+j);
          }
          delay(100);
       }
    }
    
    void init(int blocks[6][4][4])
    {
    	int i;
       for(i=0;i<6;i++)
       {
          switch(i)
          {
          	case 0:
             {
    				blocks[i][0][0] = 0;
    				blocks[i][0][1] = 0;
    				blocks[i][0][2] = 0;
    				blocks[i][0][3] = 0;
    				blocks[i][1][0] = 1;
    				blocks[i][1][1] = 0;
    				blocks[i][1][2] = 0;
    				blocks[i][1][3] = 0;
    				blocks[i][2][0] = 1;
    				blocks[i][2][1] = 0;
    				blocks[i][2][2] = 0;
    				blocks[i][2][3] = 0;
    				blocks[i][3][0] = 1;
    				blocks[i][3][1] = 1;
    				blocks[i][3][2] = 0;
    				blocks[i][3][3] = 0;
             }
             break;
          	case 1:
             {
    				blocks[i][0][0] = 0;
    				blocks[i][0][1] = 0;
    				blocks[i][0][2] = 0;
    				blocks[i][0][3] = 0;
    				blocks[i][1][0] = 0;
    				blocks[i][1][1] = 0;
    				blocks[i][1][2] = 0;
    				blocks[i][1][3] = 0;
    				blocks[i][2][0] = 0;
    				blocks[i][2][1] = 1;
    				blocks[i][2][2] = 0;
    				blocks[i][2][3] = 0;
    				blocks[i][3][0] = 1;
    				blocks[i][3][1] = 1;
    				blocks[i][3][2] = 1;
    				blocks[i][3][3] = 0;
             }
             break;
          	case 2:
             {
    				blocks[i][0][0] = 0;
    				blocks[i][0][1] = 0;
    				blocks[i][0][2] = 0;
    				blocks[i][0][3] = 0;
    				blocks[i][1][0] = 0;
    				blocks[i][1][1] = 0;
    				blocks[i][1][2] = 0;
    				blocks[i][1][3] = 0;
    				blocks[i][2][0] = 1;
    				blocks[i][2][1] = 1;
    				blocks[i][2][2] = 0;
    				blocks[i][2][3] = 0;
    				blocks[i][3][0] = 1;
    				blocks[i][3][1] = 1;
    				blocks[i][3][2] = 0;
    				blocks[i][3][3] = 0;
             }
             break;
          	case 3:
             {
    				blocks[i][0][0] = 1;
    				blocks[i][0][1] = 0;
    				blocks[i][0][2] = 0;
    				blocks[i][0][3] = 0;
    				blocks[i][1][0] = 1;
    				blocks[i][1][1] = 0;
    				blocks[i][1][2] = 0;
    				blocks[i][1][3] = 0;
    				blocks[i][2][0] = 1;
    				blocks[i][2][1] = 0;
    				blocks[i][2][2] = 0;
    				blocks[i][2][3] = 0;
    				blocks[i][3][0] = 1;
    				blocks[i][3][1] = 0;
    				blocks[i][3][2] = 0;
    				blocks[i][3][3] = 0;
             }
             break;
          	case 4:
             {
    				blocks[i][0][0] = 0;
    				blocks[i][0][1] = 0;
    				blocks[i][0][2] = 0;
    				blocks[i][0][3] = 0;
    				blocks[i][1][0] = 0;
    				blocks[i][1][1] = 0;
    				blocks[i][1][2] = 0;
    				blocks[i][1][3] = 0;
    				blocks[i][2][0] = 0;
    				blocks[i][2][1] = 1;
    				blocks[i][2][2] = 1;
    				blocks[i][2][3] = 0;
    				blocks[i][3][0] = 1;
    				blocks[i][3][1] = 1;
    				blocks[i][3][2] = 0;
    				blocks[i][3][3] = 0;
             }
             break;
          	case 5:
             {
    				blocks[i][0][0] = 0;
    				blocks[i][0][1] = 0;
    				blocks[i][0][2] = 0;
    				blocks[i][0][3] = 0;
    				blocks[i][1][0] = 0;
    				blocks[i][1][1] = 0;
    				blocks[i][1][2] = 0;
    				blocks[i][1][3] = 0;
    				blocks[i][2][0] = 1;
    				blocks[i][2][1] = 1;
    				blocks[i][2][2] = 0;
    				blocks[i][2][3] = 0;
    				blocks[i][3][0] = 0;
    				blocks[i][3][1] = 1;
    				blocks[i][3][2] = 1;
    				blocks[i][3][3] = 0;
             }
          }
       }
    }
    
    int main()
    {
    	int i,j,k;
    	int grid[30][25];
       int blocks[6][4][4];
       char key;
       randomize();
       init(blocks);
    	do
       {
         moveblocks(grid,blocks);
       } while(key!=27);
       getchar();
       return 0;
    }
    Last edited by ThLstN; 01-13-2008 at 03:02 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why a 3D block??

    If you download an ascii table (www.asciitable.com, I believe), you can find a MUCH better choice of a char to use for your block part than just 'X'.

    It's interesting seeing how someone else works out this logic.

    Indentation problem with the board:

    Go into your options menu, and find the options for your editor. It will automatically want to indent either by using a number of spaces or by using a tab.
    Set it to use spaces, only.
    Last edited by Adak; 01-13-2008 at 03:11 AM.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    3D Block is actually to define the type of block that will be randomized each loop.

    The rotate function is difficult to write so I created a pattern first and try to code it.

    Like This:

    0123
    0 0000
    1 X000
    2 X000
    3 XX00

    0123
    0 0000
    1 0000
    2 000x
    3 0xxx



    [0][0] -> [0][3]
    [0][1] -> [1][3]
    [0][2] -> [2][3]
    [0][3] -> [3][3]

    [1][0] -> [0][2]
    [1][1] -> [1][2]
    [1][2] -> [2][2]
    [1][3] -> [3][2]

    Will this work with this piece of code?

    Code:
    	for(i=0;i<4;i++)
       {
       	for(j=0;j<4;j++)
          {
          	temp = blocks[typ][i][j];
             blocks[typ][i][j] = blocks[typ][j][3-i];
             blocks[typ][j][3-i] = temp;
          }
       }

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I think I was unclear, earlier.

    Tetris uses blocks with SET patterns - not random in shape. The random part comes into play in choosing which of 6 or 8 (whatever) patterns of block will fall next, in the game.

    IMO you don't want 3D blocks - you want to save 6-8 different shapes, and then when random number has been chosen, the block pattern matching that number, will fall next.

    So maybe:

    #1 block: square
    #2 block: straight line
    #3 block: Left L shape
    #4 block: Right L shape
    etc.

    I'm not saying don't use your 3D blocks, but just that classic Tetris doesn't use random shapes of blocks (and even then you wouldn't need 3D blocks). Seems more difficult than it needs to be, from my perspective.

    One of the beauties of Tetris is that when it's played well, the block shapes will interlock in such a way as to complete a whole line of blocks and "disappear". I don't think random shaped blocks can do that.
    Last edited by Adak; 01-13-2008 at 03:32 AM.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    Yeah that's what I'm doing with the 3D arrays.
    The first dimension is to define the shapes of blocks, I'm not trying to make a random shape.
    I saved 6 different shapes of block, and then pick one up using the random function.

    Thanks for your help, I'm still trying to make the rotate function works.

    I don't know what's wrong with the rotate code because if I try all indexes, it will the match the corresponding index.

    Code:
       	for(i=0;i<4;i++)
       	{
       		for(j=0;j<4;j++)
          	       {
          		        temp = blocks[i][j];
             	        blocks[i][j] = blocks[j][3-i];
             	        blocks[j][3-i] = temp;
          	        }
       	}
    Last edited by ThLstN; 01-13-2008 at 03:42 AM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Quote Originally Posted by Adak View Post
    Indentation problem with the board:

    Go into your options menu, and find the options for your editor. It will automatically want to indent either by using a number of spaces or by using a tab.
    Set it to use spaces, only.
    OR set it to tabs only.
    Point being, you shouldn't mix spaces and tabs. IF you do that, THEN the indentation will mess up. But then again, Borland C is such an old IDE (I think?), perhaps it's better to upgrade to a newer one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21