Results 1 to 15 of 15
Thread: Help with a basic game (tetris)
- 12-18-2014, 02:51 AM #1
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; } }
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; } } }
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) ); } }
Last edited by adri_360; 12-18-2014 at 07:58 PM.
- 12-18-2014, 03:15 AM #2
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.
- 12-18-2014, 12:03 PM #3
- 12-18-2014, 05:42 PM #4
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.
- 12-18-2014, 06:57 PM #5
Re: Help with a basic game (tetris)
* *
* *
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
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.
- 12-18-2014, 07:05 PM #6
Re: Help with a basic game (tetris)
I need to put down the piece.
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.
- 12-18-2014, 07:15 PM #7
- 12-18-2014, 07:17 PM #8
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.
- 12-18-2014, 07:30 PM #9
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.
- 12-18-2014, 07:37 PM #10
- 12-18-2014, 07:44 PM #11
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-18-2014, 08:01 PM #12
- 12-18-2014, 08:10 PM #13
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.
- 12-18-2014, 08:41 PM #14
- 12-18-2014, 08:45 PM #15
Similar Threads
-
Tetris Game Problem
By Schnensch in forum New To JavaReplies: 2Last Post: 05-18-2014, 10:34 PM -
Assignment help with how to create scoreboard for tetris game
By Zaiton in forum New To JavaReplies: 3Last Post: 03-19-2012, 06:17 PM -
Normal tetris transform to Tetris with MVC.
By kuburoman in forum Advanced JavaReplies: 1Last Post: 04-24-2011, 11:53 AM -
tetris type game--- boundary help
By ftrengnr in forum New To JavaReplies: 11Last Post: 11-09-2010, 11:00 PM -
Trying to make a functioning Tetris game/ why does my board look weird?
By DeusExMachina90 in forum AWT / SwingReplies: 6Last Post: 12-15-2009, 03:33 AM
Bookmarks