2

Consider simple aggregration with my favourite example (chess): I would create chessboard like 2D array of chessmen (at least it seems to me to be good solution) e.g:

chessman [,] board=new chessman[8,8];

This means that on each position there can be a chessman. But in reality, there will be only 32 chessmen at most. And that is the problem - what should class diagram display, implementation or my assumption?

BOARD<>----Chessman 0..64 or 0..32?

Because the implementation definitely allows 64, while game logic should not allow more than 32.

John V
  • 4,855
  • 15
  • 39
  • 63

1 Answers1

2

No it should as 0...64, because there are 64 ChessMan, where only 32 of the array is not null.
in class-diagram we do not omit(decrease) the references which are null.
enter image description here
Once again, this is true that there will be only 32 object not null, but this is not a class-diagram thing, you NEED whole 64 blocks in the game, and this is the class-diagram describes.
But beside it, I really don't see your approach is very well designed, because there are always +50% of the board array is null. so my suggestion is just keeping(tracking) the ChessMans, and find each ChessMan location with itself. you also may need to remove the lost ChessMans.
enter image description here
as you see in the above example too, this is possible that for losing each ChessMan, causes setting a null value in the array, but in class-diagram we always set the ACTUAL size of associations.

There is a big difference with your and mine solutions, your solution is optimized for best process execution(optimized for speed), in case that for each move you just read the related block from the board and process the of the word.
but mine is optimized for best memory management(optimized for memory usage), in this solution you need just check whole of the array to find out who is belong to related block.

and the another solution would be keep tracking the locations

  • THank you, great explanation! With your approach, I assume you would have to have another 2D array with 64 squares to find out available locations etc? – John V Nov 03 '13 at 07:10
  • I suggest keep tracking the available `ChessMan` by a `List<>`(no an array), because the size of the list/array is decreasing, and just keeping null values is not a good idea. –  Nov 03 '13 at 10:37
  • @user25111414 yes but you need to store board coordinates somewhere. How would you count possible moves, check for availiability without having an array representing the board 8x8 grid? But I agree with chessmen being stored separately, just asking how to go further from there. – John V Nov 03 '13 at 12:35
  • as I said your approach is best for execution, it means you would find out possible moves with just checking the blocks around, but my approach is saying just keep available chessman, in this case you have to check and process all available chessman location (`x`,`y`) to find out possible movement. assume you need to move a chessman, so if you just sort the available chessman array by `x`, and `y`, you will find the target block status by small search through the array nearby the related chessman index. –  Nov 03 '13 at 13:06
  • Sorry, what do you mean by sorting chessman array by X and Y? – John V Nov 03 '13 at 14:13
  • (in the second solution)for example consider a soldier(1 ahead) move, so you need to check the whole array list to process that the movement is possible or not, so I am saying that while there are 32 chessman, and only one chessman would limit(reject) the movement, if the array is sorted by `x` and `y` chessman' locations, so you don't need to check whole array for each time, just check the nearby indexes. –  Nov 03 '13 at 14:32
  • oh right, thank you. So if you were to implement Chess, you would not have an array of squares at all? I saw a diagram where Board holds references to 64 squares and pieceSets, where each Piece then holds a reference to a square it belongs to. – John V Nov 03 '13 at 14:48
  • If I want to be honest, in this case your case may is better too, I just mention it we can count on CPU more than RAM, but here a 64-length array is not a very big thing, beside your solution has very simpler process than mine. but surely there would be better solution here, I just need some coffee to think deeper –  Nov 03 '13 at 14:54