-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
189 lines (168 loc) · 6.38 KB
/
Game.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Generic;
using System.Linq;
using GalaxyWars.Display;
using GalaxyWars.Handlers;
using GalaxyWars.Models;
using GalaxyWars.Setup;
namespace GalaxyWars
{
public class Game
{
public List<Player> Players { get; set; }
public List<Planet> Planets { get; set; }
public Cell[,] GameBoard { get; set; }
private const int BoardWidth = 20;
private const int BoardHeight = 20;
private IPlayerActionHandler _playerActionHandler;
private IFleetActionHandler _fleetActionHandler;
private GameDisplay _gameDisplay;
private PlayerSetup _playerSetup;
private PlanetSetup _planetSetup;
private bool _gameRunning;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
public Game()
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
{
Players = new List<Player>();
Planets = new List<Planet>();
GameBoard = new Cell[BoardWidth, BoardHeight];
InitializeGameBoard();
}
public void SetHandlers(IPlayerActionHandler playerActionHandler, IFleetActionHandler fleetActionHandler)
{
_playerActionHandler = playerActionHandler;
_fleetActionHandler = fleetActionHandler;
_gameDisplay = new GameDisplay(GameBoard);
_playerSetup = new PlayerSetup(GameBoard, Players, this, 2); // 2 oyunculu ayarlama
_planetSetup = new PlanetSetup(GameBoard, Planets);
}
private void InitializeGameBoard()
{
for (int x = 0; x < BoardWidth; x++)
{
for (int y = 0; y < BoardHeight; y++)
{
GameBoard[x, y] = new Cell(x, y);
}
}
}
public void InitializeGame()
{
Console.WriteLine("Initializing game...");
_planetSetup.SetupPlanets();
_playerSetup.SetupPlayers();
}
public void StartGameLoop()
{
_gameRunning = true;
int currentPlayerIndex = 0;
if (Players.Count == 0)
{
Console.WriteLine("No players initialized. Exiting game loop.");
return;
}
while (_gameRunning)
{
_gameDisplay.DisplayGameBoard();
Player currentPlayer = Players[currentPlayerIndex];
Console.WriteLine($"It's {currentPlayer.Name}'s turn.");
_playerActionHandler.DisplayPlayerOptions(currentPlayer);
string command = Console.ReadLine()!;
if (!string.IsNullOrEmpty(command))
{
bool endTurn = _playerActionHandler.ProcessCommand(currentPlayer, command);
if (endTurn)
{
currentPlayerIndex = (currentPlayerIndex + 1) % Players.Count;
}
}
foreach (var planet in Planets)
{
planet.ProduceResources();
}
}
}
public void CreateFleetOption(Player player)
{
if (player.CanCreateFleet())
{
Console.WriteLine("Enter the name of the new fleet:");
string fleetName = Console.ReadLine()!;
if (!string.IsNullOrEmpty(fleetName))
{
var newFleet = new Fleet(fleetName, player.HomeBase.Position, player, this);
player.AddShipsToFleet(newFleet);
player.Fleets.Add(newFleet);
Console.WriteLine($"Fleet '{fleetName}' has been created.");
}
else
{
Console.WriteLine("Invalid fleet name entered.");
}
}
else
{
Console.WriteLine("No available ships to create a new fleet.");
}
}
public void MoveFleet(Player player)
{
_fleetActionHandler.MoveFleet(player);
}
public void AttackSequence(Player player)
{
_fleetActionHandler.AttackSequence(player);
}
public void UpgradePlanetDefenseOption(Player player)
{
var occupiedPlanets = Planets.Where(p => p.OccupiedBy == player).ToList();
if (occupiedPlanets.Any())
{
Console.WriteLine("Select a planet to upgrade its defense:");
for (int i = 0; i < occupiedPlanets.Count; i++)
{
Console.WriteLine($"{i + 1}. {occupiedPlanets[i].Name} - Current defense: {occupiedPlanets[i].DefenseCapacity}");
}
if (int.TryParse(Console.ReadLine(), out int choice) && choice > 0 && choice <= occupiedPlanets.Count)
{
var selectedPlanet = occupiedPlanets[choice - 1];
Console.WriteLine("Enter the amount of gold to invest in defense upgrade:");
if (int.TryParse(Console.ReadLine(), out int amount))
{
player.UpgradePlanetDefense(selectedPlanet, amount);
}
else
{
Console.WriteLine("Invalid amount.");
}
}
else
{
Console.WriteLine("Invalid choice.");
}
}
else
{
Console.WriteLine("You do not occupy any planets.");
}
}
public void DisplayOccupiedPlanetsAndFleets(Player player)
{
_gameDisplay.DisplayOccupiedPlanetsAndFleets(player, Planets);
}
public void DisplayAllOccupiedPlanets()
{
_gameDisplay.DisplayAllOccupiedPlanets(Planets);
}
public void DisplayPlayerFleets(Player player)
{
_gameDisplay.DisplayPlayerFleets(player);
}
public void EndGame()
{
_gameRunning = false;
Console.WriteLine("Game has ended.");
}
}
}