}//PlayTicTacToe() /// <summary> /// Turn functionality. /// If user turn, prompts for row, then column, then attempts to insert at chosen location. /// If AI turn, generates 2 random integers from 0 to 3 for row and column, then attempts to insert at that location. /// </summary> /// <param name="handler">Socket connection is made through.</param> /// <param name="board">Existing TicTacToeBoard</param> /// <param name="player">X or O to place</param> /// <param name="rng">Randum number generator. If null, it is user turn. If it exists, AI turn.</param> private static void Turn(Socket handler, TicTacToeBoard board, string player, Random rng = null) { int row, col, BytesReceived; byte[] IncomingBuffer = new byte[1024]; bool valid = false; do //attempt to place token, repeat until valid location chosen { if (rng == null) //if rng is null, we're processing player actions { //Get row SendMessage(handler, $"<T>Please enter the row to place an {player}: "); BytesReceived = handler.Receive(IncomingBuffer); var x = Encoding.ASCII.GetString(IncomingBuffer, 0, BytesReceived); row = Int32.Parse(x); //Get column SendMessage(handler, $"<T>Please enter the column to place an {player}: "); BytesReceived = handler.Receive(IncomingBuffer); col = Int32.Parse(Encoding.ASCII.GetString(IncomingBuffer, 0, BytesReceived)); if (!board.CheckPosition(row, col)) { HandleInvalid(handler, row, col, player, true); } }//if else //if rng is not null, it's ai move { row = rng.Next(0, 3); col = rng.Next(0, 3); //Log move to server, whether valid or invalid. if (!board.CheckPosition(row, col)) { HandleInvalid(handler, row, col, player, false); } else { SendMessage(handler, $"<M>Computer played at ({row},{col})"); } }//else valid = board.Insert(row, col, player[0]); } while (!valid); Console.WriteLine($"GAME EVENT : {player} at ({row},{col})"); }//Turn(Socket, TicTacToeBoard, string, Random)