private static void PlayACard(Player player) { if (_isAi) { if (_idxCardAi >= player.GetDeck().GetDeck().Count) { _idxCardAi = 0; } _idCardToPlay = player.GetDeck().GetDeck()[_idxCardAi].GetId(); player.SendMessage("PLAY " + _idCardToPlay); return; } Console.Write("Enter the ID of the card you want to play:\n"); var input = Console.ReadLine(); while (!int.TryParse(input, out _idCardToPlay)) { Console.Write("Invalid card ID!\n"); Console.Write("Enter the ID of the card you want to play:\n"); input = Console.ReadLine(); } player.SendMessage("PLAY " + input); }
public static int Main(string[] args) { try { var client = new TcpClient(); var ip = GetIp(); var port = GetPort(); client.Connect(ip, port); Console.Write("Connection successful! Waiting for more players...\n"); var received = ""; ModelDeck.Generate32CardDeck(); _player = new Player(client); if (args.Length == 1 && args[0].ToUpper().Equals("AI")) { _isAi = true; } while (!received.Equals("END")) { var buffer = ""; while (buffer.IndexOf('\n') < 0) { var stream = client.GetStream(); var buff = new byte[1]; var readStr = stream.Read(buff, 0, 1); if (readStr == 0) { Console.Write("Cannot reach server!\n"); return(84); } for (var i = 0; i < readStr; i++) { buffer += Convert.ToChar(buff[i]); } } received = buffer.Substring(0, buffer.IndexOf('\n')); if (received.StartsWith("MSG ")) { Console.Write(received.Substring(4, received.Length - 4) + "\n"); } else if (received.StartsWith("DECK ")) { _player.EmptyDeck(); var cardsId = received.Substring(5, received.Length - 5).Split(); for (var idx = 0; idx < cardsId.Length; idx += 1) { _player.GetDeck().AddCard(ModelDeck.GetCardById(int.Parse(cardsId[idx]))); } Console.Write("Your cards:"); _player.GetDeck().PrintDeck(); } else if (received.StartsWith("BID")) { CheckBidding(received); } else if (received.StartsWith("PLAY")) { CheckPlay(received); } } Console.Write("The game is over. See you soon! :)\n"); client.Close(); } catch (SocketException) { Console.Write("Cannot reach server!\n"); return(84); } catch (Exception) { Console.Write("An error occurred!\n"); return(84); } return(0); }