Exemplo n.º 1
0
        public void StartClient(int portNumber, string hostAddress, string userName, Player.Player hostPlayer, Player.Player clientPlayer) //kanske ska vara static
        {
            var gameCommandHandler = new GameCommandHandler();
            var game       = GameRunner.Instance();
            var gameStatus = ("", true);

            StartListen(portNumber);
            var gameFlowHelper = new GameFlowHelper();
            var gameOver       = false;

            while (!gameOver)
            {
                Console.WriteLine("Waiting to connect");
                //var myTurn = false;
                //var hostUsername = "";
                //var responseToServer = "";
                //var myResponse = "";

                using (var client = new TcpClient(hostAddress, portNumber))
                    using (var networkStream = client.GetStream())
                        using (StreamReader reader = new StreamReader(networkStream, Encoding.UTF8))
                            using (var writer = new StreamWriter(networkStream, Encoding.UTF8)
                            {
                                AutoFlush = true
                            })
                            {
                                if (gameStatus.Item1 == "270")
                                {
                                    gameOver = true;
                                    client.GetStream().Close();
                                    networkStream.Close();
                                    break;
                                }
                                //Console.WriteLine("Started A Loop on Client");
                                var firstReplyIsCorrect = false;
                                while (client.Connected)
                                {
                                    var responseFromServer = "";
                                    while (!firstReplyIsCorrect)
                                    {
                                        var firstReply = "";
                                        //Console.WriteLine($"Connected to host at ip: {client.Client.RemoteEndPoint}");
                                        Console.WriteLine(reader.ReadLine());
                                        Console.WriteLine("Start the game by writing HELLO");
                                        var firstCommand = Console.ReadLine() + " " + userName;
                                        writer.WriteLine(firstCommand);
                                        firstReply = reader.ReadLine();
                                        if (firstReply.Split(' ')[0] == "220")
                                        {
                                            Console.WriteLine($"{firstReply}");
                                            game.player2.PlayerName = firstReply.Split(' ')[1];
                                            firstReplyIsCorrect     = true;
                                            Console.WriteLine("Enter START to start: ");
                                            var startCommand = Console.ReadLine();

                                            if (startCommand.ToUpper() == "QUIT")
                                            {
                                                startCommand = "270 Connection closed";
                                                gameOver     = true;
                                                writer.WriteLine(startCommand);
                                                break;
                                            }

                                            // Skicka text
                                            writer.WriteLine(startCommand);
                                        }

                                        if (!client.Connected)
                                        {
                                            break;
                                        }

                                        responseFromServer = reader.ReadLine();
                                        Console.WriteLine($"Svar: {responseFromServer}");
                                        if (string.Equals(responseFromServer.Split(' ')[0], "221", StringComparison.InvariantCultureIgnoreCase))
                                        {
                                            gameFlowHelper.StillMyTurn = true;
                                        }

                                        Console.WriteLine(responseFromServer);
                                    }


                                    // Läs minst en rad test
                                    do
                                    {
                                        if (gameFlowHelper.StillMyTurn)
                                        {
                                            Console.WriteLine("Your turn, enter command:");
                                            var myCommand = Console.ReadLine();
                                            writer.WriteLine(myCommand);
                                            responseFromServer = reader.ReadLine();
                                            gameFlowHelper.Last3Responses.Add(responseFromServer); //add last response
                                            if (gameFlowHelper.CheckForRepeatedErrors())
                                            {
                                                writer.WriteLine("QUIT");
                                                client.Close();
                                                gameOver = true;
                                                break;
                                            }
                                            gameFlowHelper.ResponsesAndCommands.Add(myCommand);
                                            gameFlowHelper.ResponsesAndCommands.Add(responseFromServer);
                                            //gameFlowHelper.Last3Responses.Add(responseFromServer);
                                            gameStatus = gameCommandHandler.ResponseSorter(responseFromServer, myCommand);
                                            if (gameStatus.Item1 == "260")
                                            {
                                                Console.WriteLine(responseFromServer);
                                                writer.WriteLine("QUIT");
                                                gameOver = true;
                                                client.Close();
                                                break;
                                            }
                                            if (gameStatus.Item1 == "270")
                                            {
                                                writer.WriteLine("QUIT");
                                                gameOver = true;
                                                client.Close();
                                                break;
                                            }
                                            //writer.WriteLine(myResponse); //need checks to see if turn is over or need to wait for next server turn (e.g. faulty input)
                                            if (!gameStatus.Item2)
                                            {
                                                gameFlowHelper.StillMyTurn = false;
                                            }
                                        }
                                        else
                                        {
                                            Console.WriteLine("Waiting for opponent move...");
                                            responseFromServer = reader.ReadLine();
                                            gameStatus         = gameCommandHandler.CommandSorter(responseFromServer.ToLower());
                                            //here we need gamestatus to know if their turn is over or if we need to continue our turn (loop around this)
                                            writer.WriteLine(gameStatus.Item1); //need checks to see if their turn is over or need to wait for next server turn (e.g. faulty input)
                                            gameFlowHelper.ResponsesAndCommands.Add(responseFromServer);
                                            gameFlowHelper.ResponsesAndCommands.Add(gameStatus.Item1);
                                            gameFlowHelper.Last3Responses.Add(gameStatus.Item1);
                                            if (gameStatus.Item1.Contains("260"))
                                            {
                                                writer.WriteLine("QUIT");
                                                gameOver = true;
                                                client.Close();
                                                break;
                                            }
                                            if (gameFlowHelper.CheckForRepeatedErrors())
                                            {
                                                writer.WriteLine("QUIT");
                                                gameOver = true;
                                                client.Close();
                                                break;
                                            }
                                            if (gameStatus.Item2)
                                            {
                                                gameFlowHelper.StillMyTurn = true;
                                            }
                                        }
                                        game.PrintBothGameBoards();
                                        gameFlowHelper.PrintLast3Responses();
                                    } while (networkStream.DataAvailable);
                                    //gameOver = true;
                                    //client.Dispose();
                                }
                                gameOver = true;
                                break;
                            }
            }
        }
Exemplo n.º 2
0
        public void StartServer(int port, string hostUsername)
        {
            var game = GameRunner.Instance();
            var gameCommandHandler = new GameCommandHandler();

            StartListen(port);
            //var myTurn = false;
            var responseFromClient = "";
            var gameFlowHelper     = new GameFlowHelper();
            var gameStatus         = ("", true);
            var gameOver           = false;

            while (!gameOver)
            {
                Console.WriteLine("Waiting for player to connect...");

                using (var client = listener.AcceptTcpClient())
                    using (var networkStream = client.GetStream())
                        using (StreamReader reader = new StreamReader(networkStream, Encoding.UTF8))
                            using (var writer = new StreamWriter(networkStream, Encoding.UTF8)
                            {
                                AutoFlush = true
                            })
                            {
                                if (gameStatus.Item1 == "270")
                                {
                                    gameOver = true;
                                    client.GetStream().Close();
                                    networkStream.Close();
                                    break;
                                }
                                var firstCommandFromClientIsHello = false;
                                var clientUserName = "";
                                var firstCommand   = "";
                                while (client.Connected)
                                {
                                    //Console.WriteLine($"Player has connected with ip: {client.Client.RemoteEndPoint}!");
                                    //Console.WriteLine("Started A Loop on Server");
                                    while (!firstCommandFromClientIsHello)
                                    {
                                        writer.WriteLine("210 BATTLESHIP/1.0");
                                        firstCommand = reader.ReadLine();
                                        if (firstCommand.ToUpper() == "QUIT")
                                        {
                                            firstCommand = "270 Connection closed";
                                            writer.WriteLine(firstCommand);
                                            gameOver = true;
                                            break;
                                        }
                                        if (!firstCommand.ToLower().Contains("hello") && !firstCommand.ToLower().Contains("helo"))
                                        {
                                            writer.WriteLine("500 Syntax Error");
                                        }

                                        if (firstCommand.ToLower().Contains("helo") || firstCommand.ToLower().Contains("hello"))
                                        {
                                            clientUserName                = firstCommand.Split(' ')[1];
                                            game.player2.PlayerName       = clientUserName;
                                            firstCommandFromClientIsHello = true;
                                            writer.WriteLine("220 " + hostUsername);
                                        }

                                        var command = reader.ReadLine().ToLower();
                                        Console.WriteLine($"Recieved: {command}");
                                        var responseToSend = gameCommandHandler.CommandSorter(command.ToLower()).Item1;

                                        if (string.Equals(responseToSend.Split(' ')[0], "222", StringComparison.InvariantCultureIgnoreCase))
                                        {
                                            gameFlowHelper.StillMyTurn = true;
                                        }

                                        writer.WriteLine(responseToSend);
                                    }


                                    var myCommand = "";
                                    if (gameFlowHelper.StillMyTurn)
                                    {
                                        Console.WriteLine("Your turn, enter command:");
                                        myCommand = Console.ReadLine();
                                        writer.WriteLine(myCommand);
                                        responseFromClient = reader.ReadLine();
                                        gameFlowHelper.Last3Responses.Add(responseFromClient); //add last response
                                        gameFlowHelper.CheckForRepeatedErrors();
                                        gameFlowHelper.ResponsesAndCommands.Add(myCommand);
                                        gameFlowHelper.ResponsesAndCommands.Add(responseFromClient);
                                        if (gameFlowHelper.CheckForRepeatedErrors())
                                        {
                                            writer.WriteLine("270 Connection closed");
                                            gameOver = true;
                                            break;
                                        }
                                        gameStatus = gameCommandHandler.ResponseSorter(responseFromClient, myCommand);
                                        if (gameStatus.Item1.Contains("260"))
                                        {
                                            writer.WriteLine("270 BYE BYE");
                                            client.Dispose();
                                            gameOver = true;
                                            break;
                                        }
                                        if (gameStatus.Item1 == "270")
                                        {
                                            break;
                                        }
                                        if (!gameStatus.Item2)
                                        {
                                            gameFlowHelper.StillMyTurn = false;
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("Waiting for opponent move...");
                                        responseFromClient = reader.ReadLine();
                                        gameStatus         = gameCommandHandler.CommandSorter(responseFromClient.ToLower());
                                        writer.WriteLine(gameStatus.Item1);
                                        gameFlowHelper.Last3Responses.Add(responseFromClient);
                                        gameFlowHelper.ResponsesAndCommands.Add(responseFromClient);
                                        gameFlowHelper.ResponsesAndCommands.Add(gameStatus.Item1);
                                        if (gameStatus.Item1.Contains("270"))
                                        {
                                            writer.WriteLine("270 BYE BYE");
                                            client.Dispose();
                                            gameOver = true;
                                            break;
                                        }
                                        if (gameStatus.Item1.Contains("260"))
                                        {
                                            writer.WriteLine("270 BYE BYE");
                                            client.Dispose();
                                            gameOver = true;
                                            break;
                                        }
                                        if (gameFlowHelper.CheckForRepeatedErrors())
                                        {
                                            writer.WriteLine("270 Connection closed");
                                            gameOver = true;
                                            networkStream.Close();
                                            break;
                                        }
                                        if (gameStatus.Item2)
                                        {
                                            gameFlowHelper.StillMyTurn = true;
                                        }
                                    }

                                    game.PrintBothGameBoards();
                                    gameFlowHelper.PrintLast3Responses();
                                }
                                gameOver = true;
                                client.Dispose();
                                break;
                            }
                //gameOver = true;
                //break;
            }
        }