mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Init server code
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
package org.toop.game;
|
||||
// TODO: Remove this, only used in ReversiCanvas. Needs to not
|
||||
public record Move(int position, char value) {}
|
||||
@@ -1,88 +0,0 @@
|
||||
package org.toop.game.gameThreads;
|
||||
|
||||
import org.toop.framework.eventbus.EventFlow;
|
||||
import org.toop.framework.gameFramework.GameState;
|
||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||
import org.toop.framework.gameFramework.model.game.threadBehaviour.AbstractThreadBehaviour;
|
||||
import org.toop.framework.gameFramework.view.GUIEvents;
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.gameFramework.model.player.Player;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Handles local turn-based game logic at a fixed update rate.
|
||||
* <p>
|
||||
* Runs a separate thread that executes game turns at a fixed frequency (default 60 updates/sec),
|
||||
* applying player moves, updating the game state, and dispatching UI events.
|
||||
*/
|
||||
public class LocalFixedRateThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractThreadBehaviour<T> implements Runnable {
|
||||
|
||||
|
||||
/**
|
||||
* Creates a fixed-rate behaviour for a local turn-based game.
|
||||
*
|
||||
* @param game the game instance
|
||||
*/
|
||||
public LocalFixedRateThreadBehaviour(T game) {
|
||||
super(game);
|
||||
}
|
||||
|
||||
/** Starts the game loop thread if not already running. */
|
||||
@Override
|
||||
public void start() {
|
||||
if (isRunning.compareAndSet(false, true)) {
|
||||
new Thread(this).start();
|
||||
}
|
||||
}
|
||||
|
||||
/** Stops the game loop after the current iteration. */
|
||||
@Override
|
||||
public void stop() {
|
||||
isRunning.set(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main loop running at a fixed rate.
|
||||
* <p>
|
||||
* Fetches the current player's move, applies it to the game,
|
||||
* updates the UI, and handles game-ending states.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
final int UPS = 1;
|
||||
final long UPDATE_INTERVAL = 1_000_000_000L / UPS;
|
||||
long nextUpdate = System.nanoTime();
|
||||
|
||||
while (isRunning.get()) {
|
||||
long now = System.nanoTime();
|
||||
if (now >= nextUpdate) {
|
||||
nextUpdate += UPDATE_INTERVAL;
|
||||
|
||||
Player<T> currentPlayer = game.getPlayer(game.getCurrentTurn());
|
||||
long move = currentPlayer.getMove(game.deepCopy());
|
||||
PlayResult result = game.play(move);
|
||||
|
||||
updateUI();
|
||||
|
||||
GameState state = result.state();
|
||||
switch (state) {
|
||||
case WIN, DRAW -> {
|
||||
isRunning.set(false);
|
||||
new EventFlow().addPostEvent(GUIEvents.GameEnded.class, state == GameState.WIN, result.player()).postEvent();
|
||||
}
|
||||
case NORMAL, TURN_SKIPPED -> { /* continue */ }
|
||||
default -> {
|
||||
logger.error("Unexpected state {}", state);
|
||||
isRunning.set(false);
|
||||
throw new RuntimeException("Unknown state: " + state);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException ignored) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package org.toop.game.gameThreads;
|
||||
|
||||
import org.toop.framework.eventbus.EventFlow;
|
||||
import org.toop.framework.gameFramework.model.game.threadBehaviour.AbstractThreadBehaviour;
|
||||
import org.toop.framework.gameFramework.view.GUIEvents;
|
||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||
import org.toop.framework.gameFramework.GameState;
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.gameFramework.model.player.Player;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Handles local turn-based game logic in its own thread.
|
||||
* <p>
|
||||
* Repeatedly gets the current player's move, applies it to the game,
|
||||
* updates the UI, and stops when the game ends or {@link #stop()} is called.
|
||||
*/
|
||||
public class LocalThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractThreadBehaviour<T> implements Runnable {
|
||||
|
||||
/**
|
||||
* Creates a new behaviour for a local turn-based game.
|
||||
*
|
||||
* @param game the game instance
|
||||
*/
|
||||
public LocalThreadBehaviour(T game) {
|
||||
super(game);
|
||||
}
|
||||
|
||||
/** Starts the game loop in a new thread. */
|
||||
@Override
|
||||
public void start() {
|
||||
if (isRunning.compareAndSet(false, true)) {
|
||||
new Thread(this).start();
|
||||
}
|
||||
}
|
||||
|
||||
/** Stops the game loop after the current iteration. */
|
||||
@Override
|
||||
public void stop() {
|
||||
isRunning.set(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main game loop: gets the current player's move, applies it,
|
||||
* updates the UI, and handles end-of-game states.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
while (isRunning.get()) {
|
||||
Player<T> currentPlayer = game.getPlayer(game.getCurrentTurn());
|
||||
long move = currentPlayer.getMove(game.deepCopy());
|
||||
PlayResult result = game.play(move);
|
||||
|
||||
updateUI();
|
||||
|
||||
GameState state = result.state();
|
||||
switch (state) {
|
||||
case WIN, DRAW -> {
|
||||
isRunning.set(false);
|
||||
new EventFlow().addPostEvent(
|
||||
GUIEvents.GameEnded.class,
|
||||
state == GameState.WIN,
|
||||
result.player()
|
||||
).postEvent();
|
||||
}
|
||||
case NORMAL, TURN_SKIPPED -> { /* continue normally */ }
|
||||
default -> {
|
||||
logger.error("Unexpected state {}", state);
|
||||
isRunning.set(false);
|
||||
throw new RuntimeException("Unknown state: " + state);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package org.toop.game.gameThreads;
|
||||
|
||||
import org.toop.framework.eventbus.EventFlow;
|
||||
import org.toop.framework.gameFramework.model.game.threadBehaviour.AbstractThreadBehaviour;
|
||||
import org.toop.framework.gameFramework.view.GUIEvents;
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.gameFramework.model.game.SupportsOnlinePlay;
|
||||
import org.toop.framework.gameFramework.model.player.Player;
|
||||
import org.toop.game.players.OnlinePlayer;
|
||||
|
||||
/**
|
||||
* Handles online multiplayer game logic.
|
||||
* <p>
|
||||
* Reacts to server events, sending moves and updating the game state
|
||||
* for the local player while receiving moves from other players.
|
||||
*/
|
||||
public class OnlineThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractThreadBehaviour<T> implements SupportsOnlinePlay {
|
||||
/**
|
||||
* Creates behaviour and sets the first local player
|
||||
* (non-online player) from the given array.
|
||||
*/
|
||||
public OnlineThreadBehaviour(T game) {
|
||||
super(game);
|
||||
}
|
||||
|
||||
/** Finds the first non-online player in the array. */
|
||||
private int getFirstNotOnlinePlayer(Player<T>[] players) {
|
||||
for (int i = 0; i < players.length; i++) {
|
||||
if (!(players[i] instanceof OnlinePlayer)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("All players are online players");
|
||||
}
|
||||
|
||||
/** Starts processing network events for the local player. */
|
||||
@Override
|
||||
public void start() {
|
||||
isRunning.set(true);
|
||||
}
|
||||
|
||||
/** Stops processing network events. */
|
||||
@Override
|
||||
public void stop() {
|
||||
isRunning.set(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the server notifies that it is the local player's turn.
|
||||
* Sends the generated move back to the server.
|
||||
*/
|
||||
@Override
|
||||
public void onYourTurn(long clientId) {
|
||||
if (!isRunning.get()) return;
|
||||
long move = game.getPlayer(game.getCurrentTurn()).getMove(game.deepCopy());
|
||||
sendMove(clientId, move);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a move received from the server for any player.
|
||||
* Updates the game state and triggers a UI refresh.
|
||||
*/
|
||||
public void onMoveReceived(long move) {
|
||||
if (!isRunning.get()) return;
|
||||
game.play(move);
|
||||
|
||||
updateUI();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the end of the game as notified by the server.
|
||||
* Updates the UI to show a win or draw result for the local player.
|
||||
*/
|
||||
public void gameFinished(String condition) {
|
||||
switch(condition.toUpperCase()){
|
||||
case "WIN", "LOSS" -> new EventFlow().addPostEvent(GUIEvents.GameEnded.class, true, game.getWinner()).postEvent();
|
||||
case "DRAW" -> new EventFlow().addPostEvent(GUIEvents.GameEnded.class, false, -1).postEvent();
|
||||
default -> {
|
||||
logger.error("Invalid condition");
|
||||
throw new RuntimeException("Unknown condition");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package org.toop.game.gameThreads;
|
||||
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.networking.events.NetworkEvents;
|
||||
|
||||
/**
|
||||
* Online thread behaviour that adds a fixed delay before processing
|
||||
* the local player's turn.
|
||||
* <p>
|
||||
* This is identical to {@link OnlineThreadBehaviour}, but inserts a
|
||||
* short sleep before delegating to the base implementation.
|
||||
*/
|
||||
public class OnlineWithSleepThreadBehaviour<T extends TurnBasedGame<T>> extends OnlineThreadBehaviour<T> {
|
||||
|
||||
/**
|
||||
* Creates the behaviour and forwards the players to the base class.
|
||||
*
|
||||
* @param game the online-capable turn-based game
|
||||
*/
|
||||
public OnlineWithSleepThreadBehaviour(T game) {
|
||||
super(game);
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits briefly before handling the "your turn" event.
|
||||
*
|
||||
* @param event the network event indicating it's this client's turn
|
||||
*/
|
||||
@Override
|
||||
public void onYourTurn(long clientId) {
|
||||
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
super.onYourTurn(clientId);
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
package org.toop.game.players;
|
||||
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
||||
import org.toop.framework.gameFramework.model.player.Player;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class LocalPlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
||||
// Future can be used with event system, IF unsubscribeAfterSuccess works...
|
||||
// private CompletableFuture<Integer> LastMove = new CompletableFuture<>();
|
||||
|
||||
private CompletableFuture<Long> LastMove;
|
||||
|
||||
public LocalPlayer(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public LocalPlayer(LocalPlayer<T> other) {
|
||||
super(other);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMove(T gameCopy) {
|
||||
return getValidMove(gameCopy);
|
||||
}
|
||||
|
||||
public void setMove(long move) {
|
||||
LastMove.complete(move);
|
||||
}
|
||||
|
||||
// TODO: helper function, would like to replace to get rid of this method
|
||||
public static boolean contains(int[] array, int value){
|
||||
for (int i : array) if (i == value) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private long getMove2(T gameCopy) {
|
||||
LastMove = new CompletableFuture<>();
|
||||
long move = 0;
|
||||
try {
|
||||
move = LastMove.get();
|
||||
System.out.println(Long.toBinaryString(move));
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
// TODO: Add proper logging.
|
||||
e.printStackTrace();
|
||||
}
|
||||
return move;
|
||||
}
|
||||
|
||||
protected long getValidMove(T gameCopy){
|
||||
// Get this player's valid moves
|
||||
long validMoves = gameCopy.getLegalMoves();
|
||||
// Make sure provided move is valid
|
||||
// TODO: Limit amount of retries?
|
||||
// TODO: Stop copying game so many times
|
||||
long move = getMove2(gameCopy.deepCopy());
|
||||
while ((validMoves & move) == 0) {
|
||||
System.out.println("Not a valid move, try again");
|
||||
move = getMove2(gameCopy.deepCopy());
|
||||
}
|
||||
return move;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalPlayer<T> deepCopy() {
|
||||
return new LocalPlayer<T>(this.getName());
|
||||
}
|
||||
|
||||
/*public void register() {
|
||||
// Listening to PlayerAttemptedMove
|
||||
new EventFlow().listen(GUIEvents.PlayerAttemptedMove.class, event -> {
|
||||
if (!LastMove.isDone()) {
|
||||
LastMove.complete(event.move()); // complete the future
|
||||
}
|
||||
}, true); // auto-unsubscribe
|
||||
}
|
||||
|
||||
// This blocks until the next move arrives
|
||||
public int take() throws ExecutionException, InterruptedException {
|
||||
int move = LastMove.get(); // blocking
|
||||
LastMove = new CompletableFuture<>(); // reset for next move
|
||||
return move;
|
||||
}*/
|
||||
}
|
||||
@@ -1,165 +0,0 @@
|
||||
package org.toop.game.players;
|
||||
|
||||
import org.toop.framework.gameFramework.GameState;
|
||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.gameFramework.model.player.AbstractAI;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class MiniMaxAI<T extends TurnBasedGame<T>> extends AbstractAI<T> {
|
||||
|
||||
private final int maxDepth;
|
||||
private final Random random = new Random();
|
||||
|
||||
public MiniMaxAI(int depth) {
|
||||
this.maxDepth = depth;
|
||||
}
|
||||
|
||||
public MiniMaxAI(MiniMaxAI<T> other) {
|
||||
this.maxDepth = other.maxDepth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MiniMaxAI<T> deepCopy() {
|
||||
return new MiniMaxAI<>(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMove(T game) {
|
||||
long legalMoves = game.getLegalMoves();
|
||||
if (legalMoves == 0) return 0;
|
||||
|
||||
List<Long> bestMoves = new ArrayList<>();
|
||||
int bestScore = Integer.MIN_VALUE;
|
||||
int aiPlayer = game.getCurrentTurn();
|
||||
|
||||
long movesLoop = legalMoves;
|
||||
while (movesLoop != 0) {
|
||||
long move = 1L << Long.numberOfTrailingZeros(movesLoop);
|
||||
T copy = game.deepCopy();
|
||||
PlayResult result = copy.play(move);
|
||||
|
||||
int score;
|
||||
switch (result.state()) {
|
||||
case WIN -> score = (result.player() == aiPlayer ? maxDepth : -maxDepth);
|
||||
case DRAW -> score = 0;
|
||||
default -> score = getMoveScore(copy, maxDepth - 1, false, aiPlayer, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
if (score > bestScore) {
|
||||
bestScore = score;
|
||||
bestMoves.clear();
|
||||
bestMoves.add(move);
|
||||
} else if (score == bestScore) {
|
||||
bestMoves.add(move);
|
||||
}
|
||||
|
||||
movesLoop &= movesLoop - 1;
|
||||
}
|
||||
|
||||
long chosenMove = bestMoves.get(random.nextInt(bestMoves.size()));
|
||||
return chosenMove;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursive minimax with alpha-beta pruning and heuristic evaluation.
|
||||
*
|
||||
* @param game Current game state
|
||||
* @param depth Remaining depth
|
||||
* @param maximizing True if AI is maximizing, false if opponent
|
||||
* @param aiPlayer AI's player index
|
||||
* @param alpha Alpha value
|
||||
* @param beta Beta value
|
||||
* @return score of the position
|
||||
*/
|
||||
private int getMoveScore(T game, int depth, boolean maximizing, int aiPlayer, int alpha, int beta) {
|
||||
long legalMoves = game.getLegalMoves();
|
||||
|
||||
// Terminal state
|
||||
PlayResult lastResult = null;
|
||||
if (legalMoves == 0) {
|
||||
lastResult = new PlayResult(GameState.DRAW, -1);
|
||||
}
|
||||
|
||||
// If the game is over or depth limit reached, evaluate
|
||||
if (depth <= 0 || legalMoves == 0) {
|
||||
if (lastResult != null) return 0;
|
||||
return evaluateBoard(game, aiPlayer);
|
||||
}
|
||||
|
||||
int bestScore = maximizing ? Integer.MIN_VALUE : Integer.MAX_VALUE;
|
||||
long movesLoop = legalMoves;
|
||||
|
||||
while (movesLoop != 0) {
|
||||
long move = 1L << Long.numberOfTrailingZeros(movesLoop);
|
||||
T copy = game.deepCopy();
|
||||
PlayResult result = copy.play(move);
|
||||
|
||||
int score;
|
||||
switch (result.state()) {
|
||||
case WIN -> score = (result.player() == aiPlayer ? depth : -depth);
|
||||
case DRAW -> score = 0;
|
||||
default -> score = getMoveScore(copy, depth - 1, !maximizing, aiPlayer, alpha, beta);
|
||||
}
|
||||
|
||||
if (maximizing) {
|
||||
bestScore = Math.max(bestScore, score);
|
||||
alpha = Math.max(alpha, bestScore);
|
||||
} else {
|
||||
bestScore = Math.min(bestScore, score);
|
||||
beta = Math.min(beta, bestScore);
|
||||
}
|
||||
|
||||
// Alpha-beta pruning
|
||||
if (beta <= alpha) break;
|
||||
|
||||
movesLoop &= movesLoop - 1;
|
||||
}
|
||||
|
||||
return bestScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple heuristic evaluation for Reversi-like games.
|
||||
* Positive = good for AI, Negative = good for opponent.
|
||||
*
|
||||
* @param game Game state
|
||||
* @param aiPlayer AI's player index
|
||||
* @return heuristic score
|
||||
*/
|
||||
private int evaluateBoard(T game, int aiPlayer) {
|
||||
long[] board = game.getBoard();
|
||||
int aiCount = 0;
|
||||
int opponentCount = 0;
|
||||
|
||||
// Count pieces for AI vs opponent
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
long bits = board[i];
|
||||
for (int j = 0; j < 64; j++) {
|
||||
if ((bits & (1L << j)) != 0) {
|
||||
// Assume player 0 occupies even indices, player 1 occupies odd
|
||||
if ((i * 64 + j) % game.getPlayerCount() == aiPlayer) aiCount++;
|
||||
else opponentCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mobility (number of legal moves)
|
||||
int mobility = Long.bitCount(game.getLegalMoves());
|
||||
|
||||
// Corner control (top-left, top-right, bottom-left, bottom-right)
|
||||
int corners = 0;
|
||||
long[] cornerMasks = {1L << 0, 1L << 7, 1L << 56, 1L << 63};
|
||||
for (long mask : cornerMasks) {
|
||||
for (long b : board) {
|
||||
if ((b & mask) != 0) corners += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Weighted sum
|
||||
return (aiCount - opponentCount) + 2 * mobility + 5 * corners;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package org.toop.game.players;
|
||||
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
||||
import org.toop.framework.gameFramework.model.player.Player;
|
||||
|
||||
/**
|
||||
* Represents a player controlled remotely or over a network.
|
||||
* <p>
|
||||
* This class extends {@link AbstractPlayer} and can be used to implement game logic
|
||||
* where moves are provided by an external source (e.g., another user or a server).
|
||||
* Currently, this class is a placeholder and does not implement move logic.
|
||||
* </p>
|
||||
*/
|
||||
public class OnlinePlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
||||
|
||||
/**
|
||||
* Constructs a new OnlinePlayer.
|
||||
* <p>
|
||||
* Currently, no additional initialization is performed. Subclasses or
|
||||
* future implementations should provide mechanisms to receive moves from
|
||||
* an external source.
|
||||
*/
|
||||
public OnlinePlayer(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public OnlinePlayer(OnlinePlayer<T> other) {
|
||||
super(other);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player<T> deepCopy() {
|
||||
return new OnlinePlayer<>(this);
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package org.toop.game.players;
|
||||
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.gameFramework.model.player.AbstractAI;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
public class RandomAI<T extends TurnBasedGame<T>> extends AbstractAI<T> {
|
||||
|
||||
public RandomAI() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RandomAI<T> deepCopy() {
|
||||
return new RandomAI<T>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMove(T game) {
|
||||
long legalMoves = game.getLegalMoves();
|
||||
int move = new Random().nextInt(Long.bitCount(legalMoves));
|
||||
return nthBitIndex(legalMoves, move);
|
||||
}
|
||||
|
||||
public static long nthBitIndex(long bb, int n) {
|
||||
while (bb != 0) {
|
||||
int tz = Long.numberOfTrailingZeros(bb);
|
||||
if (n == 0) {
|
||||
return 1L << tz;
|
||||
}
|
||||
bb &= bb - 1; // clear the least significant 1
|
||||
n--;
|
||||
}
|
||||
return 0L; // not enough 1s
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user