Exemple #1
0
        public Game_Generic(int x_size, int y_size)
        {
            gameBoard      = new int[x_size, y_size];
            gamePieces     = new List <Piece_Generic>();
            currentPlayers = new List <Player>();
            loop           = new Server_GameLoop();
            gameType       = "generic";
            maxPlayers     = 2;

            // Player 1 - temp -

            Piece_Generic piece = new Piece_Generic();

            piece.setX(2);
            piece.setY(2);
            piece.setValue(0)


            // Player 2 - temp -

            Piece_Generic piece2 = new Piece_Generic();

            piece2.setX(3);
            piece2.setY(3);
            piece2.setValue(1)

            // Add to board
            gamePieces.Add(piece1);
            gamePieces.Add(piece2);
        }
        // handlePlayerTurn METHOD THAT OVERRIDES THE ONE IN GENERIC GAME
        // Returns true always.
        // TODO: How do we end the game? Do we change the send packet so that the second character after the delim is the player's assigned number?

        public override bool handlePlayerTurn(String s) // each player will call this function with their input.
        {
            Console.Write("It is currently this player's turn: " + loop.getActivePlayer());

            Console.Write("Recieved: " + s + " from client. Handling message...");

            String[] move = s.Split('%');                               // Split the string by the delimiter.

            int placeX = System.Convert.ToInt32(move[0]);               // Convert Strings to int for the placeX

            int placeY = System.Convert.ToInt32(move[1]);               // Technically we don't need this... TODO: is there something we can do?

            int thePlayer = loop.getActivePlayer();                     // should return either 0 or 1 provided server checked to make sure there are 2 players.

            Piece_Generic piece = new Piece_Generic();

            piece.setX(placeX);

            if (thePlayer == 0)
            {
                piece.setValue(1);      // value of piece is 1, indicating player 1
            }

            else
            {
                piece.setValue(2);      // value of piece is 2, indicating player 2
            }

            // add the piece

            gamePieces.Add(piece);

            // assignPiece to the place; if it is valid, the method will go ahead and place it.
            // On a not successful attempt, placeY will instead be null.

            placeY = assignPiece(placeX, placeY, piece.getValue());                // assignPiece will "attempt" to place the piece there. it will then return the placeY value.

            // get input on where the piece has been placed and save it to gameBoard, check that the spot is already occupied.

            if (placeY == -1)
            {
                // invalid move, return early.
                return(false);
            }

            else if (placeY != -1)                                           // if placeY is not the error code
            {
                // Set the "piece" of the player to be their last move so it is printed out. (Unecessary, but shows us something)
                gamePieces[thePlayer].setX(placeX);
                gamePieces[thePlayer].setY(placeY);

                if (checkGameState(placeX, placeY))                     // using the recently placed piece, check if the state is a win condition.
                {
                    Console.Write("[CONNECT 4] Game is over! Player: " + (loop.getActivePlayer() + 1) + " is the victor!");
                    // mark gameState as false, which will in turn trigger a specific message that will be sent.
                    gameState = false;
                }
            }
            return(true); // the move was not valid
        }