publicstaticbooleanisLeapYear(int year){ // TODO: Fill in this method. return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); }
Proj0: 2048
This constructor should create a board of size size and set the instance variables for the start of the game.
This constructor creates a new instance of the game with a Board state that reflects the given rawValues array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** A new 2048 game on a board of size SIZE with no pieces * and score 0. */ publicModel(int size){ // TODO: Fill in this constructor. this._board = new Board(size); }
/** A new 2048 game where RAWVALUES contain the values of the tiles * (0 if null). VALUES is indexed by (row, col) with (0, 0) corresponding * to the bottom-left corner. Used for testing purposes. */ publicModel(int[][] rawValues, int score, int maxScore, boolean gameOver){ // TODO: Fill in this constructor. this._board = new Board(rawValues, score); this._score = score; this._maxScore = maxScore; this._gameOver = gameOver; }
Now, let’s look at the first three methods, which we can use to check certain properties of the board.
public static boolean emptySpaceExists(Board b)
Use double loop to find out whether exist a tile’s value is empty or not
1 2 3 4 5 6 7 8 9 10 11 12
publicstaticbooleanemptySpaceExists(Board b){ // TODO: Fill in this function. int size = b.size(); for(int i = 0; i < size; i ++ ) { for(int j = 0; j < size; j ++ ) { if(b.tile(i, j) == null) { returntrue; } } } returnfalse; }
public static boolean maxTileExists(Board b)
Use double loop to find out whether exist a tile has the max_piece value or not
1 2 3 4 5 6 7 8 9 10 11
publicstaticbooleanmaxTileExists(Board b){ // TODO: Fill in this function. for(int i = 0; i < b.size(); i ++ ) { for(int j = 0; j < b.size(); j ++ ) { if(b.tile(i, j) != null && b.tile(i, j).value() == MAX_PIECE) { returntrue; } } } returnfalse; }
public static boolean atLeastOneMoveExists(Board b)
convert the perspective of the view when we begin to move the tile, this step can help us from considering four directions’ movements to only consider how to move the tilt up
1
_board.setViewingPerspective(side);
At the end of the movement, we can recover the perspective of the board
1
_board.setViewingPerspective(Side.NORTH);
Move all the tiles to make them adjacent
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
for (int row = size - 1; row >= 0; row -- ) { Tile t = _board.tile(col, row); if (t != null) { // find nextPos which is null int nextPos = 3; while (nextPos >= row) { if (_board.tile(col, nextPos) == null) { break; } nextPos -- ; } // check if nextPos is a legal position if (nextPos >= row) { _board.move(col, nextPos, t); changed = true; } }
merge the tiles if they have the same value between one and the one next to it
for (int row = 3; row >= 0; row -- ) { Tile curTile = _board.tile(col, row); // find out the next row's tile int nextLine = row - 1; if (nextLine < 0) { break; } Tile nextTile = _board.tile(col, nextLine); // if one of the two tile is null we break this loop if (curTile == null || nextTile == null) { break; } int nextValue = nextTile.value(); if (nextValue == curTile.value()) { // merge the two tiles whose value are equaled _board.move(col, row, nextTile); _score += curTile.value() * 2; // move the tiles behind the two merged tiles to the place where the second tiles was for (int p = nextLine - 1; p >= 0; p -- ) { Tile tile = _board.tile(col, p); if (tile == null) { break; } if (p < size) { _board.move(col, p + 1, tile); } } changed = true; } }
// TODO: Fill in this function. // set the viewing perspective to make the operations more convenient _board.setViewingPerspective(side); int size = _board.size(); for (int col = 0; col < size; col ++ ) { // move all the tiles to make them adjacent for (int row = size - 1; row >= 0; row -- ) { Tile t = _board.tile(col, row); if (t != null) { // find nextPos which is null int nextPos = 3; while (nextPos >= row) { if (_board.tile(col, nextPos) == null) { break; } nextPos -- ; } // check if nextPos is a legal position if (nextPos >= row) { _board.move(col, nextPos, t); changed = true; } } }
// Step2. try to merge // [2, 2, x, x] -> [4, x, x, x] for (int row = 3; row >= 0; row -- ) { Tile curTile = _board.tile(col, row); // find out the next row's tile int nextLine = row - 1; if (nextLine < 0) { break; } Tile nextTile = _board.tile(col, nextLine); // if one of the two tile is null we break this loop if (curTile == null || nextTile == null) { break; } int nextValue = nextTile.value(); if (nextValue == curTile.value()) { // merge the two tiles whose value are equaled _board.move(col, row, nextTile); _score += curTile.value() * 2; // move the tiles behind the two merged tiles to the place where the second tiles was for (int p = nextLine - 1; p >= 0; p -- ) { Tile tile = _board.tile(col, p); if (tile == null) { break; } if (p < size) { _board.move(col, p + 1, tile); } } changed = true; } } } _board.setViewingPerspective(Side.NORTH);
checkGameOver(); if (changed) { setChanged(); } return changed; }
/** Returns the balance for the current account. */ publicintgetBalance(){ return balance; }
/** Deposits amount into the current account. */ publicvoiddeposit(int amount){ if (amount < 0) { System.out.println("Cannot deposit negative amount."); } else { balance += amount; } }
/** * Subtract amount from the account if possible. If subtracting amount * would leave a negative balance, print an error message and leave the * balance unchanged. */ publicbooleanwithdraw(int amount){ // TODO if (amount < 0) { System.out.println("Cannot withdraw negative amount."); returnfalse; } elseif (amount < balance) { balance -= amount; } elseif (parent != null && balance + parent.balance >= amount) { amount -= balance; balance = 0; parent.balance -= amount; } else { System.out.println("Insufficient Funds."); returnfalse; } returntrue; }
/** * Merge account other into this account by removing all money from other * and depositing it into this account. */ publicvoidmerge(Account other){ // TODO if (other == null) { System.out.println("Cannot merge null account."); } else { balance += other.balance; other.balance = 0; } } }
publicclassDateConverter{ /** * Given a day number in 2021, an integer between 1 and 365, as a * command-line argument, prints the date in month/day format. * * java DateConverter 365 * * should print 12/31 */ publicstaticvoidmain(String[] args){ int dayOfYear = 0; try { dayOfYear = Integer.parseInt(args[0]); } catch (NumberFormatException e) { e.printStackTrace(); }
int month, dateInMonth, daysInMonth; month = 1; daysInMonth = 31; while (dayOfYear > daysInMonth) { // TODO: Here is one place to put assignment statements. if (month == 2) { daysInMonth = 28; } elseif (month == 4 || month == 6 || month == 9 || month == 11) { daysInMonth = 30; } else { daysInMonth = 31; } // TODO: Here is another possible place to put assignment statements. // System.out.println(month + "/" + daysInMonth + "/" + dayOfYear);
publicclassArrayOperations{ /** * Delete the value at the given position in the argument array, shifting * all the subsequent elements down, and storing a 0 as the last element of * the array. */ publicstaticvoiddelete(int[] values, int pos){ if (pos < 0 || pos >= values.length) { return; } // TODO: YOUR CODE HERE for (int i = pos; i < values.length - 1; i ++ ) { values[i] = values[i + 1]; } values[values.length - 1] = 0; }
/** * Insert newInt at the given position in the argument array, shifting all * the subsequent elements up to make room for it. The last element in the * argument array is lost. */ publicstaticvoidinsert(int[] values, int pos, int newInt){ if (pos < 0 || pos >= values.length) { return; } // TODO: YOUR CODE HERE for (int i = values.length - 1; i > pos; i -- ) { values[i] = values[i - 1]; } values[pos] = newInt; }
/** * Returns a new array consisting of the elements of A followed by the * the elements of B. */ publicstaticint[] catenate(int[] A, int[] B) { // TODO: YOUR CODE HERE int[] C = newint[A.length + B.length]; int index = 0, indexA = 0, indexB = 0; while(indexA < A.length) { C[index ++ ] = A[indexA ++]; } while(indexB < B.length) { C[index ++ ] = B[indexB ++]; } return C; } }