コード例 #1
0
        { // start thread for TCP listener
            public void addPlayers()
            {
                while (true) // Only add players
                {
                    // count by t
                    for (int t = 0; t < games.Count; t++)
                    {
                        if (!games[t].getGame().getGameState())
                        {
                            games[t].getThread().Abort();
                        }
                    }

                    Socket sock = listener.AcceptSocket();          // accept the sock

                    Console.WriteLine("   player socket has accepted the socket ");

                    NetworkStream nws = new NetworkStream(sock);
                    StreamReader  sr  = new StreamReader(nws);
                    StreamWriter  sw  = new StreamWriter(nws);
                    sw.AutoFlush = true;

                    Player p = new Player(nws, sock, sr, sw);       // construct a player object
                    activePlayers.Add(p);                           // Add it to the list of active players

                    int playerNumber = activePlayers.IndexOf(p);

                    Console.WriteLine("new player number " + playerNumber.ToString());
                    // message 1
                    p.getPlayerWriter().WriteLine(playerNumber.ToString());

                    // message 2
                    String   startMessage = p.getPlayerReader().ReadLine();
                    String[] data         = startMessage.Split(delim);

                    /*
                     *
                     * if( login was successful)
                     *  startMessage = "Login was successful.";
                     *
                     * else if ( new user was created)
                     *  startMessage = "New User created.";
                     *
                     * else // login attempt failed
                     *  startMessage = "Login attempt failed. Try again.";
                     *
                     * p.getPlayerWriter().WriteLine
                     *
                     */

                    // username is data[0]
                    // password is data[1]
                    // gameType is data[2]

                    p.setUserName(data[0]);

                    string loginMessage = " ";

                    while (loginMessage[0] != 'W')
                    {
                        if (db.attemptToLogin(data[0], data[1]))
                        {
                            loginMessage = "Welcome back!";
                        }

                        else if (db.addNewPlayer(data[0], data[1]))
                        {
                            loginMessage = "New User created. We hope you enjoy our game!";
                        }

                        else
                        {
                            loginMessage = "Login attempt failed. Try again.";
                        }

                        // message 3
                        p.getPlayerWriter().WriteLine(loginMessage);

                        System.Threading.Thread.Sleep(1500);

                        if (loginMessage[0] != 'W')
                        {
                            p.getPlayerWriter().WriteLine(playerNumber.ToString());

                            startMessage = p.getPlayerReader().ReadLine();
                            data         = startMessage.Split(delim);
                        }


                        System.Threading.Thread.Sleep(500);
                    }

                    Console.WriteLine("message 3 sent");

                    p.setGame(data[2]);

                    int gameToJoin = getIndexOfGameToJoin(p);

                    Console.WriteLine("game to join " + gameToJoin);

                    String newPlayerNumber = "0";
                    String gameToPlay      = "";

                    // there is no existing game of the type that the player wants to play
                    // that also has room for an additional player

                    Game_Generic newGame = null;
                    GameThread   gt      = null;

                    if (gameToJoin == -1)
                    {
                        // newPlayerNumber = "1";

                        Console.WriteLine("THIS IS WHAT I'M GET: " + p.getGame());

                        if (p.getGame() == "generic")
                        {
                            newGame = new Game_Generic(8, 8);
                        }

                        else if (p.getGame() == "connectFour")
                        {
                            newGame = new Game_ConnectFour();
                        }

                        else if (p.getGame() == "othello")
                        {
                            newGame = new Game_Othello();
                        }

                        else if (p.getGame() == "snakesLadders")
                        {
                            // newGame = new Game_SnakesLadders();
                        }

                        // games.Add(newGame);

                        gameToPlay = p.getGame();

                        gt = new GameThread(newGame);
                        games.Add(gt);

                        gameToJoin = 0;
                    }

                    // found a matching game type that needs an additional player
                    else
                    {
                        newGame         = games[gameToJoin].getGame();
                        newPlayerNumber = games[gameToJoin].getGame().getNumberPlayers().ToString();
                        // newGame.addPlayer(p);
                    }

                    Console.WriteLine("Finished adding player.");

                    p.setPlayerNumber(Convert.ToInt32(newPlayerNumber));
                    newGame.addPlayer(p);

                    if (gameToJoin >= 0)
                    {
                        Console.WriteLine("Number of players check " + games[gameToJoin].getGame().getNumberPlayers().ToString() + " ?? " + games[gameToJoin].getGame().getMaxPlayers().ToString());
                    }


                    if (newGame.getNumberPlayers() == newGame.getMaxPlayers())
                    {
                        Console.WriteLine("Starting a new game.");

                        Thread gameThread = new Thread(new ThreadStart(games[gameToJoin].playGame));
                        Console.WriteLine("Attempting to run gameThread.");
                        gameThread.Start();
                        games[gameToJoin].setThread(gameThread);
                    }

                    startMessage = newPlayerNumber;

                    // the REAL message 4
                    // start string is constructed to tell the client which game to start
                    p.getPlayerWriter().WriteLine(startMessage);

                    Console.WriteLine("Writing this message to client." + startMessage);
                }
            }
コード例 #2
0
 public GameThread(Game_Generic newGame)
 {
     Console.WriteLine("CurrentGame has been set to: " + newGame.getGameType());
     currentGame = newGame;
 }
コード例 #3
0
        public void gameLoop(Game_Generic game)
        {
            // needs number of players
            // needs the Player object for each player in the game
            //      which includes stream reader/writer
            //
            int numberPlayers = game.getPlayers().Count;

            // turn = 0;

            Console.WriteLine("Starting game loop!");

            // should be message 5
            sendToAllPlayers(game.getPlayers(), numberPlayers, turn.ToString() + "&" + activePlayer.ToString() + "&&Starting Turn&1%2%1#2%1%2*");

            while (game.getGameState())
            {
                System.Threading.Thread.Sleep(1500);

                turn++;

                // String turnMessage = turn.ToString() + "&" + activePlayer.ToString() + "&&Starting Turn";
                // Console.WriteLine(turnMessage);

                // System.Threading.Thread.Sleep(1500);

                // message 6
                // game.getPlayers()[activePlayer].getPlayerWriter().WriteLine("It is your turn. Make a move.");

                //Console.WriteLine("You are located at " + game.getPieces()[activePlayer].getX().ToString() + " " + game.getPieces()[activePlayer].getY().ToString() );

                String move = game.getPlayers()[activePlayer].getPlayerReader().ReadLine();

                // player tried to make an invalid move. Force them to try again until they send a valid move
                // the condition of the while loop makes the move.
                while (!game.handlePlayerTurn(move))
                {
                    //String notValid = "Player " + activePlayer.ToString() + " attempted a move that was not valid.";
                    //sendToAllPlayers(game.getPlayers(), numberPlayers, notValid);
                    String notValid = turn + "&0&0&INVALID&0%0%0%";
                    // construct a string that contains the turn number and the "invalid" message.
                    sendToAllPlayers(game.getPlayers(), numberPlayers, notValid);

                    System.Threading.Thread.Sleep(6500);

                    move = game.getPlayers()[activePlayer].getPlayerReader().ReadLine();
                }



                Console.WriteLine("The number of players is " + numberPlayers.ToString());

                string currentX = game.getPieces()[activePlayer].getX().ToString();
                string currentY = game.getPieces()[activePlayer].getY().ToString();

                // players need to be told who the next active player is, along with the move of the current active player
                // this is crude, but since the passbyref is confusing me.. (i mean, it works)

                String toSend = game.generateMoveString(turn, getNextPlayerIndex(activePlayer, game.getMaxPlayers()), currentX, currentY, move);
                Console.WriteLine("String sent: " + toSend);
                // toSend should now hold the string.
                // if the move was valid, then it was made when handlePlayerTurn is called
                // notify all players that a valid move was made
                sendToAllPlayers(game.getPlayers(), numberPlayers, toSend);
                System.Threading.Thread.Sleep(6500);

                // increment the value of active player to the next player
                activePlayer = getNextPlayerIndex(activePlayer, game.getMaxPlayers());

                Console.WriteLine(" New Current player turn " + activePlayer);

                // This is Jason2 - I CLAIM CREDIT FOR THIS HORRIBLE SOLUTION
                //break;
            }
        }