예제 #1
0
  private int min(Game game, int depth) {

    if (timeIsUp) return INF;

    if (depth == 0 || game.isGameOver()) {
      leavesVisitedCnt++;
      leavesEvaluatedCnt++;
      int posValue = game.getPlayerOnPTurn().positionValue();
      if (winValue != -1 && (posValue >= winValue || posValue <= -winValue)) posValue *= depth + 1;

      return posValue;
    } else {
      ArrayList<Move> moveList = game.getPlayerOnTurn().generateMoves();
      int minValue = 0;
      for (int i = 0; i < moveList.size(); i++) {
        game.getPlayerOnTurn().makeMove(moveList.get(i));
        game.moveMade();

        int value = max(game, depth - 1);
        if (i == 0) {
          minValue = value;
        } else {
          if (value < minValue) {
            minValue = value;
          }
        }
        game.getPlayerOnTurn().undoMove(moveList.get(i));
        game.moveUndo();
      }
      return minValue;
    }
  }
예제 #2
0
  @Override
  public void nextStepForAllGames() {
    lock.writeLock().lock();
    try {
      for (Game game : games) {
        if (game.isGameOver()) {
          game.newGame();
        }
        game.tick();
      }

      HashMap<Player, PlayerData> map = new HashMap<Player, PlayerData>();
      for (int i = 0; i < games.size(); i++) {
        Game game = games.get(i);

        Player player = players.get(i);

        map.put(
            player,
            new PlayerData(
                gameType.getBoardSize(),
                decoder.encode(game.getBoardAsString()),
                player.getScore(),
                game.getMaxScore(),
                game.getCurrentScore(),
                player.getCurrentLevel() + 1,
                player.getMessage()));
      }

      screenSender.sendUpdates(map);

      for (int index = 0; index < players.size(); index++) {
        Player player = players.get(index);
        Game game = games.get(index);
        try {
          String board = game.getBoardAsString().replace("\n", "");

          if (logger.isDebugEnabled()) {
            logger.debug(String.format("Sent for player '%s' board \n%s", player, board));
          }

          controllers.get(index).requestControl(player, board);
        } catch (IOException e) {
          logger.error(
              "Unable to send control request to player "
                  + player.getName()
                  + " URL: "
                  + player.getCallbackUrl(),
              e);
        }
      }
    } catch (Error e) {
      e.printStackTrace();
      logger.error("nextStepForAllGames throws", e);
    } finally {
      lock.writeLock().unlock();
    }
  }
예제 #3
0
  public static int evaluate(Game game, char player) {
    if (game.isGameOver()) {
      for (int i = 0; i < Board.ROWS; i++) {
        for (int j = 0; j < Board.COLUMNS; j++) {
          if (game.getBoard().matrix[i][j] != Board.EMPTY) {
            int piece = game.getBoard().matrix[i][j];
            if (piece == Board.BLACK_SOLDIER || piece == Board.BLACK_KING) {
              if (player == Move.BLACK) {
                return 100;
              } else {
                return -100;
              }
            } else if (piece == Board.RED_SOLDIER || piece == Board.RED_KING) {
              if (player == Move.RED) {
                return 100;
              } else {
                return -100;
              }
            }
          }
        }
      }
    } else {
      int numBlackKings = 0, numRedKings = 0;
      int numBlackPieces = 0, numRedPieces = 0;

      for (int i = 0; i < Board.ROWS; i++) {
        for (int j = 0; j < Board.COLUMNS; j++) {
          int piece = game.getBoard().matrix[i][j];
          if (piece != Board.EMPTY) {
            if (piece == Board.BLACK_KING) {
              numBlackKings++;
              numBlackPieces++;
            } else if (piece == Board.RED_KING) {
              numRedKings++;
              numRedPieces++;
            } else if (piece == Board.BLACK_SOLDIER) {
              numBlackPieces++;
            } else if (piece == Board.RED_SOLDIER) {
              numRedPieces++;
            }
          }
        }
      }

      int score = 0;
      score += 6 * (numBlackPieces - numRedPieces);
      score += (28f / 12f) * (numBlackKings - numRedKings);

      if (player == Move.RED) {
        score *= -1;
      }

      return score;
    }
    return 0;
  }
예제 #4
0
파일: Runner.java 프로젝트: alhenk/snake
  public void playGame() {
    Joystick joystick = game.getJoystick();

    do {
      printBoard();

      String line = console.read();
      boolean bomb = false;
      boolean move = false;
      for (Character ch : line.toCharArray()) {
        if (ch == 's' || ch == 'ы') {
          if (move) {
            game.tick();
            bomb = false;
          }
          joystick.down();
          move = true;
        } else if (ch == 'a' || ch == 'ф') {
          if (move) {
            game.tick();
            bomb = false;
          }
          joystick.left();
          move = true;
        } else if (ch == 'd' || ch == 'в') {
          if (move) {
            game.tick();
            bomb = false;
          }
          joystick.right();
          move = true;
        } else if (ch == 'w' || ch == 'ц') {
          if (move) {
            game.tick();
            bomb = false;
          }
          joystick.up();
          move = true;
        } else if (ch == ' ') {
          if (bomb) {
            game.tick();
            move = false;
          }
          joystick.act();
          bomb = true;
        }
      }
      game.tick();
    } while (!game.isGameOver());

    printBoard();
    console.print("Game over!");
  }
예제 #5
0
  public static int MiniMax(Game game, char player, int depth, int maxDepth) {
    if (depth == maxDepth || game.isGameOver()) {
      return evaluate(game, player);
    }

    Move bestMove = null;
    int bestScore;
    if (game.getTurn() == player) {
      bestScore = Integer.MIN_VALUE;
    } else {
      bestScore = Integer.MAX_VALUE;
    }

    List<Move> allMoves = getMoves(game, game.getTurn());
    for (Move move : allMoves) {
      int[][] matrixCopy = game.getBoard().getMatrixCopy();

      game.processMove(move.getCopy());
      game.changeTurn();
      int moveScore = MiniMax(game, player, depth + 1, maxDepth);

      game.changeTurn();
      game.getBoard().setMatrix(matrixCopy);

      if (game.getTurn() == player) {
        if (moveScore > bestScore) {
          bestScore = moveScore;
          bestMove = move.getCopy();
        }
      } else {
        if (moveScore < bestScore) {
          bestScore = moveScore;
          bestMove = move.getCopy();
        }
      }
    }

    if (depth == 0) {
      computedBestMove = bestMove;
    }

    return bestScore;
  }
예제 #6
0
	@Override
	public void run() {
		long nextFrame;
		readHighScore();
		nextFrame = System.currentTimeMillis();
		while (true) {
			if(game.isGameOver()) {
				break;
			}

			long elapsed = System.currentTimeMillis();
			if (elapsed >= nextFrame) {
				// 入力処理、タスク、描画、サウンド等の処理
				this.game.step();
				if(this.game.isExit()) {
					System.exit(0);
				}
				if (elapsed < nextFrame + WAIT) {
					this.repaint();
				}
				nextFrame += WAIT;
			}
		}

		writeHighScore();

		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
				break;
			}
			if (this.game.isExit()) {
				System.exit(0);
			}
		}
	}
예제 #7
0
 public void testGameOver() {
   startingRoom.spawnMonster(new Monster("monster", 1000, 1, 1000, 1000));
   game.playGame("fight");
   assertTrue("When player dies to monster, ends the game", game.isGameOver());
 }