Skip to content

Commit 680cc7b

Browse files
committed
Refactor workflows and improve game engine primitives
The main CI/CD workflow is split into separate workflows for each type of trigger event to improve efficiency and maintainability. The game engine's Board class now supports nullable types to enhance flexibility in game design. Additionally, a new 2D array type is implemented alongside auxiliary services to facilitate 2D game scenarios. Adjustments are made to the Battleship sample game to align with these improvements.
1 parent e5a1958 commit 680cc7b

16 files changed

Lines changed: 778 additions & 189 deletions

File tree

.github/workflows/main.yml

Lines changed: 0 additions & 110 deletions
This file was deleted.

.github/workflows/merge.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Merge Workflow
2+
3+
on:
4+
push:
5+
branches: [main]
6+
workflow_dispatch:
7+
8+
jobs:
9+
merge_job:
10+
name: Merge Job
11+
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
12+
uses: frankhaugen/Workflows/.github/workflows/dotnet-publish-preview.yml@main
13+
secrets: inherit

.github/workflows/pull-request.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Pull Request Workflow
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
workflow_dispatch:
7+
8+
jobs:
9+
pull_request_job:
10+
name: Pull Request Job
11+
if: github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch'
12+
uses: frankhaugen/Workflows/.github/workflows/dotnet-pull-request.yml@main
13+
secrets: inherit

.github/workflows/release.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Release Workflow
2+
3+
on:
4+
release:
5+
types: [published]
6+
workflow_dispatch:
7+
8+
jobs:
9+
release_job:
10+
name: Release Job
11+
if: github.event_name == 'release' || github.event_name == 'workflow_dispatch'
12+
uses: frankhaugen/Workflows/.github/workflows/dotnet-publish-release.yml@main
13+
secrets: inherit
14+

Frank.GameEngine.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.0.31903.59
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionFiles", "_SolutionFiles", "{3D1F3229-6080-44F7-BE89-F2F83A275080}"
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionFiles", "SolutionFiles", "{3D1F3229-6080-44F7-BE89-F2F83A275080}"
77
ProjectSection(SolutionItems) = preProject
88
.dockerignore = .dockerignore
99
.gitignore = .gitignore
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,146 @@
11
// See https://aka.ms/new-console-template for more information
22

3+
using Frank.GameEngine.Primitives;
4+
35
Console.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

Comments
 (0)