Skip to content

Commit

Permalink
Added Ship Structs, Sidebar.Razor
Browse files Browse the repository at this point in the history
Added a sidebar that displays ship information.
  • Loading branch information
PDykes1996 committed Sep 15, 2024
1 parent 66fa286 commit 61a8dfa
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 18 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,14 @@ BlazorBattleship/obj/Debug/net6.0/project.razor.vs.json
.vs/BlazorBattleship/v17/.suo
.vs/ProjectEvaluation/blazorbattleship.metadata.v5.2
.vs/ProjectEvaluation/blazorbattleship.projects.v5.2
.vs/BlazorBattleship/DesignTimeBuild/.dtbcache.v2
.vs/BlazorBattleship/v17/.suo
BlazorBattleship/bin/Debug/net6.0/BlazorBattleship.dll
BlazorBattleship/bin/Debug/net6.0/BlazorBattleship.pdb
BlazorBattleship/obj/Debug/net6.0/BlazorBattleship.csproj.CoreCompileInputs.cache
BlazorBattleship/obj/Debug/net6.0/BlazorBattleship.dll
BlazorBattleship/obj/Debug/net6.0/BlazorBattleship.GeneratedMSBuildEditorConfig.editorconfig
BlazorBattleship/obj/Debug/net6.0/project.razor.vs.json
BlazorBattleship/obj/Debug/net6.0/project.razor.vs.json
BlazorBattleship/obj/Debug/net6.0/ref/BlazorBattleship.dll
BlazorBattleship/obj/Debug/net6.0/refint/BlazorBattleship.dll
Binary file modified .vs/BlazorBattleship/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file modified .vs/BlazorBattleship/v17/.futdcache.v2
Binary file not shown.
Binary file modified .vs/BlazorBattleship/v17/.suo
Binary file not shown.
Binary file modified .vs/ProjectEvaluation/blazorbattleship.metadata.v5.2
Binary file not shown.
Binary file modified .vs/ProjectEvaluation/blazorbattleship.projects.v5.2
Binary file not shown.
3 changes: 2 additions & 1 deletion BlazorBattleship/Hubs/ChatHub.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.SignalR;
using BlazorBattleship.Shared;
using Microsoft.AspNetCore.SignalR;
using System.Diagnostics;
namespace BlazorBattleship.Hubs
{
Expand Down
38 changes: 23 additions & 15 deletions BlazorBattleship/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
@page "/"
@using Microsoft.AspNetCore.SignalR.Client
@using BlazorBattleship.Shared
@inject NavigationManager Navigation
@implements IAsyncDisposable

<PageTitle>Battleship</PageTitle>
<PageTitle>@currentGameState</PageTitle>

<Sidebar Ships="ships"/>

@if ((currentGameState == GameState.Initial) || userInput == "")
{
Expand Down Expand Up @@ -98,7 +101,9 @@
{
int xCoord = x;//Capturing x and y coordinates so that their values at button creation are preserved.
int yCoord = y;
<button class="@(OpponentBoardInfo(xCoord, yCoord))" @onclick='(() => {
<button
class="@(OpponentBoardInfo(xCoord, yCoord))"
@onclick='(() => {
SetCoordinates(xCoord, yCoord);
})'></button>
}
Expand Down Expand Up @@ -157,13 +162,7 @@
private bool isPlayer1 = false;

Check warning on line 162 in BlazorBattleship/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / build

The field 'Index.isPlayer1' is assigned but its value is never used
#endregion
#region Ship Parameters
struct ship
{
(int x, int y) shipCoords;
int length;
int hp;
}
ship[] ships = new ship[shipLimit];
private Ship[]? ships;
private bool isShipHorizontal = true; // To manage ship placement direction
private int shipCount = 0;
private static int shipLimit;
Expand Down Expand Up @@ -301,8 +300,9 @@
{
for (int i = 0; i < shipLength; i++)
{
playerBoard[startX + i, startY] = true;
shipHPs[startX + i, startY] = shipLength;
(int x, int y) coords = (startX + i, startY);
ships[shipCount] = new Ship(shipCount, shipCount, $"Ship: {shipLength+1}", coords);

Check warning on line 304 in BlazorBattleship/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
playerBoard[coords.x, coords.y] = true;
}
shipCount++;
}
Expand All @@ -313,7 +313,9 @@
{
for (int i = 0; i < shipLength; i++)
{
playerBoard[startX, startY + i] = true;
(int x, int y) coords = (startX, startY+i);
ships[shipCount - 1] = new Ship(shipCount, shipCount, $"Ship: {shipLength}", coords);

Check warning on line 317 in BlazorBattleship/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
playerBoard[coords.x, coords.y] = true;
}
shipCount++;
}
Expand All @@ -332,7 +334,7 @@
{
return "hit-preview";
}
if (opponentDefaultBoard[x, y] && opponentBoard[x, y])
else if (opponentDefaultBoard[x, y] && opponentBoard[x, y])
{
return "hit";
}
Expand All @@ -355,6 +357,10 @@
{
return coordinates.HasValue ? $"{coordinates.Value.x}, {coordinates.Value.y}" : "None";
}
private int GetShipHP(int index)
{
return ships[index].hp;

Check warning on line 362 in BlazorBattleship/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}
#endregion

#region Networking Functionality
Expand Down Expand Up @@ -409,6 +415,7 @@
hubConnection.On<int>("ReceiveShipNum", (sl) =>
{
shipLimit = sl;
ships = new Ship[shipLimit];
currentGameState = GameState.Setup;
InvokeAsync(StateHasChanged);
});
Expand All @@ -425,7 +432,7 @@
hubConnection.On<string, int, int>("ReceiveCoordinates", (user, x, y) =>
{
opponentShots[x, y] = true;
hubConnection.SendAsync("SendHitMiss", room, playerBoard[x, y], shipHPs[x, y]);
hubConnection.SendAsync("SendHitMiss", room, playerBoard[x, y]);
var formattedMessage = $"{user} fires at {x} , {y}";
moves.Add(formattedMessage);
isTurn = true;
Expand All @@ -436,7 +443,7 @@
{
string message;

if (coordinates is not null)
if (coordinates.HasValue)
{
if (wasHit)
{
Expand Down Expand Up @@ -483,6 +490,7 @@
{
if (hubConnection is not null)
{
ships = new Ship[shipLimit];
await hubConnection.SendAsync("SendShipNum", room, shipLimit);
}
}
Expand Down
23 changes: 23 additions & 0 deletions BlazorBattleship/Shared/Ship.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace BlazorBattleship.Shared
{
public struct Ship
{
public (int x, int y) shipCoords;
public int length;
public int hp;
public string name;

public Ship(int length, int hp, string name, (int x, int y) coords)
{
this.length = length;
this.hp = hp;
this.name = name;
this.shipCoords = coords;
}

public bool IsDestroyed()
{
return hp <= 0;
}
}
}
22 changes: 22 additions & 0 deletions BlazorBattleship/Shared/Sidebar.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@using BlazorBattleship.Shared

@code {
[Parameter]
public Ship[]? Ships { get; set; } // Receiving the ships array from the parent component
}

<div class="sidebar">
<h3>Your Ships</h3>
<ul>
@if(Ships != null)
{
@foreach (var ship in Ships)
{
<li>@ship.name: @ship.hp HP @if (ship.IsDestroyed())
{
<strong>(Destroyed)</strong>
}</li>
}
}
</ul>
</div>
Binary file modified BlazorBattleship/bin/Debug/net6.0/BlazorBattleship.dll
Binary file not shown.
Binary file modified BlazorBattleship/bin/Debug/net6.0/BlazorBattleship.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ build_metadata.AdditionalFiles.CssScope =
build_metadata.AdditionalFiles.TargetPath = UGFnZXNcSW5kZXgucmF6b3I=
build_metadata.AdditionalFiles.CssScope =

[C:/Users/A Big Baby/Documents/GitHub/Battleship/BlazorBattleship/Shared/Sidebar.razor]
build_metadata.AdditionalFiles.TargetPath = U2hhcmVkXFNpZGViYXIucmF6b3I=
build_metadata.AdditionalFiles.CssScope =

[C:/Users/A Big Baby/Documents/GitHub/Battleship/BlazorBattleship/Shared/SurveyPrompt.razor]
build_metadata.AdditionalFiles.TargetPath = U2hhcmVkXFN1cnZleVByb21wdC5yYXpvcg==
build_metadata.AdditionalFiles.CssScope =
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
08879e2c620f8ba2848b93428aab9c6e21415272
b68e3f88d7f0de9d7f14c16fb8f73c37973e2b5a
Binary file modified BlazorBattleship/obj/Debug/net6.0/BlazorBattleship.dll
Binary file not shown.
Binary file modified BlazorBattleship/obj/Debug/net6.0/BlazorBattleship.pdb
Binary file not shown.
2 changes: 1 addition & 1 deletion BlazorBattleship/obj/Debug/net6.0/project.razor.vs.json

Large diffs are not rendered by default.

Binary file modified BlazorBattleship/obj/Debug/net6.0/ref/BlazorBattleship.dll
Binary file not shown.
Binary file modified BlazorBattleship/obj/Debug/net6.0/refint/BlazorBattleship.dll
Binary file not shown.

0 comments on commit 61a8dfa

Please sign in to comment.