public Boolean set_ship(Ships ship) { bool flag = true; Point xy; for (int i = 0; i < ship.Size && flag; i++) // בדיקה שכל המיקומים של הספינה נמצאים ברשימה { if (!new_point.Contains(ship.Point[i])) { flag = false; } } if (!flag) { return(false); } for (int i = 0; i < ship.Size; i++) // מוציא את המיקומים של הספינה מתוך הרשימה// { new_point.Remove(ship.Point[i]); } for (int i = 0; i < ship.Size; i++) // הוצאת הנקודות מסביב לספינה מרשימת הנקודות// { int x = ship.Point[i].X; int y = ship.Point[i].Y; if (new_point.Contains(xy = new Point(x - 1, y - 1))) { new_point.Remove(xy); } if (new_point.Contains(xy = new Point(x, y - 1))) { new_point.Remove(xy); } if (new_point.Contains(xy = new Point(x + 1, y - 1))) { new_point.Remove(xy); } if (new_point.Contains(xy = new Point(x - 1, y))) { new_point.Remove(xy); } if (new_point.Contains(xy = new Point(x - 1, y + 1))) { new_point.Remove(xy); } if (new_point.Contains(xy = new Point(x + 1, y))) { new_point.Remove(xy); } if (new_point.Contains(xy = new Point(x + 1, y + 1))) { new_point.Remove(xy); } if (new_point.Contains(xy = new Point(x, y + 1))) { new_point.Remove(xy); } } return(true); }
public MainWindow() { InitializeComponent(); this.MyField.ClickOnBox += MyField_ClickOnBox; ships = new Ships(beginShips); EnemyField.boxes.Enemy = true; }
private void player_mouseclick(object sender, MouseEventArgs e) { if (flag == 1) { string[] separators = { ",", ".", "!", "?", ";", ":", " " }; PictureBox pb = (PictureBox)sender; int x = int.Parse(((string)pb.Tag).Split(separators, StringSplitOptions.RemoveEmptyEntries)[0]); int y = int.Parse(((string)pb.Tag).Split(separators, StringSplitOptions.RemoveEmptyEntries)[1]); Boolean b = Horizontal.SelectedIndex == 1 ? true : false; Horizontal.Refresh(); Point[] a; Ships ship = null; switch (SelectShip.Text) { case "ship2": a = ship2.get_point(x, y, b); ship = new ship2(b, a); Console.WriteLine(ship.Name); break; case "ship3": a = ship3.get_point(x, y, b); ship = new ship3(b, a); break; case "ship4": a = ship4.get_point(x, y, b); ship = new ship4(b, a); break; case "ship6": a = ship6.get_point(x, y, b); ship = new ship6(b, a); break; } if (player.set_ship(ship)) { player.Ships.Add(ship); SelectShip.Items.RemoveAt(SelectShip.SelectedIndex); SelectShip.SelectedIndex = 0; SelectShip.Refresh(); if (SelectShip.Items.Count == 1) { StartButton.Visible = true; SelectShip.Visible = false; Horizontal.Visible = false; } } pic_color_ship(player); } }
private void buttonClear_Click(object sender, RoutedEventArgs e) { MyField.ClearField(); ships = new Ships(beginShips); Group1.IsEnabled = true; groupBox.IsEnabled = true; buttonStartGame.IsEnabled = false; if (MyField.ClickOnBox == null) { MyField.ClickOnBox += MyField_ClickOnBox; } }
/// <summary> /// Attack to specific coordinates. /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public CellType Fire(int x, int y) { var ship = _board.Cells.At(x, y); if (ship.Status != CellType.Battleship) { ship.Status = CellType.Miss; return(CellType.Miss); } Ships.First(item => item.HasPoint(x, y)).ShipCoordinate.First(item => item.Coordinate == (x, y)).Status = CellType.Hit; return(CellType.Hit); }
//constructor public Players() { opponentsBoard = new Gameboard(); gameboard = new Gameboard(); score = 0; destroyer = new Destroyer(); submarine = new Submarine(); battleship = new Battleship(); aircraftCarrier = new AircraftCarrier(); fleet = new List <Ships>() { destroyer, submarine, battleship, aircraftCarrier }; }
public void get_rendom_point() { Random a = new Random(); Ships ship = null; do { Point rendom_point = new_point[a.Next(0, new_point.Count)]; Boolean rendom_boolean = a.Next(0, 100) > 50 ? true : false; Point[] points = ship2.get_point(rendom_point.X, rendom_point.Y, rendom_boolean); ship = new ship2(rendom_boolean, points); } while (!this.set_ship(ship)); this.ships.Add(ship); ship = null; do { Point rendom_point = new_point[a.Next(0, new_point.Count)]; Boolean rendom_boolean = a.Next(0, 100) > 50 ? true : false; Point[] points = ship3.get_point(rendom_point.X, rendom_point.Y, rendom_boolean); ship = new ship3(rendom_boolean, points); } while (!this.set_ship(ship)); this.ships.Add(ship); ship = null; do { Point rendom_point = new_point[a.Next(0, new_point.Count)]; Boolean rendom_boolean = a.Next(0, 100) > 50 ? true : false; Point[] points = ship6.get_point(rendom_point.X, rendom_point.Y, rendom_boolean); ship = new ship6(rendom_boolean, points); } while (!this.set_ship(ship)); this.ships.Add(ship); ship = null; do { Point rendom_point = new_point[a.Next(0, new_point.Count)]; Boolean rendom_boolean = a.Next(0, 100) > 50 ? true : false; Point[] points = ship4.get_point(rendom_point.X, rendom_point.Y, rendom_boolean); ship = new ship4(rendom_boolean, points); } while (!this.set_ship(ship)); this.ships.Add(ship); }
/// <summary> /// Method returns the Length and the Symbol of the ship value passed in. /// </summary> /// <param name="ship">One of the 5 ships</param> /// <returns>integer lenght of ship and character symbol representing the ship</returns> public static KeyValuePair <int, char> SelectPiece(Ships ship) { switch (ship) { case Ships.BattleShip: return(new KeyValuePair <int, char>(BATTLESHIP_LENGTH, BATTLESHIP_SYMBOL)); case Ships.Aircraft_Carrier: return(new KeyValuePair <int, char>(AIRCRAFT_CARRIER_LENGTH, AIRCRAFT_CARRIER_SYMBOL)); case Ships.Cruiser: return(new KeyValuePair <int, char>(CRUISER_LENGTH, CRUISER_SYMBOL)); case Ships.Destroyer: return(new KeyValuePair <int, char>(DESTROYER_LENGTH, DESTROYER_SYMBOL)); case Ships.Submarine: return(new KeyValuePair <int, char>(SUBMARINE_LENGTH, SUBMARINE_SYMBOL)); } return(new KeyValuePair <int, char>(0, '\0')); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here myState = GameState.Menu; AircraftCarrier = new Ships(5, "Aircraft Carrier"); Battleship = new Ships(4, "Battleship"); Submarine = new Ships(3, "Submarine"); Destroyer = new Ships(3, "Destroyer"); PatrolBoat = new Ships(2, "Patrol Boat"); playerShips = new List <Ships>() { AircraftCarrier, Battleship, Submarine, Destroyer, PatrolBoat }; computerShips = new List <Ships>() { AircraftCarrier, Battleship, Submarine, Destroyer, PatrolBoat }; base.Initialize(); }
public void AutoPlacementShips(Ships ships) { ClearField(); Restart: int count = 0; foreach (var item in ships.GetShips()) { for (int i = 0; i < item.CountShip; i++) { count = 0; while (true) { var box = boxes[random.Next(0, 100)]; if (box.IsBusy) { continue; } var orientation = (Orientation)random.Next(0, 2); try { SupplyShip(box, item.Type, orientation); } catch { count++; if (count >= 100) { ClearField(); goto Restart; } continue; } break; } } } }
/// <summary> /// Adds a ship into the board. /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="length"></param> /// <param name="orient"></param> /// <returns></returns> public bool AddShip(int x, int y, int length, Orientation orient) { switch (orient) { case Orientation.Horizontal when x + length > 9: case Orientation.Vertical when y + length > 9: return(false); } // Check free cells if (!_board.Cells.IsSea(x, y, length, orient)) { return(false); } // add ship to client Ships.Add(new Ship(x, y, length, orient)); // update master board cells for (var i = 0; i < length; i++) { _board.Cells.At(orient == Orientation.Horizontal ? x + i : x, orient == Orientation.Vertical ? y + i : y).Status = CellType.Battleship; } return(true); }
private void buttonPlace_Click(object sender, EventArgs e) { buttonRemove.Visible = false; if (selectShip.SelectedIndex == selectShip.Items.IndexOf("Carrier(5)") && Ships.Where(s => s.Occupation == Occupation.Carrier && !s.IsPlaced).FirstOrDefault() != null) { _shipIndex = selectShip.Items.IndexOf("Carrier(5)"); _shipToBePlaced = new Carrier(); } else if (selectShip.SelectedIndex == selectShip.Items.IndexOf("Battleship(4)") && Ships.Where(s => s.Occupation == Occupation.Battleship && !s.IsPlaced).FirstOrDefault() != null) { _shipIndex = selectShip.Items.IndexOf("Battleship(4)"); _shipToBePlaced = new Battleship(); } else if (selectShip.SelectedIndex == selectShip.Items.IndexOf("Cruiser(3)") && Ships.Where(s => s.Occupation == Occupation.Cruiser && !s.IsPlaced).FirstOrDefault() != null) { _shipIndex = selectShip.Items.IndexOf("Cruiser(3)"); _shipToBePlaced = new Cruiser(); } else if (selectShip.SelectedIndex == selectShip.Items.IndexOf("Submarine(3)") && Ships.Where(s => s.Occupation == Occupation.Submarine && !s.IsPlaced).FirstOrDefault() != null) { _shipIndex = selectShip.Items.IndexOf("Submarine(3)"); _shipToBePlaced = new Submarine(); } else if (selectShip.SelectedIndex == selectShip.Items.IndexOf("Destroyer(2)") && Ships.Where(s => s.Occupation == Occupation.Destroyer && !s.IsPlaced).FirstOrDefault() != null) { _shipIndex = selectShip.Items.IndexOf("Destroyer(2)"); _shipToBePlaced = new Destroyer(); } else { _shipToBePlaced = null; _shipIndex = -1; } if (radioButtonV.Checked) { _orientationH = false; } else { _orientationH = true; } if (_shipToBePlaced == null) { labelPlace.Text = "Select ship first!"; } else { _openSquares = new List <Square>(); labelPlace.Text = "Place your " + _shipToBePlaced.Name + "."; foreach (var square in Board.Squares) { var startColumn = square.Coordinates.Column; var startRow = square.Coordinates.Row; int endRow = startRow, endColumn = startColumn; List <int> squareNumbers = new List <int>(); for (int i = 1; i < _shipToBePlaced.Width; i++) { if (!_orientationH) { endRow++; } else { endColumn++; } } if (endRow > 10 || endColumn > 10) { continue; } var affectedSquares = Board.Squares.Range(startRow, startColumn, endRow, endColumn); if (affectedSquares.Any(s => s.IsOccupied)) { continue; } _openSquares.Add(square); } Invalidate(); } }
/// <summary> /// Provides the API for all the Board actions and functionality. /// </summary> /// <param name="action">Determines which action to take upon the board passed in.</param> /// <param name="board">Board to take actions upon</param> /// <param name="x">Horizontal coordinate of the board to take action upon.</param> /// <param name="y">Vertical coordinate of the board to take action upon.</param> /// <param name="orientation">If action passed is set to SetPiece the ship will be placed to passed orientation.</param> /// <param name="ship">If action passed is set to SetPiece then the ship passed will be set to the board at location x, y and orientation passed in.</param> /// <returns></returns> public static bool Board(Actions action, char[] board, int x = 0, int y = 0, Orientation orientation = Orientation.Horizontal, Ships ship = Ships.Destroyer, bool messageFlag = true, PlayerType player = PlayerType.Player) { Random random = new Random(); switch (action) { case Actions.Initialize: case Actions.Show: int row = 0; if (action == Actions.Show) { Console.Clear(); DrawTopMargin(); DrawLeftMargin(); Console.Write(row++); } for (int i = 0; i < BOARD_SIZE; ++i) { switch (action) { case Actions.Initialize: board[i] = (char)BOARD_EMPTY_SYMBOL; break; case Actions.Show: ConsoleColor originalColor = Console.ForegroundColor; if (SHIP_COLORS.ContainsKey(board[i])) { Console.ForegroundColor = SHIP_COLORS[board[i]]; } Console.Write(board[i]); Console.ForegroundColor = originalColor; if ((i + 1) % BOARD_WIDTH == 0) { Console.Write("\n"); DrawLeftMargin(); if (row < 10) { Console.Write(row++); } } break; default: break; } } break; case Actions.SetShip: bool setpiece = true; switch (orientation) { case Orientation.Horizontal: if (x + SelectPiece(ship).Key < BOARD_WIDTH && x >= 0 && y < BOARD_HEIGHT && y >= 0) { for (int i = 0; i < SelectPiece(ship).Key; ++i) { if (board[(x + i) + y * BOARD_HEIGHT] != (char)BOARD_EMPTY_SYMBOL) { setpiece = false; if (messageFlag) { Console.Write("Sorry can't set it there.\n"); Console.ReadKey(); } return(false); } } if (setpiece) { switch (player) { case PlayerType.Player: if (!SHIP_PLACED_PLAYER[SelectPiece(ship).Value]) { for (int i = 0; i < SelectPiece(ship).Key; ++i) { board[(x + i) + y * BOARD_WIDTH] = SelectPiece(ship).Value; } if (player == PlayerType.Player) { SHIP_PLACED_PLAYER[SelectPiece(ship).Value] = true; } } else { Console.Write("Ship already placed.\n"); Console.ReadKey(); } break; case PlayerType.Computer: if (!SHIP_PLACED_COMPUTER[SelectPiece(ship).Value]) { for (int i = 0; i < SelectPiece(ship).Key; ++i) { board[(x + i) + y * BOARD_WIDTH] = SelectPiece(ship).Value; } if (player == PlayerType.Player) { SHIP_PLACED_COMPUTER[SelectPiece(ship).Value] = true; } } else { Console.Write("Ship already placed.\n"); Console.ReadKey(); } break; } } } else { if (messageFlag) { Console.Write("Sorry can't set it there.\n"); Console.ReadKey(); } return(false); } break; case Orientation.Vertical: if (y + SelectPiece(ship).Key < BOARD_HEIGHT && y >= 0 && x < BOARD_WIDTH && x >= 0) { for (int i = 0; i < SelectPiece(ship).Key; ++i) { if (board[x + (y + i) * BOARD_HEIGHT] != (char)BOARD_EMPTY_SYMBOL) { setpiece = false; if (messageFlag) { Console.Write("Sorry can't set it there.\n"); Console.ReadKey(); } return(false); } } if (setpiece) { switch (player) { case PlayerType.Player: if (!SHIP_PLACED_PLAYER[SelectPiece(ship).Value]) { for (int i = 0; i < SelectPiece(ship).Key; ++i) { board[x + (y + i) * BOARD_HEIGHT] = SelectPiece(ship).Value; } if (player == PlayerType.Player) { SHIP_PLACED_PLAYER[SelectPiece(ship).Value] = true; } } else { Console.Write("Ship already placed.\n"); Console.ReadKey(); } break; case PlayerType.Computer: if (!SHIP_PLACED_COMPUTER[SelectPiece(ship).Value]) { for (int i = 0; i < SelectPiece(ship).Key; ++i) { board[x + (y + i) * BOARD_HEIGHT] = SelectPiece(ship).Value; } if (player == PlayerType.Player) { SHIP_PLACED_COMPUTER[SelectPiece(ship).Value] = true; } } else { Console.Write("Ship already placed.\n"); Console.ReadKey(); } break; } } } else { if (messageFlag) { Console.Write("Sorry can't set it there.\n"); Console.ReadKey(); } return(false); } break; } break; case Actions.SetShot: if (x < BOARD_WIDTH && x >= 0 & y < BOARD_HEIGHT && y >= 0) { if (board[x + y * BOARD_HEIGHT] == HIT_SYMBOL || board[x + y * BOARD_HEIGHT] == MISS_SYMBOL) { if (messageFlag) { Console.Write("Sorry can't set it there.\n"); Console.ReadKey(); } return(false); } else if (board[x + y * BOARD_HEIGHT] != (char)BOARD_EMPTY_SYMBOL) { string shipname = SHIP_NAMES[board[x + y * BOARD_HEIGHT]]; //update hit count switch (player) { case PlayerType.Player: SHIP_HIT_COUNT_PLAYER[board[x + y * BOARD_HEIGHT]]--; break; case PlayerType.Computer: SHIP_HIT_COUNT_COMPUTER[board[x + y * BOARD_HEIGHT]]--; break; } Board(Actions.Show, board); switch (player) { case PlayerType.Player: Console.Write("HIT!! Player's " + shipname); if (SHIP_HIT_COUNT_PLAYER[board[x + y * BOARD_HEIGHT]] == 0) { Console.Write(" and SUNK IT!!"); } break; case PlayerType.Computer: Console.Write("HIT!! Computer's " + shipname); if (SHIP_HIT_COUNT_COMPUTER[board[x + y * BOARD_HEIGHT]] == 0) { Console.Write(" and SUNK IT!!"); } break; } board[x + y * BOARD_HEIGHT] = HIT_SYMBOL; Console.ReadKey(); return(true); } if (board[x + y * BOARD_HEIGHT] == (char)BOARD_EMPTY_SYMBOL) { board[x + y * BOARD_HEIGHT] = MISS_SYMBOL; Board(Actions.Show, board); Console.Write("Miss"); Console.ReadKey(); return(true); } } else { if (messageFlag) { Console.Write("Sorry can't set it there.\n"); Console.ReadKey(); } } break; case Actions.RandomizeShips: random = new Random(); for (int i = 0; i < NUMBER_OF_SHIPS; ++i) { int __x; int __y; int __orientation; do { __x = random.Next(0, BOARD_WIDTH); __y = random.Next(0, BOARD_HEIGHT); __orientation = random.Next(0, 2); } while (Board(Actions.SetShip, board, __x, __y, (Orientation)__orientation, (Ships)i, false, player) == false); } break; case Actions.RandomShip: int _x; int _y; int _orientation; do { _x = random.Next(0, BOARD_WIDTH); _y = random.Next(0, BOARD_HEIGHT); _orientation = random.Next(0, 2); } while (Board(Actions.SetShip, board, _x, _y, (Orientation)_orientation, ship, false, player) == false); break; case Actions.RandomShot: bool hit = false; while (!hit) { random = new Random(); hit = Board(Actions.SetShot, board, random.Next(0, BOARD_WIDTH), random.Next(0, BOARD_HEIGHT), messageFlag: false, player: player); } break; case Actions.WinCondition: switch (player) { case PlayerType.Player: int summed_ship_counts = 0; foreach (var ship_hit_count in SHIP_HIT_COUNT_PLAYER) { summed_ship_counts += ship_hit_count.Value; } if (summed_ship_counts == 0) { Console.Write("Computer Wins!\n"); return(false); } else { return(true); } break; case PlayerType.Computer: summed_ship_counts = 0; foreach (var ship_hit_count in SHIP_HIT_COUNT_COMPUTER) { summed_ship_counts += ship_hit_count.Value; } if (summed_ship_counts == 0) { Console.Write("Player Wins!\n"); return(false); } else { return(true); } break; } break; } return(true); }