Search code examples
javarandomgenerator

How to generate random board for a game in java but according to specefic conditions?


I am making a board game for college project and it needs to be done in java, the part that i'm stuck in is generating the board game now imagine with me we have a square shaped board and its size is variable meaning the player decides at the beginning of the game, and the board is composed of Cells i have a class defined that represents the cell (UML of class below) and the board is just a 2 dimension array of cells (UML of the game class) and each cell has a biome (MOUNTAIN, PLAIN, DESERT, FOREST, OCEAN) the biomes are defined in a enum, now my problem is i need to generate a random board for each game and the borad needs to fulfill two conditions:

  1. 2/3 of the board must be OCEAN
  2. a MOUNTAIN or PLAIN or FOREST or DESERT must have at least one neighbor that is not OCEAN biome and by neighbor i mean in its north, south, east or west. The first condition is easy to implement but the second one i don't know how to i looked around on the net but nothing is similar to my condition. There is an example of a board in the image below (blue cells are OCEAN, yellow is DESERT, light green is PLAIN, green is FOREST and brown is MOUNTAIN).

Code of enum of biomes

public enum Biome {
    MOUNTAIN, PLAIN, DESERT, FOREST, OCEAN;
}

UML of class Cell UML of class Game Example of a board


Solution

  • I guess you will fill your board row by row or column by column.

    You know:

    • Size of the board
    • How much must be ocean
    • How much can be other (save how much can be other ie int nonOceanBiomeLeft)
    • how much you have already filled

    while you fill up, for each field you decide randomly (depending on its weight) what you will set. When you place a non-Ocean biome you reduce nonOceanBiomeLeft by one. If it is already nearby another non-Ocean you stop here for that field, otherwise you need to reduce nonOceanBiomeLeft one more time and add one to something like int nonOceanBlocked (if your new field is without neighbor), this is to make sure you do not create a new noOcean biome somewhere and have none left to have the conditions fulfilled. So wenn you have none left you cannot create a noOcean biome.

    If you have nonOceanBlocked > 0 you need to watch for the neighbor in the row above if it is an alone nonOcean biome (so save the neighbor info for each field or calculate it on the fly). If your neighbor in the row above is alone you need to add a noOcean and you remove one from nonOceanBlocked Since this one has a neighbor you don't need to increase nonOceanBlocked again.

    Additionally if you create a new noOcean biome and the last field was an alone noOcean you can decrease nnoOceanBlocked too.

    Be careful with

    • The last row: there you need to give the neighbors immediately and cannot wait for the next row, since there will be none.
    • If only nonOceanBiomeLeft is equal to one you cannot place it somewhere it has no neighbor, since you would need to d

    This is only a theoretical description of what you could do, but for a college project I guess you should do most of the work yourself ;) I hope it helps you as a starting point for the filling of the board.