Menu

(Solved) : Part Z Questions Part Assignment Graded Question 1 Game Life 60 Points Question Write Java Q36374858 . . .

Part z The questions in this part of the assignment will be graded Question 1: Game of Life (60 points) For this question, yo5 of 12 method should display the universe using the star character ( for alive cells, and the whitespace character ( for dea1d) Method to create the next generation universe Write a method getNextGenUniverse that takes as input a two-dimensional arrQuestion 2: Blackjack (40 points) For this question, you will write two classes, which you can then use to simulate a game ofpoint of view. This means that the last card in the deck (the one at the bottom of the deck) appears in the first position of

Part z The questions in this part of the assignment will be graded Question 1: Game of Life (60 points) For this question, you will have to write a Java program that implements a simple version of Conway’s Gane Of Life (https: //en.wikipedia.org/wiki/Conway%27s_Game-of-Life) This is a zero-player game, which means that is completely determined by the initial state provided. For our purpose, we will represent the universe of the Game of Life as a finite rectangular two-dimensional array. Each element denotes a cel in the universe: if the cel is alive the value of the elemis 1, if the cell is dead the value of the element is 0. Whether a cell is alive or dead in the next generation of the universe depends on its relation with its neighboring cells (the cells that are horizontally, vertically, or diagonally adjacent to it). The next generation of cel is determined by the following rules: Any live cell with fewer than two live neighbors dies, as if caused by under population. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if caused by overpopulation . Any dead cell with exactly three live neighbors becomes a live, as if caused by reproduction. In this question, you will implement a method that will allow you to display a given number of generations of a specified universe To complete this task, you will need to implement all the methods listed below. All the code for this question must be placed in a file named GameOfLife.java. Note that you are free to write more methods if they help the design or readability of your code Note, that you are not required to write a main method for this question. Use the main method to test all your methods while writing the program. When you hand in your assignment though, make sure that your main method is empty 1a) Method to check whether a 2D array represents a valid universe Write a method isValidUniverse which takes as input a two-dimensional array of integers. The method should return true if such array is a valid representation of a universe, false otherwise. For the purpose of this assignment, we consider a two-dimensional array to be a valid representation of a universe if it is rectangul its sub-arrays are of the same size) and all the elements of its sub-arrays are either Os or 1s. For example, consider the following two-dimensional arrays intO) ato,, 1,0 1,0 int U b to,0,0, 1,1), to intO d 1,1, 1,1 Then: isValidUniverse (a) returns true e isValidUniverse (b) returns false e isValidUniverse (c) returns false e isValidUniverse (d) returns true 1b) Method to display the universe Write a method displayUniverseO which takes a two-dimensional array of integers as input. In this method you can assume that the array is a valid representation of a universe (as described above). The Page 4 5 of 12 method should display the universe using the star character ( for alive cells, and the whitespace character ( for deads. Your method should also display a box (as shown below) surrounding the actual cells of the universe For example, consider the following arrays: int block-tto,0,0,, to,1,1,0, o,1,1,0, t0,0,0,0 int[][] tub= {{0,0,0,0,0}, {0,0,1,0,0), {0,1,0,1,0. {0,0,1,0,0}, {0,0,0,0,0}); int O toad to,0,0,0,0,0, to,0,1,1,1,0, (0,1,,1,0,0, 0,0,0,0,0,0; Then: . displayUniverse (block) . displayUniverse (tub) displayUniverse(toad) prints nts prints 1c) Method to get the cell from the next generation Write a method getNextGenCell that takes as input a two-dimensional array of integers representing a valid universe, as well as two integers x and y indicating the location of a specific cell. For example, if x 2 and y-1, the cell analyzed is the one represented by the second element of the third sub-array. The method returns 1 if such cell will be alive in the next generation of the given universe, O otherwise. Remember that the next generation of cells s determined by the following rules . Any live cell with fewer than two live neighbors dies, as if caused by under population Any live cell with two or three live neighbors lives on to the next generation Any live cell with more than three live neighbors dies, as if caused by overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if caused by reproduction. For example, consider the following arrays: int®[] beehive# {{0,0,0,0,0,0}, {0,0,1,1,0,0}, {0,1,0,0,1,0}, {0,0,1,1,0,0}, int®[] toad= {{0,0,0,0,0,0}, {0,0,1,1,1,0}, {0,1,1,1,0,0}, {0,0,0,0,0,0}); Then: (0,0,0,0,0,0 getNextGenCell(beehive, 1, 3) returns 1, while getNextGenCell (beehive, 3, 1) returns 0 getNextGenCell(toad, 0, 3) returns 1, while getNextGenCell (toad, 2, 3) retums 0 Note that this method must not modify the input array in any way Page 5 1d) Method to create the next generation universe Write a method getNextGenUniverse that takes as input a two-dimensional array of integers represent- ing a valid universe and returns a two-dimensional array of integers (with equal dimensions) representing the universe in its next generation. To get full marks, your method must call getNextGenCellO For example, consider the following arrays int OC tubo,0,0,0,0, t0,0,1,0,0, ,0,,0, t0,0,1,0,0>, t0,0,0,0,0 intO toad to,0,0,0,0,0, (,1,0, t0,1,1,1,0,0, t0,0,0,0,0,0H int pentadectto,0,0,0,0,0,0,0,0, t0,0,0,0,0,0,0,0,0 t0,0,0,0,0,0,0,0,0, t0,0,0,0,1,0,0,0,0, to,0,0,0,1,0,0,0,0, t0,0,0,1,0,1,0,0,0, t0,0,0,0,1,0,0,0,0, (0,0,0,0,1,0,0,0,0, to,0,0,0,1,0,0,0,0), t0,0,0,0,1,0,0,0,0, (o,0,0,1,0,1,0,0,0) (0,0,0,0,1,0,0,0,0, to,0,0,0,1,0,0,0,0, t0,0,0,0,0,0,0,0,0, t0,0,0,0,0,0,0,0,0 t0,0,0,0,0,0,0,0,0 Then: .getNextGenUniverse (tub) returns the following array: (o,0,0,0,0, t0,0,1,0,0 0,,0,1,0, (o,0,1,0,0, t0,0,0,0,0) . getNextGenUniverse (toad) returns the following array: .getNextGenUiverse (pentadec) returns the following array: (to,0,0,0,0,0,0,0,0, (o,0,0,0,0,0,0,0,0, to,0,0,0,0,0,0,0,0, t0,0,0,0,0,0,0,0,0, (0,0,0,0,0,0,0,0,0), to,0,0,0,0,0,0,0,0, (o,0,0,0,0,0,0,0,0, to,0,0,0,0,0,0,0,0) Note that this method must not modify the input array in any way. 1e) Method to display a given number of generations Finally, create a method simulateNGenerations. This method takes a two-dimensional array of integers and one integer n as input. The method should first check if the input array represents a valid universe. If this is not the case, the method should throw an IllegalArgumentException with an appropriate message. On the other hand, if the array is a valid representation of a universe, then the method should display the original seed (i.e. the universe as it has been input) as well as the next n generations of such universe. Note that, to get full marks, your method must use the methods described above (isValidUni, displayUirseO, and getNextGenUniverse). For example, consider the following arrays intO tub (to,0,0,0,0, t0,0,1,0,0, 0,1,0,1,0, o,0,1,0,0, t0,0,0,0,0; int OC toad to,0,0,0,0,0, to,0,1,1,1,0, (o,1,1,1,0,0, (0,0,0,0,0,0 int0 pentadec-o,0,0,0,0,0,0,0,0, to,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0 (0,0,0,0,1,0,0,0,0, to,0,0,0,1,0,0,0,0, to,o,0,1,0,1,0,0,0, t0,0,0,0,1,0,0,0,0 (0,0,0,0,1,0,0,0,0, to,0,0,0,1,0,0,0,0, to,o,0,0,1,0,0,0,0), 0,0,0,1,0,1,0,0,0, (0,0,0,0,1,0,0,0,0, to,0,0,0,1,0,0,0,0, to,o,0,0,0,0,0,0,0), to,0,0,0,0,0,0,0,0, (0,0,0,0,0,0,0,0,0) Page 6 Question 2: Blackjack (40 points) For this question, you will write two classes, which you can then use to simulate a game of Blackjack. We want to represent a standard deck of playing cards which consists of 52 Cards in each of the 4 suits: Spades, Hearts, Diamonds, and Clubs. Each suit contains 13 cards: Ace, 2, 3, 4, 5, 6, 7, 8,9, 10, Jack Queen, King. For this assignme, we will represent the value of a card using an int which has a value between 1 and 13 Note that in addition to the required methods below, you are free to add as many other private methods as you want (no additional public method is allowed) (a) (8 points) Write a class Card. A Card has the following private attributes: .A int representing its value . A String representing its suit The Card class also contains the following public methods .A constructor that takes as input the value and the suit of the card and uses them to nitialize the corresponding attributes. This constructor should throw an IllegalArgumentException if the inputs do not represent a valid card. A card is considered to be valid if it has a value between 1 and 13 (both included) and if it has one of the following suits: Spades, Hearts, Diamonds and Clubs. Note, that your constructor should acceptas valid both “Spades”, “spades”, or “SPADES” for instance. .getValue O which takes no inputs and returns the value of the card as a number between 1 and 13. .getSuit O which takes no inputs and returns the suit of the card as a String. (b) (32 points) Write a class Deck. A Deck has the following private attributes .A array of Card representing the playing cards in the deck . An int representing the number of cards left in the deck . A static Random numberGenerator Initialize the Random numberGenerator “in place (ie. on the same line as its declaration) with a reference to a Random object created with the seed 123 The Deck class should also have the following public methods: . A constructor that takes no input. The constructor initializes the two non-static attributes in order for them to represent a standard deck or cards. Note that this means that the array of Cards should be initialized with an array of 52 elements containing all 52 possible cards belonging to a standard deck. You must use at least 1 loop to accomplish this (that is, you cannot write 52 statements to assign all possible values). Hint: If you create a String array of size 4 with all the possible suits value in it, you can easily use two nested loops to initialize the array of cards getNunOfCards ) which takes no inputs and returns the number of cards left in the deck . getCards ) which takes no inputs and returns an array of Cards with all the cards that are left in the deck. Note that no one should be able to use getCards ) and, for instance, made this deck a trick deck. Hint: review what we have learned about private attributes that are mutable reference types. In addition to the above methods, we need to add public methods that allow us to “play with the deck. The general idea is to never change the size of the array of cards, but instead use the attribute representing the number of cards left in the deck to keep track of which cards are still in the deck after we have played around with i. Note that to write a more efficient code, we will be thinking of the cards as listed in the reverse order as they appear in the deck from a player’s Page 9 point of view. This means that the last card in the deck (the one at the bottom of the deck) appears in the first position of our array, while the card that is on top of the deck will appear in the last position of the array which represents a card still in the deck (note that this in not necessarily the last position of the amay For instance, if we look at a fresh deck with 52 cards, then the card at the bottom of the deck appears in position 0, while the card on top of the deck appears in position 51. On the other hand, if we look at a deck with 26 cards left, then the card at the bottom of the deck appears in position 0, while the card of the top of the deck appears in position 25. Here are the additional public methods to be added to class Deck: eshowCards which takes no inputs and displays all the cards left in the deck (from top to bottom). For example, if the deck as 4 cards left, you might see displayed something similar to the following: 6 of clubs, 3 of hearts, 1 of spades, 13 of hearts. Note that you should not print words such as “king” or “queen”, you can simply display them as 13 or 12. IMPORTANT: Once again, the first card displayed should be the one on top of the deck, while the last one is the one at the bottom of the deck. See previous paragraph for a detailed explanation. shuffle) which takes no inputs and shuffles the cards left in the deck. You can do this by repeatedly generating 2 random indices (from 0 to the number of cards left in the deck) and swap the content of the array of cards at those two position. Repeat is operation for 1000 times. edeal) which takes no inputs and returns the Card which appears on top of the deck. If there are no more cards left in the deck, then the method should return null. Note that if a card was dealt, then after this operation the deck will contain one less card and the card returned by the method should not appear anymore as one of the cards left in the deck , pickACard) which takes an int as input indicating the position of a card in the deck from the player’s point of view. If the deck does not have enough cards left, then the method returns null. Otherwise, the method returns the Card in that position. Please note once again, that the cards in the array are stored in the reverse order compare to how they appear in the deck from a player’s point of view. Note that after this operation the deck will contain one less card and the card returned by the method should not appear anymore as one of the cards left in the deck restockDeck the deck will then contain all 52 cards belonging to a standard deck. which takes no inputs and resets the deck back to its original for. That is In order to give you some examples of how your methods should word, I need to start by showing you what will be displayed if I call showCards on a “freshly” made deck. This of course will depend on how the constructor initializes the attributes. On my program, if I create a deck and I call shouCards) on te following will be displayed all on one line: 1 of Clubs, 2 of Clubs, 3 of Clubs, 4 of Clubs, 5 of Clubs, 6 of Clubs, 7 of Clubs, 8 of Clubs, 9 of Clubs, 10 of Clubs, 11 of Clubs, 12 of Clubs, 13 of Clubs, of Diamonds, 2 of Diamonds, 3 of Diamonds, 4 of Diamonds, 5 of Diamonds, 6 of Diamonds, 7 of Diamonds, 8 of Diamonds, 9 of Diamonds, 10 of Diamonds, 11 of Diamonds, 12 of Diamonds, 13 of Diamonds, 1 of Hearts, 2 of Hearts, 3 of Hearts, 4 of Hearts, 5 of Hearts, 6 of Hearts, 7 of Hearts, 8 of Hearts, 9 of Hearts, 10 of Hearts, 11 of Hearts, 12 of Hearts, 13 of Hearts, 1 of Spades, 2 of Spades, 3 of Spades, 4 of Spades, 5 of Spades, 6 of Spades, 7 of Spades, 8 of Spades, 9 of Spades, 10 of Spades, 11 of Spades, 12 of Spades, 13 of Spades Make sure to initialize the attributes as I did, so that you’l be able to check that your methods Page 10 Show transcribed image text

Expert Answer


Answer to Part Z Questions Part Assignment Graded Question 1 Game Life 60 Points Question Write Java Q36374858 . . .

OR