/** * Creates a new "game" from the current engine.Globals.game variable. While the ANN stays the * same, the speed, actor positions, score, et cetera, are all reset. */ public void newGame() { stopped = true; player.setLives(STARTING_LIVES); player.setScore(0); Graphics g = strategy.getDrawGraphics(); waiting = true; Globals.state = ENEMY_HUNTER_STATE; player.setCenter(board.getPlayerStartPoint()); player.setDirection(Player.DEFAULT_STARTING_DIRECTION); board.reset(); Globals.blipsLeft = Globals.game.getBoard().getBlipCount(); for (Enemy enemy : enemies) { enemy.reset(); } GamePainters.drawBoard(board, g, this); GamePainters.drawEnemies(enemies, g, this); GamePainters.drawPlayer(player, g, this); GamePainters.paintBottomBar(player, board, g, this); strategy.show(); }
public void startMatch() { if (antherGame(R.StartQuestion) == n) { return; } player1 = new Player(1, R.player1Name, false, R.player1Mark); player2 = new Player(2, R.player2Name, true, R.player2Mark); board = new Board(player1, player2); display.out(R.instructions); display.out(R.numPad); display.out(R.begin); do { board.reset(); Player winningPlayer = startGame(); winningPlayer.addWin(); display.score(winningPlayer, player1, player2); display.out(board.toString()); } while (antherGame(R.startAnotherGame) != R.n); display.out(R.gameOver); ; }
/** * Resets the game. <br> * The board is emptied and player[0] becomes the current player. */ private void reset() { current = 0; board.reset(); }
public int playGame() { int winner = Board.EMPTY; red.setColor(Board.RED); black.setColor(Board.BLACK); turn = Board.BLACK; checkersBoard.reset(); checkersBoard.print(); gameTime = 0; int numPieces = 24; while (winner == Board.EMPTY) { int currentPieces = checkersBoard.blackPieces() + checkersBoard.redPieces(); int[] move; int legal; // tests time for draw if (currentPieces == numPieces) { gameTime++; if (gameTime > 20) return Board.EMPTY; } else { numPieces = currentPieces; } if ((checkersBoard.blackPieces() > 0) && (checkersBoard.redPieces() == 0)) { winner = Board.BLACK; System.out.println("GAME STOPPED NO PIECES"); return winner; } move = black.nextMove(checkersBoard); // System.out.println(move[0]+" "+move[1]+" "+move[2]+" "+move[3]); if ((move[0] == 0) && (move[1] == 0) || (move[2] == 0) && (move[3] == 0)) { System.out.println("GAME STOPPED NULL MOVE"); return Board.RED; } /*legal = checkersBoard.ApplyMove(move[0],move[1],move[2],move[3]); if(legal == Board.ILLEGALMOVE) { System.out.println("GAME STOPPED ILLEGAL MOVE"); return Board.RED; } */ checkersBoard.move_board(move); checkersBoard.print(); if ((checkersBoard.redPieces() > 0) && (checkersBoard.blackPieces() == 0)) { System.out.println("GAME STOPPED NO PIECES"); winner = Board.RED; return winner; } move = red.nextMove(checkersBoard); // System.out.println(move[0]+" "+move[1]+" "+move[2]+" "+move[3]); if ((move[0] == 0) && (move[1] == 0) || (move[2] == 0) && (move[3] == 0)) { System.out.println("GAME STOPPED NULL MOVE"); return Board.BLACK; } /* legal = checkersBoard.ApplyMove(move[0],move[1],move[2],move[3]); if(legal == Board.ILLEGALMOVE) { System.out.println("GAME STOPPED ILLEGAL MOVE"); return Board.BLACK; } */ checkersBoard.move_board(move); checkersBoard.print(); } return winner; }
/** * This starts the main game loop. This is the guts of the game. When the loop starts it checks to * see if the player has any lives left, if not, it prints "Game Over" and resets the game. If * there are lives left, it decides what the ghosts next move will be, detects any collisions, and * acts accordingly, and also gets input from the player */ public void start() { Globals.state = ENEMY_HUNTER_STATE; Globals.game.setPlayer(new Player()); player = Globals.game.getPlayer(); player.setCenter(board.getPlayerStartPoint()); newGame(); paused = false; waiting = true; Point regenDoorPoint = board.getRegenDoor(); Point regenPoint = board.getRegenPoint(); Point playerPoint = player.getCenter(); while ((this.isVisible()) && (player.getLives() >= 0)) { if (paused) { paintPauseScreen(); } while (paused || stopped || waiting) { Thread.yield(); } if (Globals.blipsLeft <= 0) { Globals.speed *= SPEED_MOD; resetCanvas(); board.reset(); Globals.blipsLeft = board.getBlipCount(); } // now we need to see if the current state is enemy hunted if (Globals.state == ENEMY_HUNTED_STATE) { // now we need to see if we should go out of it if (Globals.timeUntilLeaveingHuntedState <= 0) { Globals.state = ENEMY_HUNTER_STATE; Globals.timeUntilLeaveingHuntedState = TIME_IN_HUNTED_STATE; } else { // we shouldn't, so we decrement the counter Globals.timeUntilLeaveingHuntedState -= Globals.speed; } } // move the player player.move(board.getMoveOptions(player), playerChoice); if (Globals.aiMode == FSA_AI_MODE) { // now updated the enemy movements for (int i = 0; i < enemies.length; i++) { // these are put here to reduce program jumping and cache misses as well Enemy enemy = enemies[i]; int moveOptions = board.getMoveOptions(enemy); // first, we need to take apporpriate actions if we're on a regen tile if (board.getCurrentTile(enemy).isRegen()) { // first check to see if the enemy is alive if (!enemy.isAlive()) { // it's not, so we make it alive enemy.setAlive(true); } // now we tell the enemy to move towards the the door enemy.move(moveOptions, regenDoorPoint, true); } else { // We're not in the regen pen, so now we check if the enemy is alive if (!enemy.isAlive()) { // it's not, so we want to go towards the regen point enemy.move(moveOptions, regenPoint, false); } else { // the enemy is alive, so we tell it towards/away from the player enemy.move(moveOptions, playerPoint, false); } } } } else if (Globals.aiMode == ANN_AI_MODE) { // Globals.game.getNet().process(ANNTools.getBoardCondition(board, enemies, player)); player.setInputValues(board); for (Enemy enemy : enemies) { enemy.setInputValues(board); } Globals.game.getNet().generateActivations(); for (int i = 0; i < enemies.length; i++) { // Last 2 values don't matter here enemies[i].move( board.getMoveOptions(enemies[i]), board.getRegenDoor(), board.getCurrentTile(enemies[i]).isRegen()); } } // no we check for collisions and act appropriately checkCollision(); // check to see if we should notify the game that we have stepped if (Globals.trainingMode != TRAINING_MODE_OFF) { // we should, so we do. Globals.game.step(); } // And now we sleep. The sleep time is the current speed try { Thread.sleep(Globals.speed); } catch (InterruptedException e) { break; } // no update the screen update(); } if (player.getLives() <= 0) { paintGameOver(); try { Thread.sleep(2500); } catch (InterruptedException e) { // DO nothing } newGame(); start(); } }