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

code from lecture

parent 960b1cb0
using System;
namespace BattleShipBrain
{
public class BSBrain
{
private readonly BoardSquareState[,] _boardA;
private readonly BoardSquareState[,] _boardB;
private readonly Random _rnd = new Random();
public BSBrain(int xSize, int ySize)
{
_boardA = new BoardSquareState[xSize,ySize];
_boardB = new BoardSquareState[xSize,ySize];
for (var x = 0; x < xSize; x++)
{
for (var y = 0; y < ySize; y++)
{
_boardA[x, y] = new BoardSquareState
{
IsBomb = _rnd.Next(0, 2) != 0,
IsShip = _rnd.Next(0, 2) != 0
};
}
}
}
public BoardSquareState[,] GetBoard(int playerNo)
{
if (playerNo == 0) return CopyOfBoard(_boardA);
return CopyOfBoard(_boardB);
}
private BoardSquareState[,] CopyOfBoard(BoardSquareState[,] board)
{
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++)
{
res[x, y] = board[x, y];
}
}
return res;
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
namespace BattleShipBrain
{
public struct BoardSquareState
{
public bool IsShip { get; set; }
public bool IsBomb { get; set; }
public override string ToString()
{
switch (IsEmpty: IsShip, IsBomb)
{
case (false, false):
return " ";
case (false, true):
return "-";
case (true, false):
return "8";
case (true, true):
return " *";
}
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\BattleShipBrain\BattleShipBrain.csproj" />
<ProjectReference Include="..\BattleShipConsoleUI\BattleShipConsoleUI.csproj" />
</ItemGroup>
</Project>
using System;
using BattleShipBrain;
using BattleShipConsoleUI;
namespace BattleShipConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Battleship!");
var brain = new BSBrain(10,5);
BSConsoleUI.DrawBoard(brain.GetBoard(0));
var b = brain.GetBoard(0);
b[0, 0].IsBomb = true;
b[0, 0].IsShip = false;
Console.WriteLine("+---+---+--------------");
Console.WriteLine("| x | * |");
Console.WriteLine("+---+---+---------------");
Console.WriteLine("----------------------");
BSConsoleUI.DrawBoard(brain.GetBoard(0));
}
}
}
\ No newline at end of file
using System;
using BattleShipBrain;
namespace BattleShipConsoleUI
{
public static class BSConsoleUI
{
public static void DrawBoard(BoardSquareState[,] board)
{
for (var y = 0; y < board.GetLength(1); y++)
{
for (var x = 0; x < board.GetLength(0); x++)
{
Console.Write(board[x,y]);
}
Console.WriteLine();
}
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\BattleShipBrain\BattleShipBrain.csproj" />
</ItemGroup>
</Project>
......@@ -4,6 +4,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "ConsoleApp\Co
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MenuSystem", "MenuSystem\MenuSystem.csproj", "{3D6B86AF-3A4D-4145-A2D8-57FE6AF4CADF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CalculatorBrain", "CalculatorBrain\CalculatorBrain.csproj", "{E7ED49EB-2CE2-49DE-85E2-26264A553BA2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BattleShipBrain", "BattleShipBrain\BattleShipBrain.csproj", "{FCB89975-74B2-41BA-A14C-EF81A15192E3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BattleShipConsoleApp", "BattleShipConsoleApp\BattleShipConsoleApp.csproj", "{2C1776A5-855D-4B27-99D0-8419C100A6A5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BattleShipConsoleUI", "BattleShipConsoleUI\BattleShipConsoleUI.csproj", "{976FBEE4-F336-4BE5-BFB3-D95ACD6649C1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -18,5 +26,21 @@ Global
{3D6B86AF-3A4D-4145-A2D8-57FE6AF4CADF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D6B86AF-3A4D-4145-A2D8-57FE6AF4CADF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D6B86AF-3A4D-4145-A2D8-57FE6AF4CADF}.Release|Any CPU.Build.0 = Release|Any CPU
{E7ED49EB-2CE2-49DE-85E2-26264A553BA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7ED49EB-2CE2-49DE-85E2-26264A553BA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7ED49EB-2CE2-49DE-85E2-26264A553BA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7ED49EB-2CE2-49DE-85E2-26264A553BA2}.Release|Any CPU.Build.0 = Release|Any CPU
{FCB89975-74B2-41BA-A14C-EF81A15192E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FCB89975-74B2-41BA-A14C-EF81A15192E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FCB89975-74B2-41BA-A14C-EF81A15192E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FCB89975-74B2-41BA-A14C-EF81A15192E3}.Release|Any CPU.Build.0 = Release|Any CPU
{2C1776A5-855D-4B27-99D0-8419C100A6A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C1776A5-855D-4B27-99D0-8419C100A6A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C1776A5-855D-4B27-99D0-8419C100A6A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C1776A5-855D-4B27-99D0-8419C100A6A5}.Release|Any CPU.Build.0 = Release|Any CPU
{976FBEE4-F336-4BE5-BFB3-D95ACD6649C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{976FBEE4-F336-4BE5-BFB3-D95ACD6649C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{976FBEE4-F336-4BE5-BFB3-D95ACD6649C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{976FBEE4-F336-4BE5-BFB3-D95ACD6649C1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
using System;
namespace CalculatorBrain
{
public class Brain
{
public Double CurrentValue { get; private set; } = 0;
public double Add(double value)
{
CurrentValue += value;
return CurrentValue;
}
public void Clear() => CurrentValue = 0;
public void SetValue(double value) => CurrentValue = value;
public double ApplyCustomFunction(Func<double, double, double> funcToApply, double value)
{
CurrentValue = funcToApply(CurrentValue, value);
return CurrentValue;
}
public override string ToString() => CurrentValue.ToString();
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
......@@ -6,6 +6,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CalculatorBrain\CalculatorBrain.csproj" />
<ProjectReference Include="..\MenuSystem\MenuSystem.csproj" />
</ItemGroup>
......
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Xml;
using CalculatorBrain;
using MenuSystem;
namespace ConsoleApp
{
class Program
{
private static double CalculatorCurrentDisplay = 0.0;
private static readonly Brain Brain = new Brain();
static void Main(string[] args)
{
Console.Clear();
var mainMenu = new Menu("Calculator Main", EMenuLevel.Root);
var mainMenu = new Menu(ReturnCurrentDisplayValue, "Calculator Main", EMenuLevel.Root);
mainMenu.AddMenuItems(new List<MenuItem>()
{
new MenuItem("A", "Binary operations", SubmenuBinary),
new MenuItem("S", "Unary operations", SubmenuUnary),
});
mainMenu.Run();
}
public static string ReturnCurrentDisplayValue()
{
return Brain.CurrentValue.ToString();
}
public static string MethodA()
{
Console.WriteLine("Method A!!!!!");
......@@ -32,36 +41,53 @@ namespace ConsoleApp
public static string SubmenuBinary()
{
var menu = new Menu("Binary", EMenuLevel.First);
var menu = new Menu(ReturnCurrentDisplayValue, "Binary", EMenuLevel.First);
menu.AddMenuItems(new List<MenuItem>()
{
new MenuItem("+", "+", Add),
new MenuItem("-", "-", MethodA),
new MenuItem("/", "/", MethodA),
new MenuItem("*", "*", MethodA),
new MenuItem("*", "*", Multiplication),
});
var res = menu.Run();
return res;
}
public static string Multiplication()
{
Console.WriteLine("Current value: " + Brain.CurrentValue);
Console.WriteLine("multiply");
Console.Write("number: ");
var n = Console.ReadLine()?.Trim();
double.TryParse(n, out var converted);
//Brain.ApplyCustomFunction( CustomMultiply, converted);
Brain.ApplyCustomFunction( (a, b) => a * b, converted);
return "";
}
public static double CustomMultiply(double a , double b)
{
return a * b;
}
public static string Add()
{
// CalculatorCurrentDisplay
Console.WriteLine("Current value: " + CalculatorCurrentDisplay);
Console.WriteLine("Current value: " + Brain.CurrentValue);
Console.WriteLine("plus");
Console.Write("number: ");
var n = Console.ReadLine()?.Trim();
double.TryParse(n, out var converted);
CalculatorCurrentDisplay = CalculatorCurrentDisplay + converted;
Brain.Add(converted);
return "";
}
public static string SubmenuUnary()
{
var menu = new Menu("Unary", EMenuLevel.First);
var menu = new Menu(ReturnCurrentDisplayValue, "Unary", EMenuLevel.First);
menu.AddMenuItems(new List<MenuItem>()
{
new MenuItem("Negate", "Negate", MethodA),
......
......@@ -17,9 +17,12 @@ namespace MenuSystem
private readonly HashSet<string> _menuSpecialShortCuts = new HashSet<string>();
private readonly string _title;
public Menu(string title, EMenuLevel menuLevel)
private readonly Func<string> _getHeaderInfoString;
public Menu(Func<string> getHeaderInfoString, string title, EMenuLevel menuLevel)
{
_getHeaderInfoString = getHeaderInfoString;
_title = title;
_menuLevel = menuLevel;
......@@ -47,6 +50,7 @@ namespace MenuSystem
{
throw new ApplicationException($"Conflicting menu shortcut {item.ShortCut.ToUpper()}");
}
if (_menuShortCuts.Add(item.ShortCut.ToUpper()) == false)
{
throw new ApplicationException($"Conflicting menu shortcut {item.ShortCut.ToUpper()}");
......@@ -89,29 +93,37 @@ namespace MenuSystem
if (isInputValid)
{
var item = _menuItems.FirstOrDefault(t => t.ShortCut.ToUpper() == input);
input = item?.RunMethod==null ? input : item.RunMethod();
input = item?.RunMethod == null ? input : item.RunMethod();
}
runDone = _menuSpecialShortCuts.Contains(input);
if (!runDone && !isInputValid)
{
Console.WriteLine($"Unknown shortcut '{input}'!");
}
} while (!runDone);
if (input == _menuItemReturn.ShortCut.ToUpper()) return "";
return input;
}
private void OutputMenu()
{
Console.WriteLine("====> " + _title + " <====");
if (_getHeaderInfoString != null)
{
var headerInfo = _getHeaderInfoString();
if (headerInfo != null)
{
Console.WriteLine(headerInfo);
}
}
Console.WriteLine("-------------------");
foreach (var t in _menuItems)
{
Console.WriteLine(t);
......
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