Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

pls help me do this by java,thx so much testphase1: testphase1: import java.util.Arrays; public class TestA2Phase1 { //Test only the alterOneLine method in Phase 1

pls help me do this by java,thx so muchimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedtestphase1:

image text in transcribed

testphase1:

import java.util.Arrays;

public class TestA2Phase1 { //Test only the alterOneLine method in Phase 1 of Assignment 2. //The test inputs to be run through the alterOneLine method private static int[][] testCases = { {0,2,0,0,8,0,4}, {0,0,0,0,8,2,4}, {2,4,2,0,0,0,0}, {2,0,2,4,0,0,4}, {2,0,4,2,0,0,4}, {2,0,2,2,0}, {2,0,2,2,0,2}, {2,0,2,4,0,8} };

public static void main(String[] args) { //run all the tests 1 time for(int i=0; i

testphase2:

public class TestA2Phase2 { //Test the simple constructor, the toString method, and the shift method //The others are tested indirectly by these. public static void main(String[] args) { //Start with the same test board. Board2048 test = new Board2048(new int[][]{{2,0,2,4},{0,0,0,8},{2,2,2,2},{0,8,0,8}}); //Store the results of each shift in separate variables Board2048 test2,test3,test4,test5; //Test the constructor and also toString System.out.println("Blank 5x5 board: "+new Board2048(5)); System.out.println("Blank 2x2 board: "+new Board2048(2)); System.out.println("Test board is: "+test); //Test shift in all 4 directions System.out.println("After shifting LEFT: "+(test2=test.shift(Board2048.LEFT))); System.out.println("THEN shifting RIGHT: "+(test3=test2.shift(Board2048.RIGHT))); System.out.println("THEN shifting DOWN: "+(test4=test3.shift(Board2048.DOWN))); System.out.println("THEN shifting UP: "+(test5=test4.shift(Board2048.UP))); //Make sure the original remains untouched System.out.println("Original board should not have changed: "+test); } }

Phase 1: Shifting One Line The 2048 board is a square 2-dimensional grid of "tiles". Each place in the grid might be empty, or contain a tile showing a number that is a power of 2 (2, 4, 8, 16, etc.). In this assignment, a simple int[] array will be used, with the value 0 representing an empty place The main operation involved in playing the game is to shift a line of tiles in some direction. In Phase 1, only a 1 dimensional line of tiles will be used, and the tiles will only be shifted to the left (from higher index values toward 0) Define a Board2048 class. For now, it should contain only a public static boolean alterOneLine(int[]) method. This method should accept an int[] array representing one line of tiles, and alter the array according to the rules and examples given below. It should return true if the array was changed in any way, and false if it remained exactly the same as it was before. It should handle any size of array. The rules can be implemented and tested one at a time Rule 1: All the tiles should be shifted to the left as far as they will go. (Empty spaces (O's) do not count.) For example > int[] test = {0,2,0,0,8,0,4); > Board2048.alterOneLine(test) true test {2,8,4,e, > int[] test e,e,} {,0,0,0,8,2,4); Board2048.alterOneLine(test) true test {8,2,4,0, , , } > int[] test (2,4,2,0,0,0,0); >Board2048.alterOneLine (test) false (the line did not change) test (2, 4, 2, e, , e, e} Phase 1: Shifting One Line The 2048 board is a square 2-dimensional grid of "tiles". Each place in the grid might be empty, or contain a tile showing a number that is a power of 2 (2, 4, 8, 16, etc.). In this assignment, a simple int[] array will be used, with the value 0 representing an empty place The main operation involved in playing the game is to shift a line of tiles in some direction. In Phase 1, only a 1 dimensional line of tiles will be used, and the tiles will only be shifted to the left (from higher index values toward 0) Define a Board2048 class. For now, it should contain only a public static boolean alterOneLine(int[]) method. This method should accept an int[] array representing one line of tiles, and alter the array according to the rules and examples given below. It should return true if the array was changed in any way, and false if it remained exactly the same as it was before. It should handle any size of array. The rules can be implemented and tested one at a time Rule 1: All the tiles should be shifted to the left as far as they will go. (Empty spaces (O's) do not count.) For example > int[] test = {0,2,0,0,8,0,4); > Board2048.alterOneLine(test) true test {2,8,4,e, > int[] test e,e,} {,0,0,0,8,2,4); Board2048.alterOneLine(test) true test {8,2,4,0, , , } > int[] test (2,4,2,0,0,0,0); >Board2048.alterOneLine (test) false (the line did not change) test (2, 4, 2, e, , e, e}

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

Students also viewed these Databases questions