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

json, slides

parent 5dfd4299
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using GameBrain;
using GameConsoleUI;
......@@ -13,18 +14,9 @@ namespace ConsoleApp
static void Main(string[] args)
{
Console.WriteLine("=========> TIC-TAC-TOE AKAVER <================");
var menuB = new Menu(MenuLevel.Level2Plus);
menuB.AddMenuItem(new MenuItem("Sub 2.", "1", DefaultMenuAction));
var menuA = new Menu(MenuLevel.Level1);
menuA.AddMenuItem(new MenuItem("Go to submenu 2", "1", menuB.RunMenu));
menuA.AddMenuItem(new MenuItem("testing", "2", DefaultMenuAction));
var menu = new Menu(MenuLevel.Level0);
menu.AddMenuItem(new MenuItem("Go to submenu 1", "s", menuA.RunMenu));
menu.AddMenuItem(new MenuItem("New game human vs human. Pointless.", "1", TicTacToe));
menu.AddMenuItem(new MenuItem("New game puny human vs mighty AI", "2", DefaultMenuAction));
menu.AddMenuItem(new MenuItem("New game mighty AI vs superior AI", "3", DefaultMenuAction));
menu.RunMenu();
}
......@@ -55,6 +47,17 @@ namespace ConsoleApp
return "";
})
);
menu.AddMenuItem(new MenuItem(
$"Save game",
userChoice: "s",
() => { return SaveGameAction(game); })
);
menu.AddMenuItem(new MenuItem(
$"Load game",
userChoice: "l",
() => { return LoadGameAction(game); })
);
menu.AddMenuItem(new MenuItem(
$"Exit game",
userChoice: "e",
......@@ -78,34 +81,46 @@ namespace ConsoleApp
return (x, y);
}
}
public class Person
{
[AllowNull]
public string ScreenName
{
get => _screenName;
set => _screenName = value ?? GenerateRandomScreenName();
}
private string _screenName = GenerateRandomScreenName();
[DisallowNull]
public string? ReviewComment
static string LoadGameAction(TicTacToe game)
{
get => _comment;
set => _comment = value ?? throw new ArgumentNullException(nameof(value), "Cannot set to null");
}
string? _comment;
var files = System.IO.Directory.EnumerateFiles(".", "*.json").ToList();
for (int i = 0; i < files.Count; i++)
{
Console.WriteLine($"{i} - {files[i]}");
}
[return: MaybeNull]
public T Find<T>(IEnumerable<T> sequence, Func<T, bool> predicate)
{
var fileNo = Console.ReadLine();
var fileName = files[int.Parse(fileNo!.Trim())];
var jsonString = System.IO.File.ReadAllText(fileName);
game.SetGameStateFromJsonString(jsonString);
TicTacToeConsoleUI.DrawBoard(game.GetBoard());
return "";
}
public void EnsureCapacity<T>([NotNull] ref T[]? storage, int size)
static string SaveGameAction(TicTacToe game)
{
// 2020-10-12
var defaultName = "save_" + DateTime.Now.ToString("yyyy-MM-dd") + ".json";
Console.Write($"File name ({defaultName}):");
var fileName = Console.ReadLine();
if (string.IsNullOrWhiteSpace(fileName))
{
fileName = defaultName;
}
var serializedGame = game.GetSerializedGameState();
// Console.WriteLine(serializedGame);
System.IO.File.WriteAllText(fileName, serializedGame);
return "";
}
}
}
\ No newline at end of file
......@@ -2,8 +2,8 @@ namespace GameBrain
{
public enum CellState
{
Empty,
X,
O,
Empty, // = 0
X, // = 1
O, // = 2
}
}
\ No newline at end of file
......@@ -4,4 +4,8 @@
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="4.7.2" />
</ItemGroup>
</Project>
using System;
namespace GameBrain
{
public class GameState
{
public bool NextMoveByX { get; set; }
public CellState[][] Board { get; set; } = null!;
public int Width { get; set; }
public int Height { get; set; }
}
}
\ No newline at end of file
using System;
using System.Text.Json;
namespace GameBrain
{
// _, X, O, /X, /O
public class TicTacToe
{
private readonly CellState[,] _board = new CellState[3,3];
private CellState[,] _board = new CellState[3,3];
private bool _nextMoveByX = true;
public CellState[,] GetBoard()
......@@ -28,8 +29,60 @@ namespace GameBrain
return false;
}
public string GetSerializedGameState()
{
var state = new GameState
{
NextMoveByX = _nextMoveByX,
Width = _board.GetLength(0),
Height = _board.GetLength(1)
};
state.Board = new CellState[state.Width ][];
for (var i = 0; i < state.Board.Length; i++)
{
state.Board[i] = new CellState[state.Height];
}
for (var x = 0; x < state.Width; x++)
{
for (var y = 0; y < state.Height; y++)
{
state.Board[x][y] = _board[x, y];
}
}
var jsonOptions = new JsonSerializerOptions()
{
WriteIndented = true
};
return JsonSerializer.Serialize(state, jsonOptions);
}
public void SetGameStateFromJsonString(string jsonString)
{
var state = JsonSerializer.Deserialize<GameState>(jsonString);
// restore actual state from deserialized state
_nextMoveByX = state.NextMoveByX;
_board = new CellState[state.Width, state.Height];
for (var x = 0; x < state.Width; x++)
{
for (var y = 0; y < state.Height; y++)
{
_board[x, y] = state.Board[x][y];
}
}
}
}
}
\ No newline at end of file
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