Skip to content

Commit

Permalink
added fixes to 2048 game
Browse files Browse the repository at this point in the history
  • Loading branch information
Taz03 committed Mar 18, 2024
1 parent 54cc10a commit 6a1e4d6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ dependencies {
implementation 'org.apache.logging.log4j:log4j-core:2.23.0'
runtimeOnly 'org.apache.logging.log4j:log4j-slf4j18-impl:2.18.0'

implementation 'com.github.ben-manes.caffeine:caffeine:3.1.8'

testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
public final class Game2048 {
public static final int ROWS = 4;
public static final int COLUMNS = 4;
private static final Random random = new Random();

private static Random random = new Random();
private int[][] board = new int[ROWS][COLUMNS];
private GameState state = GameState.ONGOING;
private int score = 0;
private int[][] board;
private GameState state;
private int score;

public Game2048() {
board = new int[ROWS][COLUMNS];
state = GameState.ONGOING;
score = 0;

for (int i = 0; i < 2; i++) spawnNewBlock();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.togetherjava.tjplays.listeners.commands;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.togetherjava.tjplays.games.game2048.Game2048;
import com.togetherjava.tjplays.games.game2048.GameState;
import com.togetherjava.tjplays.games.game2048.Move;
Expand All @@ -28,21 +28,24 @@ public final class Game2048Command extends SlashCommand {
private static final Emoji DOWN_EMOJI = Emoji.fromUnicode("⬇️");
private static final Emoji RIGHT_EMOJI = Emoji.fromUnicode("➡️");

private Map<String, Renderer2048> sessions = new HashMap<>();
private final Cache<String, Renderer2048> sessionsCache;

public Game2048Command() {
super(Commands.slash(COMMAND_NAME, "Game 2048"));

sessionsCache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.DAYS)
.build();
}

@Override
public void onSlashCommand(SlashCommandInteractionEvent event) {
Renderer2048 gameRenderer = new Renderer2048(new Game2048());

event.reply(gameMessage(gameRenderer, event.getUser().getId()))
.queue(hook -> {
hook.retrieveOriginal().queue(message -> sessions.put(message.getId(), gameRenderer));
hook.retrieveOriginal().queueAfter(10, TimeUnit.HOURS, message -> sessions.remove(message.getId()));
});
.flatMap(hook -> hook.retrieveOriginal())
.onSuccess(message -> sessionsCache.put(message.getId(), gameRenderer))
.queue();
}

@Override
Expand All @@ -55,24 +58,26 @@ public void onButtonInteraction(ButtonInteractionEvent event) {
return;
}

if (buttonId.contains("reset"))
sessions.get(event.getMessageId()).setGame(new Game2048());
else if (buttonId.contains("delete")) {
sessions.remove(event.getMessageId());
if (buttonId.contains("delete")) {
sessionsCache.invalidate(event.getMessageId());
event.getMessage().delete().queue();
return;
}

Move move = null;
Renderer2048 gameRenderer = sessionsCache.getIfPresent(event.getMessageId());

if (buttonId.contains("up")) move = Move.UP;
else if (buttonId.contains("down")) move = Move.DOWN;
else if (buttonId.contains("left")) move = Move.LEFT;
else if (buttonId.contains("right")) move = Move.RIGHT;
if (buttonId.contains("reset")) {
gameRenderer.setGame(new Game2048());
} else {
Move move = null;

if (buttonId.contains("up")) move = Move.UP;
else if (buttonId.contains("down")) move = Move.DOWN;
else if (buttonId.contains("left")) move = Move.LEFT;
else if (buttonId.contains("right")) move = Move.RIGHT;

Renderer2048 gameRenderer = sessions.get(event.getMessageId());
if (move != null)
gameRenderer.getGame().move(move);
}

event.editMessage(MessageEditData.fromCreateData(gameMessage(gameRenderer, event.getUser().getId()))).queue();
}
Expand Down

0 comments on commit 6a1e4d6

Please sign in to comment.