void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (currentGameState.PlayerOnTurn == 0 && !currentGameState.HasOffer && currentGameState.DiceRolled && legalPlays.Count > 0 && madeMoves.Count < legalPlays[0].Count)
{
BoundingChequer chequer = ClickTest(e.Location);
if (chequer == null)
return;
int[] dice = currentGameState.Dice;
int from = chequer.Slot;
if (dice[0] != dice[1])
{
int die = -1;
if (unusedDice.Count == 2)
{
if (e.Button == MouseButtons.Left)
die = Math.Max(unusedDice[0], unusedDice[1]);
else if (e.Button == MouseButtons.Right)
die = Math.Min(unusedDice[0], unusedDice[1]);
else
return;
}
else
die = unusedDice[0];
int to = chequer.Slot - die;
if (to < -1)
to = -1;
Move move = new Move(from, to);
if (currentGameState.Board.IsLegalMove(currentGameState.PlayerOnRoll, move, die))
{
if (to >= 0 && currentGameState.Board.PointCount(currentGameState.PlayerOnRoll, to) == -1)
move.AddHitPoint(to);
watch.Stop();
/*Stack<Move> s = new Stack<Move>();
foreach (Move m in madeMoves.Reverse())
s.Push(m.Clone());*/
List<TimedMove> h = new List<TimedMove>();
foreach (TimedMove m in moveHistory)
h.Add(new TimedMove(m));
TimedMove tmove = new TimedMove(move, (int)watch.ElapsedMilliseconds, 0);
moves.Add(new GameStateMoveAction(currentGameState.Clone(), originalGameState, watch.ElapsedMilliseconds, tmove, h));
currentGameState.Board.MakeMove(currentGameState.PlayerOnRoll, move);
madeMoves.Push(move);
moveHistory.Add(tmove);
unusedDice.Remove(die);
usedDice.Push(die);
UpdateControls();
}
else
{
if (unusedDice.Count == 2)
{
die = (dice[0] == die) ? dice[1] : dice[0];
to = chequer.Slot - die;
if (to < -1)
to = -1;
move = new Move(from, to);
if (currentGameState.Board.IsLegalMove(currentGameState.PlayerOnRoll, move, die))
{
if (to >= 0 && currentGameState.Board.PointCount(currentGameState.PlayerOnRoll, to) == -1)
move.AddHitPoint(to);
watch.Stop();
/*Stack<Move> s = new Stack<Move>();
foreach (Move m in madeMoves.Reverse())
s.Push(m.Clone());*/
//moves.Add(new GameStateMoveAction(currentGameState.Clone(), watch.ElapsedMilliseconds, move.Clone(), s));
List<TimedMove> h = new List<TimedMove>();
foreach (TimedMove m in moveHistory)
h.Add(new TimedMove(m));
TimedMove tmove = new TimedMove(move, (int)watch.ElapsedMilliseconds, 0);
moves.Add(new GameStateMoveAction(currentGameState.Clone(), originalGameState, watch.ElapsedMilliseconds, tmove, h));
currentGameState.Board.MakeMove(currentGameState.PlayerOnRoll, move);
madeMoves.Push(move);
moveHistory.Add(tmove);
unusedDice.Remove(die);
usedDice.Push(die);
UpdateControls();
}
}
//else
//Console.WriteLine("Illegal move " + from + "/" + to);
}
}
else
{
int to = chequer.Slot - dice[0];
if (to < -1)
to = -1;
Move move = new Move(from, to);
if (currentGameState.Board.IsLegalMove(currentGameState.PlayerOnRoll, move, dice[0]))
{
if (to >= 0 && currentGameState.Board.PointCount(currentGameState.PlayerOnRoll, to) == -1)
move.AddHitPoint(to);
watch.Stop();
/*Stack<Move> s = new Stack<Move>();
foreach (Move m in madeMoves.Reverse())
s.Push(m.Clone());
moves.Add(new GameStateMoveAction(currentGameState.Clone(), watch.ElapsedMilliseconds, move.Clone(), s));*/
List<TimedMove> h = new List<TimedMove>();
foreach (TimedMove m in moveHistory)
h.Add(new TimedMove(m));
TimedMove tmove = new TimedMove(move, (int)watch.ElapsedMilliseconds, 0);
moves.Add(new GameStateMoveAction(currentGameState.Clone(), originalGameState, watch.ElapsedMilliseconds, tmove, h));
currentGameState.Board.MakeMove(currentGameState.PlayerOnRoll, move);
madeMoves.Push(move);
moveHistory.Add(tmove);
unusedDice.Remove(dice[0]);
usedDice.Push(dice[0]);
UpdateControls();
}
}
// Fix problem when making an illegal move the watch resets itsefl
}
}