Exemple #1
0
        private void Temp_Click(object sender, EventArgs e)
        {
            int[] point = (int[])((Control)sender).Tag;


            send("!hit" + point[0] + " " + point[1] + " " + 1);



            TicTacToeController.hit(point[0], point[1], 1);
            isMoved = true;
            txtStatus.Invoke((MethodInvoker) delegate()
            {
                txtStatus.Text = "ĐẾN LƯỢT ĐỐI THỦ";
            });

            if (TicTacToeController.winnerNumber == 1)
            {
                send("!1win");
                txtStatus.Invoke((MethodInvoker) delegate() { txtStatus.Text = "BẠN ĐÃ THẮNG!"; });
            }



            if (aTimer.Enabled == false)
            {
                aTimer.Start();
            }
            labelTime.Invoke((MethodInvoker) delegate() { labelTime.Text = TIME.ToString(); });
            refresh();
        }
Exemple #2
0
        private void Temp_Click(object sender, EventArgs e)
        {
            try
            {
                int[] point = (int[])((Control)sender).Tag;
                TicTacToeController.hit(point[0], point[1], 2);



                //SEND

                send("!hit" + point[0] + " " + point[1] + " " + 2);



                isMoved = true;
                txtStatus.Invoke((MethodInvoker) delegate()
                {
                    txtStatus.Text = "ĐẾN LƯỢT ĐỐI THỦ";
                });
                if (TicTacToeController.winnerNumber == 2)
                {
                    send("!2win");
                    txtStatus.Invoke((MethodInvoker) delegate() { txtStatus.Text = "BẠN ĐÃ THẮNG!"; });
                }



                labelTime.Invoke((MethodInvoker) delegate()
                {
                    labelTime.Text = TIME.ToString();
                });
                if (aTimer.Enabled == false)
                {
                    aTimer.Start();
                }
                refresh();
            }
            catch (Exception e1)
            {
                MessageBox.Show("Đánh với ma à?", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                connected = false;
            }
        }
Exemple #3
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //newDesk();
            //refresh();
            CreateRoomForm createRoomForm = new CreateRoomForm();

            if (createRoomForm.ShowDialog() == DialogResult.OK)
            {
                TicTacToeController.DESK_SIZE = createRoomForm.deskSize;
                labelDeskSize.Text            = TicTacToeController.DESK_SIZE.ToString();
                PORT     = (new Random()).Next(1, 1000);
                listener = new TcpListener(address, PORT);
            }
            //newDesk();
            labelDeskSize.Text = TicTacToeController.DESK_SIZE.ToString();
            labelPort.Text     = PORT.ToString();

            Thread thread = new Thread(delegate()
            {
                listener.Start();

                Console.WriteLine("Server started on " + listener.LocalEndpoint);
                Console.WriteLine("Waiting for a connection...");
                txtStatus.Invoke((MethodInvoker) delegate()
                {
                    txtStatus.Text = "ĐANG CHỜ ĐỢI 1 ĐỐI THỦ XỨNG TẦM!";
                });

                Socket socket = listener.AcceptSocket();
                Console.WriteLine("Connection received from " + socket.RemoteEndPoint);
                txtStatus.Invoke((MethodInvoker) delegate()
                {
                    txtStatus.Text = "ĐÃ TÌM THẤY ĐỐI THỦ";
                });
                connected = true;
                listener.Stop();

                flowLayoutPanel1.Invoke((MethodInvoker) delegate() { newDesk(); });
                stream = new NetworkStream(socket);
                send(TicTacToeController.DESK_SIZE.ToString());
                var reader = new StreamReader(stream);


                while (true)
                {
                    // 2. receive
                    byte[] buffer         = new byte[socket.ReceiveBufferSize];
                    int bytesRead         = stream.Read(buffer, 0, socket.ReceiveBufferSize);
                    string receivedString = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                    Console.WriteLine(receivedString);
                    if (receivedString.Contains("!hit"))
                    {
                        receivedString = receivedString.Replace("!hit", "");

                        string[] rs      = receivedString.Split(' ');
                        int i            = Convert.ToInt32(rs[0]);
                        int j            = Convert.ToInt32(rs[1]);
                        int playerNumber = Convert.ToInt32(rs[2]);



                        TicTacToeController.hit(i, j, playerNumber);
                        isMoved = false;

                        txtStatus.Invoke((MethodInvoker) delegate()
                        {
                            txtStatus.Text = "ĐẾN LƯỢT BẠN";
                        });
                        labelTime.Invoke((MethodInvoker) delegate()
                        {
                            labelTime.Text = TIME.ToString();
                        });
                        aTimer.Start();
                        flowLayoutPanel1.Invoke((MethodInvoker) delegate() { refresh(); });
                    }
                    else if (receivedString.Equals("!1win"))
                    {
                        //txtStatus.Invoke((MethodInvoker)delegate () { txtStatus.Text = "BẠN ĐÃ THUA!"; });
                        //TicTacToeController.winnerNumber = 1;
                        flowLayoutPanel1.Invoke((MethodInvoker) delegate() { refresh(); });
                    }
                }
                // 4. close
                stream.Close();
                socket.Close();
                listener.Stop();



                /////////////////////////////////////
                //Dong socket
            });

            thread.Start();
        }
Exemple #4
0
        private void refresh()
        {
            if (isMoved)
            {
                flowLayoutPanel1.Enabled = false;
            }
            else
            {
                flowLayoutPanel1.Enabled = true;
            }

            for (int i = 0; i < TicTacToeController.DESK_SIZE; i++)
            {
                for (int j = 0; j < TicTacToeController.DESK_SIZE; j++)
                {
                    foreach (Control control in flowLayoutPanel1.Controls)
                    {
                        Button temp = (Button)control;

                        if (((int[])temp.Tag)[0] == i && ((int[])temp.Tag)[1] == j)
                        {
                            if (TicTacToeController.desk[i, j] != 0)
                            {
                                temp.Enabled = false;
                            }



                            if (TicTacToeController.desk[i, j] == 1)
                            {
                                temp.BackColor = Color.Red;
                                temp.Text      = "X";
                            }
                            if (TicTacToeController.desk[i, j] == 2)
                            {
                                temp.BackColor = Color.Green;

                                temp.Text = "O";
                            }
                        }
                    }
                }
            }

            if (!TicTacToeController.canMove() || labelTime.Text.Equals("0") || TicTacToeController.winnerNumber != 0)
            {
                aTimer.Stop();
                if (TicTacToeController.winnerNumber == 1)
                {
                    txtStatus.Invoke((MethodInvoker) delegate()
                    {
                        txtStatus.Text = "BẠN ĐÃ THUA";
                    });
                }
                else if (TicTacToeController.winnerNumber == 2)
                {
                    txtStatus.Invoke((MethodInvoker) delegate()
                    {
                        txtStatus.Text = "BẠN ĐÃ THẮNG";
                    });
                }
                DialogResult rs = MessageBox.Show("Bạn có muốn chơi lại ko?", "Chơi tiếp?", MessageBoxButtons.YesNo);
                if (rs == DialogResult.Yes)
                {
                    send("!endgame");
                    send("!newgame");
                    TicTacToeController.newGame();
                    newDesk();
                }
                // return;
            }
        }
