diff --git a/Classes/Cell.cs b/Classes/Cell.cs new file mode 100644 index 0000000..93525d5 --- /dev/null +++ b/Classes/Cell.cs @@ -0,0 +1,9 @@ +namespace JewellNS; + +public class Cell +{ + public string Name { get; set; } + public int Point { get; set; } + public string Symbol { get; set; } + public int? LevelEnergy { get; set; } +} \ No newline at end of file diff --git a/Classes/Jewell.cs b/Classes/Jewell.cs index eaf23b5..77c8708 100644 --- a/Classes/Jewell.cs +++ b/Classes/Jewell.cs @@ -1,25 +1,5 @@ namespace JewellNS; -public class Jewell +public class Jewell : Cell { - private string name; - private string color; - private int point; - private int levelEnergy; - public Jewell(string color, string name, int point, int levelEnergy) - { - this.name = name; - this.color = color; - this.point = point; - this.levelEnergy = levelEnergy; - } - public string getName() { return this.name; } - public string getColor() { return this.color; } - public int getPoint() { return this.point; } - public int getLevelEnergy() { return this.levelEnergy; } - - public string toString() - { - return $"Cor: {this.color}, Point: {this.point}, Name: {this.name}, Nível de Energia: {this.levelEnergy}"; - } } diff --git a/Classes/Map.cs b/Classes/Map.cs index 51e730b..c18a9ce 100644 --- a/Classes/Map.cs +++ b/Classes/Map.cs @@ -2,7 +2,7 @@ namespace JewellNS; public class Map { - private Object[,] arrayObj; + private Cell[,] arrayObj; private int linha; private int coluna; @@ -13,13 +13,13 @@ public Map(int width, int height) { this.linha = width; this.coluna = height; - arrayObj = new Object[width, height]; + arrayObj = new Cell[width, height]; } - public Object getObject(int linha, int coluna) + public Cell getCell(int linha, int coluna) { return arrayObj[linha, coluna]; } - public void setCell(int i, int j, Object obj) + public void setCell(int i, int j, Cell obj) { if (obj is Jewell jewell) { @@ -29,9 +29,9 @@ public void setCell(int i, int j, Object obj) { arrayObj[i, j] = obstacle; } - else if (obj is Player player) + else if (obj is Robot Robot) { - arrayObj[i, j] = player; + arrayObj[i, j] = Robot; } } public void removeCell(int i, int j) @@ -52,30 +52,30 @@ public void PrintMap() } else if (arrayObj[i, j] is Jewell jewell) { - Console.Write(jewell.getName()); + Console.Write(jewell.Symbol); } else if (arrayObj[i, j] is Obstacle obstacle) { - Console.Write(obstacle.getSymbol()); + Console.Write(obstacle.Symbol); } - else if (arrayObj[i, j] is Player player) + else if (arrayObj[i, j] is Robot Robot) { - Console.Write(player.getName()); + Console.Write(Robot.Symbol); } } Console.WriteLine(); } } - public void FindPlayerPosition() + public void FindRobotPosition() { for (int i = 0; i < linha; i++) { for (int j = 0; j < coluna; j++) { - if (arrayObj[i, j] is Player player) + if (arrayObj[i, j] is Robot Robot) { - player.setplayerLine(i); - player.setplayerColumn(j); + Robot.setRobotLine(i); + Robot.setColumnRobot(j); } } } diff --git a/Classes/Obstacle.cs b/Classes/Obstacle.cs index e00c78d..a698c9b 100644 --- a/Classes/Obstacle.cs +++ b/Classes/Obstacle.cs @@ -1,19 +1,5 @@ namespace JewellNS; -public class Obstacle +public class Obstacle : Cell { - private string name; - private string symbol; - private int levelEnergy; - public Obstacle(string name, string symbol, int levelEnergy) - { - this.name = name; - this.symbol = symbol; - this.levelEnergy = levelEnergy; - } - public string getName() { return this.name; } - public string getSymbol() { return this.symbol; } - public int getLevelEnergy() { return this.levelEnergy; } - - public string toString() { return $"Obstaculo : {this.name}, Nível de energia: {this.levelEnergy}"; } -} \ No newline at end of file +} diff --git a/Classes/Player.cs b/Classes/Player.cs index fccb8de..08fe312 100644 --- a/Classes/Player.cs +++ b/Classes/Player.cs @@ -1,78 +1,68 @@ namespace JewellNS; -public class Player +public class Robot : Cell { + private Cell[] bagItems = new Cell[] { }; private int bagValue = 0; - private int energy = 5; - private Object[] bagItems = new Object[] { }; - private string name; - private int playerLine; - private int playerColumn; - public Player(string name) { this.name = name; } - public string getName() { return this.name; } - public int getEnergy() { return this.energy; } - public int getPlayerLine() { return this.playerLine; } - public int getColumnPlayer() { return this.playerColumn; } - public void setplayerLine(int linha) { this.playerLine = linha; } - public void setplayerColumn(int coluna) { this.playerColumn = coluna; } + private int RobotLine; + private int RobotColumn; + public int getRobotLine() { return this.RobotLine; } + public int getColumnRobot() { return this.RobotColumn; } + public void setRobotLine(int linha) { this.RobotLine = linha; } + public void setColumnRobot(int coluna) { this.RobotColumn = coluna; } - public string toString() { return $"Bag total items: {this.bagItems.Length} | Bag total value: {this.bagValue} | Energy: {this.energy}"; } - public void verifyEnergyLevel() + public string toString() { - Console.Clear(); - if (getEnergy() <= 0) - { - Console.WriteLine("Suas energias acabaram"); - Environment.Exit(0); - } + return $"Bag total items: {this.bagItems.Length} | Bag total value: {this.bagValue} | Energy: {this.LevelEnergy}"; } + public void moveToLeft(Map map) { - if (this.getColumnPlayer() > 0) + if (this.getColumnRobot() > 0) { - if (map.getObject(getPlayerLine(), getColumnPlayer() - 1) is not Jewell or Obstacle) + if (map.getCell(getRobotLine(), getColumnRobot() - 1) is not Jewell or Obstacle) { - energy--; - map.removeCell(getPlayerLine(), getColumnPlayer()); - map.setCell(getPlayerLine(), getColumnPlayer() - 1, this); + LevelEnergy--; + map.removeCell(getRobotLine(), getColumnRobot()); + map.setCell(getRobotLine(), getColumnRobot() - 1, this); verifyEnergyLevel(); } } } public void moveToRight(Map map) { - if (this.getColumnPlayer() <= map.getNumberOfColunas()) + if (this.getColumnRobot() <= map.getNumberOfColunas()) { - if (map.getObject(getPlayerLine(), getColumnPlayer() + 1) is not Jewell or Obstacle) + if (map.getCell(getRobotLine(), getColumnRobot() + 1) is not Jewell or Obstacle) { - energy--; - map.removeCell(getPlayerLine(), getColumnPlayer()); - map.setCell(getPlayerLine(), getColumnPlayer() + 1, this); + LevelEnergy--; + map.removeCell(getRobotLine(), getColumnRobot()); + map.setCell(getRobotLine(), getColumnRobot() + 1, this); verifyEnergyLevel(); } } } public void moveToTop(Map map) { - if (this.getPlayerLine() > 0) + if (this.getRobotLine() > 0) { - if (map.getObject(getPlayerLine() - 1, getColumnPlayer()) is not Jewell or Obstacle) + if (map.getCell(getRobotLine() - 1, getColumnRobot()) is not Jewell or Obstacle) { - energy--; - map.removeCell(getPlayerLine(), getColumnPlayer()); - map.setCell(getPlayerLine() - 1, getColumnPlayer(), this); + LevelEnergy--; + map.removeCell(getRobotLine(), getColumnRobot()); + map.setCell(getRobotLine() - 1, getColumnRobot(), this); verifyEnergyLevel(); } } } public void moveToBottom(Map map) { - if (this.getPlayerLine() <= map.getNumberOfLinhas()) + if (this.getRobotLine() <= map.getNumberOfLinhas()) { - if (map.getObject(getPlayerLine() + 1, getColumnPlayer()) is not Jewell or Obstacle) + if (map.getCell(getRobotLine() + 1, getColumnRobot()) is not Jewell or Obstacle) { - energy--; - map.removeCell(getPlayerLine(), getColumnPlayer()); - map.setCell(getPlayerLine() + 1, getColumnPlayer(), this); + LevelEnergy--; + map.removeCell(getRobotLine(), getColumnRobot()); + map.setCell(getRobotLine() + 1, getColumnRobot(), this); verifyEnergyLevel(); } } @@ -80,56 +70,49 @@ public void moveToBottom(Map map) public void captureItem(Map map) { - map.FindPlayerPosition(); - int linhaDoJogador = getPlayerLine(); - int colunaDoJogador = getColumnPlayer(); + map.FindRobotPosition(); + int linhaDoJogador = getRobotLine(); + int colunaDoJogador = getColumnRobot(); int captureRange = 1; - if (map.getObject(linhaDoJogador + captureRange, colunaDoJogador) is Jewell or Obstacle) + if (map.getCell(linhaDoJogador + captureRange, colunaDoJogador) is Jewell or Obstacle) { - updateEnergy(map.getObject(linhaDoJogador + captureRange, colunaDoJogador)); - updateBag(map.getObject(linhaDoJogador + captureRange, colunaDoJogador)); + updateBag(map.getCell(linhaDoJogador + captureRange, colunaDoJogador)); map.removeCell(linhaDoJogador + captureRange, colunaDoJogador); } - else if (map.getObject(linhaDoJogador - captureRange, colunaDoJogador) is Jewell or Obstacle) + else if (map.getCell(linhaDoJogador - captureRange, colunaDoJogador) is Jewell or Obstacle) { - updateEnergy(map.getObject(linhaDoJogador - captureRange, colunaDoJogador)); - updateBag(map.getObject(linhaDoJogador - captureRange, colunaDoJogador)); + updateBag(map.getCell(linhaDoJogador - captureRange, colunaDoJogador)); map.removeCell(linhaDoJogador - captureRange, colunaDoJogador); } - else if (map.getObject(linhaDoJogador, colunaDoJogador + captureRange) is Jewell or Obstacle) + else if (map.getCell(linhaDoJogador, colunaDoJogador + captureRange) is Jewell or Obstacle) { - updateEnergy(map.getObject(linhaDoJogador, colunaDoJogador + captureRange)); - updateBag(map.getObject(linhaDoJogador, colunaDoJogador + captureRange)); + updateBag(map.getCell(linhaDoJogador, colunaDoJogador + captureRange)); map.removeCell(linhaDoJogador, colunaDoJogador + captureRange); } - else if (map.getObject(linhaDoJogador, colunaDoJogador - captureRange) is Jewell or Obstacle) + else if (map.getCell(linhaDoJogador, colunaDoJogador - captureRange) is Jewell or Obstacle) { - updateEnergy(map.getObject(linhaDoJogador, colunaDoJogador - captureRange)); - updateBag(map.getObject(linhaDoJogador, colunaDoJogador - captureRange)); + updateBag(map.getCell(linhaDoJogador, colunaDoJogador - captureRange)); map.removeCell(linhaDoJogador, colunaDoJogador - captureRange); } } - private void updateBag(Object objeto) + private void updateBag(Cell objeto) { if (objeto is Jewell jewell) { - this.bagValue = this.bagValue + jewell.getPoint(); + this.bagValue = this.bagValue + jewell.Point; Array.Resize(ref bagItems, bagItems.Length + 1); bagItems[bagItems.Length - 1] = jewell; } } - - private void updateEnergy(Object objeto) + private void verifyEnergyLevel() { - if (objeto is Jewell jewell) - { - energy = energy + jewell.getLevelEnergy(); - } - else if (objeto is Obstacle Obstacle) + Console.Clear(); + if (LevelEnergy <= 0) { - energy = energy + Obstacle.getLevelEnergy(); + Console.WriteLine("Suas energias acabaram"); + Environment.Exit(0); } } } \ No newline at end of file diff --git a/JewellCollector.cs b/JewellCollector.cs index 4124e5f..5d91bde 100644 --- a/JewellCollector.cs +++ b/JewellCollector.cs @@ -4,13 +4,13 @@ public class JewelCollector public static void Main(string[] args) { Map map = new Map(10, 10); - Jewell jr = new Jewell("red", " JR ", 100, 0); - Jewell jg = new Jewell("green", " JG ", 50, 0 ); - Jewell jb = new Jewell("blue", " JB ", 10, 5); - Obstacle water = new Obstacle("water", " ## ", 0); - Obstacle tree = new Obstacle("tree", " $$ ", 3); - Player player = new Player(" ME "); - map.setCell(0, 0, player); + Jewell jr = new Jewell { Name = "red", Symbol = " JR ", Point = 100, LevelEnergy = 0 }; + Jewell jg = new Jewell { Name = "green", Symbol = " JG ", Point = 50, LevelEnergy = 0 }; + Jewell jb = new Jewell { Name = "blue", Symbol = " JB ", Point = 10, LevelEnergy = 5 }; + Obstacle water = new Obstacle { Name = "water", Symbol = " ## ", LevelEnergy = 0 }; + Obstacle tree = new Obstacle { Name = "tree", Symbol = " $$ ", LevelEnergy = 3 }; + Robot Robot = new Robot { Name = "robot", Symbol = " ME ", Point = 0, LevelEnergy = 5 }; + map.setCell(0, 0, Robot); map.setCell(1, 9, jr); map.setCell(8, 8, jr); map.setCell(9, 1, jg); @@ -24,38 +24,35 @@ public static void Main(string[] args) map.setCell(5, 4, water); map.setCell(5, 5, water); map.setCell(5, 6, water); - map.setCell(1, 1, tree); - map.setCell(5, 9, tree); map.setCell(3, 9, tree); map.setCell(8, 3, tree); map.setCell(2, 5, tree); map.setCell(1, 4, tree); map.PrintMap(); - map.FindPlayerPosition(); - Play(player, tree, water, jr, jb, jg, map); + map.FindRobotPosition(); + Play(Robot, tree, water, jr, jb, jg, map); } - public static void Play(Player player, Obstacle tree, Obstacle water, Jewell jr, Jewell jb, Jewell jg, Map map) + public static void Play(Robot Robot, Obstacle tree, Obstacle water, Jewell jr, Jewell jb, Jewell jg, Map map) { bool running = true; do { - Console.WriteLine(player.toString()); + Console.WriteLine(Robot.toString()); Console.WriteLine("Enter the command: "); string command = Console.ReadLine()!; - if (command.Equals("quit") || command.Equals("exit") || command.Equals("sair")) + if (command.Equals("quit")) { - Console.WriteLine("O jogo foi encerrado :c"); running = false; } else if (command.Equals("w")) { try { - map.FindPlayerPosition(); - player.moveToTop(map); + map.FindRobotPosition(); + Robot.moveToTop(map); map.PrintMap(); } catch (Exception e) @@ -67,8 +64,8 @@ public static void Play(Player player, Obstacle tree, Obstacle water, Jewell jr, { try { - map.FindPlayerPosition(); - player.moveToLeft(map); + map.FindRobotPosition(); + Robot.moveToLeft(map); map.PrintMap(); } catch (Exception e) @@ -80,8 +77,8 @@ public static void Play(Player player, Obstacle tree, Obstacle water, Jewell jr, { try { - map.FindPlayerPosition(); - player.moveToBottom(map); + map.FindRobotPosition(); + Robot.moveToBottom(map); map.PrintMap(); } catch (Exception e) @@ -93,8 +90,8 @@ public static void Play(Player player, Obstacle tree, Obstacle water, Jewell jr, { try { - map.FindPlayerPosition(); - player.moveToRight(map); + map.FindRobotPosition(); + Robot.moveToRight(map); map.PrintMap(); } catch (Exception e) @@ -106,8 +103,8 @@ public static void Play(Player player, Obstacle tree, Obstacle water, Jewell jr, { try { - map.FindPlayerPosition(); - player.captureItem(map); + map.FindRobotPosition(); + Robot.captureItem(map); map.PrintMap(); } catch