How can I store 10x10 map / game field in MySQL workbench?

Verifycations :

I'm creating the game Sokoban in java.

The map / playing field is a 10x10 array. One field in the array can contain one of the 5 different objects

  1. Target Field
  2. Chest
  3. Player
  4. Wall
  5. Empty Field ( this is just a blank field where a player can walk over)

now i want to store that map in the MySql database i'm not so sure how to approach this. I don't know what the table would look like. Later on i should be able to pull the map so a player can instantly play or modify the field.

I thought about using a string of 100 chars and each object has a specific char so i know the meaning and place of it.

Jason :

Yeah so one approach would be to have a table that has a unique-key based on the column/row. Then you can store the key(s) relative to that column/row that link to the target field, the chest, the player, the wall, empty field.

Edit: To answer your question on this answer, you can create a Location class that has and x and y that represents a spot in the grid. Then override equals/hashCode to make it unique. Then you can use a Map to store the Location and the relative GameObject at that location!

    public class Location {

        private final int x;

        private final int y;

        private final int hashCode;

        public Location(int x, int y) {
            this.x = x;
            this.y = y;
            this.hashCode = Objects.hash(x, y);
        }

        @Override
        public int hashCode() {
            return hashCode;
        }

        @Override
        public boolean equals(Object other) {
            if (other == this) {
                return true;
            }
            if (other instanceof Location) {
                Location otherLocation = (Location) other;

                return otherLocation.x == x && otherLocation.y == y;
            }
            return false;
        }
    }

    interface GameObject {

    }

    class TargetField implements GameObject {

    }

    class MyGame {

        private final Map<Location, GameObject> map;

        MyGame(Map<Location, GameObject> map) {
            this.map = map;
        }

        public void set(Location location, GameObject object) {
            map.put(location, object);
        }
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=349021&siteId=1