Friday, February 14, 2025
HomeGolangHow would you design the database mannequin by a given area mannequin?...

How would you design the database mannequin by a given area mannequin? – Code Evaluation


I wish to enhance my Go abilities and thought of making a small Minesweeper backend. For the area mannequin I began with this

import "github.com/google/uuid"

kind Sport struct {
	ID            uuid.UUID
	Board         [][]*BoardCell
	AmountOfMines uint8
}

kind BoardCell struct {
	IsRevealed                    bool
	HasBeenRevealedAfterEndOfGame bool
	HasFlag                       bool
	HasMine                       bool
	AmountOfNeighbourMines        uint8
}

With these info you’ll be able to examine the entire recreation board whereas monitoring the AmountOfNeighbourMines area is only for efficiency functions ( since we solely have to calculate it as soon as )

I wish to understand how you’d design a database mannequin for this.

I personally assume that I’d use the identical schema as SQL tables
Sport => ID, AmountOfMines
BoardCell => gameID ( overseas key ), …fields…

however in terms of a DTO I’m undecided if I ought to merely copy paste the area mannequin. If that’s the case the in reminiscence database may observe video games in a map [gameID, game] however possibly there are higher methods to handle the video games within the database layer.

Thanks prematurely

Is it typical for posts right here to go unanswered? Attempting to know if the discussion board remains to be energetic or not.

I’m right here. :blush:

Concerning your query, I’m not solely positive I perceive every part you’re asking. Nevertheless, I strategy design from a database perspective.

I perceive that you simply want the AmountOfNeighbourMines to avoid wasting processing energy sooner or later, however I want to not embrace it within the database. I assume you wish to put it aside at a particular level, right? So efficiency doesn’t matter that a lot and all data is there.

You haven’t offered a lot info, however I really feel that individuals usually use an excessive amount of object-oriented programming (OOP) for Go. Transferring a Information Switch Object (DTO) feels unnecessarily complicated. It could be higher to easily ship the required knowledge instantly and assume in these phrases.

Once more, it’s onerous to make certain with out extra particulars, however my instinct tells me that your code might need at the very least one or two pointless layers of complexity and is extra in a Java or Cpp type.

I don’t assume I’d retailer issues like AmountOfMines in my recreation desk since it may be calculated. Possibly begin with one thing like this (BTW I typed this into notepad so syntax could be barely mistaken and I’m at the moment used to MariaDB so that is MySQL-ish):

-- Create video games desk
CREATE TABLE video games (
  id int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (id)
);
-- Create recreation cells desk
CREATE TABLE game_cells (
  id int(11) NOT NULL AUTO_INCREMENT,
  game_id int(11) NOT NULL,
  x int(11) NOT NULL,
  y int(11) NOT NULL,
  is_revealed tinyint(1) NOT NULL,
  is_flagged tinyint(1) NOT NULL,
  is_mine tinyint(1) NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (game_id) REFERENCES video games(id)
);

Anyway, my level there may be: you can simply assemble issues like AmountOfMines:

choose 
	depend(id) as num_cells,
    sum(is_mine) as num_mines
from game_cells the place game_id = 1;

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments