Design Question / Architectural Question: Something belongs into the class but it doesn't

edited February 2017 in Questions about Code

hello all,

I have a more Architectural Question to improve my working code (refactoring).

let's say I am programming the old letter game Scrabble where 2 players have 8 letters each (on their hands) and must lay words like in a cross word puzzle into the main game field they own together.

As shown in the image below (which has 4 and not 2 players though).

Anyway. I made a class Cell for ONE letter.

  • Now we have 2 arrays (1D) of type Cell which hold the 8 letters of the player, vorratsfeldLinks and vorratsfeldRechts (which are letters on their hands so to speak). Those letters are movable.

  • We also have one 2D array for the entire main game field called mainGrid. Of the same type Cell. Those letters are not movable anymore.

Question.

Now I have a code segment below.

After a placing a letter, mouseReleased() kicks in and the letter gets copied from the hand to the mainGrid.

But the code looks totally ugly.

But although all objects are based of the same class, since they are in different arrays, I find it hard to beautify the code really.

I could say mainGrid[i2][i].copyFrom(vorratsfeldLinks[holding]); but this feels wrong since it can be only applied one way from vorratsfeldLinks to mainGrid but not vice versa.

I could make a new class "game logic" or "game management" and place commands like copy() therein: and the say gameManager.copy(vorratsfeldLinks[holding], mainGrid[i2][i]); but is that a real solution?

In abstract terms, what would a game logic class be called? A setter is a method within the class, but a similar term?

Any advice would be welcome.

Best, Chrisir ;-)

void mouseReleased() {

  hold=false;

  int r=0, r2=0;

  if ( whichPlayersMove == 0 ) {

    for (int i2=0; i2<max1; i2++) {
      for (int i=0; i<max1; i++) {
        if (dist(mainGrid[i2][i].x, mainGrid[i2][i].y, vorratsfeldLinks[holding].x, vorratsfeldLinks[holding].y)<12) {
          mainGrid[i2][i].cellLetter = vorratsfeldLinks[holding].cellLetter; // copy
          mainGrid[i2][i].cellColor  = vorratsfeldLinks[holding].cellColor;
          vorratsfeldLinks[holding].exist = false;
          return;
        }
      }
    }
  }//if
  else if ( whichPlayersMove == 1 ) {

    for (int i2=0; i2<max1; i2++) {
      for (int i=0; i<max1; i++) {
        if (dist(mainGrid[i2][i].x, mainGrid[i2][i].y, vorratsfeldRechts[holding].x, vorratsfeldRechts[holding].y)<12) {
          mainGrid[i2][i].cellLetter = vorratsfeldRechts[holding].cellLetter; // copy 
          mainGrid[i2][i].cellColor = vorratsfeldRechts[holding].cellColor;
          vorratsfeldRechts[holding].exist = false;
          return;
        }
      }
    }
  }//else
}//sub routine
//

PocketScrabble

Answers

Sign In or Register to comment.