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
Hide whitespace changes
Inline
Side-by-side
demos/Demo01/ConsoleApp/Program.cs
View file @
fc6e2bd3
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
siz
e
)
static
string
SaveGameAction
(
TicTacToe
gam
e
)
{
// 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
demos/Demo01/GameBrain/CellState.cs
View file @
fc6e2bd3
...
...
@@ -2,8 +2,8 @@ namespace GameBrain
{
public
enum
CellState
{
Empty
,
X
,
O
,
Empty
,
// = 0
X
,
// = 1
O
,
// = 2
}
}
\ No newline at end of file
demos/Demo01/GameBrain/GameBrain.csproj
View file @
fc6e2bd3
...
...
@@ -4,4 +4,8 @@
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="4.7.2" />
</ItemGroup>
</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.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
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