gameengine.java

来自「Java based Snakes and Ladders game.」· Java 代码 · 共 85 行

JAVA
85
字号
// The main game engine, this is where half the work takes placepublic class GameEngine{    // The Variables of the GameEngine        private Player player1, player2, currentPlayer;        private Player player;        private Dice dice;        private Gui gui;        private int playerCurrent;                // Set the game Array        private int[][] gameArray;    // The constructor of the GameEngine    public GameEngine(){        gui = new Gui();        // Settings player variables.        player1 = new Player("Player 1");        player2 = new Player("Player 2");        currentPlayer = player1;                playerCurrent = 1;        // this.player = currentPlayer; - Moved to RunGame        // Set the game Array size        gameArray = new int[49][2];                gameArray[0][1] = 0;        gameArray[1][1] = 5;    }    // Setting the game ready to play new    public void NewGame(){            }        // The methods of the GameEngine    public void RunGame(){        this.player = currentPlayer;        int playerSquare = player.playerRoll();                // Check for Snakes / Ladders / Competition        if(gameArray[playerSquare][1] == 1){            player.writePlayerPos( playerSquare + gameArray[playerSquare][2]);        }        if(gameArray[playerSquare][1] == 2){            player.writePlayerPos( playerSquare - gameArray[playerSquare][2]);        }        if(gameArray[playerSquare][1] == 3){            // END GAME HERE        }               gui.drawPlayer(playerSquare);                // Write the player possition        player.writePlayerPos(playerSquare);        changePlayer();        }        // The methods of the GameEngine    public void changePlayer(){        // Switch the players        if (currentPlayer == player1) {                currentPlayer = player2;                playerCurrent = 2;        } else if (currentPlayer == player2) {                currentPlayer = player1;                playerCurrent = 1;        } else {            // Check to make sure player = Legit                System.out.println ("Too Many Players");                System.exit (1);        }    }        public int playerCurrent(){        return playerCurrent;    }        public void debug(){        System.out.println(gameArray[1][1]);    }   }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?