Commit 2c9d0a76 by adbaga

yes

parent ccf99b58
Showing with 353 additions and 109 deletions
......@@ -22,6 +22,8 @@
<e p="CellState.cs" t="Include" />
<e p="Engine.cs" t="Include" />
<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="Debug" t="Include">
<e p="netcoreapp3.0" t="Include">
......
......@@ -60,6 +60,8 @@ namespace ConsoleUserInt
return "±";
case CellState.Hidden:
return "-";
case CellState.Open:
return "O";
default:
throw new InvalidEnumArgumentException("Unknown enum option!");
}
......@@ -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
}
}
......
......@@ -6,6 +6,7 @@ namespace Game
Mine,
Flag,
Hidden,
Open,
}
......
using System;
using Game;
using GameEngine;
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;
BoardWidth = boardWidth;
BoardHeight = settings.BoardHeight;
BoardWidth = settings.BoardWidth;
// initialize the board
Board = new CellState[boardHeight, boardWidth];
Board = new CellState[BoardHeight, BoardWidth];
}
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!");
......@@ -36,6 +37,20 @@ namespace Engine
}
public void Move(int posY, int posX)
{
if (Board[posY, posX] != CellState.Empty)
{
return;
}
Board[posY, posX] = CellState.Open;
}
......@@ -49,9 +64,6 @@ namespace Engine
}
......@@ -66,15 +78,17 @@ namespace Engine
public int minePosX { get; }
private static Random random = new Random();
public static int[,] MinesSetter(int height, int width)
{
int mineCount = 0;
int numOfMines = height * width / 8;
int numOfMines = height * width / 6;
//int[,] mineLoc = new int[width, height];
var placed = 0;
int[,] mineLoc = new int[width, height];
{
......@@ -94,7 +108,7 @@ namespace Engine
} while (mineCount <= numOfMines);
}
Console.WriteLine($"There are {numOfMines} mines");
Console.WriteLine(mineLoc.Length);
/*foreach (var x in mineLoc)
{
......@@ -104,6 +118,11 @@ namespace Engine
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 @@
<ProjectReference Include="..\MenuSystem\MenuSystem.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
</Project>
......@@ -2,6 +2,8 @@
using System.Collections.Generic;
using ConsoleUserInt.ConsoleUI;
using Engine;
using Game;
using GameEngine;
using MenuSystem;
using static System.Console;
......@@ -9,11 +11,17 @@ namespace IDoMinesweeper
{
class Program
{
private static GameSettings _settings;
static void Main(string[] args)
{
Clear();
WriteLine("Welcome to Minesweeper");
_settings = GameConfigHandler.LoadConfig();
var gameMenu = new Menu(1)
......@@ -25,22 +33,22 @@ namespace IDoMinesweeper
"1", new MenuItem()
{
Title = "Level 1",
CommandToExecute = null
CommandToExecute = TestGame
}
},
{
"2", new MenuItem()
{
Title = "Level 2",
CommandToExecute = null
CommandToExecute = OldTestGame
}
},
{
"3", new MenuItem()
{
Title = "Customize Your Board size",
CommandToExecute = TestGame
CommandToExecute = SaveSettings
}
},
......@@ -60,6 +68,16 @@ namespace IDoMinesweeper
}
},
{
"J", new MenuItem()
{
Title = "Set defaults for game (save to JSON)",
CommandToExecute = SaveSettings
}
},
{
"D", new MenuItem()
......@@ -67,125 +85,203 @@ namespace IDoMinesweeper
Title = "Donate",
CommandToExecute = null
}
}
}
}
};
menu0.Run();
static string TestGame()
{
WriteLine("Board height (Min. 8): ");
WriteLine(">");
var checkHeight = ReadLine();
int height = 0;
if (!int.TryParse(checkHeight, out height))
}
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()
{
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(">");
var checkHeight = ReadLine();
int height = 0;
if (!int.TryParse(checkHeight, out height))
{
WriteLine("Not an integer");
}
WriteLine("Board width (Min. 8): ");
//WriteLine(">");
var checkWidth = ReadLine();
int width = 0;
if (!int.TryParse(checkWidth, out width))
{
Console.WriteLine("Not an integer");
}
width = Convert.ToInt32(checkWidth);
height = Convert.ToInt32(checkHeight);
var game2 = new BoardDimOld(height, width);
var doIt = false;
do
{
if (height < 8 || width < 8)
{
WriteLine("Not an integer");
Console.WriteLine("Too small. Minimal board size is 8x8");
}
else
{
height = Convert.ToInt32(checkHeight);
}
WriteLine("Board width (Min. 8): ");
WriteLine(">");
var checkWidth = ReadLine();
int width = 0;
if (!int.TryParse(checkWidth, out width))
else if (height > 30 || width > 30)
{
Console.WriteLine("Not an integer");
Console.WriteLine("Too big. Maximum board size is 30x30");
}
else
} while (!doIt);
return "GAME OVER!!";
}
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))
{
width = Convert.ToInt32(checkWidth);
Console.WriteLine($"To cancel input enter: {cancelIntValue}" +
$"{(cancelIntValue.HasValue && !string.IsNullOrWhiteSpace(cancelStrValue) ? " or " : "")}" +
$"{cancelStrValue}");
}
var doIt = false;
do
Console.Write(">");
var consoleLine = Console.ReadLine();
if (consoleLine == cancelStrValue) return (0, true);
if (int.TryParse(consoleLine, out var userInt))
{
if (height < 8 || width < 8)
{
Console.WriteLine("Too small. Minimal board size is 8x8");
return "";
}
else if(height > 30 || width > 30)
return userInt == cancelIntValue ? (userInt, true) : (userInt, false);
}
Console.WriteLine($"'{consoleLine}' cant be converted to int value!");
} while (true);
}
{
Console.WriteLine("Too big. Maximum board size is 30x30");
return "";
}
} while (doIt = false);
var game = new BoardDim(height, width);
GameUI.PrintBoard(game);
//var mine = new Mines(height, width);
Mines.MinesSetter(height,width);
static void getBoardDim()
{
var done = true;
do{
/*_settings.Height = boardHeight;
_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];
GameUI.PrintBoard(game);
}
internal class BoardDimOld
{
private static CellState[,] Board { get; set; }
} while (done = true);
public int BoardWidth { get; }
public int BoardHeight { get; }
public BoardDimOld(int boardHeight, int boardWidth)
{
if (boardHeight > 7 || boardWidth > 7)
{
BoardHeight = boardHeight;
BoardWidth = boardWidth;
// initialize the board
Board = new CellState[boardHeight, boardWidth];
return "";
}
else
{
Console.WriteLine($"{boardHeight} & {boardWidth} is too small! Minimum 8x8");
throw new ArgumentException("Board size has to be at least 8x8!");
}
}
public CellState[,] GetBoard()
{
var result = new CellState[BoardHeight, BoardWidth];
Array.Copy(Board, result, Board.Length);
return result;
}
}
}
\ No newline at end of file
}
......@@ -10,12 +10,21 @@
"dependencies": {
"ConsoleUserInt": "1.0.0",
"Game": "1.0.0",
"MenuSystem": "1.0.0"
"MenuSystem": "1.0.0",
"Newtonsoft.Json": "12.0.2"
},
"runtime": {
"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": {
"dependencies": {
"Game": "1.0.0"
......@@ -42,6 +51,13 @@
"serviceable": false,
"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": {
"type": "project",
"serviceable": false,
......
......@@ -16,3 +16,4 @@
/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/Game.pdb
/Users/kalevkim/secureprogramming/IDoMinesweeper/IDoMinesweeper/bin/Debug/netcoreapp3.0/Newtonsoft.Json.dll
{
"version": 1,
"dgSpecHash": "wiqMLxMh6d4xGEN9mwNfnv8kqAlbM6pQlGxFOEczSsdVD4elFb5W/C5+rrHGWxVE0LpFETEMD97RWTRmL7RcHA==",
"dgSpecHash": "7wChmuC8FaWda3jmvT4pidUXZYdiNHWXL7r9Uo/Vo5KLvEr7b+/Rvi1P1olj1H8wqbLyz0OqPPxHRwx7XiwJSw==",
"success": true
}
\ No newline at end of file
......@@ -146,6 +146,12 @@
},
"frameworks": {
"netcoreapp3.0": {
"dependencies": {
"Newtonsoft.Json": {
"target": "Package",
"version": "[12.0.2, )"
}
},
"imports": [
"net461",
"net462",
......
......@@ -2,6 +2,15 @@
"version": 3,
"targets": {
".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": {
"type": "project",
"framework": ".NETCoreApp,Version=v3.0",
......@@ -38,6 +47,36 @@
}
},
"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": {
"type": "project",
"path": "../ConsoleUserInt/ConsoleUserInt.csproj",
......@@ -58,7 +97,8 @@
".NETCoreApp,Version=v3.0": [
"ConsoleUserInt >= 1.0.0",
"Game >= 1.0.0",
"MenuSystem >= 1.0.0"
"MenuSystem >= 1.0.0",
"Newtonsoft.Json >= 12.0.2"
]
},
"packageFolders": {
......@@ -109,6 +149,12 @@
},
"frameworks": {
"netcoreapp3.0": {
"dependencies": {
"Newtonsoft.Json": {
"target": "Package",
"version": "[12.0.2, )"
}
},
"imports": [
"net461",
"net462",
......
......@@ -38,6 +38,12 @@
},
"frameworks": {
"netcoreapp3.0": {
"dependencies": {
"Newtonsoft.Json": {
"target": "Package",
"version": "[12.0.2, )"
}
},
"imports": [
"net461",
"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