My codes how to fix moveUp etc. partsWhen I use it the number doesn\'t move at a
ID: 3810540 • Letter: M
Question
My codes
how to fix moveUp etc. partsWhen I use it the number doesn't move at all.
Part 4 - Moving Pieces
Problem
Shifting pieces around and combining them is what makes the game so fun! The moveTo method receives four parameters, which are two sets of coordinates. The first pair are coordinates of the cell from which we are trying to move, while the second pair are the coordinates of the cell to which we are trying to move. Since moveTo is a helper function for later overall board functions, moveTo will only move or merge a piece at most one space in any direction (i.e. the from piece and the to piece should be adjacent).
2048 has the following rules when it comes to shifting adjacent pieces:
If the to piece or the from piece is out of bounds, you cannot shift, so do nothing.
If the to piece and the from piece are not adjacent, do nothing.
If the from piece is a 0, do nothing (nothing to add/move)
If the from piece and the to piece have the same value, shift the from piece into the to space, by doubling the to space's value and reducing from to zero.
If the to piece is zero, simply move the from piece into the to space.
Preparation
Think about what values are valid for each of the four parameters to have.
Procedure
Implement the moveTo method. This method returns true if a piece was moved, otherwise it returns false.
Check the bounds of all the variables (both for adjacency and for the bounds of the board itself)
If the cell we are moving from is 0, return false
If the to cell is 0, shift the value in the from cell to the to cell and erase the from cell.
If the values in from and to are the same, add them, place the added value in to, and clear the from value.
If the cell values are not equal and greater than zero, return false.
Part 5 - Move (up|right|down|left)
Problem
If you play 2048, you can see that single pieces do not just move to the right when you send the “move right” command, but rather the entire board. Now that we can move pieces, the next step is to write functions that move an entire board in one direction (merging when necessary). The moveUp, moveDown, moveLeft, and moveRight functions will do this operation on the entire board, using the moveTo helper function to make moving cells more manageable.
Preparation
Think about how the board is represented in your program.
Think about how moveTo() can help you accomplish this task.
Procedure
Implement moveUp, moveDown, moveLeft, and moveRight. The procedure to move an entire board in direction X is as follows:
For each cell C in a row or column (depending on move direction), beginning with the cell furthest in the direction X, move or merge that cell one space in direction X unless either:
C cannot be moved due to either its value being zero or it being adjacent to a cell with a different value.
C cannot be moved because it has reached the board boundary.
After all moves are completed, determine if a cell was created with a higher value than the current score. If so, update the score variable with the highest value on the board.
If at least one move was made, return true. Otherwise, return false.
Example
Note: moveX() only moves all pieces on the board at most 1 space in that direction. A single call to moveRight(), for example, will cause the following change to occur in the below example (note the order in which the merge happens, starting with the cell furthest to the right in the row, since this is a moveRight() call):
5. If the cell values are not equal and greater than zero, return false. added value in to, and clear the from value. Part 5 Move (uplrightldownlleft) Problem If you play 2048, you can see that single pieces do not just move to the when you send the move r command, but rather the entire board. Now that we can move pieces, right moveRight functions next step is to write that move an entire board inone ng when necessary). The moveup. moveDown, moveLeft, and wil do this operation on the entie board, using the moveTo helper function to make moving cells more manageable. Preparation Think about how the board is represented in your program. Think about how moveT can help you accomplish this task Procedure Implement move moveDown, moveLeft. and moveRight. The procedure to move an entire board in direction xis as follows: 1. For each cell c in a row or column depending on move direction). beginning with the cell furthest in the direction x move or merge that cel one space in direction X unless either: a C cannot be moved due to either its value being zero oritbeing adjacent to a cell with a different value. b. C cannot be moved because it has reached the board boundary 2. After all moves are completed, determine a cell was created with a higher value than the current score. If so, update the score variable with the highest value on the board. 3. If at least one move was made, return true. Otherwise, return false. Example Note: movex0 only moves all pieces on the board at most 1 space in that direction A single call to moveRight0. for example, will cause the following change to occur in the below example (note the order in which the merge happens, starting with the cell furthest to the right in the row, since this is a noveRight() call: l 4 l 21 21 I IExplanation / Answer
See, the problem that board number doesn't move at all is because all the functions at returns fals at this moveTo call
[c = b||moveTo(i,j,i+1,j);}]. The problem is that the function is returning false because you fail to define width , and dimesnsions of the board properly, random is a random function. The solutuion is to define dimension and place number in a limited range.
For that, I would recommend to use limited range random fuvtion.
Ex: instead of using:
Random rand = new Random();
int r = rand.nextInt(boardWidth);
TRY to USE THIS RANGED RANDOM LIKE THIS:
int x = maxRange - minRange + 1; // x is your boardWidth
int r = rand.nextInt() % x;
r = minimum + i;
Try and It will solve the problem.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.