public ShotResult markShipAsSunkIfReallyIs(int x, int y) { bool[,] matrix = new bool[10, 10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { matrix[i, j] = true; } } bool result = isWholeShipSunkRecursive(x, y, ref matrix); if (result) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { matrix[i, j] = matrix[i, j] ^ true; // toggling values if (matrix[i, j]) { ships[i, j].State = Ship.ShipState.SUNK; } } } } return(new ShotResult(true, result, isGameEnded(), Coordinates.Get(x, y), result ? matrix : null)); }
public ShotResult Shoot(Coordinates field) { int x = field.X; int y = field.Y; if (ships[x, y].Shoot()) { return(markShipAsSunkIfReallyIs(x, y)); } else { return(new ShotResult(false, false, isGameEnded(), Coordinates.Get(x, y), (bool[, ])null)); } }
public static bool[,] generateMatrix() { bool[,] matrix = new bool[10, 10]; ArrayList fields = new ArrayList(); ArrayList tmp = new ArrayList(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { matrix[i, j] = false; fields.Add(Coordinates.Get(i, j)); } } Random rand = new Random(); for (int size = Global.SHIPS_COUNER.Length - 1; size > 1; size--) { for (int no = 0; no < Global.SHIPS_COUNER[size]; no++) { ShuffleInPlace(fields); foreach (Coordinates field in fields) { bool possible = true; bool isHorizontal = rand.Next() % 2 == 0; tmp.Clear(); if (isHorizontal) { for (int i = 1; i < size; i++) { if (field.X + i < 10 && fields.Contains(Coordinates.Get(field.X + i, field.Y))) { tmp.Add(Coordinates.Get(field.X + i, field.Y)); } else { possible = false; } } if (!possible) { tmp.Clear(); for (int i = 1; i < size; i++) { if (field.X - i >= 0 && fields.Contains(Coordinates.Get(field.X - i, field.Y))) { tmp.Add(Coordinates.Get(field.X - i, field.Y)); } else { possible = false; } } } } else { for (int i = 1; i < size; i++) { if (field.Y + i < 10 && fields.Contains(Coordinates.Get(field.X, field.Y + i))) { tmp.Add(Coordinates.Get(field.X, field.Y + i)); } else { possible = false; } } if (!possible) { tmp.Clear(); for (int i = 1; i < size; i++) { if (field.Y - i >= 0 && fields.Contains(Coordinates.Get(field.X, field.Y - i))) { tmp.Add(Coordinates.Get(field.X, field.Y - i)); } else { possible = false; } } } } if (possible) { tmp.Add(field); foreach (Coordinates tmpField in tmp) { matrix[tmpField.X, tmpField.Y] = true; fields.Remove(tmpField); fields.Remove(Coordinates.Get(tmpField.X - 1, tmpField.Y - 1)); fields.Remove(Coordinates.Get(tmpField.X, tmpField.Y - 1)); fields.Remove(Coordinates.Get(tmpField.X + 1, tmpField.Y - 1)); fields.Remove(Coordinates.Get(tmpField.X + 1, tmpField.Y)); fields.Remove(Coordinates.Get(tmpField.X + 1, tmpField.Y + 1)); fields.Remove(Coordinates.Get(tmpField.X, tmpField.Y + 1)); fields.Remove(Coordinates.Get(tmpField.X - 1, tmpField.Y + 1)); fields.Remove(Coordinates.Get(tmpField.X - 1, tmpField.Y)); } break; // stop searching } } } } return(matrix); }
public static GamePacket deserialize(String packetString) { GamePacket packet = null; GamePacket.TYPE type = GamePacket.TYPE.UNDEFINED; String message = null; Coordinates coordinates = null; bool whoStarts = false; ShotResult shotResult = null; GameResult gameResult = null; bool[,] matrix = null; bool isHit = false; bool isSunk = false; bool isGameEnded = false; XmlTextReader textReader = new XmlTextReader(new StringReader(packetString)); textReader.Read(); //Console.WriteLine("deserialization is ready to start:\n" + packetString); while (textReader.Read()) { if (textReader.NodeType == XmlNodeType.Element) { if (textReader.Name.Equals("gamePacket")) { String typeString = textReader.GetAttribute("type"); if (typeString.Equals("WHO_STARTS")) { type = GamePacket.TYPE.WHO_STARTS; whoStarts = textReader.GetAttribute("whoStarts").Equals("host") ? Global.HOST_FIRST : Global.CLIENT_FIRST; packet = new GamePacket(whoStarts); } else if (typeString.Equals("TEXT_MESSAGE")) { type = GamePacket.TYPE.TEXT_MESSAGE; message = textReader.GetAttribute("message"); packet = new GamePacket(message); } else if (typeString.Equals("SHOT")) { type = GamePacket.TYPE.SHOT; } else if (typeString.Equals("RESULT")) { type = GamePacket.TYPE.RESULT; } else if (typeString.Equals("GAME_RESULT")) { type = GamePacket.TYPE.GAME_RESULT; } } else if (textReader.Name.Equals("shotResult") && textReader.GetAttribute("isNull").Equals("false")) { isHit = Boolean.Parse(textReader.GetAttribute("isHit")); isSunk = Boolean.Parse(textReader.GetAttribute("isSunk")); isGameEnded = Boolean.Parse(textReader.GetAttribute("isGameEnded")); String matrixStr = textReader.GetAttribute("matrix"); if (!matrixStr.Equals("null")) { matrix = Global.stringToBoolArray(matrixStr); } } else if (textReader.Name.Equals("coordinates") && textReader.GetAttribute("isNull").Equals("false")) { int x = Int32.Parse(textReader.GetAttribute("x")); int y = Int32.Parse(textReader.GetAttribute("y")); coordinates = Coordinates.Get(x, y); } else if (textReader.Name.Equals("gameResult") && textReader.GetAttribute("isNull").Equals("false")) { gameResult = new GameResult(textReader.GetAttribute("result").Equals("winner") ? Global.GAME_RESULT_WINNER : Global.GAME_RESULT_LOOSER); } } else if (textReader.NodeType == XmlNodeType.EndElement) { if (textReader.Name.Equals("gamePacket")) { switch (type) { case GamePacket.TYPE.GAME_RESULT: packet = new GamePacket(gameResult); break; case GamePacket.TYPE.RESULT: packet = new GamePacket(shotResult); break; case GamePacket.TYPE.SHOT: packet = new GamePacket(coordinates); break; case GamePacket.TYPE.TEXT_MESSAGE: packet = new GamePacket(message); break; case GamePacket.TYPE.WHO_STARTS: packet = new GamePacket(whoStarts); break; default: break; } } else if (textReader.Name.Equals("shotResult")) { shotResult = new ShotResult(isHit, isSunk, isGameEnded, coordinates, matrix); } } } return(packet); }
public GamePacket(int x, int y) { coordinates = Coordinates.Get(x, y); type = TYPE.SHOT; }