Skip to content

Commit

Permalink
Visual Opponent Shot Info
Browse files Browse the repository at this point in the history
Added visual feedback for opponent shots through the existing tile GetCellClass(int x, int y) function.

Compares the playerBoard[] against an opponentShots[] board to determine if opponent shots are hits or misses, and changes the class of that tile accordingly.

Co-Authored-By: yung00se <177177697+yung00se@users.noreply.github.com>
  • Loading branch information
PDykes1996 and yung00se committed Sep 13, 2024
1 parent bcb2b38 commit 12a7a83
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 5 deletions.
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.
19 changes: 15 additions & 4 deletions BlazorBattleship/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
private HubConnection? hubConnection;
public bool IsConnected => hubConnection?.State == HubConnectionState.Connected;
#endregion
#region Player Input
#region Player Information
private List<string> moves = new List<string>();
private string? userInput = "";
private (int x,int y)? coordinates;
Expand All @@ -102,6 +102,7 @@
// Game board tracking (10x10 grids for both players)
private bool[,] playerBoard = new bool[10, 10]; // Stores player's ships
private bool[,] previewBoard = new bool[10, 10]; // Stores ship previews
private bool[,] opponentShots = new bool[10, 10]; // Stores the opponents previous shots
private bool[,] opponentBoard = new bool[10,10]; // Stores hits/misses on opponent
private bool[,] opponentDefaultBoard = new bool[10, 10]; // A default state for the opponents board when neither hit nor miss is recorded
Expand All @@ -128,7 +129,6 @@
{
isShipHorizontal = !isShipHorizontal;
}
#endregion

#region Board Modification
#region Player Board
Expand All @@ -137,15 +137,23 @@
/*Changes the type of grid space depending on whether or not a ship is currently there.
* Effectively this allows for the preview to not override existing ships on the setup board.
*/
if (playerBoard[x, y])//If there is a ship on this space
if (playerBoard[x, y] && !opponentShots[x,y])//If there is a ship on this space and the opponent has not shot here yet
{
return "ship";//The space is of type ship
}
else if(playerBoard[x, y] && opponentShots[x,y])//If there is a ship on this space, and the opponent has shot here
{
return "hit";//The ship has been destroyed
}
else if(!playerBoard[x, y] && opponentShots[x,y])//If the opponent has shot a tile that does not have a ship on it
{
return "miss";//The opponent has missed
}
else if (previewBoard[x, y])//If the space has a preview of a ship
{
return "preview";//The space is of type preview
}
else//If neither are true
else//If none are true
{
return "square";//The space is blank
}
Expand Down Expand Up @@ -250,6 +258,7 @@
}
#endregion
#endregion
#endregion

#region Networking Functionality
// runs when client connects
Expand All @@ -264,6 +273,8 @@
// when a user and message are received from the connection...
hubConnection.On<string, int ,int>("ReceiveCoordinates", (user, x, y) =>
{
opponentShots[x, y] = true;

hubConnection.SendAsync("SendHitMiss", playerBoard[x, y]);
var formattedMessage = $"{user} fires at {x} , {y}";
moves.Add(formattedMessage);
Expand Down
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.
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 12a7a83

Please sign in to comment.