Commit 2c9d0a76 by adbaga

yes

parent ccf99b58
Showing with 302 additions and 57 deletions
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
<e p="CellState.cs" t="Include" /> <e p="CellState.cs" t="Include" />
<e p="Engine.cs" t="Include" /> <e p="Engine.cs" t="Include" />
<e p="Game.csproj" t="IncludeRecursive" /> <e p="Game.csproj" t="IncludeRecursive" />
<e p="GameConfigHandler.cs" t="Include" />
<e p="GameSettings.cs" t="Include" />
<e p="obj" t="ExcludeRecursive"> <e p="obj" t="ExcludeRecursive">
<e p="Debug" t="Include"> <e p="Debug" t="Include">
<e p="netcoreapp3.0" t="Include"> <e p="netcoreapp3.0" t="Include">
......
...@@ -60,6 +60,8 @@ namespace ConsoleUserInt ...@@ -60,6 +60,8 @@ namespace ConsoleUserInt
return "±"; return "±";
case CellState.Hidden: case CellState.Hidden:
return "-"; return "-";
case CellState.Open:
return "O";
default: default:
throw new InvalidEnumArgumentException("Unknown enum option!"); throw new InvalidEnumArgumentException("Unknown enum option!");
} }
...@@ -67,6 +69,8 @@ namespace ConsoleUserInt ...@@ -67,6 +69,8 @@ namespace ConsoleUserInt
} }
//so, randomizer should generate an array in that range where the mines are stored. If they insert y/x where mine is stored, game ends //so, randomizer should generate an array in that range where the mines are stored. If they insert y/x where mine is stored, game ends
} }
} }
......
...@@ -6,6 +6,7 @@ namespace Game ...@@ -6,6 +6,7 @@ namespace Game
Mine, Mine,
Flag, Flag,
Hidden, Hidden,
Open,
} }
......
using System; using System;
using Game; using Game;
using GameEngine;
namespace Engine namespace Engine
{ {
...@@ -12,22 +13,22 @@ namespace Engine ...@@ -12,22 +13,22 @@ namespace Engine
public BoardDim(int boardHeight, int boardWidth) public BoardDim(GameSettings settings)
{ {
if (boardHeight > 7 || boardWidth > 7) if (settings.BoardHeight > 7 || settings.BoardWidth > 7)
{ {
BoardHeight = boardHeight; BoardHeight = settings.BoardHeight;
BoardWidth = boardWidth; BoardWidth = settings.BoardWidth;
// initialize the board // initialize the board
Board = new CellState[boardHeight, boardWidth]; Board = new CellState[BoardHeight, BoardWidth];
} }
else else
{ {
Console.WriteLine($"{boardHeight} & {boardWidth} is too small! Minimum 8x8"); Console.WriteLine($"{BoardHeight} & {BoardWidth} is too small! Minimum 8x8");
throw new ArgumentException("Board size has to be at least 8x8!"); throw new ArgumentException("Board size has to be at least 8x8!");
...@@ -39,6 +40,20 @@ namespace Engine ...@@ -39,6 +40,20 @@ namespace Engine
public void Move(int posY, int posX)
{
if (Board[posY, posX] != CellState.Empty)
{
return;
}
Board[posY, posX] = CellState.Open;
}
public CellState[,] GetBoard() public CellState[,] GetBoard()
{ {
...@@ -52,9 +67,6 @@ namespace Engine ...@@ -52,9 +67,6 @@ namespace Engine
} }
public class Mines public class Mines
...@@ -66,15 +78,17 @@ namespace Engine ...@@ -66,15 +78,17 @@ namespace Engine
public int minePosX { get; } public int minePosX { get; }
private static Random random = new Random(); private static Random random = new Random();
public static int[,] MinesSetter(int height, int width) public static int[,] MinesSetter(int height, int width)
{ {
int mineCount = 0; int mineCount = 0;
int numOfMines = height * width / 8; int numOfMines = height * width / 6;
//int[,] mineLoc = new int[width, height]; //int[,] mineLoc = new int[width, height];
var placed = 0;
int[,] mineLoc = new int[width, height]; int[,] mineLoc = new int[width, height];
{ {
...@@ -94,7 +108,7 @@ namespace Engine ...@@ -94,7 +108,7 @@ namespace Engine
} while (mineCount <= numOfMines); } while (mineCount <= numOfMines);
} }
Console.WriteLine($"There are {numOfMines} mines");
Console.WriteLine(mineLoc.Length); Console.WriteLine(mineLoc.Length);
/*foreach (var x in mineLoc) /*foreach (var x in mineLoc)
{ {
...@@ -105,6 +119,11 @@ namespace Engine ...@@ -105,6 +119,11 @@ namespace Engine
return mineLoc; return mineLoc;
} }
} }
......
using System.Text.Json;
using GameEngine;
namespace Game
{
public static class GameConfigHandler
{
private const string FileName = "gamesettings.json";
public static void SaveConfig(GameSettings settings, string fileName = FileName)
{
using (var writer = System.IO.File.CreateText(fileName))
{
var jsonString = JsonSerializer.Serialize(settings);
writer.Write(jsonString);
}
}
public static GameSettings LoadConfig(string fileName = FileName)
{
if (System.IO.File.Exists(fileName))
{
var jsonString = System.IO.File.ReadAllText(fileName);
var res = JsonSerializer.Deserialize<GameSettings>(jsonString);
return res;
}
return new GameSettings();
}
}
}
\ No newline at end of file
namespace GameEngine
{
public class GameSettings
{
public string GameName { get; set; } = "Minesweeper";
public int BoardHeight { get; set; } = 8;
public int BoardWidth { get; set; } = 8;
}
}
\ No newline at end of file
...@@ -11,4 +11,8 @@ ...@@ -11,4 +11,8 @@
<ProjectReference Include="..\MenuSystem\MenuSystem.csproj" /> <ProjectReference Include="..\MenuSystem\MenuSystem.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
</Project> </Project>
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using ConsoleUserInt.ConsoleUI; using ConsoleUserInt.ConsoleUI;
using Engine; using Engine;
using Game;
using GameEngine;
using MenuSystem; using MenuSystem;
using static System.Console; using static System.Console;
...@@ -9,6 +11,9 @@ namespace IDoMinesweeper ...@@ -9,6 +11,9 @@ namespace IDoMinesweeper
{ {
class Program class Program
{ {
private static GameSettings _settings;
static void Main(string[] args) static void Main(string[] args)
{ {
Clear(); Clear();
...@@ -16,6 +21,9 @@ namespace IDoMinesweeper ...@@ -16,6 +21,9 @@ namespace IDoMinesweeper
WriteLine("Welcome to Minesweeper"); WriteLine("Welcome to Minesweeper");
_settings = GameConfigHandler.LoadConfig();
var gameMenu = new Menu(1) var gameMenu = new Menu(1)
{ {
Title = "New Game", Title = "New Game",
...@@ -25,14 +33,14 @@ namespace IDoMinesweeper ...@@ -25,14 +33,14 @@ namespace IDoMinesweeper
"1", new MenuItem() "1", new MenuItem()
{ {
Title = "Level 1", Title = "Level 1",
CommandToExecute = null CommandToExecute = TestGame
} }
}, },
{ {
"2", new MenuItem() "2", new MenuItem()
{ {
Title = "Level 2", Title = "Level 2",
CommandToExecute = null CommandToExecute = OldTestGame
} }
}, },
...@@ -40,7 +48,7 @@ namespace IDoMinesweeper ...@@ -40,7 +48,7 @@ namespace IDoMinesweeper
"3", new MenuItem() "3", new MenuItem()
{ {
Title = "Customize Your Board size", Title = "Customize Your Board size",
CommandToExecute = TestGame CommandToExecute = SaveSettings
} }
}, },
...@@ -60,6 +68,16 @@ namespace IDoMinesweeper ...@@ -60,6 +68,16 @@ namespace IDoMinesweeper
} }
}, },
{
"J", new MenuItem()
{
Title = "Set defaults for game (save to JSON)",
CommandToExecute = SaveSettings
}
},
{ {
"D", new MenuItem() "D", new MenuItem()
...@@ -75,12 +93,57 @@ namespace IDoMinesweeper ...@@ -75,12 +93,57 @@ namespace IDoMinesweeper
}
static string SaveSettings()
{
Console.Clear();
var boardWidth = 0;
var boardHeight = 0;
var userCanceled = false;
(boardWidth, userCanceled) = GetUserIntInput("Enter board width", 8, 30, 0);
if (userCanceled) return "";
(boardHeight, userCanceled) = GetUserIntInput("Enter board height", 8, 30, 0);
if (userCanceled) return "";
_settings.BoardHeight = boardHeight;
_settings.BoardWidth = boardWidth;
GameConfigHandler.SaveConfig(_settings);
return "";
}
static string TestGame() static string TestGame()
{ {
var game = new BoardDim(_settings);
GameUI.PrintBoard(game);
Mines.MinesSetter(_settings.BoardHeight, _settings.BoardWidth);
Console.WriteLine();
return "GAME OVER!!";
}
static string OldTestGame()
{
Console.Clear();
WriteLine("Board height (Min. 8): "); WriteLine("Board height (Min. 8): ");
WriteLine(">"); //WriteLine(">");
var checkHeight = ReadLine(); var checkHeight = ReadLine();
int height = 0; int height = 0;
if (!int.TryParse(checkHeight, out height)) if (!int.TryParse(checkHeight, out height))
...@@ -88,31 +151,20 @@ namespace IDoMinesweeper ...@@ -88,31 +151,20 @@ namespace IDoMinesweeper
WriteLine("Not an integer"); WriteLine("Not an integer");
} }
else
{
height = Convert.ToInt32(checkHeight);
}
WriteLine("Board width (Min. 8): "); WriteLine("Board width (Min. 8): ");
WriteLine(">"); //WriteLine(">");
var checkWidth = ReadLine(); var checkWidth = ReadLine();
int width = 0; int width = 0;
if (!int.TryParse(checkWidth, out width)) if (!int.TryParse(checkWidth, out width))
{ {
Console.WriteLine("Not an integer"); Console.WriteLine("Not an integer");
} }
else
{
width = Convert.ToInt32(checkWidth); width = Convert.ToInt32(checkWidth);
} height = Convert.ToInt32(checkHeight);
var game2 = new BoardDimOld(height, width);
var doIt = false; var doIt = false;
do do
{ {
...@@ -120,72 +172,116 @@ namespace IDoMinesweeper ...@@ -120,72 +172,116 @@ namespace IDoMinesweeper
if (height < 8 || width < 8) if (height < 8 || width < 8)
{ {
Console.WriteLine("Too small. Minimal board size is 8x8"); Console.WriteLine("Too small. Minimal board size is 8x8");
return ""; }
else if (height > 30 || width > 30)
{
Console.WriteLine("Too big. Maximum board size is 30x30");
}
} } while (!doIt);
return "GAME OVER!!";
else if(height > 30 || width > 30)
{
Console.WriteLine("Too big. Maximum board size is 30x30");
return "";
}
}
static (int result, bool wasCanceled) GetUserIntInput(string prompt, int min, int max,
int? cancelIntValue = null, string cancelStrValue = "")
{
do
{
Console.WriteLine(prompt);
if (cancelIntValue.HasValue || !string.IsNullOrWhiteSpace(cancelStrValue))
{
Console.WriteLine($"To cancel input enter: {cancelIntValue}" +
$"{(cancelIntValue.HasValue && !string.IsNullOrWhiteSpace(cancelStrValue) ? " or " : "")}" +
$"{cancelStrValue}");
}
Console.Write(">");
var consoleLine = Console.ReadLine();
} while (doIt = false); if (consoleLine == cancelStrValue) return (0, true);
if (int.TryParse(consoleLine, out var userInt))
{
return userInt == cancelIntValue ? (userInt, true) : (userInt, false);
}
Console.WriteLine($"'{consoleLine}' cant be converted to int value!");
} while (true);
}
var game = new BoardDim(height, width); static void getBoardDim()
GameUI.PrintBoard(game); {
//var mine = new Mines(height, width);
Mines.MinesSetter(height,width);
var done = true; /*_settings.Height = boardHeight;
do{ _settings.Width = boardWidth;
GameConfigHandler.SaveConfig(_settings);
*/
}
Console.WriteLine("Check the tile"); }
Console.WriteLine("Y location:");
int playerMoveY = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("X location:");
int playerMoveX = Convert.ToInt32(Console.ReadLine());
int[,] checkMines = new int[playerMoveY,playerMoveX]; internal class BoardDimOld
GameUI.PrintBoard(game); {
private static CellState[,] Board { get; set; }
public int BoardWidth { get; }
public int BoardHeight { get; }
} while (done = true);
public BoardDimOld(int boardHeight, int boardWidth)
{
if (boardHeight > 7 || boardWidth > 7)
{
BoardHeight = boardHeight;
BoardWidth = boardWidth;
// initialize the board
Board = new CellState[boardHeight, boardWidth];
}
else
{
Console.WriteLine($"{boardHeight} & {boardWidth} is too small! Minimum 8x8");
throw new ArgumentException("Board size has to be at least 8x8!");
return "";
} }
}
public CellState[,] GetBoard()
{
var result = new CellState[BoardHeight, BoardWidth];
Array.Copy(Board, result, Board.Length);
return result;
} }
} }
} }
...@@ -10,12 +10,21 @@ ...@@ -10,12 +10,21 @@
"dependencies": { "dependencies": {
"ConsoleUserInt": "1.0.0", "ConsoleUserInt": "1.0.0",
"Game": "1.0.0", "Game": "1.0.0",
"MenuSystem": "1.0.0" "MenuSystem": "1.0.0",
"Newtonsoft.Json": "12.0.2"
}, },
"runtime": { "runtime": {
"IDoMinesweeper.dll": {} "IDoMinesweeper.dll": {}
} }
}, },
"Newtonsoft.Json/12.0.2": {
"runtime": {
"lib/netstandard2.0/Newtonsoft.Json.dll": {
"assemblyVersion": "12.0.0.0",
"fileVersion": "12.0.2.23222"
}
}
},
"ConsoleUserInt/1.0.0": { "ConsoleUserInt/1.0.0": {
"dependencies": { "dependencies": {
"Game": "1.0.0" "Game": "1.0.0"
...@@ -42,6 +51,13 @@ ...@@ -42,6 +51,13 @@
"serviceable": false, "serviceable": false,
"sha512": "" "sha512": ""
}, },
"Newtonsoft.Json/12.0.2": {
"type": "package",
"serviceable": true,
"sha512": "sha512-rTK0s2EKlfHsQsH6Yx2smvcTCeyoDNgCW7FEYyV01drPlh2T243PR2DiDXqtC5N4GDm4Ma/lkxfW5a/4793vbA==",
"path": "newtonsoft.json/12.0.2",
"hashPath": "newtonsoft.json.12.0.2.nupkg.sha512"
},
"ConsoleUserInt/1.0.0": { "ConsoleUserInt/1.0.0": {
"type": "project", "type": "project",
"serviceable": false, "serviceable": false,
......
...@@ -16,3 +16,4 @@ ...@@ -16,3 +16,4 @@
/Users/kalevkim/secureprogramming/IDoMinesweeper/IDoMinesweeper/bin/Debug/netcoreapp3.0/Game.dll /Users/kalevkim/secureprogramming/IDoMinesweeper/IDoMinesweeper/bin/Debug/netcoreapp3.0/Game.dll
/Users/kalevkim/secureprogramming/IDoMinesweeper/IDoMinesweeper/bin/Debug/netcoreapp3.0/ConsoleUserInt.pdb /Users/kalevkim/secureprogramming/IDoMinesweeper/IDoMinesweeper/bin/Debug/netcoreapp3.0/ConsoleUserInt.pdb
/Users/kalevkim/secureprogramming/IDoMinesweeper/IDoMinesweeper/bin/Debug/netcoreapp3.0/Game.pdb /Users/kalevkim/secureprogramming/IDoMinesweeper/IDoMinesweeper/bin/Debug/netcoreapp3.0/Game.pdb
/Users/kalevkim/secureprogramming/IDoMinesweeper/IDoMinesweeper/bin/Debug/netcoreapp3.0/Newtonsoft.Json.dll
{ {
"version": 1, "version": 1,
"dgSpecHash": "wiqMLxMh6d4xGEN9mwNfnv8kqAlbM6pQlGxFOEczSsdVD4elFb5W/C5+rrHGWxVE0LpFETEMD97RWTRmL7RcHA==", "dgSpecHash": "7wChmuC8FaWda3jmvT4pidUXZYdiNHWXL7r9Uo/Vo5KLvEr7b+/Rvi1P1olj1H8wqbLyz0OqPPxHRwx7XiwJSw==",
"success": true "success": true
} }
\ No newline at end of file
...@@ -146,6 +146,12 @@ ...@@ -146,6 +146,12 @@
}, },
"frameworks": { "frameworks": {
"netcoreapp3.0": { "netcoreapp3.0": {
"dependencies": {
"Newtonsoft.Json": {
"target": "Package",
"version": "[12.0.2, )"
}
},
"imports": [ "imports": [
"net461", "net461",
"net462", "net462",
......
...@@ -2,6 +2,15 @@ ...@@ -2,6 +2,15 @@
"version": 3, "version": 3,
"targets": { "targets": {
".NETCoreApp,Version=v3.0": { ".NETCoreApp,Version=v3.0": {
"Newtonsoft.Json/12.0.2": {
"type": "package",
"compile": {
"lib/netstandard2.0/Newtonsoft.Json.dll": {}
},
"runtime": {
"lib/netstandard2.0/Newtonsoft.Json.dll": {}
}
},
"ConsoleUserInt/1.0.0": { "ConsoleUserInt/1.0.0": {
"type": "project", "type": "project",
"framework": ".NETCoreApp,Version=v3.0", "framework": ".NETCoreApp,Version=v3.0",
...@@ -38,6 +47,36 @@ ...@@ -38,6 +47,36 @@
} }
}, },
"libraries": { "libraries": {
"Newtonsoft.Json/12.0.2": {
"sha512": "rTK0s2EKlfHsQsH6Yx2smvcTCeyoDNgCW7FEYyV01drPlh2T243PR2DiDXqtC5N4GDm4Ma/lkxfW5a/4793vbA==",
"type": "package",
"path": "newtonsoft.json/12.0.2",
"files": [
".nupkg.metadata",
".signature.p7s",
"LICENSE.md",
"lib/net20/Newtonsoft.Json.dll",
"lib/net20/Newtonsoft.Json.xml",
"lib/net35/Newtonsoft.Json.dll",
"lib/net35/Newtonsoft.Json.xml",
"lib/net40/Newtonsoft.Json.dll",
"lib/net40/Newtonsoft.Json.xml",
"lib/net45/Newtonsoft.Json.dll",
"lib/net45/Newtonsoft.Json.xml",
"lib/netstandard1.0/Newtonsoft.Json.dll",
"lib/netstandard1.0/Newtonsoft.Json.xml",
"lib/netstandard1.3/Newtonsoft.Json.dll",
"lib/netstandard1.3/Newtonsoft.Json.xml",
"lib/netstandard2.0/Newtonsoft.Json.dll",
"lib/netstandard2.0/Newtonsoft.Json.xml",
"lib/portable-net40+sl5+win8+wp8+wpa81/Newtonsoft.Json.dll",
"lib/portable-net40+sl5+win8+wp8+wpa81/Newtonsoft.Json.xml",
"lib/portable-net45+win8+wp8+wpa81/Newtonsoft.Json.dll",
"lib/portable-net45+win8+wp8+wpa81/Newtonsoft.Json.xml",
"newtonsoft.json.12.0.2.nupkg.sha512",
"newtonsoft.json.nuspec"
]
},
"ConsoleUserInt/1.0.0": { "ConsoleUserInt/1.0.0": {
"type": "project", "type": "project",
"path": "../ConsoleUserInt/ConsoleUserInt.csproj", "path": "../ConsoleUserInt/ConsoleUserInt.csproj",
...@@ -58,7 +97,8 @@ ...@@ -58,7 +97,8 @@
".NETCoreApp,Version=v3.0": [ ".NETCoreApp,Version=v3.0": [
"ConsoleUserInt >= 1.0.0", "ConsoleUserInt >= 1.0.0",
"Game >= 1.0.0", "Game >= 1.0.0",
"MenuSystem >= 1.0.0" "MenuSystem >= 1.0.0",
"Newtonsoft.Json >= 12.0.2"
] ]
}, },
"packageFolders": { "packageFolders": {
...@@ -109,6 +149,12 @@ ...@@ -109,6 +149,12 @@
}, },
"frameworks": { "frameworks": {
"netcoreapp3.0": { "netcoreapp3.0": {
"dependencies": {
"Newtonsoft.Json": {
"target": "Package",
"version": "[12.0.2, )"
}
},
"imports": [ "imports": [
"net461", "net461",
"net462", "net462",
......
...@@ -38,6 +38,12 @@ ...@@ -38,6 +38,12 @@
}, },
"frameworks": { "frameworks": {
"netcoreapp3.0": { "netcoreapp3.0": {
"dependencies": {
"Newtonsoft.Json": {
"target": "Package",
"version": "[12.0.2, )"
}
},
"imports": [ "imports": [
"net461", "net461",
"net462", "net462",
......
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 sign in to comment