Пример #1
0
        private void StartGame_Click(object sender, EventArgs e)
        {
            if (gameType == 3)
            {
                f.logedUser = this.logedUser;



                this.Game_Running = true;

                // za ponovno pokretanje nove partije
                StartGame.Enabled = false;
                if (f.IsDisposed)
                {
                    f      = new Form1();
                    f.Text = "f1";

                    f.p1.pobjede = win;

                    f.p2.pobjede = lost;
                    f.ClientName = clientUsername;
                    f.logedUser  = logedUser;
                    json         = JsonConvert.SerializeObject(f);
                    tcpServer.BroadcastLine(json);
                    //server subscribe
                    f.MouseClicked += this.OnMOuseClicked;

                    //form sub
                    f.ClientName = this.clientUsername;

                    f.GameEnded += this.OnGameEnded;

                    this.MoveMade          += f.OnMoveMade;
                    f.FormExiting          += this.OnFormClosing;
                    tcpServer.DataReceived += f.GameRecived;
                    f.FormUnsub            -= this.OnFormUnsub;


                    f.Show();
                }

                else
                {
                    json = JsonConvert.SerializeObject(f);



                    tcpServer.BroadcastLine(json);
                    //pretp
                    f.ClientName = this.clientUsername;

                    f.MouseClicked += this.OnMOuseClicked;
                    //f.onmovemade rfferenca na metodu
                    f.GameEnded += this.OnGameEnded;

                    this.MoveMade          += f.OnMoveMade;
                    f.FormUnsub            += this.OnFormUnsub;
                    f.FormExiting          += this.OnFormClosing;
                    tcpServer.DataReceived += f.GameRecived;

                    f.Show();
                }
            }
            else
            {
                g.logedUser = this.logedUser;



                this.Game_Running = true;
                StartGame.Enabled = false;
                // za ponovno pokretanje nove partije

                if (g.IsDisposed)
                {
                    g      = new GameFour();
                    g.Text = "gamefour";

                    g.p1.pobjede = win;

                    g.p2.pobjede = lost;
                    g.ClientName = clientUsername;
                    g.logedUser  = logedUser;
                    json         = JsonConvert.SerializeObject(g);


                    tcpServer.BroadcastLine(json);
                    //server subscribe
                    g.MouseClicked += this.OnMOuseClicked;

                    //form sub
                    g.ClientName = this.clientUsername;

                    g.GameEnded            += this.OnGameEnded;
                    tcpServer.DataReceived += g.GameRecived;
                    this.MoveMade          += g.OnMoveMade;
                    g.FormExiting          += this.OnFormClosing;

                    g.FormUnsub -= this.OnFormUnsub;


                    g.Show();
                }

                else
                {
                    json = JsonConvert.SerializeObject(g);



                    tcpServer.BroadcastLine(json);
                    //pretp
                    g.ClientName = this.clientUsername;

                    g.MouseClicked += this.OnMOuseClicked;
                    //f.onmovemade rfferenca na metodu
                    g.GameEnded            += this.OnGameEnded;
                    tcpServer.DataReceived += g.GameRecived;
                    this.MoveMade          += g.OnMoveMade;
                    g.FormUnsub            += this.OnFormUnsub;
                    g.FormExiting          += this.OnFormClosing;

                    g.Show();
                }


                MessageBox.Show(this.Game_Running.ToString());
            }
        }
