private double CalculateSafetyRatio() { if (NumMyShips + NumEnemyShips == 0) { return(1.0); } double myVal = MyShips.Sum(s => 1 / Math.Sqrt(1 + GameInfo.Distance(s, RootCell))); double enemyVal = EnemyShips.Sum(s => 1 / Math.Sqrt(1 + GameInfo.Distance(s, RootCell))); return((.5 + myVal) / (myVal + enemyVal)); }
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 11111); listener.Start(); TcpClient newClient = listener.AcceptTcpClient(); NetworkStream tcpStream = newClient.GetStream(); byte[] bytes = new byte[newClient.ReceiveBufferSize]; // буфер входящей информации tcpStream.Read(bytes, 0, bytes.Length); // проверка попал ли выстрел по кораблю string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length); int read = Convert.ToInt32(str); _dispatcher.Invoke(new Action(() => { if (MyShips.ElementAt(read).Deck == 0) { string negative = "0"; byte[] ans = Encoding.UTF8.GetBytes(negative); tcpStream.Write(ans, 0, ans.Length); MyShips.RemoveAt(read); Ship shot = new Ship(); shot.Content = ""; shot.Name = "O"; shot.IsEnabled = false; MyShips.Insert(read, shot); ShotEnabled = true; } else { string positive = "1"; byte[] ans = Encoding.UTF8.GetBytes(positive); tcpStream.Write(ans, 0, ans.Length); MyShips.RemoveAt(read); Ship shot = new Ship(); shot.Content = ""; shot.Name = "X"; shot.IsEnabled = false; MyShips.Insert(read, shot); ShotEnabled = true; } })); tcpStream.Close(); listener.Stop(); }
public void Reset(Ship x) { sh = 20; MyShips.Clear(); MyShips = new ObservableCollection <Ship>(); for (int i = 0; i < 100; i++) { Ship test = new Ship(); test.Content = ""; test.Name = i.ToString(); test.Deck = 0; test.IsEnabled = true; test.Background = "LightGray"; MyShips.Add(test); } StartEnabled = false; SetShipEnabled = true; }
private void UpdateMyShips() { var shipCatalog = GameMap.GetMyPlayer().GetShips(); foreach (var pair in shipCatalog) { if (!MyShips.ContainsKey(pair.Key)) { MyShips.Add(pair.Key, ActivateShip(pair.Value)); } else { if (pair.Value.GetHealth() <= 0) { MyShips.Remove(pair.Key); } } } }
public void SetShip(Ship x) { if (x != null && x.Deck == 0 && sh != 0) // если выбрано поле, туда еще не ставили корабль и еще остались корабли в остатке { int coordinate = Convert.ToInt32(x.Name); int[] edgeTop = { 1, 2, 3, 4, 5, 6, 7, 8 }; int[] edgeRight = { 19, 29, 39, 49, 59, 69, 79, 89 }; int[] edgeButtom = { 91, 92, 93, 94, 95, 96, 97, 98 }; int[] edgeLeft = { 10, 20, 30, 40, 50, 60, 70, 80 }; int[] corner = { 0, 9, 90, 99 }; Ship shot = new Ship(); shot.Content = ""; shot.Name = x.Name; shot.Deck = 1; shot.IsEnabled = true; shot.Background = "Green"; MyShips.RemoveAt(coordinate); MyShips.Insert(coordinate, shot); sh--; #region Углы if (corner.Any(s => s == coordinate & s == 0)) { #region Ship not1 = new Ship(); not1.Name = (coordinate + 1).ToString(); not1.Deck = 0; not1.IsEnabled = true; not1.Background = "Red"; Ship not2 = new Ship(); not2.Name = (coordinate + 10).ToString(); not2.Deck = 0; not2.IsEnabled = true; not2.Background = "Red"; Ship not3 = new Ship(); not3.Name = (coordinate + 11).ToString(); not3.Deck = 0; not3.IsEnabled = true; not3.Background = "Red"; if (MyShips.ElementAt(coordinate + 1).Background != "Green") { MyShips.RemoveAt(coordinate + 1); MyShips.Insert(coordinate + 1, not1); } if (MyShips.ElementAt(coordinate + 10).Background != "Green") { MyShips.RemoveAt(coordinate + 10); MyShips.Insert(coordinate + 10, not2); } if (MyShips.ElementAt(coordinate + 11).Background != "Green") { MyShips.RemoveAt(coordinate + 11); MyShips.Insert(coordinate + 11, not3); } #endregion } else if (corner.Any(s => s == coordinate & s == 9)) { #region Ship not1 = new Ship(); not1.Name = (coordinate - 1).ToString(); not1.Deck = 0; not1.IsEnabled = true; not1.Background = "Red"; Ship not2 = new Ship(); not2.Name = (coordinate + 10).ToString(); not2.Deck = 0; not2.IsEnabled = true; not2.Background = "Red"; Ship not3 = new Ship(); not3.Name = (coordinate + 9).ToString(); not3.Deck = 0; not3.IsEnabled = true; not3.Background = "Red"; if (MyShips.ElementAt(coordinate - 1).Background != "Green") { MyShips.RemoveAt(coordinate - 1); MyShips.Insert(coordinate - 1, not1); } if (MyShips.ElementAt(coordinate + 10).Background != "Green") { MyShips.RemoveAt(coordinate + 10); MyShips.Insert(coordinate + 10, not2); } if (MyShips.ElementAt(coordinate + 9).Background != "Green") { MyShips.RemoveAt(coordinate + 9); MyShips.Insert(coordinate + 9, not3); } #endregion } else if (corner.Any(s => s == coordinate & s == 90)) { #region Ship not1 = new Ship(); not1.Name = (coordinate + 1).ToString(); not1.Deck = 0; not1.IsEnabled = true; not1.Background = "Red"; Ship not2 = new Ship(); not2.Name = (coordinate - 10).ToString(); not2.Deck = 0; not2.IsEnabled = true; not2.Background = "Red"; Ship not3 = new Ship(); not3.Name = (coordinate - 9).ToString(); not3.Deck = 0; not3.IsEnabled = true; not3.Background = "Red"; if (MyShips.ElementAt(coordinate + 1).Background != "Green") { MyShips.RemoveAt(coordinate + 1); MyShips.Insert(coordinate + 1, not1); } if (MyShips.ElementAt(coordinate - 10).Background != "Green") { MyShips.RemoveAt(coordinate - 10); MyShips.Insert(coordinate - 10, not2); } if (MyShips.ElementAt(coordinate - 9).Background != "Green") { MyShips.RemoveAt(coordinate - 9); MyShips.Insert(coordinate - 9, not3); } #endregion } else if (corner.Any(s => s == coordinate & s == 99)) { #region Ship not1 = new Ship(); not1.Name = (coordinate - 1).ToString(); not1.Deck = 0; not1.IsEnabled = true; not1.Background = "Red"; Ship not2 = new Ship(); not2.Name = (coordinate - 10).ToString(); not2.Deck = 0; not2.IsEnabled = true; not2.Background = "Red"; Ship not3 = new Ship(); not3.Name = (coordinate - 11).ToString(); not3.Deck = 0; not3.IsEnabled = true; not3.Background = "Red"; if (MyShips.ElementAt(coordinate - 1).Background != "Green") { MyShips.RemoveAt(coordinate - 1); MyShips.Insert(coordinate - 1, not1); } if (MyShips.ElementAt(coordinate - 10).Background != "Green") { MyShips.RemoveAt(coordinate - 10); MyShips.Insert(coordinate - 10, not2); } if (MyShips.ElementAt(coordinate - 11).Background != "Green") { MyShips.RemoveAt(coordinate - 11); MyShips.Insert(coordinate - 11, not3); } #endregion } #endregion #region Грани else if (edgeTop.Any(s => s == coordinate)) { #region Ship not1 = new Ship(); not1.Name = (coordinate - 1).ToString(); not1.Deck = 0; not1.IsEnabled = true; not1.Background = "Red"; Ship not2 = new Ship(); not2.Name = (coordinate + 1).ToString(); not2.Deck = 0; not2.IsEnabled = true; not2.Background = "Red"; Ship not3 = new Ship(); not3.Name = (coordinate + 9).ToString(); not3.Deck = 0; not3.IsEnabled = true; not3.Background = "Red"; Ship not4 = new Ship(); not4.Name = (coordinate + 10).ToString(); not4.Deck = 0; not4.IsEnabled = true; not4.Background = "Red"; Ship not5 = new Ship(); not5.Name = (coordinate + 11).ToString(); not5.Deck = 0; not5.IsEnabled = true; not5.Background = "Red"; if (MyShips.ElementAt(coordinate - 1).Background != "Green") { MyShips.RemoveAt(coordinate - 1); MyShips.Insert(coordinate - 1, not1); } if (MyShips.ElementAt(coordinate + 1).Background != "Green") { MyShips.RemoveAt(coordinate + 1); MyShips.Insert(coordinate + 1, not2); } if (MyShips.ElementAt(coordinate + 9).Background != "Green") { MyShips.RemoveAt(coordinate + 9); MyShips.Insert(coordinate + 9, not3); } if (MyShips.ElementAt(coordinate + 10).Background != "Green") { MyShips.RemoveAt(coordinate + 10); MyShips.Insert(coordinate + 10, not4); } if (MyShips.ElementAt(coordinate + 11).Background != "Green") { MyShips.RemoveAt(coordinate + 11); MyShips.Insert(coordinate + 11, not5); } #endregion } else if (edgeRight.Any(s => s == coordinate)) { #region Ship not1 = new Ship(); not1.Name = (coordinate - 11).ToString(); not1.Deck = 0; not1.IsEnabled = true; not1.Background = "Red"; Ship not2 = new Ship(); not2.Name = (coordinate - 10).ToString(); not2.Deck = 0; not2.IsEnabled = true; not2.Background = "Red"; Ship not3 = new Ship(); not3.Name = (coordinate - 1).ToString(); not3.Deck = 0; not3.IsEnabled = true; not3.Background = "Red"; Ship not4 = new Ship(); not4.Name = (coordinate + 9).ToString(); not4.Deck = 0; not4.IsEnabled = true; not4.Background = "Red"; Ship not5 = new Ship(); not5.Name = (coordinate + 10).ToString(); not5.Deck = 0; not5.IsEnabled = true; not5.Background = "Red"; if (MyShips.ElementAt(coordinate - 11).Background != "Green") { MyShips.RemoveAt(coordinate - 11); MyShips.Insert(coordinate - 11, not1); } if (MyShips.ElementAt(coordinate - 10).Background != "Green") { MyShips.RemoveAt(coordinate - 10); MyShips.Insert(coordinate - 10, not2); } if (MyShips.ElementAt(coordinate - 1).Background != "Green") { MyShips.RemoveAt(coordinate - 1); MyShips.Insert(coordinate - 1, not3); } if (MyShips.ElementAt(coordinate + 9).Background != "Green") { MyShips.RemoveAt(coordinate + 9); MyShips.Insert(coordinate + 9, not4); } if (MyShips.ElementAt(coordinate + 10).Background != "Green") { MyShips.RemoveAt(coordinate + 10); MyShips.Insert(coordinate + 10, not5); } #endregion } else if (edgeButtom.Any(s => s == coordinate)) { #region Ship not1 = new Ship(); not1.Name = (coordinate - 11).ToString(); not1.Deck = 0; not1.IsEnabled = true; not1.Background = "Red"; Ship not2 = new Ship(); not2.Name = (coordinate - 10).ToString(); not2.Deck = 0; not2.IsEnabled = true; not2.Background = "Red"; Ship not3 = new Ship(); not3.Name = (coordinate - 9).ToString(); not3.Deck = 0; not3.IsEnabled = true; not3.Background = "Red"; Ship not4 = new Ship(); not4.Name = (coordinate - 1).ToString(); not4.Deck = 0; not4.IsEnabled = true; not4.Background = "Red"; Ship not5 = new Ship(); not5.Name = (coordinate + 1).ToString(); not5.Deck = 0; not5.IsEnabled = true; not5.Background = "Red"; if (MyShips.ElementAt(coordinate - 11).Background != "Green") { MyShips.RemoveAt(coordinate - 11); MyShips.Insert(coordinate - 11, not1); } if (MyShips.ElementAt(coordinate - 10).Background != "Green") { MyShips.RemoveAt(coordinate - 10); MyShips.Insert(coordinate - 10, not2); } if (MyShips.ElementAt(coordinate - 9).Background != "Green") { MyShips.RemoveAt(coordinate - 9); MyShips.Insert(coordinate - 9, not3); } if (MyShips.ElementAt(coordinate - 1).Background != "Green") { MyShips.RemoveAt(coordinate - 1); MyShips.Insert(coordinate - 1, not4); } if (MyShips.ElementAt(coordinate + 1).Background != "Green") { MyShips.RemoveAt(coordinate + 1); MyShips.Insert(coordinate + 1, not5); } #endregion } else if (edgeLeft.Any(s => s == coordinate)) { #region Ship not1 = new Ship(); not1.Name = (coordinate - 10).ToString(); not1.Deck = 0; not1.IsEnabled = true; not1.Background = "Red"; Ship not2 = new Ship(); not2.Name = (coordinate + 1).ToString(); not2.Deck = 0; not2.IsEnabled = true; not2.Background = "Red"; Ship not3 = new Ship(); not3.Name = (coordinate - 9).ToString(); not3.Deck = 0; not3.IsEnabled = true; not3.Background = "Red"; Ship not4 = new Ship(); not4.Name = (coordinate + 10).ToString(); not4.Deck = 0; not4.IsEnabled = true; not4.Background = "Red"; Ship not5 = new Ship(); not5.Name = (coordinate + 11).ToString(); not5.Deck = 0; not5.IsEnabled = true; not5.Background = "Red"; if (MyShips.ElementAt(coordinate - 10).Background != "Green") { MyShips.RemoveAt(coordinate - 10); MyShips.Insert(coordinate - 10, not1); } if (MyShips.ElementAt(coordinate + 1).Background != "Green") { MyShips.RemoveAt(coordinate + 1); MyShips.Insert(coordinate + 1, not2); } if (MyShips.ElementAt(coordinate - 9).Background != "Green") { MyShips.RemoveAt(coordinate - 9); MyShips.Insert(coordinate - 9, not3); } if (MyShips.ElementAt(coordinate + 10).Background != "Green") { MyShips.RemoveAt(coordinate + 10); MyShips.Insert(coordinate + 10, not4); } if (MyShips.ElementAt(coordinate + 11).Background != "Green") { MyShips.RemoveAt(coordinate + 11); MyShips.Insert(coordinate + 11, not5); } #endregion } #endregion else { #region покраска Ship not1 = new Ship(); not1.Name = (coordinate - 11).ToString(); not1.Deck = 0; not1.IsEnabled = true; not1.Background = "Red"; Ship not2 = new Ship(); not2.Name = (coordinate - 10).ToString(); not2.Deck = 0; not2.IsEnabled = true; not2.Background = "Red"; Ship not3 = new Ship(); not3.Name = (coordinate - 9).ToString(); not3.Deck = 0; not3.IsEnabled = true; not3.Background = "Red"; Ship not4 = new Ship(); not4.Name = (coordinate - 1).ToString(); not4.Deck = 0; not4.IsEnabled = true; not4.Background = "Red"; Ship not5 = new Ship(); not5.Name = (coordinate + 1).ToString(); not5.Deck = 0; not5.IsEnabled = true; not5.Background = "Red"; Ship not6 = new Ship(); not6.Name = (coordinate + 9).ToString(); not6.Deck = 0; not6.IsEnabled = true; not6.Background = "Red"; Ship not7 = new Ship(); not7.Name = (coordinate + 10).ToString(); not7.Deck = 0; not7.IsEnabled = true; not7.Background = "Red"; Ship not8 = new Ship(); not8.Name = (coordinate + 11).ToString(); not8.Deck = 0; not8.IsEnabled = true; not8.Background = "Red"; if (MyShips.ElementAt(coordinate - 11).Background != "Green") { MyShips.RemoveAt(coordinate - 11); MyShips.Insert(coordinate - 11, not1); } if (MyShips.ElementAt(coordinate - 10).Background != "Green") { MyShips.RemoveAt(coordinate - 10); MyShips.Insert(coordinate - 10, not2); } if (MyShips.ElementAt(coordinate - 9).Background != "Green") { MyShips.RemoveAt(coordinate - 9); MyShips.Insert(coordinate - 9, not3); } if (MyShips.ElementAt(coordinate - 1).Background != "Green") { MyShips.RemoveAt(coordinate - 1); MyShips.Insert(coordinate - 1, not4); } if (MyShips.ElementAt(coordinate + 1).Background != "Green") { MyShips.RemoveAt(coordinate + 1); MyShips.Insert(coordinate + 1, not5); } if (MyShips.ElementAt(coordinate + 9).Background != "Green") { MyShips.RemoveAt(coordinate + 9); MyShips.Insert(coordinate + 9, not6); } if (MyShips.ElementAt(coordinate + 10).Background != "Green") { MyShips.RemoveAt(coordinate + 10); MyShips.Insert(coordinate + 10, not7); } if (MyShips.ElementAt(coordinate + 11).Background != "Green") { MyShips.RemoveAt(coordinate + 11); MyShips.Insert(coordinate + 11, not8); } #endregion } } if (sh == 0) { MessageBox.Show("Корабли закончились!"); StartEnabled = true; SetShipEnabled = false; } }