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.) Please don't waste my time or questions by copying what I have already done as I have had to post this about 3 times. This also is done in Scala
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 positiony. 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. a 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 Y 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 w 2 C 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)):
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 7 8 9 Y 0 S 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 saved position to -1,-1, which indicates no saved position. Walls count as covered positions 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 by 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 Y 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)
1 package cw 2. 3 import scala.io.StdIn 4 6 * This class holds an instance of a simple game where 16e class Game(wall: List[(Int, Int)], coin: List[(Int, Int, Int)], initialX: Int, in 17 18 //the current grid, a 10x10 field, where -1=empty, B=wall, any positive number= 19 private var field: Array(Array[Int]) = Array ofDim[Int](18, 10) 20 21 /* Please note - to align with the overall case study (see explanation sheet), 22 * should be accessed in the format field(col) (row) so field(2)(a) would retrie 23 * equivalent to an (x,y) coordinate of (2,0). You may therefore visualise each 24 */ 25 26 //the current score, initially o 27 private var score: Int = @ 28 //the current player position. As the player moves these positions update. 29 private var position: Int = initial 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 all 36 * It uses the list of walls provided to initialise the walls in the field arra 37 * It then uses the list of coins to initialise the coins in the field array by 38 */ 39 for (i <- until 10; k <- until 10) field(i)(k) = -1 40 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 tople, in (x,y) order */ 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 checkCoin() is made * A more advanced requirement would be to call checkCoins() if completed 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 to checkCoin() is made * A more advanced requirement would be to call checkCoins() if completed def moveRight() { if (position) positionX= positionX + 1 checkCoin()
amelesscala E ModuleMarks scala 5 TestModuleMarks. def moveUp O { if(position>0){ positionY= positionY - 1 checkCoin() } 1 2 3 4 5 -6 -7 38 39 10 ** * Move the player one place down. * If there is a wall or the field ends, nothing happens. * If there is a coin, it is collected (ie, a call to checkCoin * A more advanced requirement would be to call checkCoins() if */ def moveDown() { if(positionY ) { positiony = positionY + 1 checkCoin() } } 12 43 44 45 46 47 148 149 158 151 152 153 154 155 156 157 158 159 160 161 162 163 І * Move the player n places to the left. Negative numbers or a * If there is a wall or the field ends, the player stops before * Any coins are collected (i.e. a call to checkCoin() is made * A more advanced requirement would be to call checkCoins() if def moveLeft(n: Int) { if(n) { for(ic- i to n) { moveLeft 3 } /** * Move the player n places to the right. Negative numbers or @ eldends the plaver stops before
scale 82 5 GameApp.scala GameTestscala 5 ModuleMarks.scala 5 TexModulares def moveRight(n: Int) { if(n)) { for(ic- I to n) { moveRight() 3 3 * Move the player n places up. Negative numbers or as a parameter c. * If there is a wall or the field ends, the player stops before the w * Any coins are collected (ie, a call to checkCoin() is made after e *A more advanced requirement would be to call checkCoins() if comple */ def moveUp (n: Int) { if(n>0){ for(i<- 1 to n) { moveUp() 3 3 3 I 3 3 1 2 3 4 5 6 17 58 99 /** * Move the player n places down. Negative numbers or as a parameter If there is a wall or the field ends, the player stops before the wa * Any coins are collected (.e. a call to checkCoin() is made after ea * A more advanced requirement would be to call checkCoins() if complet */ def moveDown(n: Int) { if(n>0){ for(ic- 1 to n){ moveDown() 3 3 ) 31 32
Appucola B Gamelestscala Module Marrosscala 5 TesModule tale scala s 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 th * score, and erases the coins. Also resets the saved position to -1,-1. 3 1 2 def checkCoins() { 3 4 DI
scala 3 GameApp.scala G GameTestscala 5 Module Marksscala E TestModule Marks.scala 1 5 Counter.scala UseCounter scala 5 Letters Match.scala E *StringMenu.scala def suggestSolution(): String = /** * 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 ". /* --- The three save methods below are used by the unit tests to simulate certain conditions --- */ I /** * 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 Y } . > 7 9 © 1 32 /** * 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 33 34 35 26
ame.scala X GameApp.scala 6 GameTest.scala 6 Module Marks.scala TestModule Mar * def setSavepos (savex: Int, saveY: Int): Unit = { { 1 /* This method is already implemented. You should not change this. saveX = saveX B this. saveY = saveY 1 6} 7 8 /** 9 * This object builds and returns a standard instance of Game. * It is used by the unit tests to initialise the game in differe 1 * Currently, there are three ways in which a game can be initial 2. * the first has been completed but the other two initialisation 33 34 object GameBuilder { 35 26 /** 27 * @return A game with 08 walls in positions 3,0 3,1 and 3,2 a coin at 4,1 which increases score by 100 10 a coin at 3,3 which increases score by 250 11 the player starting in position 0,0 12 */ 133 def initialiseGame1(): Game = { 14 /* This method is already implemented. You should not change 315 return new Game(List(e), , 1), (3, 2)), List((4, 1, 100 316 } 09
/** * @return A game with walls in positions 3,3 3,4 3,5 5,3 5,4 and 5 e coin at val local GameBuilder: Tre by 200 a coin at pre by 200 - the played Press FX for focus, 2 def initialiseGame2(): Game = { return new Game(List(), List(), e, e) } * @return A game with - walls in positions 3,0 3,1 and 3,2 * - a coin at 4,1 which increases score by 300 * - a coin at 3,3 which increases score by 150 - the player starting in position 4,1 */ def initialiseGame(): Game = { return new Game(List(), List(), , 0) 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
-
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
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!