-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (103 loc) · 3.52 KB
/
Program.cs
File metadata and controls
117 lines (103 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
namespace Advance;
public class Program
{
private static void Main(string[] args)
{
Game game = new Game();
if (ParseArguments(args, out string infile,
out string outfile, out Colour colour
))
{
Load(game, infile);
PlayOneTurn(game, colour);
Save(game, outfile);
}
else
{
Console.WriteLine("Error processing command line arguments. Expected: ");
Console.WriteLine("args[0] == colour (white or black)");
Console.WriteLine("args[1] == input file path");
Console.WriteLine("args[2] == output file path");
Console.WriteLine("Your assignment may have different requirements.");
}
}
/// <summary>
/// Parses the command-line arguments to extract the input file, output file, and color.
/// </summary>
/// <param name="args">Command-line arguments.</param>
/// <param name="infile">Output variable to store the input file path.</param>
/// <param name="outfile">Output variable to store the output file path.</param>
/// <param name="colour">Output variable to store the color (white or black).</param>
/// <returns>True if the arguments were successfully parsed; otherwise, false.</returns>
private static bool ParseArguments(
string[] args,
out string infile,
out string outfile,
out Colour colour
)
{
infile = string.Empty;
outfile = string.Empty;
colour = Colour.White;
if (args.Length >= 3)
{
infile = args[1];
outfile = args[2];
return Enum.TryParse<Colour>(args[0], ignoreCase: true, out colour);
}
else
{
return false;
}
}
/// <summary>
/// Loads the game from the specified input file.
/// </summary>
/// <param name="game">The game object.</param>
/// <param name="infile">The input file path.</param>
private static void Load(Game game, string infile)
{
try
{
using StreamReader reader = new StreamReader(infile);
game.Read(reader);
}
catch (Exception ex)
{
Console.WriteLine("Error loading game from '{0}': {1}", infile, ex.Message);
}
}
/// <summary>
/// Plays one turn of the game for the specified color.
/// </summary>
/// <param name="game">The game object.</param>
/// <param name="colour">The color to play the turn (white or black).</param>
private static void PlayOneTurn(Game game, Colour colour)
{
Player currentPlayer = colour == Colour.White ? game.White : game.Black;
AI gameBot = new AI(game, currentPlayer);
Action nextMove = gameBot.ChooseBestAction();
if (nextMove != null)
{
nextMove.DoAction();
}
}
/// <summary>
/// Saves the game to the specified output file.
/// </summary>
/// <param name="game">The game object.</param>
/// <param name="outfile">The output file path.</param>
private static void Save(Game game, string outfile)
{
try
{
using StreamWriter writer = new StreamWriter(outfile);
game.Write(writer);
}
catch (Exception ex)
{
Console.WriteLine("Error saving game to '{0}': {1}", outfile, ex.Message);
}
}
}