Commit f6f8a170 by Theo Boulaire

### Placement of ships for AI.

parent f208f789
 ... ... @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; import javafx.util.Pair; ... ... @@ -188,6 +189,80 @@ public class GameMediator { } } /** * */ public void setComputerShips() { Player cPlayer = this.currentGame.getComputerPlayer(); List listShips = this.currentGame.getTemplateShips(); for(int s = 0; s < listShips.size(); s++) { craftCoordinates(listShips, listShips.get(s)); } } /** * Gives a random position to a ship. * * @param previousShips * @param ship */ private void craftCoordinates(List previousShips, Ship ship) { List> allCoords = this.createAvailableCoordinates(previousShips, ship); Random r = new Random(); int position = r.nextInt(allCoords.size()); ship.setListCoord(allCoords.get(position)); } private List> createAvailableCoordinates(List previousShips, Ship ship) { int size = ship.getSize(); List> returnList = new ArrayList(); for(int x = 0; x < 10; x++) { for(int y = 0; y < 10; y++) { Coordinate c = new Coordinate(x,y); if(c.isAllowed(previousShips)) { //Crafting the West -> East coordinates. List coordsWE = new ArrayList(); coordsWE.add(new Coordinate(x,y)); boolean allowed = true; for(int s = 0; s < previousShips.size(); s++) { Coordinate cSuite = new Coordinate(x + s, y); if(!cSuite.isAllowed(previousShips)) { allowed = false; break; } else { coordsWE.add(cSuite); } } if(allowed) { returnList.add(coordsWE); } //Crafting the South->North coordinates. List coordsSN = new ArrayList(); coordsSN.add(new Coordinate(x,y)); allowed = true; for(int s = 0; s < previousShips.size(); s++) { Coordinate cSuite = new Coordinate(x, y + s); if(!cSuite.isAllowed(previousShips)) { allowed = false; break; } else { coordsSN.add(cSuite); } } if(allowed) { returnList.add(coordsSN); } } } } return returnList; } /** * ... ...
 ... ... @@ -18,6 +18,7 @@ public class ComputerPlayer extends Player{ public ComputerPlayer() { super(LightPublicUser.generateComputerProfile()); this.focus = null; this.setComputer(true); } ... ...
 ... ... @@ -4,6 +4,8 @@ * and open the template in the editor. */ package com.utclo23.data.structure; import java.util.ArrayList; import java.util.List; /** * ... ... @@ -34,7 +36,45 @@ public class Coordinate extends SerializableEntity{ this.y = y; } @Override public boolean equals(Object o){ if(o == this) { return true; } if (!(o instanceof Coordinate)) { return false; } Coordinate c = (Coordinate) o; return ((c.getX() == this.getX()) && (c.getY() == this.getY())); } /** * Checks if the coordinate is taken by a ship of a list. * * @param ships * @return */ public boolean isAllowed(List ships) { if(!this.isInBoard()) { return false; } boolean available = true; for(int s = 0; s < ships.size(); s++) { if(ships.get(s).isCrossed(this)) { available = false; break; } } return available; } /** * Ckecks if the coordinate is within the board. * @return */ private boolean isInBoard() { return (x < 10 && x >= 0 && y < 10 && y >= 0); } }
 ... ... @@ -131,8 +131,12 @@ public abstract class Game extends SerializableEntity { public List getRecipients() { List listRecipients = new ArrayList<>(); for(int i = 0; i < this.getPlayers().size(); ++i) listRecipients.add(this.getPlayers().get(i).getLightPublicUser()); for(int i = 0; i < this.getPlayers().size(); ++i) { if(!(this.getPlayers().get(i).isComputer())) { listRecipients.add(this.getPlayers().get(i).getLightPublicUser()); } } listRecipients.addAll(this.getSpectators()) ; return listRecipients; } ... ... @@ -447,5 +451,20 @@ public abstract class Game extends SerializableEntity { return isSpectator; } /** * * @return If a player of this game is a computer */ public boolean isComputerGame() { return (this.getPlayers().get(0).isComputer() || this.getPlayers().get(1).isComputer()); } public Player getComputerPlayer() { for(int i = 0; i < players.size(); i++) { if(players.get(i).isComputer()) { return players.get(i); } } return null; } }
 ... ... @@ -36,6 +36,21 @@ public class Ship extends SerializableEntity{ public ShipType getType() { return type; } /** * Checks if a coordinate belongs to the ship. * * @param coord * @return */ public boolean isCrossed(Coordinate coord) { for(int i = 0; i < listCoord.size(); i++){ if(coord.equals(listCoord.get(i))) { return true; } } return false; } public void setType(ShipType type) { this.type = type; ... ...
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment