private void btnStartGame_Click(object sender, EventArgs e) { if (clients.Count > 1) { //stop broadcasting the IP tmrBroadCastIP.Stop(); //generate board for (int i = 0; i < clients.Count; i++) { if (clients[i].Rank != 0) { clients[i].player.Send(Encoding.ASCII.GetBytes(string.Format("numberOfPlayers#{0}#{1}#", clients.Count, clients[i].Rank))); clients[i].player.Send(Encoding.ASCII.GetBytes(string.Format("GameStarted#"))); } } GenerateSnakesAndLadders(); char[,] board = GenerateBoard(snakes, ladders); GamePlayingScreen gpc = new GamePlayingScreen(board, snakes, ladders, clients, clients.Count, 0, currentSocket, true); gpc.Show(); this.Visible = false; } else { MessageBox.Show("there is no enough clients "); } }
public GamePlayingScreen(char[,] board, Dictionary <Point, int> snakes, Dictionary <Point, int> ladders, List <Client> clients, int numberOfPlayers, Socket me, bool Server, UdpClient udp) { activeForm = this; InitializeComponent(); Clients = clients; gameBoard = board; Snakes = snakes; Ladders = ladders; currentPlayer = me; this.numberOfPlayers = numberOfPlayers; PlayersLocation = new List <Point>(); for (int i = 0; i < numberOfPlayers; i++) { PlayersLocation.Add(new Point(0, 0)); } GeneratePlayerList(numberOfPlayers); isServer = Server; DoubleBuffered = true; SetStyle(ControlStyles.OptimizedDoubleBuffer, true); DrawBoard(); if (isServer) { this.serverUdpClient = udp; btnRollTheDice.Enabled = true; myIndex = 0; for (int i = 1; i < clients.Count; i++) { Thread t = new Thread(new ParameterizedThreadStart(RecieveFromClients)); t.Start(clients[i]); } for (int i = 1; i < clients.Count; i++) { clients[i].playerSocket.Send(Encoding.ASCII.GetBytes(clients[i].Rank.ToString())); } } else { Console.WriteLine("Sending join game notification to server"); currentPlayer.Send(Encoding.ASCII.GetBytes("Client with IP " + GameStart.GetLocalIPAddress() + " joined the game")); Console.WriteLine("Sent join game notification to server"); byte[] myIndexBytes = new byte[20]; currentPlayer.Receive(myIndexBytes); Console.WriteLine("My index is " + Encoding.ASCII.GetString(myIndexBytes)); myIndex = Int32.Parse(Encoding.ASCII.GetString(myIndexBytes)); } }
private void timerStartGame_Tick(object sender, EventArgs e) { try { if (startGame) { gpc = new GamePlayingScreen(board, snakes, ladders, null, numberOfPlayers, currentSocket, false, null); Console.WriteLine("Game will start"); Thread.Sleep(1000); this.Visible = false; gpc.Show(); timerStartGame.Stop(); Console.WriteLine("Game started"); return; } } catch (Exception ex) { Console.WriteLine("Game not started\n" + ex.Message + '\n' + ex.Data + '\n' + ex.StackTrace); } }
private void btnStartGame_Click(object sender, EventArgs e) { //foreach(Client c in clients) //{ // byte[] numPlayersBytes = new byte[20]; // numPlayersBytes = Encoding.ASCII.GetBytes(numberOfPlayers.ToString()); // c.playerSocket.Send(numPlayersBytes); //} tmrBroadCastIP.Stop(); IPEndPoint endPoint = new IPEndPoint(IPAddress.Broadcast, groupPort); String serverIP = GameStart.GetLocalIPAddress(); byte[] serverIPBytes = Encoding.ASCII.GetBytes("start"); udp.Send(serverIPBytes, serverIPBytes.Length, endPoint); Console.WriteLine("Broadcasted start game"); GenerateSnakesAndLadders(); char[,] board = GenerateBoard(snakes, ladders); gpc = new GamePlayingScreen(board, snakes, ladders, clients, clients.Count, currentSocket, true, udp); this.Visible = false; gpc.Show(); }
void RecieveDataFromServer() { //this function in different thread as it will halt the application during recieving from server //write code to recieve (numberOfPlayers) while (recieving) { byte[] arr = new byte[2014]; currentSocket.Receive(arr); var message = Encoding.ASCII.GetString(arr); Console.WriteLine(message); ParseMessage(message); } GenerateSnakesAndLadders(); char[,] board = GenerateBoard(snakes, ladders); GamePlayingScreen gpc = new GamePlayingScreen(board, snakes, ladders, null, numberOfPlayers, Rank, currentSocket, false); this.Visible = false; gpc.ShowDialog(); Console.WriteLine("opening gpc"); }