Exemplo n.º 1
0
        public Game(NetworkStream ns, string op_name)
        {
            InitializeComponent();
            GridX = new int[15];
            GridY = new int[10];
            Network_Stream = ns;
            arr = new char[19, 29];
            lines = new int[29];

            player1 = new Player();
            client = new Client();
            player1.OpponentName = op_name;
            if (Player.IsServer)
            {
                turn = Player.Name;
                waiting = player1.OpponentName;
            }
            else
            {
                turn = player1.OpponentName;
                waiting = Player.Name;
            }

            Thread read = new Thread(() => Read_Stream());
            read.IsBackground = true;
            read.Start();
        }
Exemplo n.º 2
0
        void btn_list_Click(object sender, EventArgs e)
        {
            names.Clear();
            ips.Clear();
            using (Client client = new Client())
            {
                string RecievedData = client.Retrieve(Player.Name);
                string name = RecievedData.Substring(0, RecievedData.IndexOf('&'));
                string ip = RecievedData.Substring(RecievedData.IndexOf('&') + 1);

                for (int startindx = 0, length = name.IndexOf('*'), prv = length; startindx < name.Length; )
                {
                    names.Add(name.Substring(startindx, length));
                    startindx = prv + 1;
                    prv = name.IndexOf('*', startindx);
                    length = prv - startindx;

                }

                for (int startindx = 0, length = ip.IndexOf('*'), prv = length; prv != -1; )
                {
                    ips.Add(ip.Substring(startindx, length));
                    startindx = prv + 1;
                    prv = ip.IndexOf('*', startindx);
                    length = prv - startindx;

                }
                foreach (string s in names)
                    AvailablePlayers.Items.Add(s);
            }
        }
Exemplo n.º 3
0
        private void strt_btn_Click(object sender, EventArgs e)
        {
            if (AvailablePlayers.SelectedIndex != -1)
            {
                using (Client client = new Client())
                {
                    NetworkStream ns = client.Connect(ips[AvailablePlayers.SelectedIndex]);
                    client.Write(ns, Player.Name);
                    string data = client.Read(ns);
                    data = data.Substring(0, data.IndexOf('\0'));
                    if (data == "&|&")
                    {
                        MessageBox.Show("Sorry this player is busy", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ns.Close();
                    }
                    else
                    {
                        Player.IsServer = false;
                        //Starting Game
                        Game game = new Game(ns, names[AvailablePlayers.SelectedIndex]);
                        game.Show();
                        Close();
                    }

                }

            }
            else
                MessageBox.Show("Please Select Any Player", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Exemplo n.º 4
0
        public static void Start()
        {
            try
            {

                IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress[] addr = ipEntry.AddressList;

                //addr[1] will contain IPAdress of local machine
                TcpListener Server = new TcpListener(IPAddress.Parse("127.0.0.9"), 9876);
                Server.Start();
                TcpClient client = Server.AcceptTcpClient();

                if (Player.State == PlayerState.Available)
                {
                    DialogResult dr = MessageBox.Show("A new challenger\nDo you want to play", "Game Request", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (dr == DialogResult.Yes)
                    {

                        using (Client Client = new Client())
                        {
                            //Read Opponent Name from Client.
                            string opname = Client.Read(client.GetStream());
                            opname = opname.Substring(0, opname.IndexOf('\0'));
                            //Writing Yes to the client.
                            Client.Write(client.GetStream(), "yes");
                            Player.IsServer = true;
                            //Starting Game
                            Application.Run(new Game(client.GetStream(), opname));

                        }
                    }
                    else
                    {
                        using (Client Client = new Client())
                        {
                            //If Player doesnot accept the request.
                            Client.Write(client.GetStream(), "&|&");

                        }
                    }
                }
                else
                {
                    using (Client Client = new Client())
                    {
                        //If Player is not available.
                        Client.Write(client.GetStream(), "&|&");

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Reading Indicies from Stream
        /// When Opponent Player is at turn.
        /// </summary>
        private void Read_Stream()
        {
            using (Client c = new Client())
            {
                try
                {
                    while (true)
                    {
                        string data = c.Read(Network_Stream);
                        //lock the shared variables.
                        lock (this)
                        {
                            xindx = Convert.ToInt16(data.Substring(0, data.IndexOf('&')));
                            yindx = Convert.ToInt16(data.Substring(data.IndexOf('&') + 1));
                            //Drawing line without giving point.
                            DrawLine(new Point(0, 0));
                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
        }