-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
128 lines (111 loc) · 3.59 KB
/
Board.java
File metadata and controls
128 lines (111 loc) · 3.59 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************
| Copyright 2016 Stephen Smith. All rights reserved. |
*******************************************************************************/
package tetrisjava;
/**
* The Tetris game board
* @author Stephen Smith
* @version 1.1
*/
public class Board {
/**
* The height of the board
*/
public final static int BoardHeight = 20;
/**
* The width of the board
*/
public final static int BoardWidth = 11;
private int[][] BoardArray = new int[20][11];
/**
* Creates a new board instance
*/
public Board() {
}
/**
* Returns an array represnetion of the board, a blank space on the board is 0.
* @return An array represnetion of the board
*/
public int[][] getBoardArray(){
return BoardArray;
}
/**
* Places a Tetris game piece on the game board
* @param Piece An 2d array (usally a representation of a piece) to place on the board
* @param R The Row the piece will be placed
* @param C The Column the piece will be placed
* @param test True, if you only want to test if a piece can be placed
* on the board in the definded row/column position.
* False if you would actually like to place the piece there
* (will become part of the board)
* @return
*/
public int placePiece(int[][] Piece, int R, int C,boolean test){
int OrigonalC = C;
for (int r = 0;r < Piece.length;r++){
for (int c = 0;c < Piece[0].length;c++){
try{
if (this.BoardArray[R][C] != 0 && Piece[r][c] != 0){
return 1;
}else{
if (test == false){
this.BoardArray[R][C] |= Piece[r][c];
}
C++;
}
}catch (java.lang.ArrayIndexOutOfBoundsException ex){return 1;}
}
R++;
C = OrigonalC;
}
return 0;
}
private void removeLine(int y) {
if (y < 0 || y >= BoardHeight) {
return;
}
for (; y > 0; y--) {
for (int x = 0; x < BoardWidth; x++) {
BoardArray[y][x] = BoardArray[y - 1][x];
}
}
for (int x = 0; x < BoardWidth; x++) {
BoardArray[0][x] = 0;
}
}
/**
* Checks to see if the games over (a placed piece is on the top row)
* I may change this
* @return True if game is over, false if it is not
*/
public boolean gameOver(){
for (int i = 0; i < BoardArray[0].length; i++){
if (BoardArray[0][i] != 0){
return true;
}
}
return false;
}
/**
* Calculates the number of rows completed, and removes these rows.
* Also, adds new blank rows to the top of the board.
* @return The number of rows completed after the last call to the
*/
public int calculatePoints(){
int RowsComplete = 0;
int CellsInRow = 0;
for (int r = 0;r < BoardArray.length;r++){
for (int c = 0;c < BoardArray[0].length;c++){
if (BoardArray[r][c] != 0){
CellsInRow++;
}
}
if (CellsInRow >= BoardArray[0].length){
removeLine(r);
RowsComplete++;
}
CellsInRow = 0;
}
return RowsComplete;
}
}