add dual segments

This commit is contained in:
James 2024-04-20 20:22:57 +01:00
parent 57e975c7c7
commit 64b12d17a6
5 changed files with 101 additions and 10 deletions

View file

@ -71,6 +71,10 @@ public class Die extends Actor {
return getFace().getValue();
}
public boolean isBlank() {
return getFaceValue() <= 0;
}
public boolean isLocked() {
return locked;
}

View file

@ -6,6 +6,9 @@ import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
import com.monjaro.gamejam.segment.DualSegment;
import com.monjaro.gamejam.segment.KinSegment;
import com.monjaro.gamejam.segment.Segment;
import java.util.ArrayList;
import java.util.HashSet;
@ -17,6 +20,7 @@ public class Game extends ApplicationAdapter {
private final Set<Actor> actors = new HashSet<>();
private final List<Die> dice = new ArrayList<>();
private final List<Segment> segments = new ArrayList<>();
private SpriteBatch batch;
private Texture img;
@ -35,6 +39,12 @@ public class Game extends ApplicationAdapter {
for (int i = 1; i <= 5; i++) {
dice.add(new Die((i*80), 20, 64, 64));
}
for (int i = 1; i <= 5; i++) {
segments.add(new KinSegment(i));
}
segments.add(new DualSegment(false));
segments.add(new DualSegment(true));
}
public void tick() {
@ -48,7 +58,13 @@ public class Game extends ApplicationAdapter {
if (input.isKeyJustPressed(Input.Keys.R)) { //reroll dice that aren't locked
dice.stream().filter(d -> !d.isLocked()).forEach(Die::roll);
System.out.println("=".repeat(100));
for (Segment segment : segments) {
System.out.println(segment.getName() + ": " + segment.isDestroyedBy(dice));
}
}
for (int i = 0; i < dice.size(); i++) { //lock dice, iterating over for each keycode
Die die = dice.get(i); //die iterator is looking at

View file

@ -0,0 +1,58 @@
package com.monjaro.gamejam.segment;
import com.monjaro.gamejam.Die;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class DualSegment extends Segment {
private final boolean firstTrio;
public DualSegment(boolean firstTrio) { //first is a trio or duo, second is a duo
this.firstTrio = firstTrio;
if (firstTrio) {
Random random = new Random();
if (random.nextInt(10) != 0) {
name = "Full House";
} else {
String[] names = new String[]{
"Complete Household",
"Entire Residence",
"Occupied Home",
"Family Residence",
"Filled Dwelling",
"Packed Abode",
"Crowded Domicile",
"Busy Household",
"Well-occupied Home",
"Houseful"
}; //thank you ChatGPT for alternate names
name = names[random.nextInt(names.length)];
}
} else {
name = "Dual Duo";
}
}
@Override
public boolean isDestroyedBy(List<Die> dice) {
Map<Integer, Integer> counts = countValues(dice);
Map<Integer, Integer> countCounts = new HashMap<>(); //count of counts
for (int count : counts.values()) {
for (int i = count; i >= 1; i--) { //add from count to 1 - this is so a trio is also a pair, e.g. 11122 is a dual duo
int countCount = countCounts.getOrDefault(i, 0) + 1;
countCounts.put(i, countCount);
}
}
return countCounts.getOrDefault(2, 0) >= 2
&& (!firstTrio || countCounts.getOrDefault(3, 0) >= 1);
}
}

View file

@ -2,7 +2,6 @@ package com.monjaro.gamejam.segment;
import com.monjaro.gamejam.Die;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -15,7 +14,7 @@ public class KinSegment extends Segment { //multiple dice of the same value
name = switch (requirement) {
case 1 -> "Any";
case 2 -> "Pair";
case 2 -> "Duo";
case 3 -> "Trio";
case 4 -> "Quartet";
case 5 -> "Quintet";
@ -24,14 +23,13 @@ public class KinSegment extends Segment { //multiple dice of the same value
}
@Override
public boolean destroyedBy(List<Die> dice) {
Map<Integer, Integer> counts = new HashMap<>();
for (Die die : dice) {
int count = counts.getOrDefault(die.getFaceValue(), 1) + 1;
public boolean isDestroyedBy(List<Die> dice) {
Map<Integer, Integer> counts = countValues(dice);
if (count >= requirement) return true;
counts.put(die.getFaceValue(), count);
for (int count : counts.values()) {
if (count >= requirement) {
return true;
}
}
return false;

View file

@ -2,7 +2,9 @@ package com.monjaro.gamejam.segment;
import com.monjaro.gamejam.Die;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class Segment {
@ -14,10 +16,23 @@ public abstract class Segment {
destroyed = true;
}
public abstract boolean destroyedBy(List<Die> die);
public abstract boolean isDestroyedBy(List<Die> die);
public String getName() {
return name;
}
protected Map<Integer, Integer> countValues(List<Die> dice) {
Map<Integer, Integer> counts = new HashMap<>();
for (Die die : dice) {
if (die.isBlank()) continue;
int count = counts.getOrDefault(die.getFaceValue(), 0) + 1;
counts.put(die.getFaceValue(), count);
}
return counts;
}
}