11// See https://aka.ms/new-console-template for more information
22
3+ using Frank . GameEngine . Primitives ;
4+
35Console . WriteLine ( "Hello, World!" ) ;
46
7+ var game = new BattleshipGame ( ) ;
8+
9+ game . Board . SetupBoard ( game . Player1 ) ;
10+
11+ game . Board . SetupBoard ( game . Player2 ) ;
12+
13+ Console . WriteLine ( game . Board . ToString ( ) ) ;
14+
15+
16+
17+
18+ public class BattleshipGame
19+ {
20+ public Player Player1 { get ; set ; }
21+ public Player Player2 { get ; set ; }
22+
23+ public BattleshipGame ( )
24+ {
25+ Player1 = new Player ( PlayerType . Human ) ;
26+ Player2 = new Player ( PlayerType . Computer ) ;
27+ Board = new Board < ShipType > ( 20 , 10 ) ;
28+ }
29+
30+ public Board < ShipType > Board { get ; set ; }
31+ }
32+
33+
34+ public enum ShipType
35+ {
36+ None ,
37+ Carrier ,
38+ Battleship ,
39+ Destroyer ,
40+ Submarine ,
41+ PatrolBoat
42+ }
43+
44+ public static class BoardExtensions
45+ {
46+ public static void SetupBoard ( this Board < ShipType > board , Player player )
47+ {
48+ var offset = player . PlayerType == PlayerType . Human ? 0 : 10 ;
49+
50+ var carrier = new Ship { PlayerType = player . PlayerType , ShipType = ShipType . Carrier } ;
51+ var battleship = new Ship { PlayerType = player . PlayerType , ShipType = ShipType . Battleship } ;
52+ var destroyer = new Ship { PlayerType = player . PlayerType , ShipType = ShipType . Destroyer } ;
53+ var submarine = new Ship { PlayerType = player . PlayerType , ShipType = ShipType . Submarine } ;
54+ var patrolBoat = new Ship { PlayerType = player . PlayerType , ShipType = ShipType . PatrolBoat } ;
55+ board . AddShip ( carrier , new BoardPosition ( GetRandomNumber ( offset , board . RowCount - offset ) , 0 ) , ShipDirection . Horizontal ) ;
56+ board . AddShip ( battleship , new BoardPosition ( GetRandomNumber ( offset , board . RowCount - offset ) , 0 ) , ShipDirection . Horizontal ) ;
57+ board . AddShip ( destroyer , new BoardPosition ( GetRandomNumber ( offset , board . RowCount - offset ) , 0 ) , ShipDirection . Horizontal ) ;
58+ board . AddShip ( submarine , new BoardPosition ( GetRandomNumber ( offset , board . RowCount - offset ) , 0 ) , ShipDirection . Horizontal ) ;
59+ board . AddShip ( patrolBoat , new BoardPosition ( GetRandomNumber ( offset , board . RowCount - offset ) , 0 ) , ShipDirection . Horizontal ) ;
60+ player . Ships . Add ( carrier ) ;
61+ player . Ships . Add ( battleship ) ;
62+ player . Ships . Add ( destroyer ) ;
63+ player . Ships . Add ( submarine ) ;
64+ player . Ships . Add ( patrolBoat ) ;
65+ }
66+
67+ private static int GetRandomNumber ( int min , int max )
68+ {
69+ return Random . Shared . Next ( min , max ) ;
70+ }
71+
72+ public static void AddShip ( this Board < ShipType > board , Ship ship , BoardPosition startingPosition , ShipDirection direction )
73+ {
74+ var positions = new List < BoardPosition > ( ) ;
75+ for ( var i = 0 ; i < ship . Length ; i ++ )
76+ {
77+ var position = direction switch
78+ {
79+ ShipDirection . Horizontal => new BoardPosition ( startingPosition . Row , startingPosition . Column + i ) ,
80+ ShipDirection . Vertical => new BoardPosition ( startingPosition . Row + i , startingPosition . Column ) ,
81+ _ => throw new ArgumentOutOfRangeException ( nameof ( direction ) , direction , null )
82+ } ;
83+ positions . Add ( position ) ;
84+ }
85+ ship . Positions = positions ;
86+ foreach ( var position in positions )
87+ {
88+ board . Set ( position , ship . ShipType ) ;
89+ }
90+ }
91+ }
92+
93+ public enum ShipDirection
94+ {
95+ Horizontal ,
96+ Vertical
97+ }
98+
99+ public static class ShipTypeExtensions
100+ {
101+ public static int GetLength ( this ShipType shipType )
102+ {
103+ return shipType switch
104+ {
105+ ShipType . Carrier => 5 ,
106+ ShipType . Battleship => 4 ,
107+ ShipType . Destroyer => 3 ,
108+ ShipType . Submarine => 3 ,
109+ ShipType . PatrolBoat => 2 ,
110+ _ => throw new ArgumentOutOfRangeException ( nameof ( shipType ) , shipType , null )
111+ } ;
112+ }
113+ }
114+
115+ public class Ship
116+ {
117+ public PlayerType PlayerType { get ; set ; }
118+
119+ public ShipType ShipType { get ; set ; }
120+
121+ public List < BoardPosition > Positions { get ; set ; }
122+
123+ public int Length => ShipType . GetLength ( ) ;
124+
125+ public int Hits { get ; set ; }
126+ public bool IsSunk => Hits >= Length ;
127+ }
128+
129+ public enum PlayerType
130+ {
131+ Human ,
132+ Computer
133+ }
134+
135+ public class Player
136+ {
137+ public PlayerType PlayerType { get ; set ; }
138+
139+ public List < Ship > Ships { get ; set ; }
140+
141+ public Player ( PlayerType playerType )
142+ {
143+ PlayerType = playerType ;
144+ Ships = new List < Ship > ( ) ;
145+ }
146+ }
0 commit comments