public void Play() { Task.Run(() => { m_controller.Compute(); m_status = m_controller.GetStatus(); }); }
public void Run() { Debug.WriteLine("Thread starts"); try { do { m_status = ReadAndWait(); } while (m_status == IController.Status.OK || m_status == IController.Status.CLOSEDMILL); } catch (IOException ex) { m_error = ex; } Debug.WriteLine("Thread terminates"); }
public void Finish() { Debug.WriteLine("FINISH COMPUTER PLAYER"); m_status = IController.Status.FINISHED; m_signal = true; }
private IController.Status ReadAndWait() { if (m_client.Socket == null) { throw new Exception(); } var buffer = new byte[1024]; int iRx = m_client.Socket.Receive(buffer); char[] chars = new char[iRx]; var d = Encoding.ASCII.GetDecoder(); d.GetChars(buffer, 0, iRx, chars, 0); var s = new string(chars); String[] token = s.Split(' '); if (token[0].Equals("PLAY")) { // I have to play: compute move var a = m_client.Controller.Compute(); var status = m_client.Controller.GetStatus(); if (status == IController.Status.FINISHED) { m_client.StopGame(m_client.Controller.GetWinner()); } else if (status == IController.Status.OK && a != null) { // send move to server a.Writeln(new NetworkStream(m_client.Socket, true)); } return(status); } else if (token[0].Equals("WHITE")) { // I will play white stones and open the game m_client.Controller.SetStarter(true); return(IController.Status.OK); } else if (token[0].Equals("BLACK")) { // I will play black stones m_client.Controller.SetStarter(false); return(IController.Status.OK); } else if (token[0].Equals("STOP")) { // close connection m_client.Controller.CloseConnection(); return(IController.Status.FINISHED); } else if (token[0].Equals("FINISH")) { m_client.StopGame(IController.NONE); return(IController.Status.FINISHED); } else { // opponent plays action a var a = Core.Actions.Action.Readln(token); if (a != null) { IController.Status status = m_client.Controller.Play(a); if (status == IController.Status.FINISHED) { m_client.StopGame(m_client.Controller.GetWinner()); } return(status); } else { return(IController.Status.INVALIDACTION); } } }