You have been given a class Game, which consists of a field of size 10x10.( I have been trying to start this as practice

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

You have been given a class Game, which consists of a field of size 10x10.( I have been trying to start this as practice

Post by answerhappygod »

You have been given a class Game, which consists of a field of size 10x10.( I have been trying to start this as practice but I'm already stuck, if someone could solve it to see what I could have done it would be helpful.)
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 1
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 1 (200.21 KiB) Viewed 42 times
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 2
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 2 (192.39 KiB) Viewed 42 times
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 3
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 3 (89.14 KiB) Viewed 42 times
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 4
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 4 (84.08 KiB) Viewed 42 times
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 5
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 5 (48.04 KiB) Viewed 42 times
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 6
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 6 (66.17 KiB) Viewed 42 times
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 7
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 7 (62.86 KiB) Viewed 42 times
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 8
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 8 (86.78 KiB) Viewed 42 times
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 9
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 9 (95.75 KiB) Viewed 42 times
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 10
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 10 (66.23 KiB) Viewed 42 times
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 11
You Have Been Given A Class Game Which Consists Of A Field Of Size 10x10 I Have Been Trying To Start This As Practice 11 (46.12 KiB) Viewed 42 times
You have been given a class Game, which consists of a field of size 10x10. The field is numbered as follows: X 0 1 2 3 4 5 6 7 8 9 Y 0 p . w 1 w 2 w 3 с 4 5 6 7 8 9 The grid above should help you to visualise this scenario, and we have used p to show the current player position and w to show a wall. Coordinates are given as pairs in the form (x,y). Although different conventions can be used, here we define the origin as being in the upper left corner (which is common in computer graphics). Moving in the x direction would go right (or left) and moving in the y direction would go down (or up). Therefore, in the snapshot shown above, the player is positioned in (0,0) at the origin. In the Game class you have been provided, the current position of the player is stored in positionX and position. The use of "left", "right", "up", and "down" allows the player to be moved around in the field. You cannot move diagonally. You will note the variable called field which is of type Array(Array[Int]] - this represents the grid shown above, where each cell is either empty (-1), a wall (O), or a coin (positive number). In the above example, there are walls in (2,0), (2,1), and (2,2). The player can move within the field. If the player tries moving into a wall or off the field, nothing happens. So, in the example above, the player could move right or down, but not up or left. If the player has moved right once, it cannot move right again (because of the wall). In these examples we use the symbols. for empty cell, w for wall, and c for coin because they are easier to read. In the field, they are represented by the aforementioned numbers. There are also coins to collect, represented by positive numbers. If the player moves onto a coin, the score is increased by the amount the coin is worth and that coin is removed (the cell becomes -1). This is done by the checkCoin method. For example, if the score is O and the player moves onto position (1,3) - this contains a coin in the field above, suppose of value 100. This means the score would increase to 100. Any further moves onto (1,3) would not change the score, as the coin would have been removed. You can generally compare cells of the grid to determine if there is a wall or a coin. So something like if(field(5) (6)==0) would check if there is a wall at this position. if(field(5)(6)>0) would check for a coin (any value greater than 0 is a coin). Of course, you may wish to look at several cells using loops etc. The player can also save the current position to saveX and saveY by calling save(). If the player continues moving, the covered positions are evaluated by the checkCoins method. Covered positions are those inside a rectangle where the current position and the saved positions form two opposite corners of the rectangle. As soon as 9 or more positions are covered, the saved position is reset to -1,-1, indicating no saved position (these are the initial values of saveX and save as well) and all coins in the covered positions are collected. For example, the player is in position (0,0), save is applied, and the player then moves right (parts of the field have been left out to save space, the same is true in other examples further down): X 0 1 2 3 4 5 6 7 8 9 Y 0 S р w 1 w 2 с w 3 с Here, the player (p) is in (1,0), the saved position (s) is in (0,0). There are two positions covered. The player moves down one position. There are now 4 positions covered. With a further move down, 6 positions are covered and the coin at (1,2) will be collected. A further move down would result in 8 positions being covered. Nothing happens here. However, with a further move down, the situation is this (note the player is now at (1,4)):
0 1 2 3 4 5 6 7 8 9 Y 0 S p w 1 W 2 с w 3 c Here, the player (p) is in (1,0), the saved position (s) is in (0,0). There are two positions covered. The player moves down one position. There are now 4 positions covered. With a further move down, 6 positions are covered and the coin at (1,2) will be collected. A further move down would result in 8 positions being covered. Nothing happens here. However, with a further move down, the situation is this (note the player is now at (1,4)): X 0 1 2 3 4 5 6 7 8 9 0 w . 1 w 2 w 3 с 4 p 5 There are now 10 positions covered. Since 9 or more are covered, we now collect all remaining coins in here, which would include (0,1),(0,2), (0,3), and (0,4), even though these positions were not touched by the player - note (0,3) contains a coin, which will therefore be collected. It would also reset the say position -1,-1, which indicates no saved position. Walls count as covered position as well, if they are in the rectangle, however, you cannot move through them of course. The method move takes a string where up, left, down, and right are encoded as w,a,s, and d respectively. The moves are executed according to the string, so "aaddw" would move to the left twice, to the right twice and up once. If there is a wall, an individual move (but not the complete string) is not executed. After each individual move a coin is collected if moved onto and the saved position is evaluated to see if 9 or more positions have been covered. This is the same as happens after moving left, right, up, or down. The method suggest Move returns a string in the same format as noted for the move method, which moves the player from the current position to position (x,y). No specific requirements for the efficiency of the solution exist. The move cannot jump walls. The method is restricted to finding a path which follows two sides of a rectangle defined the current position and the target. This means there is a given number of moves in one direction first, followed by a given number of moves in another direction. If the first move was horizontal, the second is vertical, and vice versa. If this is not possible due to walls, the method returns an empty string. If two paths are possible, any of them can be returned. No actual move is done. For example, below the two potential paths to get from (0,0) to (3,3) are indicated: х 0 1 2 3 4 5 6 7 8 9 0 V>P > >w V 1 V W V 2 V w V 3 > > 4 Since the move "dddsss" is blocked by a wall, it is not possible, so the method would return "sssddd". If this pathway would be blocked as well, an empty string would be returned. The file Game.scala also contains an object GameBuilder which allows the initialisation of predefined game scenarios, such as those shown throughout this explanation. This object could go in its own file, but has been placed within the same file as the Game class as we want you to be able to submit a single file. There are three ways in which the game can be initialised, and the first has been completed for you. Use the comments above the other initialisation methods to help complete them (we suggest you do this at an early stage as the unit tests refer to these). Lists of Tuples are used to provide coordinates. Remember that a tuple like (3,3) represents a single value of type tuple - in this case a 2-tuple (pair), whereas (4,5,7) is a 3-tuple (triplet). The elements of a tuple can be accessed individually using_1,_2, etc. (This may be helpful: https://docs.scala-lang.org/tour/tuples.html)
package cw import scala.io.StdIn 1 5* This class holds an instance of a simple game whered 5e class Game(wall: List[(Int, Int)], coin: List[(Int, Int, Int)], initialX: Int 7 3 //the current grid, a 10x10 field, where -1=empty, O=wall, any positive num 9 private var field: Array Array(Intl) = Array ofDim[Int](10, 10) 3 1 /* Please note - to align with the overall case study (see explanation shee 2 * should be accessed in the format field(col) (row) so field(2)(0) would re 3 * equivalent to an (x,y) coordinate of (2,0). You may therefore visualise 4. */ 5 6 //the current score, initially o 17 private var score: Int = 0 8 //the current player position. As the player moves these positions update. 9 private var position: Int = initialx 30 private var positionY: Int = initialy 31 //the current X and Y save position initially -1 32 private var savex: Int = -1 33 private var saveY: Int = -1 34 35 /* This code is executed as part of the constructor. It firstly initialises 86 * It uses the list of walls provided to initialise the walls in the field 87 * It then uses the list of coins to initialise the coins in the field arra 38 */ 39 for (i <- until 10; k <- until 10) field(i)(k) = -1 wall. foreach(w => field(w._1)(w._2) = C) 41 coin.foreach(w => field(w._1) (w._2) = w. _3)
0 8 9 def rpt(n: Int) (commands: => Unit) { for (i <- 1 to n) { commands } -1 } 2 53 /** 54 * Utilised for GameApp.scala 55 */ 560 def printField(): Unit = { 57 for(k<- until 10){ 58 for(i<- until 10){ 59 if(positionX==i && positionY==k){ 60 print("p") 61 }else if(saveX==i && saveY==k){ 62 print("s") 63 }else if(field(i)(k)== 0){ 64 print("w") 65 }else if(field(i)(k)== -1){ 66 print(".") 67 }else{ 68 print(field(i)(k)) 69 } I 70 } 71 println() 72 } 73 } 74 + * * ********** *****
* Returns the current position of the player as a toplo */ def getPlayerPos(): (Int, Int) = return (position, position); } 83 84 85 86 87 88 89 90 91 92 93 94 95 96 Returns the current score. def getScore(): Int = score * Move the player one place to the left. * If there is a wall or the field ends, nothing happens * If there is a coin, it is collected (ie a call to * A more advanced requirement would be to call checkCoi 97 def moveLeft() { if(position) { position = positionX - I checkCoin() } 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 ** * Move the player one place to the right. * If there is a wall or the field ends, nothing happens If there is a coin, it is collected (i.e. a call toc * A more advanced requirement would be to call checkCoi def moveRight() { if (position) positionX= positionX + 1 checkCoin()
ISESCala 28 529 El Module Marks scala 5) TestModuleMarl def moveUp ( if(position Y>0){ BO positionY= positiony = 1 131 checkCoin() 132 } 133 } 134 135 /** 136 * Move the player one place down 137 * If there is a wall or the field ends, nothing happens. 138 * If there is a coin, it is collected (i.e. a call to checkCo 139 * A more advanced requirement would be to call checkCoins() i 140 1416 def moveDown() { 142 if(positionY<9) { 143 positiony = positionY + 1 144 checkCoin() 145 146 147 148 /** I 149 * Move the player n places to the left. Negative numbers or e 150 * If there is a wall or the field ends, the player stops befo 151 * Any coins are collected (i.e. a call to checkCoin() is made 152 * A more advanced requirement would be to call checkCoins() i 153 */ 154 def moveLeft(n: Int) { 155 if(n) { 156 for(ic- I to n){ 157 moveLefto 158 3 159 160 } 161 162 /** 163 * Move the player n places to the right. Negative numbers or wall or the field ends, the plaver stops bef
ame scale GameApp.scala GameTestscala 6 Module Marksscala 5 TextModule 59 10 71 22 73 74 75 76 77 78 def moveRight(n: Int) { if(n)) { for(ic. I to n){ moveRight() 3 } ) 79 80 81 825 183 184 185 186 * Move the player n places up. Negative numbers or @ as a p * If there is a wall or the field ends, the player stops be * Any coins are collected (i.e, a call to checkCoin() is ma * A more advanced requirement would be to call checkCoins() */ def moveUp (n: Int) { if(n>0) for(ic- i to n) { moveUp() 187 3 } I 188 189 198 191 192 193 194 195 196 197 198 199 200 201 202 203 /** * Move the player n places down. Negative numbers or @ as a * If there is a wall or the field ends, the player stops be * Any coins are collected (i.e. a call to checkCoin() is ma * A more advanced requirement would be to call checkCoins() */ def moveDown(n: Int) { if(n)) { for(i<- i to n){ moveDown() 3 3
Appucola B Gamelestscala Module Marrosscala 5 TesModule tale scala Counters def checkCoin() { if(field(positionx) (positiony) ) score= score + field(positionx) (position); field(position) (positionY)= -1 3 //The methods beyond this point aside to those in GameBuilder which is a separ /** * This moves the player according to a string. The string can contain the * letters w, a, s, d representing up, left, down, right moves. If * there is a wall or the field ends, the individual move is not * executed. Any further moves are done. Any coins are collected and the * save position is evaluated) */ def move(s: String) { 1 /** * Identifies the maximum overall score in the game. This is the sum * of the current score and the possible score from collecting all of the remai * No coins are collected here, only the max score is returned. */ def maxScore(): Int = 0 ** * Checks if the rectangle defined by the current position and saved position * covers nine or more positions. If yes, it collects coins in it, increases the * score, and erases the coins. Also resets the saved position to -1,-1. 3 1 2 def checkCoins() { 3 4 DI
ulemanis.scala 5 Counter.scala 6 UseCounterscala 5 Letters Match.scala def suggestsolution(): String 5 5 7 8 9 - 1 52 536 54 65 66 /** * This gives a string in the format for move, which moves from the current position to * position x,y. No specific requirements for the efficiency of the solution exist. The move * cannot jump walls. The method is restricted to finding a path which is combined of a number of * left and then a number of up movement, or left/down, or right/up, or right/down movements only. * If this is not possible due to walls, it returns an empty string. No actual move is done. If * xor y are outside the field, an empty string is returned as well. */ def suggestMove(x: Int, y: Int): String { return ". 3 67 */ /* --- The three save methods below are used by the unit tests to simulate certain conditions --- 68 169 270 I 271 272 273 /** * Updates savex and savey to the current player position. */ def save(): Unit = { /* This method is already implemented. You should not change it */ saveX = positionX saveY = position } 274 /** 275 276 277 278 279 280 281 282 283 284 285 286 * Returns the current save position as a tuple, in (x,y) order. */ def getSavepos(): (Int, Int) = { /* This method is already implemented. You should not change it */ return (savex, saveY); 3
(6) Cou Samescala X GameApp.scala B GameTest.scala B ModuleMarks.scala TestModule Marks.scala 9 */ 0 def setSavepos (savex: Int, saveY: Int): Unit = { 11 /* This method is already implemented. You should not change it */ 2 this. saveX = savex 3 this.saveY = saveY 34 } 25 86 } 27 98 /** 99 * This object builds and returns a standard instance of Game. ee * It is used by the unit tests to initialise the game in different states. 01 * Currently, there are three ways in which a game can be initialised, 02 * the first has been completed but the other two initialisation met methods need 03 -84object GameBuilder { 05 06 307 * @return A game with 308 walls in positions 3,0 3,1 and 3,2 B09 a coin at 4,1 which increases score by 100 310 a coin at 3,3 which increases score by 250 311 the player starting in position 0,0 312 */ 313 def initialiseGame1(): Game = { 314 /* This method is already implemented. You should not change it */ 315 return new Game(List(0, e), 0, 1), (, 2)), List((4, 1, 100), (3, 3, 258 316 * * 2
318 319 * @return A game with 320 * - walls in pesitions 3,3 3,4 3,5 5,3 5,4 and 5,5 321 - a coin at val clocal GameBuilder: Tre by 200 322 - a coin at pre by 200 323 the played Press 'FZ for focus, 2 324 325 def initialiseGame2(): Game = { 326 return new Game(List(), List(), 0, 0) 327 } 328 329 33e * @return A game with 331 * - walls in positions 3,0 3,1 and 3,2 332 * - a coin at 4,1 which increases score by 300 333 *- a coin at 3,3 which increases score by 150 334 the player starting in position 4,1 335 */ 336 def initialiseGame(): Game = { 337 return new Game(List(), List(), , 0) 338 3 339)
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply