예제 #1
0
        public void StartHost(int port)
        {
            TcpListener server = new TcpListener(port);
            TcpClient   client = new TcpClient();

            try
            {
                server.Start();
                int i1 = 0;
                client = server.AcceptTcpClient();
                NetworkStream str = client.GetStream();
                if (client.Connected && i1 == 0)
                {
                    Display.WriteCyan(centerX - 20, centerY - 5, "Welcome to TicTacToe Network Version!");
                    i1++;
                    Console.Clear(); Game.Table();
                }
                while (true)
                {
                    if (JoinGame.CheckIfWin("X") == true)
                    {
                        Display.WriteYellow(menuX - 10, menuY + 6, "                                                          ");
                        Display.WriteYellow(menuX, menuY + 6, "Back");
                        Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "                        ");
                        Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "Winner: " + JoinGame.Winner);
                        while (true)
                        {
                            Program.ClearKeyBuffer();

                            ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                            if (keyInfo.Key == ConsoleKey.Enter)
                            {
                                Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 4, "                        ");
                                server.Stop();
                                client.Close();
                                Console.Clear();
                                return;
                            }
                        }
                    }
                    if (JoinGame.CheckIfWin("O") == true)
                    {
                        Display.WriteYellow(menuX - 10, menuY + 6, "                                                          ");
                        Display.WriteYellow(menuX, menuY + 6, "Back");
                        Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "                        ");
                        Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "Winner: " + JoinGame.Winner);
                        while (true)
                        {
                            Program.ClearKeyBuffer();

                            ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                            if (keyInfo.Key == ConsoleKey.Enter)
                            {
                                Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 4, "                        ");
                                server.Stop();
                                client.Close();
                                Console.Clear();
                                return;
                            }
                        }
                    }

                    string text          = "";
                    byte[] receiveBuffer = new byte[999];
                    byte[] sendData      = new byte[999];

                    Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 4, "                        ");
                    Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 4, "Other player turn: ");

                    string msg;

                    str.Read(receiveBuffer, 0, receiveBuffer.Length);
                    msg = Encoding.ASCII.GetString(receiveBuffer, 0, receiveBuffer.Length);



                    if (msg.Contains("1 1"))
                    {
                        Game.first = "O";
                    }
                    if (msg.Contains("1 2"))
                    {
                        Game.second = "O";
                    }
                    if (msg.Contains("1 3"))
                    {
                        Game.third = "O";
                    }
                    if (msg.Contains("2 1"))
                    {
                        Game.fourth = "O";
                    }
                    if (msg.Contains("2 2"))
                    {
                        Game.fifth = "O";
                    }
                    if (msg.Contains("2 3"))
                    {
                        Game.sixth = "O";
                    }
                    if (msg.Contains("3 1"))
                    {
                        Game.seventh = "O";
                    }
                    if (msg.Contains("3 2"))
                    {
                        Game.eight = "O";
                    }
                    if (msg.Contains("3 3"))
                    {
                        Game.nineth = "O";
                    }


                    Game.Table();

                    for (int i = 0; i < receiveBuffer.Length; i++)
                    {
                        receiveBuffer[i] = 0;
                    }

                    if (JoinGame.CheckIfWin("X") == true)
                    {
                        Display.WriteYellow(menuX - 10, menuY + 6, "                                                          ");
                        Display.WriteYellow(menuX, menuY + 6, "Back");
                        Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "                        ");
                        Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "Winner: " + JoinGame.Winner);
                        while (true)
                        {
                            Program.ClearKeyBuffer();

                            ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                            if (keyInfo.Key == ConsoleKey.Enter)
                            {
                                Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 4, "                        ");
                                server.Stop();
                                client.Close();
                                Console.Clear();
                                return;
                            }
                        }
                    }
                    if (JoinGame.CheckIfWin("O") == true)
                    {
                        Display.WriteYellow(menuX - 10, menuY + 6, "                                                          ");
                        Display.WriteYellow(menuX, menuY + 6, "Back");
                        Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "                        ");
                        Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "Winner: " + JoinGame.Winner);
                        Display.WriteYellow(menuX - 10, menuY + 5, "                                                 ");
                        while (true)
                        {
                            Program.ClearKeyBuffer();

                            ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                            if (keyInfo.Key == ConsoleKey.Enter)
                            {
                                Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 4, "                        ");
                                server.Stop();
                                client.Close();
                                Console.Clear();
                                return;
                            }
                        }
                    }

                    Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 4, "                        ");
                    Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 4, "Your turn:");
                    Game.Start();
                    sendData = Encoding.ASCII.GetBytes(JoinGame.txtME);
                    Game.Table();
                    str.Write(sendData, 0, sendData.Length);

                    Game.ex = true;
                }
            }

            catch (SocketException e)
            {
                Console.Clear();
                Display.WriteCyan(centerX - 20, centerY - 5, "           Server already exist...");
                Display.WriteYellow(menuX - 10, menuY + 6, "                                                          ");
                Display.WriteYellow(menuX, menuY + 6, "Back");
                while (true)
                {
                    Program.ClearKeyBuffer();
                    ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                    if (keyInfo.Key == ConsoleKey.Enter)
                    {
                        Console.Clear();
                        break;;
                    }
                }
            }

            catch (Exception e)
            {
                Console.Clear();
                Display.WriteCyan(centerX - 20, centerY - 5, "           No player avaible...");
                Display.WriteYellow(menuX - 10, menuY + 6, "                                                          ");
                Display.WriteYellow(menuX, menuY + 6, "Back");
                while (true)
                {
                    Program.ClearKeyBuffer();
                    ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                    if (keyInfo.Key == ConsoleKey.Enter)
                    {
                        Console.Clear();
                        break;;
                    }
                }
                server.Stop();
                client.Close();
            }
        }
예제 #2
0
        public void Network(string ip, int port)
        {
            Console.Clear();

            try
            {
                TcpClient     client        = new TcpClient(ip, port);
                NetworkStream str           = client.GetStream();
                byte[]        receiveBuffer = new byte[999];
                Display.WriteCyan(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 6, "Enter cords: ");
                while (!exit)
                {
                    try
                    {
                        if (CheckIfWin("X") == true)
                        {
                            Display.WriteYellow(menuX - 10, menuY + 6, "                                                          ");
                            Display.WriteYellow(menuX, menuY + 6, "Back");
                            Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "                        ");
                            Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "Winner: " + Winner);
                            while (true)
                            {
                                Program.ClearKeyBuffer();

                                ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                                if (keyInfo.Key == ConsoleKey.Enter)
                                {
                                    Display.WriteYellow(menuX - 10, menuY + 7, "                                                          ");
                                    client.Close();
                                    Console.Clear();
                                    return;
                                }
                            }
                        }
                        if (CheckIfWin("O") == true)
                        {
                            Display.WriteYellow(menuX - 10, menuY + 6, "                                                          ");
                            Display.WriteYellow(menuX, menuY + 6, "Back");
                            Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "                        ");
                            Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "Winner: " + Winner);
                            while (true)
                            {
                                Program.ClearKeyBuffer();

                                ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                                if (keyInfo.Key == ConsoleKey.Enter)
                                {
                                    Display.WriteYellow(menuX - 10, menuY + 7, "                                                          ");
                                    client.Close();
                                    Console.Clear();
                                    return;
                                }
                            }
                        }
                        Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 4, "                        ");
                        Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 4, "Your turn:");
                        Game.Start();
                        Game.Table();

                        byte[] sendData = new byte[999];

                        sendData = Encoding.ASCII.GetBytes(txtME);



                        str.Write(sendData, 0, sendData.Length);


                        string msg = "";


                        bool exit = false;
                        Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 4, "                        ");
                        Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 4, "Other player turn: ");
                        if (CheckIfWin("X") == true)
                        {
                            Display.WriteYellow(menuX - 10, menuY + 6, "                                                          ");
                            Display.WriteYellow(menuX, menuY + 6, "Back");
                            Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "                        ");
                            Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "Winner: " + Winner);
                            while (true)
                            {
                                Program.ClearKeyBuffer();

                                ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                                if (keyInfo.Key == ConsoleKey.Enter)
                                {
                                    Display.WriteYellow(menuX - 10, menuY + 7, "                                                          ");
                                    client.Close();
                                    Console.Clear();
                                    return;
                                }
                            }
                        }
                        if (CheckIfWin("O") == true)
                        {
                            Display.WriteYellow(menuX - 10, menuY + 7, "                                                          ");
                            Display.WriteYellow(menuX, menuY + 6, "Back");
                            Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "                        ");
                            Display.WriteRed(Console.WindowWidth / 2 - 15, Console.WindowHeight / 2 + 3, "Winner: " + Winner);
                            while (true)
                            {
                                client.Close();
                                Program.ClearKeyBuffer();

                                ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                                if (keyInfo.Key == ConsoleKey.Enter)
                                {
                                    Display.WriteYellow(menuX - 10, menuY + 7, "                                                          ");
                                    Console.Clear();
                                    return;
                                }
                            }
                        }
                        while (!exit)
                        {
                            str.Read(receiveBuffer, 0, receiveBuffer.Length);
                            msg = Encoding.ASCII.GetString(receiveBuffer, 0, receiveBuffer.Length);

                            if (msg.Contains("1 1"))
                            {
                                Game.first = "O";
                            }
                            if (msg.Contains("1 2"))
                            {
                                Game.second = "O";
                            }
                            if (msg.Contains("1 3"))
                            {
                                Game.third = "O";
                            }
                            if (msg.Contains("2 1"))
                            {
                                Game.fourth = "O";
                            }
                            if (msg.Contains("2 2"))
                            {
                                Game.fifth = "O";
                            }
                            if (msg.Contains("2 3"))
                            {
                                Game.sixth = "O";
                            }
                            if (msg.Contains("3 1"))
                            {
                                Game.seventh = "O";
                            }
                            if (msg.Contains("3 2"))
                            {
                                Game.eight = "O";
                            }
                            if (msg.Contains("3 3"))
                            {
                                Game.nineth = "O";
                            }

                            Game.Table();
                            exit    = true;
                            Game.ex = true;
                        }
                    }
                    catch (Exception e)
                    {
                        client.Close();
                        str.Close();
                        Console.Clear(); Display.WriteCyan(Console.WindowWidth / 2 - 10, Console.WindowHeight / 2 - 5, "The player exit...");
                        Display.WriteYellow(Console.WindowWidth / 2 - 5, Console.WindowHeight / 2 + 8, "Back");
                        while (true)
                        {
                            ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                            if (keyInfo.Key == ConsoleKey.Enter)
                            {
                                exit = true;

                                Console.Clear();

                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Clear(); Display.WriteCyan(Console.WindowWidth / 2 - 10, Console.WindowHeight / 2 - 5, "Server not found...");
                Display.WriteYellow(Console.WindowWidth / 2 - 5, Console.WindowHeight / 2 + 8, "Back");
                while (true)
                {
                    ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                    if (keyInfo.Key == ConsoleKey.Enter)
                    {
                        exit = true;

                        Console.Clear();

                        break;
                    }
                }
            }
        }