Fix: Forgoten copy semantict to tiles and color

This commit is contained in:
Christos Houtouridis 2018-11-11 18:21:39 +02:00
parent cdf89ec953
commit f89d442dcc
2 changed files with 22 additions and 15 deletions

View File

@ -35,7 +35,7 @@ public class Apple {
Apple (int appleId, int appleTileId, String color, int points) { Apple (int appleId, int appleTileId, String color, int points) {
this.appleId = appleId; this.appleId = appleId;
this.appleTileId = appleTileId; this.appleTileId = appleTileId;
this.color = color; this.color = new String(color);
this.points = points; this.points = points;
} }
/** Copy constructor /** Copy constructor
@ -44,7 +44,7 @@ public class Apple {
Apple (Apple a) { Apple (Apple a) {
appleId = a.getAppleId (); appleId = a.getAppleId ();
appleTileId = a.getAppleTileId (); appleTileId = a.getAppleTileId ();
color = a.getColor (); color = new String (a.getColor ());
points = a.getPoints (); points = a.getPoints ();
} }
/** @} */ /** @} */
@ -92,7 +92,7 @@ public class Apple {
* }</pre> * }</pre>
* would be better and far less error prone. * would be better and far less error prone.
* In order to support this style of color encoding we have to strict the * In order to support this style of color encoding we have to strict the
* color names to lower case letters and also convert user input. * color names to lower case letters and also "low case-convert" the user input.
* We also update the points sign to make calculations easier. * We also update the points sign to make calculations easier.
* @see setColor * @see setColor
*/ */

View File

@ -6,9 +6,9 @@ import java.lang.Math;
* @class Board * @class Board
* @brief The game's board representation. * @brief The game's board representation.
* *
* The board in a square collection of tiles numbered in a * The board is a square collection of tiles numbered in a
* boustrophedon (zig-zag) way. A number of snakes, ladders * boustrophedon (zig-zag) way. A number of snakes, ladders
* and apples which called elements are placed on the board * and apples which we called elements are placed on the board
* for each game. * for each game.
* *
* @author Christos Choutouridis 8997 * @author Christos Choutouridis 8997
@ -82,7 +82,7 @@ public class Board {
ladders = new Ladder[B.getLadders().length]; ladders = new Ladder[B.getLadders().length];
apples = new Apple[B.getApples().length]; apples = new Apple[B.getApples().length];
// Copy B's guts into new memory // Copy B's guts into new memory
setTiles(B.getTiles()); // primitive copyTiles(B.getTiles());
copySnakes(B.getSnakes()); copySnakes(B.getSnakes());
copyLadders(B.getLadders()); copyLadders(B.getLadders());
copyApples(B.getApples()); copyApples(B.getApples());
@ -110,15 +110,7 @@ public class Board {
* @param tiles Source of tiles to use * @param tiles Source of tiles to use
* @note This has to be called if the board is default constructed * @note This has to be called if the board is default constructed
*/ */
void setTiles (int[][] tiles) { void setTiles (int[][] tiles) { this.tiles = tiles; }
// Check if we need allocation
if (this.tiles == null)
this.tiles = new int[N][M];
// Assign values
for (int i =0 ; i<N ; ++i)
for (int j =0 ; j<M ; ++j)
this.tiles[i][j] = tiles[i][j];
}
/** Get reference to snakes */ /** Get reference to snakes */
Snake[] getSnakes() { return snakes; } Snake[] getSnakes() { return snakes; }
@ -147,6 +139,21 @@ public class Board {
*/ */
void setApples(Apple[] apples) { this.apples = apples; } void setApples(Apple[] apples) { this.apples = apples; }
/**
* Copy tiles
* @param tiles Source of tiles to use
* @note This has to be called if the board is default constructed
*/
void copyTiles (int[][] tiles) {
// Check if we need allocation
if (this.tiles == null)
this.tiles = new int[N][M];
// Copy-assign the values
for (int i =0 ; i<N ; ++i)
for (int j =0 ; j<M ; ++j)
this.tiles[i][j] = tiles[i][j];
}
/** /**
* Copy snakes (deep copy) * Copy snakes (deep copy)
* @param snakes Source of snakes to use * @param snakes Source of snakes to use