示例#1
0
        public bool PlaceShipAtCoordinates(Ship ship, BoardCoordinates startCoordinates, BoardCoordinates endCoordinates)
        {
            var cellsBetweenCoordinates = GetCellsBetween(Cells.At(startCoordinates), Cells.At(endCoordinates));

            if (cellsBetweenCoordinates != null)
            {
                if (ship.Size == cellsBetweenCoordinates.Count)
                {
                    List <BoardCell> affectedCells = new List <BoardCell>();
                    foreach (var cell in cellsBetweenCoordinates)
                    {
                        if (!cell.IsOccupied)
                        {
                            cell.CellOccupant = ship.ShipType;
                            affectedCells.Add(cell);
                        }
                        else
                        {
                            Console.WriteLine("Un autre bateau est déja sur la cellule " + cell.BoardCoordinates.Coordinates);
                            foreach (var affectedCell in affectedCells)//rollback on affected cells
                            {
                                affectedCell.CellOccupant = ShipType.NONE;
                            }
                            return(false);
                        }
                    }
                    if (affectedCells.Count == ship.Size)
                    {
                        ship.OccupedCells = affectedCells;
                        return(true);
                    }
                }
                else if (ship.Size > cellsBetweenCoordinates.Count)
                {
                    Console.WriteLine(Environment.NewLine);
                    Console.WriteLine("Les coordonnées entrées sont trop courtes");
                }
                else
                {
                    Console.WriteLine(Environment.NewLine);
                    Console.WriteLine("Les coordonnées entrées sont trop longues");
                }
            }

            Console.WriteLine(Environment.NewLine);
            return(false);
        }
示例#2
0
        public void PlayPlayerTurn(Player player1, Player player2)
        {
            Console.WriteLine("C'est à " + player1.Name + " de jouer");
            Console.WriteLine(player1.BuildGameBoard());
            BoardCoordinates shootedCoordinates;

            do
            {
                BoardCoordinates fireCoordinates;
                do
                {
                    Console.WriteLine(player1.Name + ", entrez vos coordonnées de tir");
                    fireCoordinates = BoardCoordinates.Parse(InputHandler.GetPlayerInput());
                } while (fireCoordinates == null);
                shootedCoordinates = player1.CheckFireCoordinates(fireCoordinates);
            } while (shootedCoordinates == null);
            var shotResult = player2.ReactToShot(shootedCoordinates);

            player1.UpdateBoardWithShotResult(shotResult, shootedCoordinates);
        }
 public bool AsSameCoordinates(BoardCoordinates bc)
 {
     return(this.x == bc.x && this.y == bc.y);
 }