Exemple #5
0
        private void Form1_Load(object sender, EventArgs e)
        {
            // refresh();
            bool joinSuccessfully = false;

            do
            {
                JoinRoomForm joinRoomForm = new JoinRoomForm();
                if (joinRoomForm.ShowDialog() == DialogResult.OK)
                {
                    PORT = joinRoomForm.PORT;
                    try
                    {
                        client.Connect("127.0.0.1", PORT);
                        joinSuccessfully = true;
                    }
                    catch (Exception e1)
                    {
                        joinSuccessfully = false;
                        MessageBox.Show("Không thể tham gia bàn!\n + Bàn đang chơi\n + Bàn không tồn tại", "Thất bại", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            } while (!joinSuccessfully);

            Thread thread = new Thread(delegate()
            {
                // 1. connect

                stream = client.GetStream();

                Console.WriteLine("Đã kết nối");

                //GET DESK SIZE
                byte[] buffer                 = new byte[client.ReceiveBufferSize];
                int bytesRead                 = stream.Read(buffer, 0, client.ReceiveBufferSize);
                string receivedString         = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                TicTacToeController.DESK_SIZE = Convert.ToInt32(receivedString);
                groupBox2.Invoke((MethodInvoker) delegate()
                {
                    labelDeskSize.Text = TicTacToeController.DESK_SIZE.ToString();
                    labelPort.Text     = PORT.ToString();
                });
                //NEW GAME
                flowLayoutPanel1.Invoke((MethodInvoker) delegate() { newDesk(); });
                while (true)
                {
                    buffer         = new byte[client.ReceiveBufferSize];
                    bytesRead      = stream.Read(buffer, 0, client.ReceiveBufferSize);
                    receivedString = Encoding.UTF8.GetString(buffer, 0, bytesRead);

                    if (receivedString.Contains("!hit"))
                    {
                        receivedString = receivedString.Replace("!hit", "");

                        Console.WriteLine(receivedString);

                        string[] rs      = receivedString.Split(' ');
                        int i            = Convert.ToInt32(rs[0]);
                        int j            = Convert.ToInt32(rs[1]);
                        int playerNumber = Convert.ToInt32(rs[2]);

                        TicTacToeController.hit(i, j, playerNumber);
                        isMoved = false;
                        txtStatus.Invoke((MethodInvoker) delegate()
                        {
                            txtStatus.Text = "ĐẾN LƯỢT BẠN";
                        });
                        if (aTimer.Enabled == false)
                        {
                            aTimer.Start();
                        }
                        labelTime.Invoke((MethodInvoker) delegate() { labelTime.Text = TIME.ToString(); });
                        flowLayoutPanel1.Invoke((MethodInvoker) delegate() { refresh(); });
                    }


                    else if (receivedString.Equals("!endgame"))
                    {
                        if (isMoved)
                        {
                            TicTacToeController.winnerNumber = 1;
                            txtStatus.Invoke((MethodInvoker) delegate() { txtStatus.Text = "HẾT GIỜ! BẠN ĐÃ THAWSNG!"; });
                        }
                        else
                        {
                            TicTacToeController.winnerNumber = 2;
                            txtStatus.Invoke((MethodInvoker) delegate() { txtStatus.Text = "HẾT GIỜ! BẠN ĐÃ THUA!"; });
                        }
                        this.Invoke((MethodInvoker) delegate() { this.Enabled = false; });

                        endGame = true;
                        this.Invoke((MethodInvoker) delegate() { this.Enabled = false; });
                        flowLayoutPanel1.Invoke((MethodInvoker) delegate() { refresh(); });
                        labelTime.Invoke((MethodInvoker) delegate() { labelTime.Text = TIME.ToString(); });
                    }
                    else if (receivedString.Equals("!newgame"))
                    {
                        newGame = true;
                        this.Invoke((MethodInvoker) delegate() { this.Enabled = false; });
                        endGame = true;
                        flowLayoutPanel1.Invoke((MethodInvoker) delegate() { refresh(); });
                        labelTime.Invoke((MethodInvoker) delegate() { labelTime.Text = TIME.ToString(); });
                    }

                    else if (receivedString.Equals("!2win"))
                    {
                        txtStatus.Invoke((MethodInvoker) delegate() { txtStatus.Text = "BẠN ĐÃ THUA!"; });
                        this.Invoke((MethodInvoker) delegate() { this.Enabled = false; });


                        endGame = true;
                        flowLayoutPanel1.Invoke((MethodInvoker) delegate() { refresh(); });
                        labelTime.Invoke((MethodInvoker) delegate() { labelTime.Text = TIME.ToString(); });
                    }
                }



                // 4. close
                stream.Close();
                client.Close();
                //Client nhan message tu server
                //size = sck.Receive(data);

                //}
                ///////////////////////////////////////
            });

            thread.Start();
        }
Exemple #6
0
        private void refresh()
        {
            if (isMoved)
            {
                flowLayoutPanel1.Enabled = false;
            }
            else
            {
                flowLayoutPanel1.Enabled = true;
            }
            for (int i = 0; i < TicTacToeController.DESK_SIZE; i++)
            {
                for (int j = 0; j < TicTacToeController.DESK_SIZE; j++)
                {
                    foreach (Control control in flowLayoutPanel1.Controls)
                    {
                        Button temp = (Button)control;

                        if (((int[])temp.Tag)[0] == i && ((int[])temp.Tag)[1] == j)
                        {
                            if (TicTacToeController.desk[i, j] != 0)
                            {
                                temp.Enabled = false;
                            }


                            if (TicTacToeController.desk[i, j] == 1)
                            {
                                temp.BackColor = Color.Red;
                                temp.Text      = "X";
                            }
                            if (TicTacToeController.desk[i, j] == 2)
                            {
                                temp.BackColor = Color.Green;

                                temp.Text = "O";
                            }
                        }
                    }
                }
            }

            if (!TicTacToeController.canMove() || endGame || TicTacToeController.winnerNumber != 0)
            {
                TicTacToeController.newGame();
                endGame = false;

                aTimer.Stop();



                if (newGame)
                {
                    this.Invoke((MethodInvoker) delegate() { this.Enabled = true; });
                    newGame = false;
                    newDesk();
                }


                // return;
            }
        }