Skip to content

Commit bf30f31

Browse files
authored
start castle java backend 🏰 (#81)
start castle backend 🏰
1 parent 5773407 commit bf30f31

File tree

13 files changed

+162
-1
lines changed

13 files changed

+162
-1
lines changed

.ijwb/.bazelproject

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ additional_languages:
1515
# Uncomment any additional languages you want supported
1616
scala
1717
go
18+
19+
java_language_level: 17
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
java_library(
2+
name = "cards",
3+
srcs = [
4+
"Card.java",
5+
"Deck.java",
6+
"Rank.java",
7+
"Suit.java",
8+
],
9+
visibility = ["//visibility:public"],
10+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.muchq.cards;
2+
3+
import java.util.List;
4+
5+
public record Card(Suit suit, Rank rank) {
6+
private static final List<Suit> SUITS = List.of(Suit.CLUBS, Suit.DIAMONDS, Suit.HEARTS, Suit.SPADES);
7+
private static final List<Rank> RANKS = List.of(
8+
Rank.TWO, Rank.THREE, Rank.FOUR, Rank.FIVE, Rank.SIX, Rank.SEVEN, Rank.EIGHT,
9+
Rank.NINE, Rank.TEN, Rank.JACK, Rank.QUEEN, Rank.KING, Rank.ACE);
10+
11+
public static Card forIndex(int index) {
12+
Suit s = SUITS.get(index % 4);
13+
Rank r = RANKS.get(index % 13);
14+
return new Card(s, r);
15+
}
16+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.muchq.cards;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.Deque;
7+
import java.util.List;
8+
9+
public record Deck(Deque<Card> cards) {
10+
11+
public boolean hasNext() {
12+
return !cards.isEmpty();
13+
}
14+
15+
public Card nextCard() {
16+
return cards.removeLast();
17+
}
18+
19+
public Deck shuffled() {
20+
List<Card> newCards = new ArrayList<>(cards);
21+
Collections.shuffle(newCards);
22+
return new Deck(new ArrayDeque<>(newCards));
23+
}
24+
25+
public static Deck withJokers() {
26+
var cards = cardsListWithNoJokers();
27+
cards.add(new Card(Suit.NONE, Rank.JOKER));
28+
cards.add(new Card(Suit.NONE, Rank.JOKER));
29+
return new Deck(cards);
30+
}
31+
32+
public static Deck noJokers() {
33+
return new Deck(cardsListWithNoJokers());
34+
}
35+
36+
private static Deque<Card> cardsListWithNoJokers() {
37+
List<Card> cards = new ArrayList<>();
38+
for (var suit : Suit.values()) {
39+
for (var value : Rank.values()) {
40+
if (suit != Suit.NONE && value != Rank.JOKER) {
41+
cards.add(new Card(suit, value));
42+
}
43+
}
44+
}
45+
return new ArrayDeque<>(cards);
46+
}
47+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.muchq.cards;
2+
3+
public enum Rank {
4+
TWO,
5+
THREE,
6+
FOUR,
7+
FIVE,
8+
SIX,
9+
SEVEN,
10+
EIGHT,
11+
NINE,
12+
TEN,
13+
JACK,
14+
QUEEN,
15+
KING,
16+
ACE,
17+
JOKER
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.muchq.cards;
2+
3+
public enum Suit {
4+
CLUBS,
5+
DIAMONDS,
6+
HEARTS,
7+
SPADES,
8+
NONE;
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
java_library(
2+
name = "castle",
3+
srcs = [
4+
"GameState.java",
5+
"Player.java",
6+
"ThreeDown.java",
7+
"ThreeUp.java",
8+
"Turn.java",
9+
],
10+
visibility = ["//visibility:public"],
11+
deps = [
12+
"//jvm/src/main/java/com/muchq/cards",
13+
],
14+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.muchq.cards.castle;
2+
3+
import com.muchq.cards.Card;
4+
5+
import java.util.Deque;
6+
import java.util.List;
7+
8+
public record GameState(Deque<Card> drawPile, List<Player> players, Turn lastPlayed) {
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.muchq.cards.castle;
2+
3+
import com.muchq.cards.Card;
4+
5+
import java.util.List;
6+
7+
public record Player(String name, List<Card> hand, ThreeUp faceUp, ThreeDown faceDown) {
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.muchq.cards.castle;
2+
3+
import com.muchq.cards.Card;
4+
5+
import java.util.Optional;
6+
7+
public record ThreeDown(Optional<Card> left, Optional<Card> center, Optional<Card> right) {
8+
public boolean isEmpty() {
9+
return left().isEmpty() && center().isEmpty() && right().isEmpty();
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.muchq.cards.castle;
2+
3+
import com.muchq.cards.Card;
4+
5+
import java.util.Optional;
6+
7+
public record ThreeUp(Optional<Card> left, Optional<Card> center, Optional<Card> right) {
8+
public boolean isEmpty() {
9+
return left().isEmpty() && center().isEmpty() && right().isEmpty();
10+
}
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.muchq.cards.castle;
2+
3+
import com.muchq.cards.Rank;
4+
5+
public record Turn(Rank rank, int howMany) {
6+
}

jvm/src/main/java/com/muchq/json/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
java_library(
22
name = "json",
3-
srcs = glob(["*.java"]),
3+
srcs = ["JsonUtils.java"],
44
visibility = ["//visibility:public"],
55
deps = [
66
"@maven//:com_fasterxml_jackson_core_jackson_core",

0 commit comments

Comments
 (0)