-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePlay.java
More file actions
105 lines (100 loc) · 3.46 KB
/
GamePlay.java
File metadata and controls
105 lines (100 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.event.KeyEvent;
/**
*
* By: Nishant Chintala
*/
/*
The GamePlay class contains moving methods for the tiles as well as the reActivate
method to reactivate the Tiles and allow them to move in the future.
*/
public class GamePlay{
static Tile[][] board;
KeyEvent key;
static int score;
public GamePlay(Tile[][] gameBoard){
board = gameBoard;
score = 0;
}
/*moveUp() is called when the up arrow key is pressed. it moves all the tiles
up in the board*/
public void moveUp(){
for(int i = 1; i <board.length; i++){
for(int j = 0; j<board.length; j++){
int x = i;
while(x > 0 && board[x-1][j].number == 0){
board[x][j].combineWithEmpty(board[x-1][j]);
x--;
}
if(board[i][j].number == board[i-1][j].number){
board[i][j].combine(board[i-1][j]);
}
}
}
reActivate();
}
/*moveDown() is called when the down arrow key is pressed. it moves all the tiles
down in the board*/
public void moveDown(){
for(int i = 2; i >= 0; i--){
for(int j = 0; j < board.length; j++){
int x = i;
while(x < 3 && board[x+1][j].number == 0){
board[x][j].combineWithEmpty(board[x+1][j]);
x++;
}
if(board[i][j].number == board[i+1][j].number){
board[i][j].combine(board[i+1][j]);
}
}
}
reActivate();
}
/*moveRight() is called when the right arrow key is pressed. it moves all the tiles
right in the board*/
public void moveRight(){
for(int i = 0; i < board.length; i++){
for(int j = 2; j>=0; j--){
int x = j;
while(x < 3 && board[i][x+1].number == 0){
board[i][x].combineWithEmpty(board[i][x+1]);
x++;
}
if(board[i][j].number == board[i][j+1].number && board[i][j].number !=0){
board[i][j].combine(board[i][j+1]);
}
}
}
reActivate();
}
/*moveLeft() is called when the left arrow key is pressed. it moves all the tiles
left in the board*/
public void moveLeft(){
for(int i = 0; i < board.length; i++){
for(int j = 1; j<board.length; j++){
int x = j;
while(x > 0 && board[i][x-1].number==0){
board[i][x].combineWithEmpty(board[i][x-1]);
x--;
}
if(board[i][j].number == board[i][j-1].number){
board[i][j].combine(board[i][j-1]);
}
}
}
reActivate();
}
/*reActivate() sets the isActive attribute of each tile to true so that the
tile can recombine with other tiles in the future.*/
public static void reActivate(){
for(int i = 0; i<board.length; i++){
for(int j = 0; j< board[0].length; j++){
board[i][j].isActive = true;
}
}
}
}