Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from WROracer/UNO-4
UNO-4 Add Game Logic
- Loading branch information
Showing
16 changed files
with
244 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package de.wroracer.uno.engine; | ||
|
||
import de.wroracer.uno.engine.card.Card; | ||
import de.wroracer.uno.engine.card.Deck; | ||
import de.wroracer.uno.engine.card.Special; | ||
import de.wroracer.uno.engine.error.CardNotPlayableError; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
public class Game { | ||
|
||
private final Deck deck; | ||
private final UUID id; | ||
private final List<Player> players; | ||
private final boolean clockwise = true; | ||
private State state; | ||
private Player currentTurn; | ||
|
||
public Game() { | ||
deck = new Deck(); | ||
players = new ArrayList<>(); | ||
id = UUID.randomUUID(); | ||
|
||
System.out.println(deck); | ||
} | ||
|
||
|
||
public Player addPlayer() { | ||
Player p = new Player(); | ||
players.add(p); | ||
return p; | ||
} | ||
|
||
public void removePlayer(Player p) { | ||
players.remove(p); | ||
} | ||
|
||
public void startGame() { | ||
this.state = State.IN_GAME; | ||
for (int i = 0; i < players.size() * 7; i++) { | ||
players.get(i % players.size()).getHand().add(deck.drawCard()); | ||
} | ||
deck.discard(deck.drawCard()); | ||
this.currentTurn = players.get(0); | ||
} | ||
|
||
public Card getCurrentCard() { | ||
return deck.currentCard(); | ||
} | ||
|
||
|
||
public void play(Card card) { | ||
if (card.canPlayedOn(deck.currentCard())) { | ||
deck.discard(card); | ||
currentTurn.getHand().remove(card); | ||
if (card instanceof Special) { | ||
((Special) card).execute(this); | ||
} | ||
nextTurn(); | ||
} else { | ||
throw new CardNotPlayableError(card, deck.currentCard()); | ||
} | ||
} | ||
|
||
private void nextTurn() { | ||
if (currentTurn.getHand().size() == 0) { | ||
state = State.FINISHED; | ||
return; | ||
} | ||
int index = players.indexOf(currentTurn); | ||
if (clockwise) { | ||
index++; | ||
if (index >= players.size()) { | ||
index = 0; | ||
} | ||
} else { | ||
index--; | ||
if (index < 0) { | ||
index = players.size() - 1; | ||
} | ||
} | ||
currentTurn = players.get(index); | ||
} | ||
|
||
public boolean canBePlay(Card card) { | ||
return card.canPlayedOn(deck.currentCard()); | ||
} | ||
|
||
public void draw(Player player) { | ||
if (player == currentTurn) { | ||
player.getHand().add(deck.drawCard()); | ||
nextTurn(); | ||
} else { | ||
player.getHand().add(deck.drawCard()); | ||
} | ||
} | ||
|
||
public UUID getId() { | ||
return id; | ||
} | ||
|
||
public Player getCurrentTurn() { | ||
return currentTurn; | ||
} | ||
|
||
public List<Player> getPlayers() { | ||
return players; | ||
} | ||
|
||
public enum State { | ||
LOBBY, | ||
IN_GAME, | ||
FINISHED | ||
} | ||
} |
10 changes: 5 additions & 5 deletions
10
...in/java/de/wroracer/unoengine/Player.java → ...n/java/de/wroracer/uno/engine/Player.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...java/de/wroracer/unoengine/card/Card.java → ...ava/de/wroracer/uno/engine/card/Card.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...java/de/wroracer/unoengine/card/Deck.java → ...ava/de/wroracer/uno/engine/card/Deck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
.../wroracer/unoengine/card/DrawTwoCard.java → ...wroracer/uno/engine/card/DrawTwoCard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package de.wroracer.unoengine.card; | ||
package de.wroracer.uno.engine.card; | ||
|
||
import de.wroracer.unoengine.Game; | ||
import de.wroracer.uno.engine.Game; | ||
|
||
public class DrawTwoCard extends Card implements Special { | ||
public DrawTwoCard(Color color) { | ||
super(color, Type.DRAW_TWO); | ||
} | ||
|
||
@Override | ||
public void performAction(Game game) { | ||
public void execute(Game game) { | ||
//TODO | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
.../wroracer/unoengine/card/ReverseCard.java → ...wroracer/uno/engine/card/ReverseCard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package de.wroracer.unoengine.card; | ||
package de.wroracer.uno.engine.card; | ||
|
||
import de.wroracer.unoengine.Game; | ||
import de.wroracer.uno.engine.Game; | ||
|
||
public class ReverseCard extends Card implements Special { | ||
public ReverseCard(Color color) { | ||
super(color, Type.REVERSE); | ||
} | ||
|
||
@Override | ||
public void performAction(Game game) { | ||
public void execute(Game game) { | ||
//TODO | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
.../de/wroracer/unoengine/card/SkipCard.java → ...de/wroracer/uno/engine/card/SkipCard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package de.wroracer.unoengine.card; | ||
package de.wroracer.uno.engine.card; | ||
|
||
import de.wroracer.unoengine.Game; | ||
import de.wroracer.uno.engine.Game; | ||
|
||
public class SkipCard extends Card implements Special { | ||
public SkipCard(Color color) { | ||
super(color, Type.SKIP); | ||
} | ||
|
||
@Override | ||
public void performAction(Game game) { | ||
public void execute(Game game) { | ||
//TODO | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package de.wroracer.uno.engine.card; | ||
|
||
import de.wroracer.uno.engine.Game; | ||
|
||
public interface Special { | ||
|
||
void execute(Game game); | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
.../de/wroracer/unoengine/card/WildCard.java → ...de/wroracer/uno/engine/card/WildCard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...acer/unoengine/card/WildDrawFourCard.java → ...cer/uno/engine/card/WildDrawFourCard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package de.wroracer.unoengine.card; | ||
package de.wroracer.uno.engine.card; | ||
|
||
import de.wroracer.unoengine.Game; | ||
import de.wroracer.uno.engine.Game; | ||
|
||
public class WildDrawFourCard extends WildCard implements Special { | ||
public WildDrawFourCard() { | ||
this.type = Type.WILDE_DRAW_FOUR; | ||
} | ||
|
||
@Override | ||
public void performAction(Game game) { | ||
public void execute(Game game) { | ||
//TODO | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/de/wroracer/uno/engine/error/CardNotPlayableError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package de.wroracer.uno.engine.error; | ||
|
||
import de.wroracer.uno.engine.card.Card; | ||
|
||
public class CardNotPlayableError extends UnoError { | ||
public CardNotPlayableError(Card play, Card current) { | ||
super("The card " + play + " is not playable on " + current); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package de.wroracer.uno.engine.error; | ||
|
||
public class UnoError extends Error { | ||
public UnoError(String message) { | ||
super(message); | ||
} | ||
|
||
public UnoError(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public UnoError(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.