Commit c45b02b9 authored by Andres Käver's avatar Andres Käver
Browse files

code

parent bd716493
using System;
using System.Collections.Generic;
using System.Text.Json;
namespace BattleShipBrain
{
public class BSBrain
{
private int _currentPlayerNo = 0;
private GameBoard[] GameBoards = new GameBoard[2];
private readonly BoardSquareState[,] _boardA;
private readonly BoardSquareState[,] _boardB;
private readonly Random _rnd = new Random();
public BSBrain(int xSize, int ySize)
public BSBrain(GameConfig config)
{
_boardA = new BoardSquareState[xSize,ySize];
_boardB = new BoardSquareState[xSize,ySize];
GameBoards[0] = new GameBoard();
GameBoards[1] = new GameBoard();
GameBoards[0].Board = new BoardSquareState[config.BoardSizeX, config.BoardSizeY];
GameBoards[1].Board = new BoardSquareState[config.BoardSizeX, config.BoardSizeY];
for (var x = 0; x < xSize; x++)
for (var x = 0; x < config.BoardSizeX; x++)
{
for (var y = 0; y < ySize; y++)
for (var y = 0; y < config.BoardSizeY; y++)
{
_boardA[x, y] = new BoardSquareState
GameBoards[0].Board[x, y] = new BoardSquareState
{
IsBomb = _rnd.Next(0, 2) != 0,
IsShip = _rnd.Next(0, 2) != 0
......@@ -30,14 +34,13 @@ namespace BattleShipBrain
public BoardSquareState[,] GetBoard(int playerNo)
{
if (playerNo == 0) return CopyOfBoard(_boardA);
return CopyOfBoard(_boardB);
return CopyOfBoard(GameBoards[playerNo].Board);
}
private BoardSquareState[,] CopyOfBoard(BoardSquareState[,] board)
{
var res = new BoardSquareState[board.GetLength(0),board.GetLength(1)];
var res = new BoardSquareState[board.GetLength(0), board.GetLength(1)];
for (var x = 0; x < board.GetLength(0); x++)
{
for (var y = 0; y < board.GetLength(1); y++)
......@@ -49,5 +52,21 @@ namespace BattleShipBrain
return res;
}
public string GetBrainJson()
{
var jsonOptions = new JsonSerializerOptions()
{
WriteIndented = true
};
var dto = new SaveGameDTO();
var jsonStr = JsonSerializer.Serialize(dto, jsonOptions);
return jsonStr;
}
public void RestoreBrainFromJson(string json)
{
}
}
}
\ No newline at end of file
namespace BattleShipBrain
{
public struct Coordinate
{
public int X { get; set; }
public int Y { get; set; }
public override string ToString() => $"X: {X}, Y: {Y}";
}
}
\ No newline at end of file
namespace BattleShipBrain
{
public enum EShipTouchRule
{
NoTouch,
CornerTouch,
SideTouch
}
}
\ No newline at end of file
using System.Collections.Generic;
namespace BattleShipBrain
{
public class GameBoard
{
public BoardSquareState[,] Board { get; set; } = null!;
public List<Ship> Ships { get; set; } = default!;
}
}
\ No newline at end of file
using System.Collections.Generic;
using System.Text.Json;
namespace BattleShipBrain
{
public class GameConfig
{
public int BoardSizeX { get; set; } = 10;
public int BoardSizeY { get; set; } = 10;
public List<ShipConfig> ShipConfigs { get; set; } = new List<ShipConfig>()
{
new ShipConfig()
{
Name = "Patrol",
Quantity = 5,
ShipSizeY = 1,
ShipSizeX = 1,
},
new ShipConfig()
{
Name = "Cruiser",
Quantity = 4,
ShipSizeY = 1,
ShipSizeX = 2,
},
new ShipConfig()
{
Name = "Submarine",
Quantity = 3,
ShipSizeY = 1,
ShipSizeX = 3,
},
new ShipConfig()
{
Name = "Battleship",
Quantity = 2,
ShipSizeY = 1,
ShipSizeX = 4,
},
new ShipConfig()
{
Name = "Carrier",
Quantity = 1,
ShipSizeY = 1,
ShipSizeX = 5,
},
};
public EShipTouchRule EShipTouchRule { get; set; } = EShipTouchRule.NoTouch;
public override string ToString()
{
var jsonOptions = new JsonSerializerOptions()
{
WriteIndented = true
};
return JsonSerializer.Serialize(this, jsonOptions);
}
}
}
\ No newline at end of file
using System.Collections.Generic;
namespace BattleShipBrain
{
// DTO - Data Transfer Object
public class SaveGameDTO
{
public int CurrentPlayerNo { get; set; } = 0;
public GameBoardDTO[] GameBoards { get; set; } = new GameBoardDTO[2];
public class GameBoardDTO
{
public List<List<BoardSquareState>> Board { get; set; }
public List<Ship> Ships { get; set; }
}
}
}
\ No newline at end of file
using System.Collections.Generic;
using System.Linq;
namespace BattleShipBrain
{
public class Ship
{
public string Name { get; private set; }
private readonly List<Coordinate> _coordinates = new List<Coordinate>();
public Ship(string name, Coordinate position, int length, int height)
{
Name = name;
for (var x = 0; x < position.X + length; x++)
{
for (var y = 0; y < position.Y + height; y++)
{
_coordinates.Add(new Coordinate(){X = x, Y = y});
}
}
}
public int GetShipSize() => _coordinates.Count;
public int GetShipDamageCount(BoardSquareState[,] board) =>
// count all the items that match the predicate
_coordinates.Count(coordinate => board[coordinate.X, coordinate.Y].IsBomb);
public bool IsShipSunk(BoardSquareState[,] board) =>
// returns true when all the items in the list match predicate
_coordinates.All(coordinate => board[coordinate.X, coordinate.Y].IsBomb);
}
}
\ No newline at end of file
namespace BattleShipBrain
{
public class ShipConfig
{
public string Name { get; set; }
public int Quantity { get; set; }
public int ShipSizeY { get; set; }
public int ShipSizeX { get; set; }
}
}
\ No newline at end of file
......@@ -10,4 +10,9 @@
<ProjectReference Include="..\BattleShipConsoleUI\BattleShipConsoleUI.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Configs" />
<Folder Include="SaveGames" />
</ItemGroup>
</Project>
{
"BoardSizeX": 100,
"BoardSizeY": 100,
"ShipConfigs": [
{
"Name": "Patrol",
"Quantity": 5,
"ShipSizeY": 1,
"ShipSizeX": 1
},
{
"Name": "Cruiser",
"Quantity": 4,
"ShipSizeY": 1,
"ShipSizeX": 2
},
{
"Name": "Submarine",
"Quantity": 3,
"ShipSizeY": 1,
"ShipSizeX": 3
},
{
"Name": "Battleship",
"Quantity": 2,
"ShipSizeY": 1,
"ShipSizeX": 4
},
{
"Name": "Carrier_1x1",
"Quantity": 1,
"ShipSizeY": 1,
"ShipSizeX": 5
}
],
"EShipTouchRule": 0
}
\ No newline at end of file
using System;
using System.Text.Json;
using BattleShipBrain;
using BattleShipConsoleUI;
......@@ -6,11 +7,46 @@ namespace BattleShipConsoleApp
{
class Program
{
private static string _basePath;
static void Main(string[] args)
{
Console.WriteLine("Hello Battleship!");
var brain = new BSBrain(10,5);
_basePath = args.Length == 1 ? args[0] : System.IO.Directory.GetCurrentDirectory();
Console.WriteLine($"Base path: {_basePath}");
var conf = new GameConfig();
var jsonOptions = new JsonSerializerOptions()
{
WriteIndented = true
};
var fileNameStandardConfig = _basePath + System.IO.Path.DirectorySeparatorChar + "Configs" + System.IO.Path.DirectorySeparatorChar + "standard.json";
var confJsonStr = JsonSerializer.Serialize(conf, jsonOptions);
Console.WriteLine("Standard conf is in: " + fileNameStandardConfig);
if (!System.IO.File.Exists(fileNameStandardConfig))
{
Console.WriteLine("Saving default config!");
System.IO.File.WriteAllText(fileNameStandardConfig, confJsonStr);
}
if (System.IO.File.Exists(fileNameStandardConfig))
{
Console.WriteLine("Loading config...");
var confText = System.IO.File.ReadAllText(fileNameStandardConfig);
conf = JsonSerializer.Deserialize<GameConfig>(confText);
Console.WriteLine(conf);
}
var brain = new BSBrain(conf);
var fileNameSave = _basePath + System.IO.Path.DirectorySeparatorChar + "SaveGames" + System.IO.Path.DirectorySeparatorChar + "game.json";
Console.WriteLine(brain.GetBrainJson());
/*
var brain = new BSBrain(new GameConfig());
BSConsoleUI.DrawBoard(brain.GetBoard(0));
......@@ -26,7 +62,7 @@ namespace BattleShipConsoleApp
Console.WriteLine("----------------------");
BSConsoleUI.DrawBoard(brain.GetBoard(0));
*/
}
}
}
\ No newline at end of file
{
"BoardSizeX": 10,
"BoardSizeY": 10,
"ShipConfigs": [
{
"Name": "Patrol",
"Quantity": 5,
"ShipSizeY": 1,
"ShipSizeX": 1
},
{
"Name": "Cruiser",
"Quantity": 4,
"ShipSizeY": 1,
"ShipSizeX": 2
},
{
"Name": "Submarine",
"Quantity": 3,
"ShipSizeY": 1,
"ShipSizeX": 3
},
{
"Name": "Battleship",
"Quantity": 2,
"ShipSizeY": 1,
"ShipSizeX": 4
},
{
"Name": "Carrier",
"Quantity": 1,
"ShipSizeY": 1,
"ShipSizeX": 5
}
],
"EShipTouchRule": 0
}
\ No newline at end of file
{
"BoardSizeX": 10,
"BoardSizeY": 10,
"ShipConfigs": [
{
"Name": "Patrol",
"Quantity": 5,
"ShipSizeY": 1,
"ShipSizeX": 1
},
{
"Name": "Cruiser",
"Quantity": 4,
"ShipSizeY": 1,
"ShipSizeX": 2
},
{
"Name": "Submarine",
"Quantity": 3,
"ShipSizeY": 1,
"ShipSizeX": 3
},
{
"Name": "Battleship",
"Quantity": 2,
"ShipSizeY": 1,
"ShipSizeX": 4
},
{
"Name": "Carrier",
"Quantity": 1,
"ShipSizeY": 1,
"ShipSizeX": 5
}
],
"EShipTouchRule": 0
}
\ No newline at end of file
......@@ -12,6 +12,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BattleShipConsoleApp", "Bat
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BattleShipConsoleUI", "BattleShipConsoleUI\BattleShipConsoleUI.csproj", "{976FBEE4-F336-4BE5-BFB3-D95ACD6649C1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0222F757-14A1-436E-8DC6-6F83E5DCA8EA}"
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
.gitignore = .gitignore
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603;CS8613;CS8618;CS8625</WarningsAsErrors>
</PropertyGroup>
</Project>
\ No newline at end of file
......@@ -4,7 +4,7 @@ namespace MenuSystem
{
public class MenuItem
{
public MenuItem(string shortCut, string title, Func<string> runMethod)
public MenuItem(string shortCut, string title, Func<string>? runMethod)
{
if (string.IsNullOrEmpty(shortCut))
{
......@@ -23,7 +23,7 @@ namespace MenuSystem
public string ShortCut { get; private set; }
public string Title { get; private set; }
public Func<string> RunMethod { get; private set; }
public Func<string>? RunMethod { get; private set; }
public override string ToString()
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment