Untitled documentpackage game;import java.util.ArrayList;/*** 2048 Board* Methods to complete:* updateOpenSpaces(), addRandomTile(), swipeLeft(), mergeLeft(),* transpose(), flipRows(),...

1 answer below »
Check out the assignment


Untitled document package game; import java.util.ArrayList; /** * 2048 Board * Methods to complete: * updateOpenSpaces(), addRandomTile(), swipeLeft(), mergeLeft(), * transpose(), flipRows(), makeMove(char letter) * * @author Kal Pandit * @author Ishaan Ivaturi **/ public class Board { private int[][] gameBoard; // the game board array private ArrayList openSpaces; // the ArrayList of open spots: board cells without numbers. /** * Zero-argument Constructor: initializes a 4x4 game board. **/ public Board() { gameBoard = new int[4][4]; openSpaces = new ArrayList<>(); } /** * One-argument Constructor: initializes a game board based on a given array. * * @param board the board array with values to be passed through **/ public Board ( int[][] board ) { gameBoard = new int[board.length][board[0].length]; for ( int r = 0; r < gameboard.length;="" r++="" )="" {="" for="" (="" int="" c="0;" c="">< gameboard[r].length;="" c++="" )="" {="" gameboard[r][c]="board[r][c];" }="" }="" openspaces="new"><>(); } /** * 1. Initializes the instance variable openSpaces (open board spaces) with an empty array. * 2. Adds open spots to openSpaces (the ArrayList of open BoardSpots). * * Note: A spot (i, j) is open when gameBoard[i][j] = 0. * * Assume that gameBoard has been initialized. **/ public void updateOpenSpaces() { // WRITE YOUR CODE HERE } /** * Adds a random tile to an open spot with a 90% chance of a 2 value and a 10% chance of a 4 value. * Requires separate uses of StdRandom.uniform() to find a random open space and determine probability of a 4 or 2 tile. * * 1. Select a tile t by picking a random open space from openSpaces * 2. Pick a value v by picking a double from 0 to 1 (not inclusive of 1); < .1="" means="" the="" tile="" is="" a="" 4,="" otherwise="" 2="" *="" 3.="" update="" the="" tile="" t="" on="" gameboard="" with="" the="" value="" v="" *="" *="" note:="" on="" the="" driver="" updateopenstapes()="" is="" called="" before="" this="" method="" to="" ensure="" that="" openspaces="" is="" up="" to="" date.="" **/="" public="" void="" addrandomtile()="" {="" write="" your="" code="" here="" }="" **="" *="" swipes="" the="" entire="" board="" left,="" shifting="" all="" nonzero="" tiles="" as="" far="" left="" as="" possible.="" *="" maintains="" the="" same="" number="" and="" order="" of="" tiles.="" *="" after="" swiping="" left,="" no="" zero="" tiles="" should="" be="" in="" between="" nonzero="" tiles.="" *="" (ex:="" 0="" 4="" 0="" 4="" becomes="" 4="" 4="" 0="" 0).="" **/="" public="" void="" swipeleft()="" {="" write="" your="" code="" here="" }="" **="" *="" find="" and="" merge="" all="" identical="" left="" pairs="" in="" the="" board.="" ex:="" "2="" 2="" 2="" 2"="" will="" become="" "2="" 0="" 2="" 0".="" *="" the="" leftmost="" value="" takes="" on="" double="" its="" own="" value,="" and="" the="" rightmost="" empties="" and="" becomes="" 0.="" **/="" public="" void="" mergeleft()="" {="" write="" your="" code="" here="" }="" **="" *="" rotates="" 90="" degrees="" clockwise="" by="" taking="" the="" transpose="" of="" the="" board="" and="" then="" reversing="" rows.="" *="" (complete="" transpose="" and="" fliprows).="" *="" provided="" method.="" do="" not="" edit.="" **/="" public="" void="" rotateboard()="" {="" transpose();="" fliprows();="" }="" **="" *="" updates="" the="" instance="" variable="" gameboard="" to="" be="" its="" transpose.="" *="" transposing="" flips="" the="" board="" along="" its="" main="" diagonal="" (top="" left="" to="" bottom="" right).="" *="" *="" to="" transpose="" the="" gameboard="" interchange="" rows="" and="" columns.="" *="" col="" 1="" becomes="" row="" 1,="" col="" 2="" becomes="" row="" 2,="" etc.="" *="" **/="" public="" void="" transpose()="" {="" write="" your="" code="" here="" }="" **="" *="" updates="" the="" instance="" variable="" gameboard="" to="" reverse="" its="" rows.="" *="" *="" reverses="" all="" rows.="" columns="" 1,="" 2,="" 3,="" and="" 4="" become="" 4,="" 3,="" 2,="" and="" 1.="" *="" **/="" public="" void="" fliprows()="" {="" write="" your="" code="" here="" }="" **="" *="" calls="" previous="" methods="" to="" make="" right,="" left,="" up="" and="" down="" moves.="" *="" swipe,="" merge="" neighbors,="" and="" swipe.="" rotate="" to="" achieve="" this="" goal="" as="" needed.="" *="" *="" @param="" letter="" the="" first="" letter="" of="" the="" action="" to="" take,="" either="" 'l'="" for="" left,="" 'u'="" for="" up,="" 'r'="" for="" right,="" or="" 'd'="" for="" down="" *="" note:="" if="" "letter"="" is="" not="" one="" of="" the="" above="" characters,="" do="" nothing.="" **/="" public="" void="" makemove(char="" letter)="" {="" write="" your="" code="" here="" }="" **="" *="" returns="" true="" when="" the="" game="" is="" lost="" and="" no="" empty="" spaces="" are="" available.="" ignored="" *="" when="" testing="" methods="" in="" isolation.="" *="" *="" @return="" the="" status="" of="" the="" game="" --="" lost="" or="" not="" lost="" **/="" public="" boolean="" isgamelost()="" {="" return="" openspaces.size()="=" 0;="" }="" **="" *="" shows="" a="" final="" score="" when="" the="" game="" is="" lost.="" do="" not="" edit.="" **/="" public="" int="" showscore()="" {="" int="" score="0;" for="" (="" int="" r="0;" r="">< gameboard.length;="" r++="" )="" {="" for="" (="" int="" c="0;" c="">< gameboard[r].length;="" c++="" )="" {="" score="" +="gameBoard[r][c];" }="" }="" return="" score;="" }="" **="" *="" prints="" the="" board="" as="" integer="" values="" in="" the="" text="" window.="" do="" not="" edit.="" **/="" public="" void="" print()="" {="" for="" (="" int="" r="0;" r="">< gameboard.length;="" r++="" )="" {="" for="" (="" int="" c="0;" c="">< gameboard[r].length;="" c++="" )="" {="" string="" g="Integer.toString(gameBoard[r][c]);" stdout.print((g.equals("0"))="" "-"="" :="" g);="" for="" (="" int="" o="0;" o="">< (5="" -="" g.length());="" o++="" )="" {="" stdout.print("="" ");="" }="" }="" stdout.println();="" }="" }="" **="" *="" prints="" the="" board="" as="" integer="" values="" in="" the="" text="" window,="" with="" open="" spaces="" denoted="" by="" "**"".="" used="" by="" textdriver.="" **/="" public="" void="" printopenspaces()="" {="" for="" (="" int="" r="0;" r="">< gameboard.length;="" r++="" )="" {="" for="" (="" int="" c="0;" c="">< gameboard[r].length;="" c++="" )="" {="" string="" g="Integer.toString(gameBoard[r][c]);" for="" (="" boardspot="" bs="" :="" getopenspaces()="" )="" {="" if="" (r="=" bs.getrow()="" &&="" c="=" bs.getcol())="" {="" g="**" ;="" }="" }="" stdout.print((g.equals("0"))="" "-"="" :="" g);="" for="" (="" int="" o="0;" o="">< (5="" -="" g.length());="" o++="" )="" {="" stdout.print("="" ");="" }="" }="" stdout.println();="" }="" }="" **="" *="" seed="" constructor:="" allows="" students="" to="" set="" seeds="" to="" debug="" random="" tile="" cases.="" *="" *="" @param="" seed="" the="" long="" seed="" value="" **/="" public="" board(long="" seed)="" {="" stdrandom.setseed(seed);="" gameboard="new" int[4][4];="" }="" **="" *="" gets="" the="" open="" board="" spaces.="" *="" *="" @return="" the="" arraylist="" of="" boardspots="" containing="" open="" spaces="" **/="" public=""> getOpenSpaces() { return openSpaces; } /** * Gets the board 2D array values. * * @return the 2D array game board **/ public int[][] getBoard() { return gameBoard; } } Connect with Rutgers Rutgers Home Rutgers Today myRutgers Academic Calendar Calendar of Events SAS Events Explore SAS Departments & Degree-Granting Programs Other Instructional Programs Majors & Minors Research Programs, Centers, & Institutes International Programs Division of Life Sciences Explore CS We are Hiring! Research News Events Resources Search CS Home Back to Top Copyright 2020, Rutgers, The State University of New Jersey. All rights reserved. Rutgers is an equal access/equal opportunity institution. Individuals with disabilities are encouraged to direct suggestions, comments, or complaints concerning any accessibility issues with Rutgers web sites to: [email protected] or complete the Report Accessibility Barrier or Provide Feedback Form. Rutgers Home SAS CS Search ... Data Structures Computer Science Department HOME SYLLABUS LECTURES ASSIGNMENTS EXAMS STAFF Play 2048 – 100 course points In this assignment, you will be coding the game 2048 using 2-D arrays. You will practice array manipulation, how to navigate references to objects, and the object-oriented programming (OOP) paradigm. Be sure to start your assignment early – don’t wait until the last minute! You will need time to understand this assignment and the many questions it may present. Refer to our Programming Assignments FAQ for instructions on how to install VSCode, how to use the command line and how to submit your assignments. See this video on how to import the project into VSCode and how to submit into Autolab. Overview 2048 is a puzzle game where you use the arrow keys to move left, right, up or down to merge tiles of the same number together. Check it out if you never played it before. The goal of the game is to keep merging numbered (non-zero) tiles until you get one 2048 tile. If two tiles with the same number touch each other, they are merged together in the direction swiped into a new tile with twice the value. (ex: if you move up, the topmost value doubles and the bottom value becomes 0). Be careful with your moves – if the board is full at the end of a turn and there are no other valid moves, it’s game over! In this assignment, we will represent the 2048 grid as a 4×4 array of integers, with 0’s representing empty tiles. Note that we will grade your individual operations – not your score or ability to get to 2048.  Implementation Overview of files provided We provide two drivers (text and graphic) to help you test the individual methods. We suggest that you start with the text driver but once you have a good understanding of the assignment then you may use the graphic driver. Board: The Board class contains all methods needed to construct a functioning 2048 game. Edit the empty methods with you solution, but DO NOT edit the provided ones or the methods signatures of any method. This is the file you submit. BoardSpot: An object used to represent a location on the board. It houses a row and a column, along with getters and setters. Don’t edit or submit to Autolab. TextDriver: A tool to test your 2048 board interactively using only text-based boards. This driver is equivalent to AnimatedDriver and functions in the same way – you are free to use this driver to test all of your methods. Feel free to edit this class, as it is provided only to help you test. It is not submitted and/or graded. To use this driver, pick whether you want to test individual methods or play the full game (this option requires all methods to be completed) by selecting the number that appears for each option.  To test individual methods, first type in the full file name for an input file. Then, select a method to test by typing in the number that appears for each method in the list. Additionally, for makeMove, moves are represented by using the WASD keys like arrow keys (w is up, a is left, s is down, and d is right). Type in the letter corresponding to the move you want to test. Playing the full game makes use of the WASD keys like arrow keys (W is up, A is left, S is down, and D is right). Press “Q” to quit. AnimatedDriver: A tool to test your 2048 board interactively through rendered images. This driver is equivalent to TextDriver and functions in the same way – you are free to use this driver to test all of your methods. Feel free to edit this class, as it is provided only to help you test. It is not submitted and/or graded. To use this driver, pick whether you want to test individual methods or play the full game (this option requires all methods to be completed) by selecting the number that appears for each option.  To test individual methods, first type in the full file name for an input file. Then, select a method to test by typing in the number that appears for each method in the list. You will then see the board stored in the input file. For all methods besides makeMove, press any key to test your method. Press any key again to exit testing that method. For makeMove, moves are represented by using the WASD keys like arrow keys (w is up, a is left, s is down, and d is right). Type in the letter corresponding to the move you want to test when you see the board stored in the input file. Once you type that letter, the move you want to test will be made. Press any key afterward to exit testing that method. Playing the full game makes use of the WASD keys like arrow keys (w is up, a is left, s is down, and d is right). Press “q” to quit. StdIn and StdOut: libraries to handle input and output. Do not edit these classes. StdRandom: Provides methods for generating random numbers. Don’t edit or submit to Autolab. Collage, Picture, StdDraw: Helper libraries to support AnimatedDriver. Don’t edit or submit to Autolab. Input Files: Preset boards you’re able to use to test your 2048 board in the drivers (intput1.in, input2.in,…). You can use all input files to test all methods. Each input file contains 4 lines, each with 4 space separated numbers. Each number is either 0 (represents an empty space) or a power of 2 (represents a normal tile). Feel free to make your own input files, as they will not be submitted to Autolab. Board.java DO NOT add new import statements. DO NOT change any of the method’s signatures. Methods to be implemented by you: 1. updateOpenSpaces This method adds a BoardSpot to the openSpaces array for every  board open spot. A spot (i, j) is open when gameBoard[i][j] = 0. Initialize a new ArrayList of BoardSpot objects in openSpaces. Add to openSpaces all pairs (row, column) where gameBoard[row][column] is 0 (ie. the tile is empty). Use BoardSpot objects to represent these pairs. Submit Board.java with this method completed under Early Submission to receive extra credit. Note: DO NOT call updateOpenSpaces in any method. The driver/Autolab will call updateOpenSpaces and addRandomTile before making a move. Here is an example of testing this method using input1.in in both drivers. In TextDriver, open spaces in the openSpaces ArrayList are denoted by two asterisks **. Text Driver Animated Driver 2. addRandomTile Note: updateOpenSpaces must be completed before starting this method.
Answered 1 days AfterFeb 08, 2023

Answer To: Untitled documentpackage game;import java.util.ArrayList;/*** 2048 Board* Methods to...

Vikas answered on Feb 10 2023
34 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here