[Maintenance] Pentris

Pentris (+Tetris) V.1. is finished!

Source code can be downloaded from downloads page, so check it out if you are willing to see how it is implemented.
As usual, any changes on software (updates) will be posted here.

Pentris Screenshot

Pentris Screenshot

Advertisements

[Implementation] Pentris – Piece

Updates
  • TYPE enum: TYPE contains every possible type of piece in Tetris and Pentris. Also, a method to randomly select a type is implemented.
  • Direction enum: Direction contains four possible directions of a move.
  • Piece class: this models the data of Tetris and Pentris pieces

Piece class contains

  • Static HashMap variables that are used to contain unchangeable data (color of a piece, coordinates (shape) of a piece, and min/max points for each type)
  • x and y that record the center of a piece
  • basic methods that a piece needs

[Implementation] Pentris – MoodChooser

ModeChooser is a class generally made for Menu screen. I designed the class so that when I use it, I can give information of buttons and # of buttons that user will choose. Using this class, I can, in return, get information buttons chosen by users.

Also, I made three constructors to allow to choose the dimension of menu screen and to set the title of the menu screen.

Most importantly, I used notify() and wait() method overriden from Object class to efficiently make the main frame Thread till this menu Thread is finished.

//call other thread to work
			try{
				synchronized(out){
					out.notify();
				}
			}catch(Exception exp){
				exp.printStackTrace();
			}

In this class, ‘in’ is a group of Buttons that were received from user and ‘out’ is a group of Buttons chosen by user. Both are implemented using simple arrays.

Now, check the full sourcecode.

package gygd.pentris.choi;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * This class was created for the functionality of displaying a set of buttons and allowing the user to choose one
 * This class was made very generally for future reuse
 * This class uses wait() and notify()
 * @author Wonjohn Choi
 *
 */
public class ModeChooser extends JFrame implements Runnable, ActionListener{
	
	protected JButton[][] in; //given buttons
	public JButton[] out; //chosen buttons
	protected int idx; //index needed to fill the 'out' array
	
	/**
	 * constructor
	 * @param buttons
	 * @param re
	 */
	public ModeChooser(JButton[][] buttons, int n){
		//initialize process
		in = buttons;
		out = new JButton[n]; //n indicates # of expected chosen buttons
		idx = 0;
		
		//set layout
		setLayout(new GridLayout(buttons.length, buttons[0].length));
		
		//for each buttons,
		for(int r=0;r<buttons.length;r++){
			for(int c=0;c<buttons[0].length;c++){
				//if a button does not exit,
				if(buttons[r][c]==null){
					//put a dead button
					buttons[r][c]=new JButton();
					buttons[r][c].setEnabled(false);
				}
				
				//reaction to mouse click
				buttons[r][c].addActionListener(this);
				//add to frame
				add(buttons[r][c]);
			}
		}
		
		//set the best optimized size
		setResizable(false);
		pack();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	/**
	 * constructor
	 * @param buttons
	 * @param n
	 * @param name
	 */
	public ModeChooser(JButton[][] buttons, int n, String name){
		this(buttons, n);
		setTitle(name);
	}
	
	/**
	 * constuctor that sets the size of chooser
	 * @param buttons
	 * @param re
	 * @param dimen
	 */
	public ModeChooser(JButton[][] buttons, int n, Dimension dimen){
		this(buttons, n); //call standard constructor
		
		//set size of the button chooser
		setSize(dimen);
	}
	
	/**
	 * for thread purpose
	 */
	@Override
	public void run() {
		//start by making it visible
		setVisible(true);
	}

	/**
	 * when clicked by mouse
	 */
	@Override
	public void actionPerformed(ActionEvent event) {
		JButton chosen = (JButton) event.getSource(); //get button
		chosen.setEnabled(false); //kill its reaction
		out[idx] = chosen; //store it to the chosen button list
		idx++; 
		
		//if required buttons are chosen,
		if(out.length == idx){
			//invisible frame
			setVisible(false);
			
			//call other thread to work
			try{
				synchronized(out){
					out.notify();
				}
			}catch(Exception exp){
				exp.printStackTrace();
			}
			
		}
		
	}

}

[Implementation] Pentris – Board

Basic structure for Board class was finished: general GUI is done.

Check the source code posted. The class uses bunch of buttons to simulate what I want.

package gygd.pentris.choi;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

/**
 * board class
 * @author Wonjohn Choi
 *
 */
public class Board extends JPanel {
	//array to control each block
	protected JButton[][] blockGrid;

	//mathematical grid
	protected boolean[][] grid;

	static final boolean EMPTY = false;
	static final boolean FILLED = true;

	//width and height of the board
	protected int HEIGHT, WIDTH;

	public Board(int height, int width){
		HEIGHT = height; WIDTH = width;

		grid = new boolean[HEIGHT][WIDTH];
		blockGrid = new JButton[HEIGHT][WIDTH]; //instantiate
		setLayout(new GridLayout(HEIGHT, WIDTH)); //set its layout

		//for each and every block,
		for(int row=0;row<HEIGHT;row++){
			for(int col=0;col
				blockGrid[row][col] = new JButton();//set each block as a button
				blockGrid[row][col].setBorder(new LineBorder(Color.getHSBColor(0, 0,0.8F)));//set its border color
				blockGrid[row][col].setEnabled(false);//disable it
				add(blockGrid[row][col]);//add to the panel
			}
		}

		setSize(WIDTH*30, HEIGHT*30); //set the size (each block has a size length of 30)
	}

	public static void main(String args[]){
		JFrame test = new JFrame();
		Board board = new Board(20, 20);
		test.add(board);
		test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		test.setVisible(true);
		test.setSize(board.WIDTH*30, board.HEIGHT*30);
	}

}

Pentris_Board

Pentris_Board

The left image is earned by running above code.

[Design] Pentris

Board.java

This class will extend JPanel and will be used to manage blocks in pentris board. Each block will be made by JButton. Also, this class will receive two integer variables to set the size of the board.

For “Only Tetris” mode, the board size will be 10*20.

For “Tetris+Pentris” or “Pentris” mode, the board size will be 20*20 because it’s much harder.

Piece.java

A model class for every piece.

ModeChooser.java

A class to choose modes (“Only Tetris,” “Tetris + Pentris,” “Only Pentris”)

Pentris.java

A class to manage the whole game.

This class should include Timer, Board.java, TetrisPieces.java, PentrisPieces.java, Level Option

**Other classes/functions will be added in future if necessary

[Requirement] Pentris

Hello I am Wonjohn Choi.

I named the game I am going to develop as Pentris.

As I do not think there was any game similar to this before, I need to explain a bit. This game is a variation of Tetris, the traditional game. But, in Pentris, we use pentominoes (polyomino composed of five blocks) instead of Tetris blocks.

Tetris Image from Flickr

Tetris Image from Flickr

I am going to develop this with Java.

———————————Update——————————————-

Decided to allow user to choose between three moods: only Tetris, Tetris+Pentris, only Pentris