Thread: problem with tetris game...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    problem with tetris game...

    well this is just the prototype, not the best method i guess(this is the 20th day with c) yet i cant manage to find whats wrong... the blocks just stop "randomly" in the air, please help!
    Code:
    //headers declaration
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <time.h>
    //var declaration
    char choice=NULL,name[4];
    int board[16][10],tbm[4][2],score=0,loop1,loop2,check,checkfar[4],i,p,level;
    time_t now,later;
    FILE *fp=NULL;
    //function declaration
    void main();
    int tetris();
    void genblock();
    void disp();
    void moveblock();
    void blockdown();
    void clrln();
    int gameisover();
    
    void main()
    {
    	// highscore rules tetris game and exit here
    	tetris();
    }
    
    int tetris()
    {
    	// gets:name,level. give:score
    	tbm[0][0]=20;
    	for(loop1=0;loop1<16;loop1++)
    		for(loop2=0;loop2<10;loop2++)
    			board[loop1][loop2]=0;
    	while(1)
    	{
    		if(tbm[0][0]==20)
    			genblock();
    		disp();
    		while(1)
    		{
    			now=time(0);
    			later=time(0);
    			while(difftime(later,now)<1)
    			{
    				if(kbhit())
    					moveblock();
    				later=time(0);
    			}
    			blockdown();
    			clrln();
    			score+=10;
    			disp();
    			if(tbm[0][0]==20)
    				break;
    		}
    		if(gameisover())
    			break;
    	}
    	return 0;
    }
    
    void genblock()
    {
    	now=time(0);
    	now%=7;
    	switch(now)
    	{
    	case 0:
    		tbm[0][0]=15;
    		tbm[0][1]=4;
    		tbm[1][0]=15;
    		tbm[1][1]=5;
    		tbm[2][0]=14;
    		tbm[2][1]=4;
    		tbm[3][0]=14;
    		tbm[3][1]=5;
    		break;
    	case 1:
    		tbm[0][0]=14;
    		tbm[0][1]=3;
    		tbm[1][0]=14;
    		tbm[1][1]=4;
    		tbm[2][0]=14;
    		tbm[2][1]=5;
    		tbm[3][0]=14;
    		tbm[3][1]=6;
    		break;
    	case 2:
    		tbm[0][0]=15;
    		tbm[0][1]=4;
    		tbm[1][0]=14;
    		tbm[1][1]=4;
    		tbm[2][0]=14;
    		tbm[2][1]=5;
    		tbm[3][0]=14;
    		tbm[3][1]=6;
    		break;
    	case 3:
    		tbm[0][0]=15;
    		tbm[0][1]=5;
    		tbm[1][0]=14;
    		tbm[1][1]=4;
    		tbm[2][0]=14;
    		tbm[2][1]=5;
    		tbm[3][0]=14;
    		tbm[3][1]=6;
    		break;
    	case 4:
    		tbm[0][0]=15;
    		tbm[0][1]=6;
    		tbm[1][0]=14;
    		tbm[1][1]=4;
    		tbm[2][0]=14;
    		tbm[2][1]=5;
    		tbm[3][0]=14;
    		tbm[3][1]=6;
    		break;
    	case 5:
    		tbm[0][0]=15;
    		tbm[0][1]=4;
    		tbm[1][0]=15;
    		tbm[1][1]=5;
    		tbm[2][0]=14;
    		tbm[2][1]=5;
    		tbm[3][0]=14;
    		tbm[3][1]=6;
    		break;
    	case 6:
    		tbm[0][0]=15;
    		tbm[0][1]=6;
    		tbm[1][0]=15;
    		tbm[1][1]=5;
    		tbm[2][0]=14;
    		tbm[2][1]=5;
    		tbm[3][0]=14;
    		tbm[3][1]=4;
    		break;
    	}
    	for(loop1=0;loop1<4;loop1++)
    		board[tbm[loop1][0]][tbm[loop1][1]]=1;
    }
    void disp()
    {
    	system("cls");
    	for(loop1=14;loop1>=0;loop1--)
    	{
    		putchar('|');
    		for(loop2=0;loop2<10;loop2++)
    		{
    			if(board[loop1][loop2])
    				putchar('x');
    			else
    				putchar(' ');
    		}
    		putchar('|');
    		putchar(10);
    	}
    	puts(" ==========");
    }
    
    void moveblock()
    {
    	choice=getch();
    	switch(choice)
    	{
    	case 'a':
    		check=0;
    		checkfar[0]=20;
    		for(loop1=0;loop1<4;loop1++)
    		{
    			if(tbm[loop1][1]<checkfar[0])
    				checkfar[check]=tbm[loop1][1];
    			else if (tbm[loop1][1]==checkfar[0])
    			{
    				checkfar[check]=checkfar[check+1];
    				check++;
    			}
    		}
    		check++;
    		i=0;
    		p=0;
    		while(check>i)
    		{
    			for(loop1=0;loop1<4;loop1++)
    				if(board[tbm[loop1][0]][checkfar[i]-1]==1 || tbm[loop1][1]==0)
    					p=1;
    				i++;
    		}
    		if(p!=1)
    		{
    		for(loop1=0;loop1<4;loop1++)
    		{
    			board[tbm[loop1][0]][tbm[loop1][1]]=0;
    			tbm[loop1][1]-=1;
    			board[tbm[loop1][0]][tbm[loop1][1]]=1;
    		}
    		disp();
    		}
    		break;
    	case 's':
    		blockdown();
    		break;
    	case 'd':
    		check=0;
    		checkfar[0]=0;
    		for(loop1=0;loop1<4;loop1++)
    		{
    			if(tbm[loop1][1]>checkfar[0])
    				checkfar[check]=tbm[loop1][1];
    			else if (tbm[loop1][1]==checkfar[0])
    			{
    				checkfar[check]=checkfar[check+1];
    				check++;
    			}
    		}
    		check++;
    		i=0;
    		p=0;
    		while(check>i)
    		{
    			for(loop1=0;loop1<4;loop1++)
    				if(board[tbm[loop1][0]][checkfar[i]+1]==1 || tbm[loop1][1]==9)
    					p=1;
    				i++;
    		}
    		if(p!=1)
    		{
    		for(loop1=0;loop1<4;loop1++)
    		{
    			board[tbm[loop1][0]][tbm[loop1][1]]=0;
    			tbm[loop1][1]+=1;
    			board[tbm[loop1][0]][tbm[loop1][1]]=1;
    		}
    		disp();
    		}
    		break;
    	default:
    		break;
    	}
    }
    void blockdown()
    {
    		check=0;
    		checkfar[0]=20;
    		for(loop1=0;loop1<4;loop1++)
    		{
    			if(tbm[loop1][0]<checkfar[0])
    				checkfar[check]=tbm[loop1][1];
    			else if (tbm[loop1][1]==checkfar[0])
    			{
    				checkfar[check]=checkfar[check+1];
    				check++;
    			}
    		}
    		check++;
    		i=0;
    		while(check>i)
    		{
    			for(loop1=0;loop1<4;loop1++)
    				if(board[checkfar[i]-1][tbm[loop1][1]]==1 || tbm[loop1][0]==0)
    					tbm[0][0]=20;
    				i++;
    		}
    		if(tbm[0][0]!=20)
    		{
    		for(loop1=0;loop1<4;loop1++)
    		{
    			board[tbm[loop1][0]][tbm[loop1][1]]=0;
    			tbm[loop1][0]-=1;
    			board[tbm[loop1][0]][tbm[loop1][1]]=1;
    		}
    		disp();
    		}
    }
    
    void clrln()
    {
    	check=0;
    	for(loop1=0;loop1<15;loop1++)
    	{
    		for(loop2=0;loop2<10;loop2++)
    			if(board[loop1][loop2])
    				check+=1;
    		if(check==10)
    			break;
    	}
    	if(check==10)
    	{
    		while(loop1<14)
    		{
    			for(loop2=0;loop2<10;loop2++)
    				board[loop1][loop2]=board[loop1+1][loop2];
    			loop1++;
    		}
    		for(loop2=0;loop2<10;loop2++)
    				board[loop1][loop2]=0;
    		disp();
    	}
    }
    int gameisover()
    {
    	for(loop2=0;loop2<10;loop2++)
    	{
    		if(board[14][loop2]==1 && board[13][loop2]==1 && board[12][loop2]==1 && board[11][loop2]==1 && board[10][loop2]==1)
    		{
    			puts("you lose press enter something to continue");
    			scanf("%d",&check);
    			return 1;
    		}
    	}
    	return 0;
    }

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    can you tell us some more? That's a lot of code to just read through

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    well,this is just the prototype XD,basicly the method is based on 2 var, board which represent every block on the board and tbm(ToBasiclyMove) which control the blocks that the player has controls.
    i assume the problem is in blockdown() && moveblock() or in tetris()
    i think if u just compile it ull probably get this way fast than me,
    disp(); -display board, nothing srs.
    gameover();- not tbest method, checks if the user lose/
    clrln();- checks if the line is full, if(1) copies all the line above and erase the last one.
    genblock();-take time%7 and generate block with it (gives tbm[][]).
    tetris();- the whole complex calls functions and wait...
    moveblock(); && blockdown();- gets key, check if block or can go any further, if can it does

    thats what u meant or what does the loops and stuff do?
    Last edited by Sokay; 10-10-2009 at 06:44 PM.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    hmm you need to fix the code a bit

    Code:
    void main()
    {
    	// highscore rules tetris game and exit here
    	tetris();
    }
    change it to "int" or some other return type since void main ....doesnt make sense

    and why are you declaring main method as a function declaration.. dnt do that..

    Code:
    //function declaration
    void main();----------------> Why?
    int tetris();
    void genblock();
    void disp();
    .....
    .....

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I ran your game program and here's my suggestions:

    1) A lot of your variables are global: loop1, loop2, check, etc. That is going to make you
    *RUE* the day you ever heard of global variables.

    Make a FEW variables that are used by most of your functions, global, and make ALL the rest, local. That will stop a lot of of bugs, and a bad kind of bugs (because the bug can be in any of several functions).

    2) Use a better name for your variables and functions. Check for instance - OK, but *what* does it check? You know now, but two years from now, you'll have no idea.
    disp() - I know what you mean, but for crying out loud, add the other 3 letters, so everyone will know *at once*, what the "disp" function is for. (display).

    Use C conventional names for loop counters: i, j, and k if needed, and make them local, not global.

    3) Stop the screen running with every movement. You're going to give people a headache, and you need to see - carefully - what the movement of the block has been while you're developing your program. That's hard to do with a constantly running screen.

    Draw your chute that the blocks drop in, and then just draw the blocks, using gotoxy(). You're using conio.h, so you have that function available. Use it.

    4) You've made the logic a bit more complex than it needs to be. Try and keep it as simple as possible to eliminate bugs and make it easier to spot things you want to change or fix.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  2. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  3. Game Idea (Tetris Maze)
    By SlyMaelstrom in forum Game Programming
    Replies: 13
    Last Post: 06-07-2006, 05:48 PM
  4. Linking problem
    By Agent89 in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2005, 03:03 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM