public Move GetMove() { List <Move> possibleMoves = new List <Move>(); for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if (!board[x, y].occupied) { Move m = new Move(x, y, white); if (Referee.IsLegal(m, board)) { possibleMoves.Add(m); } } } } Move move = possibleMoves[random.Next(possibleMoves.Count)]; SendMove(move); return(move); }
public void RunMatch() { Debug.WriteLine(Prefix + "Starting match..."); Stopwatch matchTime = Stopwatch.StartNew(); Board board = new Board(); boards.Add(board); PlayerInstance playerW = white.GetInstance(this); PlayerInstance playerB = black.GetInstance(this); playerW.Start(); playerB.Start(); Debug.WriteLine(Prefix + "Instantiated players..."); playerW.Designate(true); playerB.Designate(false); for (int t = 0; t < 30; t++) // TODO: get rid of some of the duplicate code { try { Move whiteMove = playerW.GetMove(); if (!Referee.IsLegal(whiteMove, board)) { throw new IllegalMove(whiteMove); } moves.Add(whiteMove); board = board.GetBoard(whiteMove); boards.Add(board); playerB.SendMove(whiteMove); } catch (DisqualificationException e) { if (e is Crash) { Debug.WriteLine(Prefix + "White crashed and has been disqualified.", true); whiteDisqualification = new Disqualification(true, moves.Count, "White crashed."); } else if (e is Timeout) { Debug.WriteLine(Prefix + "White used too much time and has been disqualified.", true); whiteDisqualification = new Disqualification(true, moves.Count, "White exceeded the time limit."); } else if (e is InvalidMove) { Debug.WriteLine(Prefix + "White sent an invalid move and has been disqualified.", true); whiteDisqualification = new Disqualification(true, moves.Count, "White sent an invalid move: " + (e as InvalidMove).move + "."); } else if (e is IllegalMove) { Debug.WriteLine(Prefix + "White made an illegal move and has been disqualified.", true); whiteDisqualification = new Disqualification(true, moves.Count, "White attempted to make an illegal move: " + (e as IllegalMove).move + "."); } playerW.Quit(); if (playerW is ExePlayerInstance) { whiteLog = (playerW as ExePlayerInstance).PlayerLog; whiteTime = (playerW as ExePlayerInstance).Time; } playerW = new RandomPlayerInstance(board, true); Move whiteMove = playerW.GetMove(); moves.Add(whiteMove); board = board.GetBoard(whiteMove); boards.Add(board); playerB.SendMove(whiteMove); } try { Move blackMove = playerB.GetMove(); if (!Referee.IsLegal(blackMove, board)) { throw new IllegalMove(blackMove); } moves.Add(blackMove); board = board.GetBoard(blackMove); boards.Add(board); playerW.SendMove(blackMove); } catch (DisqualificationException e) { if (e is Crash) { Debug.WriteLine(Prefix + "Black crashed and has been disqualified.", true); blackDisqualification = new Disqualification(true, moves.Count, "Black crashed."); } else if (e is Timeout) { Debug.WriteLine(Prefix + "Black used too much time and has been disqualified.", true); blackDisqualification = new Disqualification(true, moves.Count, "Black exceeded the time limit."); } else if (e is InvalidMove) { Debug.WriteLine(Prefix + "Black sent an invalid move and has been disqualified.", true); blackDisqualification = new Disqualification(true, moves.Count, "Black sent an invalid move: " + (e as InvalidMove).move + "."); } else if (e is IllegalMove) { Debug.WriteLine(Prefix + "Black made an illegal move and has been disqualified.", true); blackDisqualification = new Disqualification(true, moves.Count, "Black attempted to make an illegal move: " + (e as IllegalMove).move + "."); } playerB.Quit(); if (playerB is ExePlayerInstance) { blackLog = (playerB as ExePlayerInstance).PlayerLog; blackTime = (playerB as ExePlayerInstance).Time; } playerB = new RandomPlayerInstance(board, false); Move blackMove = playerB.GetMove(); moves.Add(blackMove); board = board.GetBoard(blackMove); boards.Add(board); playerW.SendMove(blackMove); } } playerW.Quit(); playerB.Quit(); if (!whiteDisqualification.disqualified) { if (playerW is ExePlayerInstance) { whiteLog = (playerW as ExePlayerInstance).PlayerLog; whiteTime = (playerW as ExePlayerInstance).Time; } } if (!blackDisqualification.disqualified) { if (playerB is ExePlayerInstance) { blackLog = (playerB as ExePlayerInstance).PlayerLog; blackTime = (playerB as ExePlayerInstance).Time; } } matchTime.Stop(); scoreWhite = whiteDisqualification.disqualified ? 0 : board.CountColor(true) - 2; scoreBlack = blackDisqualification.disqualified ? 0 : board.CountColor(false) - 2; Debug.WriteLine(Prefix + "Match finished in " + matchTime.ElapsedMilliseconds + " ms. Score: " + scoreWhite + " - " + scoreBlack + ".", true); }