Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Why does this code output that the rows are invalid and how can I fix it? //TwoDimensional2048.java public class TwoDimensional2048 implements TwoDimensional{ public static void

Why does this code output that the rows are invalid and how can I fix it?

//TwoDimensional2048.java

public class TwoDimensional2048 implements TwoDimensional{

public static void main(String[] args) {

int[][] b, br, brc;

int[][] b2;

int tmp;

int[][] initb = {

{0,2,0,0,2},

{0,2,0,0,0},

{0,2,0,2,0},

{0,2,0,2,2},

{2,0,2,0,0}};

int[][] lb = {

{4,0,0,0,0},

{2,0,0,0,0},

{4,0,0,0,0},

{4,2,0,0,0},

{4,0,0,0,0}};

int[][] ub = {

{2,4,2,4,4},

{0,4,0,0,0},

{0,0,0,0,0},

{0,0,0,0,0},

{0,0,0,0,0}};

int[][] rb = {

{0,0,0,0,4},

{0,0,0,0,2},

{0,0,0,0,4},

{0,0,0,2,4},

{0,0,0,0,4}};

int[][] db = {

{0,0,0,0,0},

{0,0,0,0,0},

{0,0,0,0,0},

{0,4,0,0,0},

{2,4,2,4,4}};

int[][] multb = {

{0,0,0,0,0},

{0,0,0,0,0},

{0,0,0,0,0},

{0,0,0,0,4},

{0,0,0,0,16}};

int[][] bprerot = {

{0,0,2,2},

{0,2,2,0},

{0,2,0,2},

{2,0,0,0},

{0,2,0,2},

{0,0,0,2},

{0,0,0,0}};

int[][] brot1 = {

{2,0,2,0,2,2,0},

{2,2,0,0,0,0,0},

{0,2,2,0,2,0,0},

{0,0,0,2,0,0,0}};

int[][] brot3 = {

{0,0,0,2,0,0,0},

{0,0,2,0,2,2,0},

{0,0,0,0,0,2,2},

{0,2,2,0,2,0,2}};

TwoDimensional2048 T =new TwoDimensional2048();

b = T.blankBoard(5, 5);

assert(T.validateBoard(b));

for (int i = 0 ; i < 10 ; i++) {

T.addNewValue(b);

}

assert(T.validateBoard(b));

tmp = b[1][4];

b[1][4] = 33;

assert(!T.validateBoard(b));

b[1][4] = tmp;

assert(T.identicalBoard(initb, b));

brc = T.blankBoard(5, 6);

brc[0][0] = 2;

brc[1][1] = 2;

brc[2][2] = 2;

brc[3][3] = 2;

brc[4][4] = 2;

brc[4][5] = 2;

int[][] coordAnswers = {

{0, 1},

{3, 2},

{2, 0},

{0, 4}};

for (int i = 0 ; i < 4 ; i++) {

int[] coord = T.randCoord(brc, (i * 41)%T.numUnoccupied(brc));

assert(coord[0] == coordAnswers[i][0] &&

coord[1] == coordAnswers[i][1]);

}

// Check rotation.

br = T.blankBoard(7, 4);

for (int i = 0 ; i < 10 ; i++) {

T.addNewValue(br);

}

assert(T.validateBoard(br));

assert(T.identicalBoard(br, bprerot));

br = T.rotateLeft(br);

assert(T.identicalBoard(br, brot1));

br = T.rotateLeft(T.rotateLeft(br));

assert(T.identicalBoard(br, brot3));

b2 = b;

b = T.left(b2);

assert(T.identicalBoard(lb, b));

b = T.up(b2);

assert(T.identicalBoard(ub, b));

b = T.right(b2);

assert(T.identicalBoard(rb, b));

b = T.down(b2);

assert(T.identicalBoard(db, b));

b = b2;

b = T.left(b);

b = T.up(b);

b = T.right(b);

b = T.down(b);

b = T.right(b);

b = T.down(b);

assert(T.identicalBoard(multb, b));

System.out.println("Victory!");

}

public boolean validateBoard(int[][] b){

OneDimensional2048 J = new OneDimensional2048();

int rows = b.length;

int entries = 0;

if(b.length <= 0)

{

return false;

}

for(int i=0; i

{

if(!J.validateRow(b[i]))

{

System.out.println("Error: Row enteries are not valid.");

return false;

}

}

for(int j=0; j

{

entries = entries + b[j].length;

if(entries % b[j].length != 0)

{

System.out.println("Error: The rows are not equal in length.");

return false;

}

}

if((entries / b[0].length) != rows)

{

return false;

}

return true;

}

public int[][] blankBoard(int x, int y) {

int[][] board = new int[x][y];

return board;

}

public boolean identicalBoard(int[][] m, int[][] n) {

int q = 0;

if(!validateBoard(m) && !validateBoard(n))

{

System.out.println("fAIL: validate Board");

return false;

}

if(m.length != n.length)

{

System.out.println("fAIL: rows not equal");

return false;

}

if(m[q].length != n[q].length)

{

System.out.println("fAIL: cols not equal");

return false;

}

for(int i=0; i

{

for(int j=0; j

{

if(m[i][j] != n[i][j])

{

System.out.println("Error: The boards are not equal. At row " + i + " and col " + j + " Val: " + m[i][j] + " " + n[i][j]);

return false;

}

}

}

return true;

}

public int numUnoccupied(int[][] b) {

int count = 0;

for(int i=0; i

{

for(int j=0; j

{

if(b[i][j] == 0)

{

count++;

}

}

}

return count;

}

public int[] randCoord(int[][] b, int offset) {

int offsetCnt = -1;

int[] rtnCoordinate = new int[2];

int a = numUnoccupied(b);

if(a < offset)

{

System.out.println("Error: Offset is larger than number of avaliable spaces");

return new int[] {-1,-1};

}

if(offset<0)

{

System.out.println("Error: Offset is less than 0");

return new int[] {-1,-1};

}

for(int i=0; i

{

for(int j=0; j

{

if(b[i][j] == 0)

{

offsetCnt++;

if(offsetCnt == offset)

{

rtnCoordinate[0] = i;

rtnCoordinate[1] = j;

return rtnCoordinate;

}

}

}

}

return new int[]{-1, -1};

}

public boolean addNewValue(int[][] b) {

int val = Rnd2048.randValue();

int offset = Rnd2048.randNum(numUnoccupied(b));

int[] coord = new int[2];

coord = randCoord(b, offset);

int coord1 = coord[0];

int coord2 = coord[1];

b[coord1][coord2] = val;

return true;

}

public int[][] combineLeft(int[][] b) {

OneDimensional2048 J = new OneDimensional2048();

int[][] lol = copyBoard(b);

for(int i=0; i

{

J.combineLeft(b[i]);

}

return b;

}

public int[][] rotateLeft(int[][] b) {

int row = b.length;

int col = b[0].length;

int colP = col - 1;

int[][] WOW = new int[col][row];

for(int i=0; i

{

for(int j=0; j

{

WOW[colP-j][i] = b[i][j];

}

}

return WOW;

}

public int[][] left(int[][] b) {

int[][] temp = new int[b.length][b[0].length];

for(int i=0; i

{

for(int j=0; j

{

temp[i][j] = b[i][j];

}

}

temp = combineLeft(temp);

return temp;

}

public int[][] right(int[][] b) {

b = rotateLeft(b);

b = rotateLeft(b);

b = combineLeft(b);

b = rotateLeft(b);

b = rotateLeft(b);

return b;

}

public int[][] up(int[][] b) {

b = rotateLeft(b);

b = combineLeft(b);

b = rotateLeft(b);

b = rotateLeft(b);

b = rotateLeft(b);

return b;

}

public int[][] down(int[][] b) {

b = rotateLeft(b);

b = rotateLeft(b);

b = rotateLeft(b);

b = combineLeft(b);

b = rotateLeft(b);

return b;

}

public int numMax(int[][] b){

return 0;

}

public int numOccupied(int[][] b){

return 0;

}

public boolean addValue(int[][] b, int x, int y,int val){

return true;

}

public int[][] copyBoard(int[][] b){

int[][] copy = new int[b.length][b[0].length];

for(int i=0; i

{

for(int j=0; j

{

copy[i][j] = b[i][j];

}

}

return copy;

}

public void printBoard(int[][] b){

for(int i=0; i

{

System.out.println("");

for(int j=0; j

{

System.out.print(b[i][j]);

}

}

}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//TwoDimensional.java

public interface TwoDimensional {

boolean validateBoard(int[][] b);

public int[][] blankBoard(int x, int y);

public boolean identicalBoard(int[][] a, int[][] b);

public int numUnoccupied(int[][] b);

public int[] randCoord(int[][] b, int offset);

public boolean addNewValue(int[][] b);

public int[][] combineLeft(int[][] b);

public int[][] rotateLeft(int[][] b);

public int[][] left(int[][] b);

public int[][] right(int[][] b);

public int[][] up(int[][] b);

public int[][] down(int[][] b);

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// OneDimensional2048.java

public class OneDimensional2048 implements OneDimensional {

public boolean identicalRows(int[] row1, int[] row2) {

//your code here

if (row1.length != row2.length) {

return false;

}

int flag = 0;

for (int i = 0; i < row1.length; i++) {

if (row1[i] == row2[i]) {

flag = flag + 1;

}

}

if (flag == row1.length) {

return true;

}

return false;

}

public boolean validateValue(int val, int maxPowerOfTwo) {

int thresh = 0;

if (val == 0) {

return true;

}

for (int i = 1; i < 12; i++) {

thresh = 2;

for (int j = 1; j < i; j++) {

thresh = thresh * 2;

}

if ((val == thresh) && (val <= maxPowerOfTwo)) {

return true;

}

}

return false;

}

public boolean validateRow(int[] row) {

OneDimensional2048 d = new OneDimensional2048();

int max = 2048;

for (int i = 0; i < row.length; i++) {

if (!d.validateValue(row[i], max)) {

return false;

}

}

return true;

}

public boolean moveLeft(int[] row) {

OneDimensional2048 d = new OneDimensional2048();

int l = row.length;

if (!d.validateRow(row)) {

return false;

}

int cnt1 = 0;

int cnt2 = 0;

for (int i = 0; i < l; i++) {

if (row[i] != 0) {

row[cnt1] = row[i];

cnt1++;

} else {

cnt2++;

}

}

int disp = l - cnt2;

for (int j = 0; j < cnt2; j++) {

row[disp + j] = 0;

}

return true;

}

public boolean combineLeft(int[] row) {

OneDimensional2048 d = new OneDimensional2048();

if (!d.validateRow(row)) {

return false;

}

d.moveLeft(row);

for (int i = 0; i < (row.length - 1); i++) {

if (row[i] == row[i + 1]) {

row[i] = 2 * row[i];

for (int j = 1; j < (row.length - i - 1); j++) {

row[i + j] = row[i + j + 1];

}

row[row.length - 1] = 0;

}

}

return true;

}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//OneDimensional.java

public interface OneDimensional {

public boolean identicalRows(int[] row1, int[] row2);

public boolean validateValue(int val, int maxPowerOfTwo);

public boolean validateRow(int[] row);

public boolean moveLeft(int[] row);

public boolean combineLeft(int[] row);

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//Rnd2048.java

public class Rnd2048 {

public static int randValue() {

return 2;

}

private static int x = 7, y = 3, z = 127, w = 2345;

public static int randNum(int ceil) {

if (ceil == 0)

{

return 0;

}

int t = x ^ (x << 11);

x = y;

y = z;

z = w;

w = w^(w>>19)^t^(t>>8);

return w % ceil;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions