public static void Main() { //Menu options Console.WriteLine("1. Join Game\n2. Host Game"); string pick; pick = Console.ReadLine(); Console.Clear(); IPAddress ip; Console.Clear(); //Console.ReadLine(); switch (pick) { case "1": //CLIENT Client connection = new Client(); while (true) { Console.WriteLine("Enter IP of game host: "); string ipstring = Console.ReadLine(); Console.Clear(); if (IPAddress.TryParse(ipstring, out ip)) { break; } } try { Console.WriteLine("Attempting Connection..."); connection.connect(ip); } catch (SocketException) { Console.WriteLine("Connection timed out. Server is unavailable.\nPress ANY key to continue"); Console.ReadKey(); Console.Clear(); goto case "1"; } Console.Clear(); Console.WriteLine("Connection successful! Waiting for host to start."); connection.receiveString(5); Console.Clear(); //////////////////// /// GAME RUNNING /// //////////////////// Int32 playerCount = connection.receiveInt(); Console.WriteLine(playerCount.ToString() + " players"); Int32 bet = 0; Int32 currentBet = 0; Int32 money = connection.receiveInt(); Console.WriteLine("Your cash: " + money.ToString()); Card[] hand = new Card[2]; Card[] board = new Card[5]; Int32 allCash = 0; Int32 cardsDisplayed; Int32[,] playerList = new Int32[playerCount, 5]; string tempString; string[] displayedPlayers; while (true) { tempString = connection.receiveString(4); if (tempString == "TURN") { //Options need to be implemented in the display //1 = check, 2= call, 3= raise, 4= fold Console.WriteLine("Input number: 1 = Check, 2= Call, 3= Raise, 4 = Fold"); picking: string answer = Console.ReadLine(); if (answer.Length == 1) { try { Convert.ToInt32(answer); } catch (FormatException) { Console.WriteLine("Invalid input"); goto picking; } } else { Console.WriteLine("Invalid input"); goto picking; } switch (answer) { case "1": if (bet == currentBet) { connection.sendString("CHCK"); } else { goto picking; } break; case "2": bet = currentBet; connection.sendString("CALL"); break; case "3": if (money < currentBet - bet + 1) { Console.WriteLine("You don't have enough money left for that, pick your option again."); goto picking; } Console.WriteLine("Amount (minimum " + currentBet.ToString() + "): "); Int32 raise = Convert.ToInt32(Console.ReadLine()); if (raise + bet > currentBet) { connection.sendString("RAIS"); connection.sendInt(raise); } else { Console.WriteLine("Not high enough, pick your option again."); goto picking; } break; case "4": connection.sendString("FOLD"); break; } } else if (tempString == "SMLL") { bet = 100; } else if (tempString == "BIGB") { bet = 200; } else if (tempString == "KILL") { break; } else if (tempString == "PING") { //Console.Clear(); //Player count playerCount = connection.receiveInt(); displayedPlayers = new string[playerCount]; //Client money money = connection.receiveInt(); Console.WriteLine("Money: " + money.ToString()); //Betting values for players for (Int32 i = 0; i < playerCount; i++) { playerList[i, 0] = connection.receiveInt(); } //Sockets for players for (Int32 i = 0; i < playerCount; i++) { playerList[i, 2] = connection.receiveInt(); } //Current bet currentBet = connection.receiveInt(); Console.WriteLine("Pool: " + currentBet); //Pool amount allCash = connection.receiveInt(); Print("Pool: " + allCash.ToString()); //Cash for everyone for (Int32 i = 0; i < playerCount; i++) { playerList[i, 3] = connection.receiveInt(); } //Status flags for (Int32 i = 0; i < playerCount; i++) { playerList[i, 1] = connection.receiveInt(); } //Cards of board to display cardsDisplayed = connection.receiveInt(); Print("Cardsdisplayed: " + cardsDisplayed.ToString()); PrintHand(hand); if (cardsDisplayed > 2) { printBoard(board, cardsDisplayed); } Print("Printed board"); for (Int32 i = 0; i < playerCount; i++) { displayedPlayers[i] = "Player " + (playerList[i, 2] + 1).ToString() + ": Money - " + playerList[i, 3].ToString(); if (playerList[i, 1] != 4) { displayedPlayers[i] += ", Folded"; } else { displayedPlayers[i] += ", Bet - " + playerList[i, 0].ToString(); } Print("i did donnered"); } } else if (tempString == "HAND") //Recieves hand { for (int i = 0; i < 2; i++) { hand[i] = connection.receiveCard(); } PrintHand(hand); } else if (tempString == "BORD") //recieves board { for (Int32 i = 0; i < 5; i++) { board[i] = connection.receiveCard(); } printBoard(board, 5); } else if (tempString == "WINR") { Console.Clear(); Console.WriteLine("Player " + connection.receiveInt().ToString() + " has won, and takes " + connection.receiveInt().ToString()); } } break; case "2": //HOST Int32 playerAmount; Int32 initialMoney; Int32 dead = 0; Int32 round = 1; Int32[] bets; Int32 pool = 0; Deck mainDeck = new Deck(); mainDeck.Shuffle(); //Set IP address to host on while (true) { Console.WriteLine("Your local IP: " + Game.getLocalIP().ToString()); Console.WriteLine("Enter IP to host on, 'local' for local ip: "); string ipstring = Console.ReadLine(); Console.Clear(); if (ipstring == "local") { if (IPAddress.TryParse(Game.getLocalIP().ToString(), out ip)) { break; } } else if (IPAddress.TryParse(ipstring, out ip)) { break; } } //Set player amount while (true) { Console.WriteLine("Enter amount of players (max 16): "); string setplayers = Console.ReadLine(); Console.Clear(); try { playerAmount = Convert.ToInt32(setplayers); if (playerAmount > 16 || playerAmount < 2) { continue; } break; } catch (FormatException) { continue; } } //Set amount of money you start with while (true) { Console.WriteLine("Enter initial amount of money: "); string setmoney = Console.ReadLine(); Console.Clear(); try { initialMoney = Convert.ToInt32(setmoney); if (initialMoney > 10000000 || initialMoney < 1000) { continue; } break; } catch (OverflowException) { initialMoney = 10000000; } catch (FormatException) { continue; } } //Creates a tracking array for the players, noting the amount of money the have as well as being used to keep general statistics for running the game. //[player, 0] for money, [player, 1] for status flags, [player, 2] for associated socket to tie it into the network system Int32[,] players = new Int32[playerAmount + 1, 3]; for (Int32 i = 0; i < playerAmount; i++) { players[i, 0] = initialMoney; //Flag for status checks, big blind, small blind and bankruptcy //0 = nothing, 1 = small blind, 2 = big blind, 3 = shit creek without a paddle, 4 = in-round players[i, 1] = 0; } //Initial big/small blind players[0, 1] = 1; players[1, 1] = 2; //Ghost player to make handling of players easier later on, sits outside the visible range of for loops, because they run towards the playerAmount players[playerAmount, 0] = 0; players[playerAmount, 1] = 3; //Accepts players, by looping until everyone is in. //Will look smooth when running, .acceptPlayer will wait for ~30 seconds before letting the program continue, unless someone tries to connect Server server = new Server(ip); server.listen(); while (true) { Console.Clear(); if (playerAmount == server.players) { break; } Console.WriteLine("Your local IP: " + Game.getLocalIP().ToString()); Console.WriteLine("Waiting for " + (playerAmount - server.players).ToString() + " more players.\nCurrent players:"); server.acceptPlayer(server.players); players[server.players, 2] = server.players - 1; } server.sendStringToAll("START"); server.sendIntToAll(playerAmount); server.sendIntToAll(initialMoney); //////////////////////////// /// RUNS THE ACTUAL GAME /// //////////////////////////// bool running = true; while (running) { mainDeck.Shuffle(); Card[,] hands = mainDeck.PlayerHands(playerAmount); //Plank is board, know your synonyms Card[] plank = mainDeck.BuildBoard(); //Kills bankrupt players for (Int32 i = 0; i < dead; i++) { for (Int32 j = 0; j < playerAmount; j++) { if (players[j, 1] == 3) { players[j, 0] = players[j + 1, 0]; players[j, 1] = players[j + 1, 1]; players[j, 2] = players[j + 1, 2]; } } } //Reduce playerAmount by amount of players that got killed playerAmount -= dead; dead = 0; //Send hands to all players server.sendStringToAll("HAND"); for (Int32 i = 0; i < playerAmount; i++) { for (int j = 0; j < 2; j++) { server.sendCard(hands[i, j], i); } } //Send board to players server.sendStringToAll("BORD"); for (int i = 0; i < playerAmount; i++) { for (int j = 0; j < 5; j++) { server.sendCard(plank[j], i); } } //Assigns big and small blinds for (Int32 i = 0; i < playerAmount; i++) { if (players[i, 1] == 1) { players[i, 1] = 2; server.sendString("BIGB", players[i, 2]); if (players[i + 1, 1] == 3) { players[0, 1] = 1; server.sendString("SMLL", players[i, 2]); break; } players[i + 1, 1] = 1; server.sendString("SMLL", players[i, 2]); break; } else if (players[i, 1] == 2) { players[i, 1] = 0; break; } } ///////////////////////// /// ROUNDS BEGIN HERE /// ///////////////////////// bets = new Int32[playerAmount]; for (Int32 i = 0; i < playerAmount; i++) { bets[i] = 0; } //Big blind and small blind for (Int32 i = 0; i < playerAmount; i++) { if (players[i, 1] == 1) { players[i, 0] -= 100; bets[i] = 100; } if (players[i, 1] == 2) { players[i, 0] -= 200; bets[i] = 200; } } for (Int32 i = 0; i < playerAmount; i++) { players[i, 1] = 4; } //Update board for everyone here. server.sendStringToAll("PING"); //Player count server.sendIntToAll(playerAmount); //Send client money for (Int32 i = 0; i < playerAmount; i++) { server.sendInt(players[i, 0], players[i, 2]); } //Bet amounts for all players for (Int32 i = 0; i < playerAmount; i++) { server.sendIntToAll(bets[i]); } //Sockets are sent for (Int32 i = 0; i < playerAmount; i++) { server.sendIntToAll(players[i, 2]); } //Current bet server.sendIntToAll(bets.Max()); //Pool amount server.sendIntToAll(pool); //Cash for all players for (Int32 i = 0; i < playerAmount; i++) { server.sendIntToAll(players[i, 0]); } //Status flags for (Int32 i = 0; i < playerAmount; i++) { server.sendIntToAll(players[i, 1]); } //Displayed cards this round server.sendIntToAll(round - 1); Console.WriteLine(round); while (round < 5) { for (Int32 cP = 0; cP < playerAmount; cP++) { server.sendString("TURN", players[cP, 2]); //Tager imod input fra spillere, går ud fra at clients'ne ved hvad de laver så serveren ikke behøver at sende beskeder frem og tilbage indtil den får et gyldigt svar string answer = server.receiveString(players[cP, 2], 4); Int32 temp; switch (answer) { case "FOLD": players[cP, 1] = 0; bets[cP] = 0; break; case "RAIS": temp = server.receiveInt(players[cP, 2]); bets[cP] += temp; pool += temp; break; case "CALL": temp = server.receiveInt(players[cP, 2]); bets[cP] += temp; pool += temp; break; case "CHCK": break; } //Update board for everyone here. server.sendStringToAll("PING"); //Player count server.sendIntToAll(playerAmount); //Cash for all players for (Int32 i = 0; i < playerAmount; i++) { server.sendInt(players[i, 0], players[i, 2]); } //Bet amounts for all players for (Int32 i = 0; i < playerAmount; i++) { server.sendIntToAll(bets[i]); } //Sockets are sent for (Int32 i = 0; i < playerAmount; i++) { server.sendIntToAll(players[i, 2]); } //Current bet server.sendIntToAll(bets.Max()); //Pool amount server.sendIntToAll(pool); //Cash for all players for (Int32 i = 0; i < playerAmount; i++) { server.sendIntToAll(players[i, 0]); } //Status flags for (Int32 i = 0; i < playerAmount; i++) { server.sendIntToAll(players[i, 1]); } //Displayed cards this round server.sendIntToAll(round + 2); } round++; } //Check for who won somewhere in here, and update player on that information int[] handRatings = new int[playerAmount]; for (int i = 0; i < playerAmount; i++) { if (players[i, 1] != 4) { continue; } handRatings[i] = Game.BestHand(HandfromArray(hands, i), plank); } int maxValue = handRatings.Max(); int maxIndex = handRatings.ToList().IndexOf(maxValue); players[maxIndex, 1] = 5; //Check for bankruptcy and pay winner for (Int32 i = 0; i < playerAmount; i++) { if (players[i, 0] < 200 && players[i, 1] == 1) { players[i, 1] = 3; //RIP server.sendString("KILL", i); dead += 1; } else if (players[i, 0] < 100) { players[i, 1] = 3; //RIP server.sendString("KILL", i); dead += 1; } else if (players[i, 1] == 4) { players[i, 1] = 0; } else if (players[i, 1] == 5) { players[i, 0] += pool; //Big money, ayyy server.sendStringToAll("WINR"); server.sendIntToAll(players[i, 2]); } } } break; } }