// Compares to previous tile instead of a second ship private void SetTileStateWithOneShip(BooleanDCNode tile, LabelledDCNode ship, BooleanDCNode node, string name) { int shipLength = Settings.boardWidth + 1 - ship.GetTable().GetData().Length / (Settings.dimension * Settings.boardWidth); int xCoord = name[0] - 'A'; int yCoord = name[1] - '0'; Point tilePlace = new Point(xCoord, yCoord); List <Point> shipPoints; bool labelIsTrue; string shipName; ulong count = 0; for (ulong i = 0; i < 2; i++) { labelIsTrue = node.GetStateLabel(i) == "True"; for (ulong j = 0; j < ship.GetNumberOfStates(); j++) { shipName = ship.GetStateLabel(j); shipPoints = ReturnCoordinates(shipLength, shipName); if (labelIsTrue || shipPoints.Contains(tilePlace)) { tile.GetTable().SetDataItem(count++, 0); tile.GetTable().SetDataItem(count++, 1); } else { tile.GetTable().SetDataItem(count++, 1); tile.GetTable().SetDataItem(count++, 0); } } } }
private string FindShipPos(int length, LabelledDCNode ship) { List <List <Point> > allPossiblePos = new List <List <Point> >(); // Runs through a list of hit tiles, which aren't labeled sunken yet for (int i = 0; i < previousHits.Count; i++) { if (FindShipOrientation(length, 0, 1, previousHits[i])) { allPossiblePos.Add(CreateShipPosList(length, 'V', previousHits[i])); } else if (FindShipOrientation(length, 1, 0, previousHits[i])) { allPossiblePos.Add(CreateShipPosList(length, 'H', previousHits[i])); } } return(SelectBestBelief(ship, allPossiblePos)); }
// Finds the ship position with the greatest probability private string SelectBestBelief(LabelledDCNode ship, List <List <Point> > allPossiblePos) { List <double> beliefs = new List <double>(); List <string> startCoords = new List <string>(); double bestBelief = 0; string startCoord = ""; long beliefIndex; // Runs through all possible ship positions foreach (List <Point> list in allPossiblePos) { // Enters if the two first Y-coordinates have the same value if (list[0].Y - list[1].Y == 0) { startCoord = $"H_{list[0].X}{list[0].Y}"; } else { startCoord = $"V_{list[0].X}{list[0].Y}"; } beliefIndex = ship.GetStateIndex(startCoord); beliefs.Add(ship.GetBelief((ulong)beliefIndex)); startCoords.Add(startCoord); } for (int i = 0; i < beliefs.Count; i++) { // Enters if the probability is greater than those already found if (beliefs[i] > bestBelief) { bestBelief = beliefs[i]; startCoord = startCoords[i]; } } if (startCoord != "") { return(startCoord); } throw new ArgumentException("Something went wrong while trying to find sunken ship's start coordinate."); }
private void SetAllStatesForShips(LabelledDCNode ship, int length) { double possiblePosForRow = Settings.boardWidth - length + 1; // Finds all possible positions on the board for the ship double numberOfStates = Settings.boardWidth * possiblePosForRow * Settings.dimension; ulong count = 0; ship.SetNumberOfStates((ulong)numberOfStates); // Sets state labels and creates table for all states of the ship // Runs through the two orientations; horizontal and vertical for (int orientation = 0; orientation < Settings.dimension; orientation++) { for (int i = 0; i < (orientation == 0 ? Settings.boardWidth : possiblePosForRow); i++) { for (int j = 0; j < (orientation == 1 ? Settings.boardWidth : possiblePosForRow); j++) { ship.SetStateLabel(count, (orientation == 1 ? "H" : "V") + $"_{i}{j}"); ship.GetTable().SetDataItem(count++, 1 / numberOfStates); } } } }
// Compares to two ships private void SetTileStateWithTwoShips(BooleanDCNode tile, LabelledDCNode secondShip, LabelledDCNode firstShip, string name) { int firstShipLength = Settings.boardWidth + 1 - firstShip.GetTable().GetData().Length / (Settings.dimension * Settings.boardWidth); int secondShipLength = Settings.boardWidth + 1 - secondShip.GetTable().GetData().Length / (Settings.dimension * Settings.boardWidth); int xCoord = name[0] - 'A'; int yCoord = name[1] - '0'; List <Point> firstPoints; List <Point> secondPoints; Point tilePlace = new Point(xCoord, yCoord); ulong count = 0; string firstName, secondName; for (int i = 0; i < (int)firstShip.GetNumberOfStates(); i++) { firstName = firstShip.GetStateLabel((ulong)i); // Gets coordinates for first ship firstPoints = ReturnCoordinates(firstShipLength, firstName); for (int j = 0; j < (int)secondShip.GetNumberOfStates(); j++) { secondName = secondShip.GetStateLabel((ulong)j); // Gets coordinates for second ship secondPoints = ReturnCoordinates(secondShipLength, secondName); if (secondPoints.Contains(tilePlace) || firstPoints.Contains(tilePlace)) { tile.GetTable().SetDataItem(count++, 0); tile.GetTable().SetDataItem(count++, 1); } else { tile.GetTable().SetDataItem(count++, 1); tile.GetTable().SetDataItem(count++, 0); } } } }
private void SetStatesForOverlaps(BooleanDCNode overlap, LabelledDCNode secondShip, LabelledDCNode firstShip) { int firstShipLength = Settings.boardWidth + 1 - firstShip.GetTable().GetData().Length / (Settings.dimension * Settings.boardWidth); int secondShipLength = Settings.boardWidth + 1 - secondShip.GetTable().GetData().Length / (Settings.dimension * Settings.boardWidth); List <Point> firstPoints; List <Point> secondPoints; ulong count = 0; string firstName, secondName; // Iterates through entire tables on constraint's parents for (int i = 0; i < (int)firstShip.GetNumberOfStates(); i++) { firstName = firstShip.GetStateLabel((ulong)i); // Gets coordinates for first ship firstPoints = ReturnCoordinates(firstShipLength, firstName); for (int j = 0; j < (int)secondShip.GetNumberOfStates(); j++) { secondName = secondShip.GetStateLabel((ulong)j); // Gets coordinates for second ship secondPoints = ReturnCoordinates(secondShipLength, secondName); // Checks if any ship coordinates overlap if (CheckForOverlap(firstPoints, secondPoints)) { overlap.GetTable().SetDataItem(count++, 0); overlap.GetTable().SetDataItem(count++, 1); } else { overlap.GetTable().SetDataItem(count++, 1); overlap.GetTable().SetDataItem(count++, 0); } } } }