Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
CSharp 2020 Fall
Course Materials
Commits
fc6e2bd3
Commit
fc6e2bd3
authored
Oct 09, 2020
by
Andres Käver
Browse files
json, slides
parent
5dfd4299
Changes
5
Show whitespace changes
Inline
Side-by-side
demos/Demo01/ConsoleApp/Program.cs
View file @
fc6e2bd3
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Diagnostics.CodeAnalysis
;
using
System.Diagnostics.CodeAnalysis
;
using
System.Linq
;
using
System.Runtime.CompilerServices
;
using
System.Runtime.CompilerServices
;
using
GameBrain
;
using
GameBrain
;
using
GameConsoleUI
;
using
GameConsoleUI
;
...
@@ -13,18 +14,9 @@ namespace ConsoleApp
...
@@ -13,18 +14,9 @@ namespace ConsoleApp
static
void
Main
(
string
[]
args
)
static
void
Main
(
string
[]
args
)
{
{
Console
.
WriteLine
(
"=========> TIC-TAC-TOE AKAVER <================"
);
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
);
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 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
();
menu
.
RunMenu
();
}
}
...
@@ -55,6 +47,17 @@ namespace ConsoleApp
...
@@ -55,6 +47,17 @@ namespace ConsoleApp
return
""
;
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
(
menu
.
AddMenuItem
(
new
MenuItem
(
$"Exit game"
,
$"Exit game"
,
userChoice
:
"e"
,
userChoice
:
"e"
,
...
@@ -78,33 +81,45 @@ namespace ConsoleApp
...
@@ -78,33 +81,45 @@ namespace ConsoleApp
return
(
x
,
y
);
return
(
x
,
y
);
}
}
}
public
class
Person
static
string
LoadGameAction
(
TicTacToe
game
)
{
{
[
AllowNull
]
var
files
=
System
.
IO
.
Directory
.
EnumerateFiles
(
"."
,
"*.json"
).
ToList
();
public
string
ScreenName
for
(
int
i
=
0
;
i
<
files
.
Count
;
i
++)
{
{
get
=>
_screenName
;
Console
.
WriteLine
(
$"
{
i
}
-
{
files
[
i
]}
"
);
set
=>
_screenName
=
value
??
GenerateRandomScreenName
();
}
}
private
string
_screenName
=
GenerateRandomScreenName
();
[
DisallowNull
]
var
fileNo
=
Console
.
ReadLine
();
public
string
?
ReviewComment
var
fileName
=
files
[
int
.
Parse
(
fileNo
!.
Trim
())];
{
get
=>
_comment
;
var
jsonString
=
System
.
IO
.
File
.
ReadAllText
(
fileName
);
set
=>
_comment
=
value
??
throw
new
ArgumentNullException
(
nameof
(
value
),
"Cannot set to null"
);
game
.
SetGameStateFromJsonString
(
jsonString
);
TicTacToeConsoleUI
.
DrawBoard
(
game
.
GetBoard
());
return
""
;
}
}
string
?
_comment
;
[
return
:
MaybeNull
]
static
string
SaveGameAction
(
TicTacToe
game
)
public
T
Find
<
T
>(
IEnumerable
<
T
>
sequence
,
Func
<
T
,
bool
>
predicate
)
{
// 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
;
}
}
public
void
EnsureCapacity
<
T
>([
NotNull
]
ref
T
[]?
storage
,
int
size
)
{
var
serializedGame
=
game
.
GetSerializedGameState
();
// Console.WriteLine(serializedGame);
System
.
IO
.
File
.
WriteAllText
(
fileName
,
serializedGame
);
return
""
;
}
}
}
}
...
...
demos/Demo01/GameBrain/CellState.cs
View file @
fc6e2bd3
...
@@ -2,8 +2,8 @@ namespace GameBrain
...
@@ -2,8 +2,8 @@ namespace GameBrain
{
{
public
enum
CellState
public
enum
CellState
{
{
Empty
,
Empty
,
// = 0
X
,
X
,
// = 1
O
,
O
,
// = 2
}
}
}
}
\ No newline at end of file
demos/Demo01/GameBrain/GameBrain.csproj
View file @
fc6e2bd3
...
@@ -4,4 +4,8 @@
...
@@ -4,4 +4,8 @@
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="4.7.2" />
</ItemGroup>
</Project>
</Project>
demos/Demo01/GameBrain/GameState.cs
0 → 100644
View file @
fc6e2bd3
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
demos/Demo01/GameBrain/TicTacToe.cs
View file @
fc6e2bd3
using
System
;
using
System
;
using
System.Text.Json
;
namespace
GameBrain
namespace
GameBrain
{
{
// _, X, O, /X, /O
// _, X, O, /X, /O
public
class
TicTacToe
public
class
TicTacToe
{
{
private
readonly
CellState
[,]
_board
=
new
CellState
[
3
,
3
];
private
CellState
[,]
_board
=
new
CellState
[
3
,
3
];
private
bool
_nextMoveByX
=
true
;
private
bool
_nextMoveByX
=
true
;
public
CellState
[,]
GetBoard
()
public
CellState
[,]
GetBoard
()
...
@@ -29,7 +30,59 @@ namespace GameBrain
...
@@ -29,7 +30,59 @@ namespace GameBrain
return
false
;
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment