Results 1 to 15 of 15
  1. #1
    adri_360's Avatar
    adri_360 is offline Member
    Join Date
    Dec 2014
    Posts
    9
    Rep Power
    0

    Question Help with a basic game (tetris)

    Good nights, I post the code I'm developing for a tetris-based characters.
    The problem I have is that I can't print the piece within the board. Could you help solve it?
    I haven't any error.

    P.D: Sorry for my bad english.

    Thanks

    Class Piezas

    Java Code:
    public class Piezas {
    	
    public Piezas() {}
    //atributos
    private int i;
    private int j;
    private char m[][];
    private int formaPieza;
    
    //Piece1
    public void pieza1(){
    	this.i=0;
    	this.j=0;
    	this.m = new char [3][3];
    	
    	for(i=0;i<m.length;i++){
    		for(j=0;j<m.length;j++){
    			if(i<3 && j==0){
    				m[i][j]='*';
    			}
    			else{
    				m[i][j]=' ';
    			}
    			System.out.print(m[i][j]+" ");
    		}
    		System.out.println();
    	}
    }
    //Piece2
    public void pieza2(){
    	this.i=0;
    	this.j=0;
    	this.m = new char [3][3];
    	
    	for(i=0;i<m.length;i++){
    		for(j=0;j<m.length;j++){
    			if((i==1 && j==0)||(i==2 && j<3)){
    				m[i][j]='*';
    			}
    			else{
    				m[i][j]=' ';
    			}
    			System.out.print(m[i][j]+" ");
    		}
    		System.out.println();
    	}
    }
    //Piece3
    public void pieza3(){
    	this.i=0;
    	this.j=0;
    	this.m = new char [3][3];
    	
    	for(i=0;i<m.length;i++){
    		for(j=0;j<m.length;j++){
    					if((i==1 && j==2)||(i==2 && j<3)){
    						m[i][j]='*';
    					}
    					else{
    						m[i][j]=' ';
    					}
    					System.out.print(m[i][j]+" ");
    				}
    				System.out.println();
    	}
    }
    //Piece4
    public void pieza4(){
    	this.i=0;
    	this.j=0;
    	this.m = new char [3][3];
    	
    	for(i=0;i<m.length;i++){
    		for(j=0;j<m.length;j++){
    			if((i==1 && j==1)||(i==2 && j<3)){
    				m[i][j]='*';
    			}
    			else{
    				m[i][j]=' ';
    			}
    			System.out.print(m[i][j]+" ");
    		}
    		System.out.println();
    	}
    }
    //Piece5
    public void pieza5(){
    	this.i=0;
    	this.j=0;
    	this.m = new char [3][3];
    	
    	for(i=0;i<m.length;i++){
    		for(j=0;j<m.length;j++){
    			if((i==1 && j>0)||(i==2 && j<2)){
    				m[i][j]='*';
    			}
    			else{
    				m[i][j]=' ';
    			}
    			System.out.print(m[i][j]+" ");
    		}
    		System.out.println();
    	}
    
    }
    //Piece6
    public void pieza6(){
    	this.i=0;
    	this.j=0;
    	this.m = new char [3][3];
    	
    	for(i=0;i<m.length;i++){
    		for(j=0;j<m.length;j++){
    			if((i==1 && j<2)||(i==2 && j>0)){
    				m[i][j]='*';
    			}
    			else{
    				m[i][j]=' ';
    			}
    			System.out.print(m[i][j]+" ");
    		}
    		System.out.println();
    	}
    }
    //The piece is randomly chosen
    public void asignarPiezas() {
    	int n = 0;
    	n = (int) Math.floor(Math.random()*6+1);
    	
    	//System.out.println(n);
    	
    	switch(n){
    	case 1:
    		Piezas p1 = new Piezas();
    		p1.pieza1();
    		formaPieza=1;
    		break;
    	case 2: 
    		Piezas p2 = new Piezas();
    		p2.pieza2();
    		 formaPieza=2;
    		break;
    	case 3:
    		Piezas p3 = new Piezas();
    		p3.pieza3();
    		 formaPieza=3;
    		break;
    	case 4: 
    		Piezas p4 = new Piezas();
    		p4.pieza4();
    		 formaPieza=4;
    		break;
    	case 5: 
    		Piezas p5 = new Piezas();
    		p5.pieza5();
    		 formaPieza=5;
    		break;
    	case 6: 
    		Piezas p6 = new Piezas();
    		p6.pieza6();
    		 formaPieza=6;
    		break;
    	}
    	
    }
    //index for each piece so that then the board behaves in a way or another to place
    
    public int getformaPieza(){
    	return formaPieza;
    }
    }
    Class Tablero
    Java Code:
    import java.util.Scanner;
    
    public class Tablero extends Casillas{
    	Tablero() {}
    	private int i;
    	private int j;
    	private char matrizTablero [][];
    	private boolean comprobacion;
    	private boolean meter;
    
    //method that prints the empty board
    public void creacionTablero(){
    	this.matrizTablero= new char[8][8];
    	this.i=0;
    	this.j=0;
    	//Casillas obj_casilla = new Casillas();
    	//obj_casilla.setCasilla('_');
    	
    	for(i=0;i<8;i++){
    	for(j=0;j<8;j++){
    			matrizTablero[i][j]='_';//obj_casilla.getCasilla();
    			System.out.print(matrizTablero[i][j]+" ");
    		}
    		System.out.println();
    	}
    		
    	}
    //Prints piece placed on the board
    
    public void imprimirPieza(Piezas p){
    	this.meter=false;
    	this.i=0;
    	this.j=0;
    	
    	for(j=0;j<8;j++){
    		for(i=7;i<=0;i--){
    			meter = comprobarPieza(i, j, p);
    		}
    		if(comprobacion == true){
    			colocarPieza(i, j, p);
    		}
    		}
    	
    }
    
    //according to the piece that is (by number) checks of different article so the free holes on the board
    
    public boolean comprobarPieza(int i, int j, Piezas p){
    	int numeroPieza = p.getformaPieza();
    	this.comprobacion=true;
    	
    	switch(numeroPieza)
    	{
    	case 1:
    		for(i=7;i>=2;i--){
    			for(j=0;j<8;j++){
    		if((matrizTablero[i][j]=='_') && (matrizTablero[i-1][j]=='_') && (matrizTablero[i-2][j]=='_')){
    				comprobacion=true;
    			}
    			else{
    				comprobacion=false;
    			}
    		}
    	}
    
    		break;
    	case 2:
    		for(i=7;i>=0;i--){
    			for(j=0;j<8;j++){
    		if((matrizTablero[i][j]=='_') && (matrizTablero[i-1][j]=='_') && (matrizTablero[i][j-1]=='_') && (matrizTablero[i][j-2]=='_')){
    			comprobacion=true;
    			
    		}else{
    			comprobacion=false;
    		}
    			}
    		}
    
    		break;
    	case 3:
    		for(i=7;i>=0;i--){	
    		for(j=0;j<8;j++){
    				if((matrizTablero[i][j]=='_') && (matrizTablero[i][j-1]=='_') && (matrizTablero[i][j-2]=='_') && (matrizTablero[i-1][j-2]=='_')){
    					comprobacion=true;
    			}
    			else{
    				comprobacion=false;
    			}
    			}
    		}
    
    		break;
    	case 4:
    		for(i=7;i>=0;i--){	
    		for(j=0;j<8;j++){
    				if((matrizTablero[i][j]=='_') && (matrizTablero[i][j-1]=='_') && (matrizTablero[i][j-2]=='_') && (matrizTablero[i-1][j-1]=='_')){
    					comprobacion=true;
    				}
    				else{
    					comprobacion=false;
    				}
    			}
    		}
    
    		break;
    	case 5:
    		for(i=7;i>=0;i--){
    		for(j=0;j<8;j++){
    				if((matrizTablero[i][j]=='_') && (matrizTablero[i][j-1]=='_') && (matrizTablero[i-1][j-2]=='_') && (matrizTablero[i-1][j-1]=='_')){
    					comprobacion=true;
    				}
    				else{
    					comprobacion=false;
    				}
    			}
    				
    		}
    		break;
    	case 6:
    		for(i=7;i>=0;i--){
    		for(j=0;j<8;j++){
    				if((matrizTablero[i-1][j]=='_') && (matrizTablero[i-1][j-1]=='_') && (matrizTablero[i][j-2]=='_') && (matrizTablero[i][j-1]=='_')){
    					comprobacion=true;
    				}
    				else{
    					comprobacion=false;
    				}
    			}
    		}
    		break;
    
    	}
    	return comprobacion;
    }
    
    //form of randomly generated piece to place on the board
    public void colocarPieza( int i ,int j,  Piezas p){
    	int numeroPieza = p.getformaPieza();
    	
    	switch(numeroPieza)
    	{
    		case 1 :
    			matrizTablero [i][j]='*';
    			matrizTablero [i-1][j]='*';
    			matrizTablero [i-2][j]='*';
    			break;
    		case 2 :
    			matrizTablero [i][j]='*';
    			matrizTablero [i-1][j]='*';
    			matrizTablero[i][j-1]='*';
    			matrizTablero[i][j-2]='*';
    			break;
    		case 3 :
    			matrizTablero [i][j]='*';
    			matrizTablero[i][j-1]='*';
    			matrizTablero[i][j-2]='*';
    			matrizTablero[i-1][j-1]='*';
    			break;
    		case 4	:
    			matrizTablero[i][j]='*';
    			matrizTablero[i][j-1]='*';
    			matrizTablero[i][j-2]='*';
    			matrizTablero[i-1][j-1]='*';
    			break;
    		case 5 :
    			matrizTablero[i][j]='*';
    			matrizTablero[i][j-1]='*';
    			matrizTablero[i][j-2]='*';
    			matrizTablero[i-1][j-1]='*';
    			break;
    		case 6:
    			matrizTablero[i-1][j]='*';
    			matrizTablero[i-1][j-1]='*';
    			matrizTablero[i][j-2]='*';
    			matrizTablero[i][j-1]='*';
    			break;		
    }
    	
    		
    }
    }
    Class Partida
    Java Code:
    import java.util.Scanner;
    
    public class Partida extends Tablero{
    
    //Método para introducir "enter"	
    public String funcionEnter(){
    	String enter = "";
    	Scanner leeCadena = new Scanner (System.in);
    	String tecla = "";
    	System.out.println("<Pulse enter>");
    	enter = leeCadena.nextLine();
    	return enter;
    }
    
    public static void main(String[] args) {
    		int c=0;
    		String tecla="";
    		
    		
    		do{
    		//Pieza elegida
    		Piezas p_elegida = new Piezas();
    		p_elegida.asignarPiezas();
    			
    		//Tablero
    		Tablero obj_tablero = new Tablero();
    		obj_tablero.creacionTablero();
    		obj_tablero.imprimirPieza(p_elegida);
    		//Contador de piezas
    		System.out.println("");
    		System.out.println("Piezas = " +c);
    		c++;
    		
    		//Tecla enter
    		Partida enter = new Partida();
    		tecla = enter.funcionEnter();
    		}while((tecla != "\n") && (c!=250) );
    	}
    
    }
    Attached Files Attached Files
    Last edited by adri_360; 12-18-2014 at 07:58 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help with a basic game (tetris)

    Please post the code here on the forum so it can be read without a download.
    Be sure to wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    adri_360's Avatar
    adri_360 is offline Member
    Join Date
    Dec 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with a basic game (tetris)

    Quote Originally Posted by Norm View Post
    Please post the code here on the forum so it can be read without a download.
    Be sure to wrap the code in code tags.
    It gave me an error sending my code

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help with a basic game (tetris)

    Can you copy the console from when you execute the program and paste it here?
    What is the required input for a user of the program?
    Last edited by Norm; 12-18-2014 at 05:56 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    adri_360's Avatar
    adri_360 is offline Member
    Join Date
    Dec 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with a basic game (tetris)

    Quote Originally Posted by Norm View Post
    Can you copy the console from when you execute the program and paste it here?
    What is the required input for a user of the program?
    * *
    * *
    _ _ _ _ _ _ _ _
    _ _ _ _ _ _ _ _
    _ _ _ _ _ _ _ _
    _ _ _ _ _ _ _ _
    _ _ _ _ _ _ _ _
    _ _ _ _ _ _ _ _
    _ _ _ _ _ _ _ _
    _ _ _ _ _ _ _ _

    Piezas = 0
    <Pulse enter>
    I haven't any mistakes. I need to put down the piece.
    The user only have to press 'enter' to show the next piece to display the next piece to be placed, and shows on the 'board' the previous piece.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help with a basic game (tetris)

    I need to put down the piece.
    Can you explain what "put down the piece" means?

    What is the purpose of waiting for the user to press Enter? Is that all the user has to do: Press Enter?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    adri_360's Avatar
    adri_360 is offline Member
    Join Date
    Dec 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with a basic game (tetris)

    Quote Originally Posted by Norm View Post
    Can you explain what "put down the piece" means?

    What is the purpose of waiting for the user to press Enter? Is that all the user has to do: Press Enter?
    The aim is that whenever the user press enter, the piece that goes out above appears below to the left side

  8. #8
    adri_360's Avatar
    adri_360 is offline Member
    Join Date
    Dec 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with a basic game (tetris)

    That is the exercise:
    The objective of this project is to create a program that implements the known
    Tetris game. The game is formed by a 8x8 board, in which
    will positioning pieces. All parts fit within a square
    3x3. The representation of the different parts depend on whether the symbol
    draw a blank or *.

    If all boxes in any row of the board are occupied, it is
    emptied and boxes positioned above that row, will occupy the
    bottom row.
    Once there are no free spaces on the board to place a new
    piece, the game is ended. It is considered that there is no free space,
    if the part does not fit entirely within the board.
    The practice consists of two parts: basic options and advanced options. to the
    implement only the basic part is eligible for the 60% of the grade. the
    implementation of the advanced part opt to allow 100% of the grade.
    basic Options
    This basic part will have to simulate a game of Tetris in which they are to
    position x (the value will be introduced as an argument to launch the application)
    parts. The x pieces will be chosen at random. The pieces are
    positioning should go from left to right in the lower row of the board
    containing enough space to fit that piece. On display will go
    showing the state of the board as follows:
    1. State the board (at the beginning it will be empty)
    2. number of pieces have been placed,
    3. The message "Press Enter", which will leave the implementation of the
    simulation on hold until the user presses the Enter key.
    4. After pressing a key, to show the new piece selected
    place on the board and he will repeat the whole process.
    The game ends when no more pieces to position or not fit
    on the board.
    Last edited by adri_360; 12-18-2014 at 07:22 PM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help with a basic game (tetris)

    Why is there a need to wait for the user to Press Enter? All that does is slow down the program.

    Without waiting for the user, the program automatically loops 250 times.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    adri_360's Avatar
    adri_360 is offline Member
    Join Date
    Dec 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with a basic game (tetris)

    Quote Originally Posted by Norm View Post
    Why is there a need to wait for the user to Press Enter? All that does is slow down the program.

    Without waiting for the user, the program automatically loops 250 times.
    It's a project that the teacher sends to me
    Last edited by adri_360; 12-18-2014 at 07:40 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help with a basic game (tetris)

    There are lots of lines of code without any comments saying what the code is trying to do.

    One thing I see is that there are lots of new objects being created and the results of what the called methods do in those objects are lost when a new object is created.

    Why is a new object created every time the funcionEnter() method is called?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    adri_360's Avatar
    adri_360 is offline Member
    Join Date
    Dec 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with a basic game (tetris)

    Quote Originally Posted by Norm View Post
    There are lots of lines of code without any comments saying what the code is trying to do.

    One thing I see is that there are lots of new objects being created and the results of what the called methods do in those objects are lost when a new object is created.

    Why is a new object created every time the funcionEnter() method is called?
    Oh is a mistake. I'm noob on java :(. I just add some comments

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help with a basic game (tetris)

    Do any of the objects that are created need to be saved to preserve their contents?
    If the references to the created objects are not saved, their contents is lost.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    adri_360's Avatar
    adri_360 is offline Member
    Join Date
    Dec 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with a basic game (tetris)

    Quote Originally Posted by Norm View Post
    Do any of the objects that are created need to be saved to preserve their contents?
    If the references to the created objects are not saved, their contents is lost.
    How to save their contents?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help with a basic game (tetris)

    Save the reference to the object and reuse it as needed.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Tetris Game Problem
    By Schnensch in forum New To Java
    Replies: 2
    Last Post: 05-18-2014, 10:34 PM
  2. Replies: 3
    Last Post: 03-19-2012, 06:17 PM
  3. Normal tetris transform to Tetris with MVC.
    By kuburoman in forum Advanced Java
    Replies: 1
    Last Post: 04-24-2011, 11:53 AM
  4. tetris type game--- boundary help
    By ftrengnr in forum New To Java
    Replies: 11
    Last Post: 11-09-2010, 11:00 PM
  5. Replies: 6
    Last Post: 12-15-2009, 03:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •