Tetris Shape Generation Algorithm in Java -


i'm building tetris game in java using software design patterns. basically, created factory algorithm retrieves string figure out type of tetris object make when it's given request main game loop, see code:

public class tvshapefactory {      protected tvshape tvshape = null;     protected gamecontainer gc;      tvshapefactory(gamecontainer gc) {         this.gc = gc;     }      public tvshape createshape(string shape) {         if (shape=="tvishape") {             tvshape = new tvishape(gc);         }         else if (shape=="tvoshape") {             tvshape = new tvoshape(gc);         }         else if (shape=="tvlshape") {             tvshape = new tvlshape(gc);         }         else if (shape=="tvzshape") {             tvshape = new tvzshape(gc);         }         else if (shape=="tvjshape") {             tvshape = new tvjshape(gc);         }         else if (shape=="tvsshape") {             tvshape = new tvsshape(gc);         }         else if (shape=="tvtshape") {             tvshape = new tvtshape(gc);         }         else {             system.out.println("error: invalid type of shape");         }         return tvshape;     } } 

if don't know gc is, it's part of slick 2d's library , used work full game environment. anyways, created few other methods randomly generate shape game (such main game loop receives random shape every time, feel rng doesn't cut it, want make harder. noticed there's famous tetris algorithm called bastet tetris, didn't make sense me. suggestions guys have making hard tetris shape generation algorithm? here's simple rng algorithm:

public tvshape getrandomshape() {      tvshape[] shapes = new tvshape[7];     shapes[0] = createshape("tvishape");     shapes[1] = createshape("tvoshape");     shapes[2] = createshape("tvlshape");     shapes[3] = createshape("tvzshape");     shapes[4] = createshape("tvjshape");     shapes[5] = createshape("tvsshape");     shapes[6] = createshape("tvtshape");      int index = new random().nextint(shapes.length);     return shapes[index]; } 

instead of "==" sign use .equals() function compare strings. == used check whether 2 strings have same reference or not. whereas .equals() method used check whether 2 string has same value or not. instead of

(shape=="tvishape") 

use

(shape.equals("tvishape") 

Popular posts from this blog