public void TestTurning(Ship.TurnDirection turnDirection, int degrees, Ship.Direction direction) { var ship = new Ship(); ship.Turn(turnDirection, degrees); ship.CurrentDirection.ShouldBe(direction); }
private void PlayerFieldPictureBox_Click(object sender, EventArgs e) { if (status != GameStatus.placing) { return; } var me = e as MouseEventArgs; if (me.Button == MouseButtons.Left) { int x = me.X / Globals.cellSize; int y = me.Y / Globals.cellSize; var cell = model.playerField.Field[y, x]; bool success = model.playerField.PlaceShip(cell, pickedShipSize, pickedShipDirection); if (success) { model.playerField.PossibleToPlaceShipCheck(pickedShipSize); shipsRemaining[pickedShipSize]--; shipLabels[pickedShipSize].Text = String.Format("{0} - палубный, осталось: {1}", pickedShipSize, shipsRemaining[pickedShipSize]); if (shipsRemaining[pickedShipSize] <= 0) { status = GameStatus.none; } Redraw(); int sum = 0; foreach (int count in shipsRemaining) { sum += count; } if (sum == 0) { StartGame(); } } } else if (me.Button == MouseButtons.Right) { if (pickedShipDirection == Ship.Direction.horizontal) { pickedShipDirection = Ship.Direction.vertical; } else { pickedShipDirection = Ship.Direction.horizontal; } Redraw(); } }
public void addShip(int i, int j) { string s = comboBox1.Text; Ship.Direction dir = Ship.Direction.horiz; int size = 1; switch (s) { case "2 deck vertical": dir = Ship.Direction.vert; size = 2; break; case "2 deck horizontal": dir = Ship.Direction.horiz; size = 2; break; case "3 deck vertical": dir = Ship.Direction.vert; size = 3; break; case "3 deck horizontal": dir = Ship.Direction.horiz; size = 3; break; case "4 deck vertical": dir = Ship.Direction.vert; size = 4; break; case "4 deck horizontal": dir = Ship.Direction.horiz; size = 4; break; } //MessageBox.Show(" (" + e.j + ", " + e.i + ")"); Ship ship = new Ship(i, j, dir, size); if (!game1.addShip(ship)) { MessageBox.Show("Cannot place ship here"); } }
private (int, int, int, int) GenerateTuple(int x, int y, int length, Ship.Direction dir) { int ymax = y; int xmax = x; int len = length - 1; if (dir == Ship.Direction.horizontal) { xmax += len; } else { ymax += len; } return(x, xmax, y, ymax); }
void AddOneShipManual(Board board, Ship ship) { int x = 0; int y = 0; int size = board.Size; Ship.Direction dir = Ship.Direction.horizontal; ConsoleKey key; bool CordsNotSet = true; bool wrongPositionMessage = false; do { ship.OriginPoint = (x, y); ship.InstanceDirection = dir; _display.PrintBoard(board, ship); if (wrongPositionMessage) { _display.PrintMessage("Invalid Ship Position", ConsoleColor.Red); wrongPositionMessage = false; } key = _input.ReadKey(); switch (key) { case ConsoleKey.UpArrow: if (y == 0 && dir == Ship.Direction.vertical) { y = size - ship.Length; } else if (y == 0 && dir == Ship.Direction.horizontal) { y = size - 1; } else { y--; } break; case ConsoleKey.DownArrow: if ((y == size - ship.Length && dir == Ship.Direction.vertical) || (y == size - 1)) { y = 0; } else { y++; } break; case ConsoleKey.LeftArrow: if (x == 0 && dir == Ship.Direction.horizontal) { x = size - ship.Length; } else if (x == 0 && dir == Ship.Direction.vertical) { x = size - 1; } else { x--; } break; case ConsoleKey.RightArrow: if ((x == size - ship.Length && dir == Ship.Direction.horizontal) || (x == size - 1)) { x = 0; } else { x++; } break; case ConsoleKey.Spacebar: if (dir == Ship.Direction.horizontal) { dir = Ship.Direction.vertical; if (y >= size - ship.Length) { y = size - ship.Length; } } else { dir = Ship.Direction.horizontal; if (x >= size - ship.Length) { x = size - ship.Length; } } break; case ConsoleKey.Enter: if (board.possibleShip(ship)) { board.AddShip(ship); CordsNotSet = false; } else { wrongPositionMessage = true; } break; default: break; } } while (CordsNotSet); }
public bool PlaceShip(Cell start, int size, Ship.Direction direction) { //если нельзя поставить, выходим if (direction == Ship.Direction.horizontal && !start.possibleToPlaceShipH) { return(false); } if (direction == Ship.Direction.vertical && !start.possibleToPlaceShipV) { return(false); } var ship = new Ship(size); //обновлем поле if (direction == Ship.Direction.horizontal) { for (int i = 0; i < size; i++) { Field[start.y, start.x + i].status = Cell.Status.ship; Field[start.y, start.x + i].ship = ship; if (start.y + 1 < Globals.fieldSize) { ship.nearCells.Add(Field[start.y + 1, start.x + i]); } if (start.y - 1 >= 0) { ship.nearCells.Add(Field[start.y - 1, start.x + i]); } } Tuple <int, int>[] deltas = { Tuple.Create(-1, -1), Tuple.Create(size, -1), Tuple.Create(-1, 0), Tuple.Create(size, 0), Tuple.Create(-1, 1), Tuple.Create(size, 1) }; foreach (var delta in deltas) { if (start.x + delta.Item1 < 0 || start.x + delta.Item1 >= Globals.fieldSize || start.y + delta.Item2 < 0 || start.y + delta.Item2 >= Globals.fieldSize) { continue; } ship.nearCells.Add(Field[start.y + delta.Item2, start.x + delta.Item1]); } } else if (direction == Ship.Direction.vertical) { for (int i = 0; i < size; i++) { Field[start.y + i, start.x].status = Cell.Status.ship; Field[start.y + i, start.x].ship = ship; if (start.x + 1 < Globals.fieldSize) { ship.nearCells.Add(Field[start.y + i, start.x + 1]); } if (start.x - 1 >= 0) { ship.nearCells.Add(Field[start.y + i, start.x - 1]); } } Tuple <int, int>[] deltas = { Tuple.Create(-1, -1), Tuple.Create(0, -1), Tuple.Create(1, -1), Tuple.Create(-1, size), Tuple.Create(0, size), Tuple.Create(1, size) }; foreach (var delta in deltas) { if (start.x + delta.Item1 < 0 || start.x + delta.Item1 >= Globals.fieldSize || start.y + delta.Item2 < 0 || start.y + delta.Item2 >= Globals.fieldSize) { continue; } ship.nearCells.Add(Field[start.y + delta.Item2, start.x + delta.Item1]); } } foreach (var cell in ship.nearCells) { cell.status = Cell.Status.nearShip; } ships.Add(ship); return(true); }
public void GenerateBitmap(Ship.Direction direction = Ship.Direction.horizontal, bool showForbiddenCells = false) { //заливаем поле фоновым цветом var brush = new SolidBrush(Color.White); g.FillRectangle(brush, 0, 0, Globals.fieldSize * Globals.cellSize, Globals.fieldSize * Globals.cellSize); //рисуем клетки Color color = Color.White; for (int i = 0; i < Globals.fieldSize; i++) { for (int j = 0; j < Globals.fieldSize; j++) { var cell = Field[i, j]; if (cell.status == Cell.Status.ship && (ShowShips || cell.shotStatus == Cell.ShotStatus.damagedShip)) { if (cell.ship.isAlive) { color = Color.Black; } else { color = Color.DarkViolet; } } else if (ShowShips && cell.status == Cell.Status.nearShip) { color = Color.Linen; } else if (showForbiddenCells && (direction == Ship.Direction.horizontal && !cell.possibleToPlaceShipH || direction == Ship.Direction.vertical && !cell.possibleToPlaceShipV) ) { color = Color.Pink; } else { color = Color.White; } brush = new SolidBrush(color); int x1 = cell.x * Globals.cellSize; int y1 = cell.y * Globals.cellSize; g.FillRectangle(brush, x1, y1, Globals.cellSize, Globals.cellSize); //рисуем результат выстрелов if (cell.shotStatus == Cell.ShotStatus.miss) { brush = new SolidBrush(Color.Blue); int x = x1 + Globals.cellSize * 2 / 5; int y = y1 + Globals.cellSize * 2 / 5; int size = Globals.cellSize / 5; g.FillEllipse(brush, x, y, size, size); } if (cell.shotStatus == Cell.ShotStatus.damagedShip) { var p = new Pen(Color.Red); p.Width = 2; g.DrawLine(p, x1, y1, x1 + Globals.cellSize, y1 + Globals.cellSize); g.DrawLine(p, x1, y1 + Globals.cellSize, x1 + Globals.cellSize, y1); } } } //рисуем сетку Pen pen = new Pen(Color.Black); for (int i = 1; i < Globals.fieldSize; i++) { g.DrawLine(pen, i * Globals.cellSize, 0, i * Globals.cellSize, Globals.fieldSize * Globals.cellSize); } for (int i = 1; i < Globals.fieldSize; i++) { g.DrawLine(pen, 0, i * Globals.cellSize, Globals.fieldSize * Globals.cellSize, i * Globals.cellSize); } }