Skip to content

Commit

Permalink
refactor code #8 #12
Browse files Browse the repository at this point in the history
Co-authored-by: Gabriel Rodrigues <[email protected]>
  • Loading branch information
FrancoEdu and ApenasGabs committed Jun 3, 2023
1 parent 5834042 commit cf09332
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 142 deletions.
9 changes: 9 additions & 0 deletions Classes/Cell.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
22 changes: 1 addition & 21 deletions Classes/Jewell.cs
Original file line number Diff line number Diff line change
@@ -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}";
}
}
28 changes: 14 additions & 14 deletions Classes/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace JewellNS;

public class Map
{
private Object[,] arrayObj;
private Cell[,] arrayObj;
private int linha;
private int coluna;

Expand All @@ -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)
{
Expand All @@ -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)
Expand All @@ -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);
}
}
}
Expand Down
18 changes: 2 additions & 16 deletions Classes/Obstacle.cs
Original file line number Diff line number Diff line change
@@ -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}"; }
}
}
115 changes: 49 additions & 66 deletions Classes/Player.cs
Original file line number Diff line number Diff line change
@@ -1,135 +1,118 @@
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();
}
}
}

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);
}
}
}
Loading

0 comments on commit cf09332

Please sign in to comment.