public void ReceiveAnswer() { // We know that the server will send a JSON string // so we prepare the statement for it int answer = GameStore.Instance.Game.PlayerList.Find(p => p.Turn).BinaryReader.Read(); while (answer == 0) { answer = GameStore.Instance.Game.PlayerList.Find(p => p.Turn).BinaryReader.Read(); Thread.Sleep(100); } string hint = string.Empty; NetworkInstruction targetNetworkInstruction = NetworkInstruction.Wait; if (answer == GameStore.Instance.Game.TargetNumber) { hint = "O jogador acertou no número correto!"; GameStore.Instance.Game.GameState = GameState.GameEnded; targetNetworkInstruction = NetworkInstruction.GameEnded; } else if (answer < GameStore.Instance.Game.TargetNumber) { hint = "O número é superior ao valor introduzido"; } else if (answer > GameStore.Instance.Game.TargetNumber) { hint = "O número é inferior ao valor introduzido"; } NetworkMessage networkMessageToSend = new NetworkMessage() { Message = "O jogador " + GameStore.Instance.Game.PlayerList.Find(p => p.Turn).PlayerName + " tentou " + answer + "\n" + hint, NetworkInstruction = targetNetworkInstruction }; string networkMessageToSenndJsonString = JsonConvert.SerializeObject(networkMessageToSend); foreach (Player player in GameStore.Instance.Game.PlayerList) { player.BinaryWriter.Write(networkMessageToSenndJsonString); } //TODO: Log player moves }
public void ReceiveAnswer() { // We know that the server will send a JSON string // so we prepare the statement for it int currentPlayer = GameStore.Instance.Game.PlayerList.FindIndex(p => p.Turn) + 1; //1 if player 1, 2 if player 2 int k; string answer; do { Console.WriteLine("ANTES"); answer = GameStore.Instance.Game.PlayerList.Find(p => p.Turn).BinaryReader.ReadString(); Console.WriteLine("DEPOIS"); Console.WriteLine(answer); } while (!Int32.TryParse(answer, out k) || k < 1 || k > 9 || Positions[(k - 1) / 3, (k - 1) % 3] == 'O' || Positions[(k - 1) / 3, (k - 1) % 3] == 'X'); Console.WriteLine("SAI DO CICLOOOO!!"); //while (Int32.TryParse(answer, out k)) //{ // while (k < 1 || k > 9 || Positions[(k - 1) / 3, (k - 1) % 3] == 'O' || Positions[(k - 1) / 3, (k - 1) % 3] == 'X') // { // answer = GameStore.Instance.Game.PlayerList.Find(p => p.Turn).BinaryReader.ReadString(); // Thread.Sleep(100); // Int32.TryParse(answer, out k); // } // break; //} //Console.WriteLine((answer-1) / 3); //Console.WriteLine((answer-1) % 3); //string hint = string.Empty; NetworkInstruction targetNetworkInstruction = NetworkInstruction.Wait; //if (answer == Positions[(answer-1)/3, (answer-1)%3]) //{ Console.WriteLine((k - 1) / 3); Console.WriteLine((k - 1) % 3); if (currentPlayer == 1) { Positions[(k - 1) / 3, (k - 1) % 3] = 'X'; } else { Positions[(k - 1) / 3, (k - 1) % 3] = 'O'; } //} //for (int i = 0; i < 3; i++) //{ // for (int j = 0; j < 3; j++) // { // if (answer == Positions[i, j]) // { // if (player1 == null) // { // player1 = GameStore.Instance.Game.PlayerList.Find(p => p.Turn); // Positions[i, j] = 'X'; // } // else if (player2 == null) // { // player2 = GameStore.Instance.Game.PlayerList.Find(p => p.Turn); // Positions[i, j] = 'O'; // } // else if (player1.Id == GameStore.Instance.Game.PlayerList.Find(p => p.Turn).Id) // { // Positions[i, j] = 'X'; // } // else if (player2.Id == GameStore.Instance.Game.PlayerList.Find(p => p.Turn).Id) // { // Positions[i, j] = 'O'; // } // } // else // { // if (i == 2 && j == 2) // { // //AskPlayerToPlay(); // answer = GameStore.Instance.Game.PlayerList.Find(p => p.Turn).BinaryReader.Read(); // Thread.Sleep(100); // } // } // } //} //if (answer == GameStore.Instance.Game.TargetNumber) //{ // hint = "O jogador acertou no número correto!"; // GameStore.Instance.Game.GameState = GameState.GameEnded; // targetNetworkInstruction = NetworkInstruction.GameEnded; //} //else if (answer < GameStore.Instance.Game.TargetNumber) //{ // hint = "ERRADO! O número é superior ao valor introduzido!"; //} //else if (answer > GameStore.Instance.Game.TargetNumber) //{ // hint = "ERRADO! O número é inferior ao valor introduzido!"; //} //string message = ""; //for (int i = 0; i < 3; i++) //{ // for (int j = 0; j < 3; j++) // { // message += Positions[i, j]; // message += " "; // if (j == 2) // { // message += "\n"; // } // } //} //NetworkMessage networkMessageToSend = new NetworkMessage() //{ // Message = message, // NetworkInstruction = NetworkInstruction.Wait //}; NetworkMessage networkMessageToSend = new NetworkMessage() { Message = "O jogador " + GameStore.Instance.Game.PlayerList.Find(p => p.Turn).PlayerName + " tentou " + answer + "\n", NetworkInstruction = targetNetworkInstruction }; string networkMessageToSenndJsonString = JsonConvert.SerializeObject(networkMessageToSend); foreach (Player player in GameStore.Instance.Game.PlayerList) { player.BinaryWriter.Write(networkMessageToSenndJsonString); } //TODO: Log player moves }
public void StartClient() { // Start udp client TcpClient tcpClient = new TcpClient(); tcpClient.Connect(IPAddress.Parse("127.0.0.1"), 7777); BinaryWriter binaryWriter = new BinaryWriter(tcpClient.GetStream()); BinaryReader binaryReader = new BinaryReader(tcpClient.GetStream()); string receivedNetworkMessageJsonString; NetworkMessage receivedNetworkMessage; // This while::cycle works like a game cycle. // A boolean should take it's place so that the client // can exit the cycle when it wants. // It is here to make sure that the client can make // several communications with the server. while (true) { switch (_playerState) { case PlayerState.ReceivePlayerInformation: Console.WriteLine("Input player name:"); // Here we initiate a new Player class with // information from the player input (player name) Player player = new Player { PlayerName = Console.ReadLine() }; // Check player name for null if (!string.IsNullOrEmpty(player.PlayerName)) { // Create a new instance of a class // called networkMessage and set the current // player in that model so that we can send // this object to the server NetworkMessage networkMessage = new NetworkMessage() { Player = player }; // Serialize the NetworkMessage object to a JSON string string networkMessageJsonStrong = JsonConvert.SerializeObject(networkMessage); binaryWriter.Write(networkMessageJsonStrong); // We know that the server will send a JSON string // so we prepare the statement for it receivedNetworkMessageJsonString = binaryReader.ReadString(); // Unserialize the JSON string to the object NetworkMessage receivedNetworkMessage = JsonConvert.DeserializeObject <NetworkMessage>(receivedNetworkMessageJsonString); // Temporary and simple validation indicating // that we received a positive connected state // from the server if (receivedNetworkMessage.Connected) { Console.WriteLine(receivedNetworkMessage.Message); _playerState = PlayerState.GameStarted; } else { Console.WriteLine("Not Connected"); } } break; case PlayerState.GameStarted: switch (_networkInstruction) { case NetworkInstruction.Wait: // We know that the server will send a JSON string // so we prepare the statement for it receivedNetworkMessageJsonString = binaryReader.ReadString(); // Unserialize the JSON string to the object NetworkMessage receivedNetworkMessage = JsonConvert.DeserializeObject <NetworkMessage>(receivedNetworkMessageJsonString); _networkInstruction = receivedNetworkMessage.NetworkInstruction; Console.WriteLine(receivedNetworkMessage.Message); break; case NetworkInstruction.MakeMove: string answer = Console.ReadLine(); binaryWriter.Write(answer); break; case NetworkInstruction.GameEnded: _playerState = PlayerState.GameEnded; break; } break; case PlayerState.GameEnded: Thread.Sleep(100); break; } } }
public ClientController() { _playerState = PlayerState.ReceivePlayerInformation; _networkInstruction = NetworkInstruction.Wait; }