Пример #2
0
        public void Client_Received(object sender, SimpleTCP.Message m)
        {
            string end    = m.MessageString.Substring(0, m.MessageString.Length - 1);
            string kicked = m.MessageString.Substring(0, m.MessageString.Length - 1);

            if (kicked.Equals(left))
            {
                button2_Click(this, EventArgs.Empty);
            }
            if (end.Equals("e") || end.Equals("e!!"))
            {
                this.serverWin++;

                if (this.serverWin > 0)
                {
                    MessageBox.Show("you lost");

                    winForm.pictureBox1.Image = Properties.Resources.lost;
                    winForm.ShowDialog();
                    DialogResult dialog = MessageBox.Show("New Game", "exit", MessageBoxButtons.YesNo);

                    if (dialog == DialogResult.Yes)
                    {
                        if (gameType == 3)
                        {
                            this.serverWin   = 0;
                            this.clientWin   = 0;
                            f.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= f.OnClientMove;
                            f.Dispose();
                        }
                        else
                        {
                            this.serverWin   = 0;
                            this.clientWin   = 0;
                            g.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= g.OnClientMove;
                            g.Dispose();
                        }
                    }

                    //klijent ne zeli novu igru te izlazi iz servera
                    else if (dialog == DialogResult.No)
                    {
                        try
                        {
                            button2_Click(null, new EventArgs());
                            //this.Client_FormClosing(null, new FormClosingEventArgs(CloseReason.None,false));
                            if (gameType == 3)
                            {
                                if (!f.IsDisposed)
                                {
                                    f.Dispose();
                                }
                            }
                            else
                            {
                                if (!g.IsDisposed)
                                {
                                    g.Dispose();
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(e.Message.ToString());
                        }
                    }
                }

                //server dobio partiju u malim
                else
                {
                    DialogResult dialog = MessageBox.Show("Continue", "exit", MessageBoxButtons.YesNo);

                    if (dialog == DialogResult.Yes)
                    {
                        if (gameType == 3)
                        {
                            f.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= f.OnClientMove;

                            f.Dispose();
                        }
                        else
                        {
                            g.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= g.OnClientMove;

                            g.Dispose();
                        }
                    }
                    else if (dialog == DialogResult.No)
                    {
                        button2_Click(null, new EventArgs());
                        if (gameType == 3)
                        {
                            if (!f.IsDisposed)
                            {
                                f.Dispose();
                            }
                        }
                        else
                        {
                            if (!g.IsDisposed)
                            {
                                g.Dispose();
                            }
                        }
                    }
                }
            }
            else if (end.Equals("c") || end.Equals("c!!"))
            {
                this.gameRunning = false;
                this.clientWin++;
                if (this.clientWin > 0)
                {
                    MessageBox.Show("you won");

                    winForm.pictureBox1.Image = Properties.Resources.winner;
                    winForm.ShowDialog();
                    DialogResult dialog = MessageBox.Show("New Game", "exit", MessageBoxButtons.YesNo);

                    if (dialog == DialogResult.Yes)
                    {
                        if (gameType == 3)
                        {
                            this.serverWin   = 0;
                            this.clientWin   = 0;
                            f.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= f.OnClientMove;
                            f.Dispose();
                        }
                        else
                        {
                            this.serverWin   = 0;
                            this.clientWin   = 0;
                            g.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= g.OnClientMove;
                            g.Dispose();
                        }
                    }
                    else if (dialog == DialogResult.No)
                    {
                        button2_Click(null, new EventArgs());

                        if (gameType == 3)
                        {
                            if (!f.IsDisposed)
                            {
                                f.Dispose();
                            }
                        }
                        else
                        {
                            if (!g.IsDisposed)
                            {
                                g.Dispose();
                            }
                        }
                    }
                }


                else
                {
                    MessageBox.Show("opponent won");
                    DialogResult dialog = MessageBox.Show("Continue", "exit", MessageBoxButtons.YesNo);
                    if (dialog == DialogResult.Yes)
                    {
                        if (gameType == 3)
                        {
                            f.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= f.OnClientMove;

                            f.Dispose();
                        }
                        else
                        {
                            g.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= g.OnClientMove;

                            g.Dispose();
                        }
                    }
                    else if (dialog == DialogResult.No)
                    {
                        button2_Click(null, new EventArgs());
                        if (gameType == 3)
                        {
                            if (!f.IsDisposed)
                            {
                                f.Dispose();
                            }
                        }
                        else
                        {
                            if (!g.IsDisposed)
                            {
                                g.Dispose();
                            }
                        }
                    }
                }
            }
            else if (end.Equals("t") || end.Equals("t!!"))
            {
                MessageBox.Show("Game draw");
                DialogResult dialog = MessageBox.Show("Continue", "exit", MessageBoxButtons.YesNo);

                if (dialog == DialogResult.Yes)
                {
                    if (gameType == 3)
                    {
                        f.MouseClicked  -= this.OnMOuseClicked;
                        this.ClientMove -= f.OnClientMove;

                        f.Dispose();
                    }
                    else
                    {
                        g.MouseClicked  -= this.OnMOuseClicked;
                        this.ClientMove -= g.OnClientMove;

                        g.Dispose();
                    }
                }
                else if (dialog == DialogResult.No)
                {
                    button2_Click(null, new EventArgs());
                    if (gameType == 3)
                    {
                        if (!f.IsDisposed)
                        {
                            f.Dispose();
                        }
                    }
                    else
                    {
                        if (!g.IsDisposed)
                        {
                            g.Dispose();
                        }
                    }
                }
            }
            textBox1.Invoke((MethodInvoker) delegate()
            {
                //da ne postavlja pitcureboxove u listview
                string m1 = m.MessageString.Substring(0, m.MessageString.Length - 1);
                if ((!m1.Equals("pictureBox1")) && (!m1.Equals("pictureBox2")) && (!m1.Equals("pictureBox3")) && (!m1.Equals("pictureBox4")))
                {
                    if (m1.Length > 800)
                    {
                        listView1.Items.Add("game started" + "\n");
                    }
                    else
                    {
                        listView1.Items.Add(m.MessageString + "\n");
                    }
                }
                potez = m.MessageString;
            });
            start = m.MessageString;

            if (start.Length > 800)
            {
                //bez ovoga ne uspije ucitat cili string

                BeginInvoke((MethodInvoker) delegate
                {
                    string s = start.Substring(0, start.Length - 1);
                    try
                    {
                        if (gameType == 3)
                        {
                            f = JsonConvert.DeserializeObject <Form1>(s);
                        }
                        else
                        {
                            g = JsonConvert.DeserializeObject <GameFour>(s);
                        }
                        Thread.Sleep(1);
                    }

                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message.ToString());
                    }


                    if (gameType == 3)
                    {
                        var p1 = f.p1;
                        var p2 = f.p2;
                        f.p1   = p2;
                        f.p2   = p1;

                        f.Text               = "client";
                        f.MouseClicked      += this.OnMOuseClicked;
                        this.ClientMove     += f.OnClientMove;
                        client.DataReceived += f.GameRecived;
                        this.gameRunning     = true;
                        f.FormClosing       += this.Game_Form_Closing;

                        f.Show();
                    }
                    else
                    {
                        var p1 = g.p1;
                        var p2 = g.p2;
                        g.p1   = p2;
                        g.p2   = p1;


                        g.Text               = "client";
                        g.MouseClicked      += this.OnMOuseClicked;
                        this.ClientMove     += g.OnClientMove;
                        this.gameRunning     = true;
                        client.DataReceived += g.GameRecived;

                        g.FormClosing += this.Game_Form_Closing;

                        g.Show();
                    }
                });
            }



            string mess = potez.Substring(0, potez.Length - 1);

            if ((mess.Equals("pictureBox1")) || (mess.Equals("pictureBox2")) || (mess.Equals("pictureBox3")) || mess.Equals("pictureBox4"))
            {
                ClientMove(mess);
            }
        }