コード例 #1
0
        private void btnRollTheDice_Click(object sender, EventArgs e)
        {
            //write the button code here:
            //1- disable "RollTheDice" button
            //2- generate random number and write it in textbox
            //3- after 3 sec move the player coin
            //4- check if new location is ladder or snake using gameBoard array and modify the new location based on the value of gameBoard[y,x] = 'S' or = 'L'
            //6- update the location of currentPlayer (to be modified in drawing)
            Random r = new Random();

            int val = r.Next(1, 6);

            textBox1.Text = val.ToString();
            Thread.Sleep(1000);

            //if (!IsServer)
            //    val /= 2;

            UpdatePlayerLoc(myIndex, val);

            var obj = (Bitmap)Properties.Resources.ResourceManager.GetObject(string.Format("_{0}", val - 1));

            ((Button)sender).BackgroundImage = obj;

            if (IsServer)
            {
                BroadCastLocation();
                BroadCastWhoseTurn(0);
                if (IWon)
                {
                    BroadCastTheWinnerIs(0);
                    WinningForm wf = new WinningForm(0);
                    this.Visible = false;
                    wf.ShowDialog();
                }
                //call BroadCastLocation(0) as the server index is always 0 in the client list
                //call BroadCastWhoseTurn(0) to see which player will play after server
            }
            else
            {
                if (IWon)
                {
                    SendTheWinnerIsMeToServer();
                }
                else
                {
                    SendLocationToServer();
                }


                //if final location is the winning location then call the function SendTheWinnerIsMeToServer()
                //else send the final location to server by calling SendLocationToServer()
            }

            btnRollTheDice.Enabled = false;
        }
コード例 #2
0
        void BroadCastTheWinnerIs(int playerNumber)
        {
            //send to all clients message, containing IP,playerNumber
            for (int i = 1; i < Clients.Count; i++)
            {
                Clients[i].player.Send(Encoding.ASCII.GetBytes(string.Format("gameEndedS#{0}#", playerNumber)));
            }
            WinningForm wf = new WinningForm(playerNumber);

            this.Visible = false;
            wf.ShowDialog();
        }
コード例 #3
0
        void ParseMessage(string message)
        {
            var tockens = message.Split(new char[] { '#' });

            //server
            if (tockens[0] == "myLocation")
            {
                int PlayerIndexWhoSendTheMessage = int.Parse(tockens[1]);
                int px = int.Parse(tockens[2]);
                int py = int.Parse(tockens[3]);
                PlayersLocation[PlayerIndexWhoSendTheMessage] = new Point(px, py);
                BroadCastLocation();
                BroadCastWhoseTurn(PlayerIndexWhoSendTheMessage);
                Console.WriteLine("mylocation me " + PlayerIndexWhoSendTheMessage + " point " + PlayersLocation[PlayerIndexWhoSendTheMessage]);
            }

            //server
            if (tockens[0] == "gameEnded")
            {
                BroadCastTheWinnerIs(int.Parse(tockens[1]));
            }
            //client
            if (tockens[0] == "gameEndedS")
            {
                WinningForm wf = new WinningForm(int.Parse(tockens[1]));
                this.Visible = false;
                wf.ShowDialog();
            }
            //client
            if (tockens[0] == "currentPlayerTurn")
            {
                currPlayerTurn = int.Parse(tockens[1]);
                if (currPlayerTurn == myIndex)
                {
                    btnRollTheDice.Enabled = true;
                }

                Console.WriteLine("currentPlayerTurn" + tockens[1] + "myindex " + myIndex);
            }
        }