Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JComponent;
- public class Game extends JComponent{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public GamePiece[][] gameBoard;
- public Game() {
- gameBoard = new GamePiece[4][4];
- for (int k = 0; k < 4; k++) {
- for (int j = 0; j < 4; j++) {
- int x = 12 + (160 + 12) * k;
- int y = 12 + (160 + 12) * j;
- gameBoard[k][j] = new GamePiece(x, y, 0);
- }
- }
- }
- public void start() {
- }
- public void restart() {
- for (int k = 0; k < 4; k++) {
- for (int j = 0; j < 4; j++) {
- gameBoard[k][j].setValue(0);
- }
- }
- }
- public void random() {
- int randomRow = (int) Math.random() * 4;
- int randomCol = (int) Math.random() * 4;
- while(gameBoard[randomRow][randomCol].getValue() == 0) {
- randomRow = (int) Math.random() * 4;
- randomCol = (int) Math.random() * 4;
- }
- gameBoard[randomRow][randomCol].setValue(2);
- }
- public void up() {
- System.out.println("Up Arrow Pressed");
- for (int c = 0; c < 4; c++) {
- for (int r = 3; r > 0; r--) {
- if (gameBoard[r][c] == gameBoard[r - 1][c]) {
- gameBoard[r][c - 1].setValue(gameBoard[r][c].getValue() * 2);
- gameBoard[r][c].setValue(0);
- gameBoard[r][c].moveUp();
- }
- }
- }
- random();
- }
- public void down() {
- System.out.println("Down Arrow Pressed");
- for (int c = 0; c < 4; c++) {
- for (int r = 0; r < 4; r++) {
- if (gameBoard[r][c] == gameBoard[r + 1][c]) {
- gameBoard[r][c + 1].setValue(gameBoard[r][c].getValue() * 2);
- gameBoard[r][c].setValue(0);
- gameBoard[r][c].moveDown();
- }
- }
- }
- random();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement