-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cs
95 lines (81 loc) · 2.06 KB
/
game.cs
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
using System;
using System.Collections.Generic;
public class Game
{
public int Round { get; private set; }
public static bool GameInProgress { get; private set; }
public State PromptingTeam { get; private set; }
public Board Board { get; private set; }
private Dictionary<State, AI> teams = new Dictionary<State, AI>();
private int _auth;
private bool _load;
private string _loadString;
private int _loadCounter;
public Game(Board board, Dictionary<State, AI> teams, int auth, bool load = false, string loadString = null)
{
Board = board;
this.teams = teams;
_auth = auth;
_load = load;
_loadString = loadString;
}
public void RunGame()
{
if (GameInProgress) throw new Exception("A game is already in progress.");
GameInProgress = true;
Round = 0;
State startingTeam = Board.CurrentTeam;
while (Board.GameInProgress)
{
Board.Draw(Round);
PromptingTeam = Board.CurrentTeam;
if (PromptingTeam == startingTeam) Round++;
int input = -1;
if (_load && _loadCounter < _loadString.Length)
{
Console.WriteLine("Replaying... (press enter)");
try
{
if (ConnectLibrary.TryDec(_loadString[_loadCounter].ToString(), out input)) Board.InputMove(input, _auth, Round);
else throw new Exception();
}
catch
{
Console.WriteLine("Invalid Load String!");
_loadCounter = _loadString.Length;
}
Console.ReadLine();
_loadCounter++;
}
else do
{
try
{
input = teams[Board.CurrentTeam].Prompt(Board, Round);
}
catch (Exception e)
{
Console.WriteLine($"{PromptingTeam} errored and has ended the game.");
Console.WriteLine(e);
Console.ReadLine();
Board.Forfeit();
}
}
while (!Board.InputMove(Convert.ToInt32(input), _auth, Round));
}
Board.DisableCreation(_auth);
foreach (AI ai in teams.Values)
{
try
{
ai.MatchEnd(Board.Victor, Round);
}
catch
{
Console.WriteLine($"{ai.Team} team, with AI {ai.Name} errored in it's MatchEnd() method.");
}
}
Board.EnableCreation(_auth);
GameInProgress = false;
}
}