From 6edf9df251935c9b16e8a8da7407450c52e06edb Mon Sep 17 00:00:00 2001 From: Katchan Date: Fri, 15 Oct 2021 18:54:21 +0200 Subject: [PATCH] add grid basis whith extern wall generation --- .../fr/lnl/game/server/games/grid/Grid.java | 78 ++++++++++++++++++- .../fr/lnl/game/server/games/grid/Wall.java | 24 ++++++ 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java b/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java index 5ab2d3e..2781ba6 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java @@ -1,12 +1,86 @@ package fr.lnl.game.server.games.grid; +import fr.lnl.game.server.utils.Cardinal; import fr.lnl.game.server.utils.Point; -import fr.lnl.game.server.utils.Tuple; +import fr.lnl.game.server.utils.Triplet; import java.util.HashMap; + public class Grid { + private HashMap, Triplet> board; + private int x; + private int y; - private HashMap, Tuple> board; + public Grid(int x, int y){ + this.x = x; + this.y = y; + board = new HashMap, Triplet>(); + } + public void initGrid(){ + for (int i = 0; i < x; i++) { + for (int j = 0; j < y; j++) { + Box box; + if (i == 0 && j == 0) { + box = new Wall(Cardinal.NORTH_WEST, i, j); + } else if (i == 0 && j == y-1) { + box = new Wall(Cardinal.NORTH_EAST, i, j); + } else if (i == x-1 && j == 0) { + box = new Wall(Cardinal.SOUTH_WEST, i, j); + } else if (i == x-1 && j == y-1) { + box = new Wall(Cardinal.SOUTH_EAST, i, j); + } else if (i == 0) { + box = new Wall(Cardinal.NORTH, i, j); + } else if (i == x-1) { + box = new Wall(Cardinal.SOUTH, i, j); + } else if (j == 0) { + box = new Wall(Cardinal.WEST, i, j); + } else if (j == y-1) { + box = new Wall(Cardinal.EAST, i, j); + } else { + box = null; + } + board.put(new Point(i,j),new Triplet(box,null,null)); + } + } + } + + + public void printGrid() { + for (int i = 0; i < x; i++) { + System.out.print("\n"); + for (int j = 0; j < y; j++) { + Triplet value = board.get(new Point(i, j)); + if (value.getA() instanceof Wall) { + if (((Wall) value.getA()).getCardinal() == Cardinal.NORTH) { + System.out.print(" \033[0;34m—\033[0m"); + } else if (((Wall) value.getA()).getCardinal() == Cardinal.SOUTH) { + System.out.print(" \033[0;31m—\033[0m"); + } else if (((Wall) value.getA()).getCardinal() == Cardinal.WEST) { + System.out.print(" \033[0;33m|\033[0m"); + } else if (((Wall) value.getA()).getCardinal() == Cardinal.EAST) { + System.out.print(" \033[0;32m|\033[0m"); + } else if (((Wall) value.getA()).getCardinal() == Cardinal.NORTH_EAST) { + System.out.print(" \033[0;32mN\033[0m"); + } else if (((Wall) value.getA()).getCardinal() == Cardinal.NORTH_WEST) { + System.out.print(" \033[0;33mN\033[0m"); + } else if (((Wall) value.getA()).getCardinal() == Cardinal.SOUTH_EAST) { + System.out.print(" \033[0;32mS\033[0m"); + } else if (((Wall) value.getA()).getCardinal() == Cardinal.SOUTH_WEST) { + System.out.print(" \033[0;33mS\033[0m"); + } + } + else { + System.out.print(" \033[0;31mO\033[0m"); + } + } + } + } + + public static void main(String[] args) { + Grid grid = new Grid(10,10); + grid.initGrid(); + grid.printGrid(); + } } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/Wall.java b/server/src/main/java/fr/lnl/game/server/games/grid/Wall.java index f61d246..8b1bdf1 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/Wall.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/Wall.java @@ -1,4 +1,28 @@ package fr.lnl.game.server.games.grid; +import fr.lnl.game.server.utils.Cardinal; + public class Wall implements Box{ + + private Cardinal cardinal; + private int x; + private int y; + + public Wall(Cardinal cardinal, int x, int y){ + this.cardinal = cardinal; + this.x = x; + this.y = y; + } + + public Cardinal getCardinal() { + return cardinal; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } }