Beispiel #1
0
        //method that launches a host or player game depending on input
        private static void manageGame()
        {
            //take in user input
            Console.WriteLine("Host(1) or Player(2)? Quit(3)");
            string status = Console.ReadLine();

            //if they choose 1, they want to be a host
            if (status.Equals("1", StringComparison.InvariantCultureIgnoreCase))
            {
                //create client object for the host, initialize the board, and call on hostGame
                Player_Client host = new Player_Client();
                initBoard();
                hostGame(host);
            }
            //if they choose 2, they want to be a connecting player
            else if (status.Equals("2", StringComparison.InvariantCultureIgnoreCase))
            {
                //create client object for the player, initialize the board, and call on clientGame
                Player_Client client = new Player_Client();
                initBoard();
                clientGame(client);
            }
            //any other input should take them back to the main menu
            else
            {
                return;
            }
        }
Beispiel #2
0
        //method to run a client game that takes in a player_client object to manage the netcode
        private static void clientGame(Player_Client client)
        {
            //ask if they want to do a LAN or online game
            Console.WriteLine("LAN(1) or Online(2)");
            string input = Console.ReadLine();

            //if they want to do a lan game
            if (input.Equals("1", StringComparison.InvariantCultureIgnoreCase))
            {
                //status of game initialization and bool to keep track of the "bad port" message
                int  initStatus = -1;
                bool retry      = false;

                //while the initStatus is incomplete
                while (initStatus == -1)
                {
                    //if we've taken an input and the port failed, print out bad port
                    if (retry == true)
                    {
                        Console.WriteLine("Bad Port");
                    }

                    //call on startLocal() which connects to to a game based on the port number given
                    initStatus = client.startLocal();

                    //if the client was able to initialize, exit
                    if (initStatus == 0)
                    {
                        break;
                    }
                    retry = true;
                }
            }

            //if they want to join an online game
            else if (input.Equals("2", StringComparison.InvariantCultureIgnoreCase))
            {
                int  initStatus = -1;
                bool retry      = false;

                //while the server name is bad
                while (initStatus == -1)
                {
                    //print bad server name if we've retried atleast once
                    if (retry == true)
                    {
                        Console.WriteLine("Bad Server Name");
                    }

                    //take server name input
                    Console.WriteLine("Input Server Name: (ex: csslab11.uwb.edu)");
                    string temp = Console.ReadLine();

                    //start a remote instance and see if it completes
                    initStatus = client.startRemote(temp);

                    //if the initialization was successful
                    if (initStatus == 0)
                    {
                        //recieve the rooms from the server
                        int[] rooms = client.recieveRooms();

                        //display available rooms and ask for a choice
                        Console.WriteLine("What room would you like to choose?");
                        for (int i = 0; i < rooms.Length; i += 2)
                        {
                            Console.WriteLine("Room: " + (rooms[i] + 1) + " Slots Open: " + (rooms[i + 1]));
                        }

                        string roomChoice = "";

                        //while the room entered is invalid
                        while (true)
                        {
                            //take in the room choice
                            roomChoice = Console.ReadLine();
                            int roomChoiceInt;

                            //check if the given is an integer
                            bool isInt = int.TryParse(roomChoice, out roomChoiceInt);

                            //if it is an integer, check the value
                            if (isInt)
                            {
                                //check if the room choice is a valid one
                                bool valid = checkRoomChoice(rooms, roomChoiceInt);

                                //if the choice is either invalid or outside of the range, loop again
                                if ((valid == false) || (roomChoiceInt > 5 || roomChoiceInt < 0))
                                {
                                    Console.WriteLine("Bad Room Number, Pick again");
                                    continue;
                                }
                            }
                            else if (isInt == false)
                            {
                                Console.WriteLine("Enter an integer");
                                continue;
                            }

                            //if we get a valid input, break
                            break;
                        }

                        //send the room choice to the server
                        client.sendRoom(roomChoice[0]);

                        //exit the loop
                        Console.WriteLine("Game Started");
                        break;
                    }

                    retry = true;
                }

                Console.WriteLine("Waiting for opponent");
            }

            bool game = true;

            //while game is running
            while (game)
            {
                //create array for board
                char[] boardToSend = new char[9];

                //recieve the board and update the board, recieveBoard freezes user while waiting for return
                char[] returnBoard = client.recieveBoard();

                //if the return board is null, then they have quit the game
                if (returnBoard == null)
                {
                    Console.WriteLine("Opponent has quit, ending game");
                    return;
                }

                //if the board is populated, update the board locally
                updateBoard(returnBoard);

                //check if the board is a winning board
                char winner = checkWin();

                //if the winner is x, close the game
                if (winner == 'x')
                {
                    Console.WriteLine(createDisplay() + "\nOpponent Wins\nClosing connection");
                    return;
                }

                //if there is a draw, close the game
                if (winner == 'd')
                {
                    Console.WriteLine(createDisplay() + "\nDraw");
                    return;
                }

                //take user input for position they wanna fill
                int check = displayBoard();

                //update board to put an 'x' where user inputs
                populateBoard(ref boardToSend, check, "client");

                //check to see if the user's input was a winning input
                winner = checkWin();

                //if the client is the winner, send the winning board
                if (winner == 'o')
                {
                    client.sendWin(boardToSend);
                    Console.WriteLine(createDisplay() + "\nYou Win!");
                    return;
                }

                //if it is a draw, send a draw board
                if (winner == 'd')
                {
                    client.sendDraw(boardToSend);
                    Console.WriteLine(createDisplay() + "\nDraw");
                    return;
                }

                //if the board isnt a win, loss, or draw, send the board naturally
                bool sendStatus = client.sendBoard(boardToSend);

                //if the send was failed, they quit the game
                if (sendStatus == false)
                {
                    Console.WriteLine("\nOppenent has quit, closing game");
                    return;
                }
            }
        }
Beispiel #3
0
        //function to handle a host player
        private static void hostGame(Player_Client host)
        {
            Console.WriteLine("LAN(1) or Online(2)");
            string input = Console.ReadLine();

            if (input.Equals("1", StringComparison.InvariantCultureIgnoreCase))
            {
                host.hostLocal();
            }
            else if (input.Equals("2", StringComparison.InvariantCultureIgnoreCase))
            {
                int  initStatus = -1;
                bool retry      = false;

                //while the server name is bad
                while (initStatus == -1)
                {
                    //print bad server name if we've retried atleast once
                    if (retry == true)
                    {
                        Console.WriteLine("Bad Server Name");
                    }

                    //take server name input
                    Console.WriteLine("Input Server Name: (ex: csslab11.uwb.edu)");
                    string temp = Console.ReadLine();

                    //if the status is successful
                    initStatus = host.startRemote(temp);
                    if (initStatus == 0)
                    {
                        int[] rooms = host.recieveRooms();
                        Console.WriteLine("What room would you like to choose?");
                        for (int i = 0; i < rooms.Length; i += 2)
                        {
                            Console.WriteLine("Room: " + (rooms[i] + 1) + " Slots Open: " + (rooms[i + 1]));
                        }
                        string roomChoice = "";

                        //while the room entered is invalid
                        while (true)
                        {
                            //take in the room choice
                            roomChoice = Console.ReadLine();
                            int roomChoiceInt;

                            //check if the given is an integer
                            bool isInt = int.TryParse(roomChoice, out roomChoiceInt);

                            //if it is an integer, check the value
                            if (isInt)
                            {
                                //check if the room choice is a valid one
                                bool valid = checkRoomChoice(rooms, roomChoiceInt);

                                //if the choice is either invalid or outside of the range, loop again
                                if ((valid == false) || (roomChoiceInt > 5 || roomChoiceInt < 0))
                                {
                                    Console.WriteLine("Bad Room Number, Pick again");
                                    continue;
                                }
                            }
                            else if (isInt == false)
                            {
                                Console.WriteLine("Enter an integer");
                                continue;
                            }

                            //if we get a valid input, break
                            break;
                        }

                        host.sendRoom(roomChoice[0]);

                        Console.WriteLine("Game Started");
                        break;
                    }

                    retry = true;
                }
            }

            bool game = true;

            while (game)
            {
                //create array for board
                char[] boardToSend = new char[9];

                //take user input for position they wanna fill
                int check = displayBoard();

                //update board to put an 'x' where user inputs
                populateBoard(ref boardToSend, check, "host");

                char winner = checkWin();
                if (winner == 'x')
                {
                    host.sendWin(boardToSend);
                    Console.WriteLine(createDisplay() + "\nYou Win!");
                    return;
                }

                if (winner == 'd')
                {
                    host.sendDraw(boardToSend);
                    Console.WriteLine(createDisplay() + "\nDraw");
                    return;
                }

                //send the board
                bool sendStatus = host.sendBoard(boardToSend);
                if (sendStatus == false)
                {
                    Console.WriteLine("\nOppenent has quit, closing game");
                    return;
                }

                //recieve the board and update the board, recieveBoard freezes user while waiting for return
                char[] returnBoard = host.recieveBoard();
                if (returnBoard == null)
                {
                    Console.WriteLine("\nOpponent has quit, ending game");
                    return;
                }

                updateBoard(returnBoard);

                winner = checkWin();
                if (winner == 'o')
                {
                    Console.WriteLine(createDisplay() + "\nOpponent Wins\nClosing connection");
                    return;
                }

                if (winner == 'd')
                {
                    Console.WriteLine(createDisplay() + "\nDraw");
                    return;
                }
            }
        }