Skip to content

Commit

Permalink
sääntökorttien toteutusta
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeemlei committed Apr 28, 2020
1 parent ca0b898 commit 65902e7
Show file tree
Hide file tree
Showing 14 changed files with 305 additions and 49 deletions.
17 changes: 15 additions & 2 deletions Juomapeli/src/main/java/dao/JSONCardsDao.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package dao;

import domain.deck.BasicCard;
import domain.deck.Card;
import domain.deck.BasicCard;
import domain.deck.RuleCard;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -28,6 +29,7 @@ public class JSONCardsDao implements CardsDao {
*/
public JSONCardsDao() throws IOException {
this.getJSON();
this.cards = new ArrayList<>();
this.parseCards();
}

Expand All @@ -48,8 +50,8 @@ private void getJSON() throws IOException {
}

private void parseCards() {
this.cards = new ArrayList<>();
this.parseBasicCards();
this.parseRuleCards();
}

private void parseBasicCards() {
Expand All @@ -63,6 +65,17 @@ private void parseBasicCards() {
}
}

private void parseRuleCards() {
JSONArray cardArr = this.jsonFile.getJSONArray("ruleCards");
for (int i = 0; i < cardArr.length(); i++) {
String name = cardArr.getJSONObject(i).getString("name");
String description = cardArr.getJSONObject(i).getString("description");
int[] pcs4 = parsePcsArray(cardArr.getJSONObject(i).getJSONArray("max4plrs"));
int[] pcs8 = parsePcsArray(cardArr.getJSONObject(i).getJSONArray("max8plrs"));
this.cards.add(new RuleCard(name, description, pcs4, pcs8));
}
}

private int[] parsePcsArray(JSONArray json) {
int[] arr = new int[json.length()];
for (int i = 0; i < json.length(); i++) {
Expand Down
52 changes: 35 additions & 17 deletions Juomapeli/src/main/java/domain/GameServices.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package domain;

import domain.deck.Card;
import domain.deck.Deck;
import domain.deck.EndCard;
import domain.deck.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

/**
Expand All @@ -14,16 +13,19 @@
*/
public class GameServices {

private ArrayList<String> players;
private ArrayList<String> playerOrder;
private HashMap<String, Player> players;
private Deck deck;
private int turn;
private Card cardInTurn;
private ArrayList<RuleCard> activeRules;

/**
* Creates new game platform.
*/
public GameServices() {
this.players = new ArrayList<>();
this.playerOrder = new ArrayList<>();
this.players = new HashMap<>();
}

/**
Expand All @@ -35,14 +37,15 @@ public GameServices() {
*/
public String addPlayer(String playerName) {
playerName = playerName.trim();
if (this.players.contains(playerName)) {
if (this.playerOrder.contains(playerName)) {
return "Pelaaja on jo lisätty!";
} else if (playerName.length() < 1) {
return "Kirjoita pelaajan nimi!";
} else if (this.players.size() >= 8) {
} else if (this.playerOrder.size() >= 8) {
return "Peli on täynnä!";
}
this.players.add(playerName);
this.playerOrder.add(playerName);
this.players.put(playerName, new Player(playerName));
return "";
}

Expand All @@ -54,20 +57,23 @@ public String addPlayer(String playerName) {
* @return possible error message
*/
public String removePlayer(int playerNo) {
if (playerNo >= this.players.size() || playerNo < 0) {
if (playerNo >= this.playerOrder.size() || playerNo < 0) {
return "Pelaajan poistaminen epäonnistui!";
}
this.players.remove(playerNo);
this.players.remove(this.playerOrder.get(playerNo));
this.playerOrder.remove(playerNo);
return "";
}

/**
* Creates new Deck, based on the number of players in the game.
* Creates new Deck, based on the number of playerOrder in the game.
*/
public void initGame() {
this.deck = new Deck();
this.deck.generateNewDeck(this.getPlayerCount());
this.cardInTurn = this.deck.nextCard();
this.activeRules = new ArrayList<>();
this.turn--;
this.nextTurn();
}

/**
Expand All @@ -79,6 +85,9 @@ public void nextTurn() {
this.turn = 0;
}
this.cardInTurn = this.deck.nextCard();
if (this.cardInTurn.getType() == Card.RULE_CARD) {
this.activeRules.add((RuleCard) this.cardInTurn);
}
}

/**
Expand Down Expand Up @@ -112,16 +121,16 @@ public void forfeitPlayerInTurn() {
* @return number of players in the game
*/
public int getPlayerCount() {
return this.players.size();
return this.playerOrder.size();
}

/**
* Tells the names of the players in the game.
* Tells the names of the playerOrder in the game.
*
* @return ArrayList of the names of the players
* @return ArrayList of the names of the playerOrder
*/
public ArrayList<String> getPlayers() {
return this.players;
return this.playerOrder;
}

/**
Expand All @@ -141,6 +150,15 @@ public Card getCardInTurn() {
* @return name of the player currently in turn
*/
public String getPlayerInTurn() {
return this.players.get(this.turn);
return this.playerOrder.get(this.turn);
}

/**
* Tells the rules currently active.
*
* @return ArrayList of the currently active RuleCard-objects
*/
public ArrayList<RuleCard> getActiveRules() {
return this.activeRules;
}
}
19 changes: 19 additions & 0 deletions Juomapeli/src/main/java/domain/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package domain;

/**
* A player
*
* @author Eemeli
*/
public class Player {

private String name;

public Player(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
10 changes: 5 additions & 5 deletions Juomapeli/src/main/java/domain/deck/BasicCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public BasicCard(String name, String description, int[] pcs4, int[] pcs8) {
this.pcs8 = pcs8;
}

@Override
public int getType() {
return Card.BASIC_CARD;
}

@Override
public String getName() {
return this.name;
Expand All @@ -42,11 +47,6 @@ public String getDescription() {
return this.description;
}

@Override
public int getType() {
return Card.BASIC_CARD;
}

@Override
public int getPcsRandom(int playerCount) {
if (playerCount <= 4) {
Expand Down
4 changes: 4 additions & 0 deletions Juomapeli/src/main/java/domain/deck/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public interface Card {
* The card type constant for BasicCard
*/
static final int BASIC_CARD = 1;
/**
* The card type constant for RuleCard
*/
static final int RULE_CARD = 2;

/**
* Tells the type of the card.
Expand Down
35 changes: 30 additions & 5 deletions Juomapeli/src/main/java/domain/deck/Deck.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,17 @@ private void generateNewQuarters() {
switch (card.getType()) {
case Card.BASIC_CARD:
this.addBasicCard(card);
break;
case Card.RULE_CARD:
this.addRuleCard(card);
break;
}
}
}

private void addBasicCard(Card card) {
Random r = new Random();
for (int i = 0; i < card.getPcsRandom(playerCount); i++) {
for (int i = 0; i < card.getPcsRandom(this.playerCount); i++) {
switch (r.nextInt(4)) {
case 0:
this.pcsFirst.add(new BasicCard(card.getName(), card.getDescription(), new int[0], new int[0]));
Expand All @@ -99,20 +103,38 @@ private void addBasicCard(Card card) {
break;
}
}
for (int i = 0; i < card.getPcsFirst(playerCount); i++) {
for (int i = 0; i < card.getPcsFirst(this.playerCount); i++) {
this.pcsFirst.add(new BasicCard(card.getName(), card.getDescription(), new int[0], new int[0]));
}
for (int i = 0; i < card.getPcsSecond(playerCount); i++) {
for (int i = 0; i < card.getPcsSecond(this.playerCount); i++) {
this.pcsSecond.add(new BasicCard(card.getName(), card.getDescription(), new int[0], new int[0]));
}
for (int i = 0; i < card.getPcsThird(playerCount); i++) {
for (int i = 0; i < card.getPcsThird(this.playerCount); i++) {
this.pcsThird.add(new BasicCard(card.getName(), card.getDescription(), new int[0], new int[0]));
}
for (int i = 0; i < card.getPcsFourth(playerCount); i++) {
for (int i = 0; i < card.getPcsFourth(this.playerCount); i++) {
this.pcsFourth.add(new BasicCard(card.getName(), card.getDescription(), new int[0], new int[0]));
}
}

private void addRuleCard(Card card) {
Random r = new Random();
for (int i = 0; i < card.getPcsRandom(this.playerCount) + card.getPcsFirst(this.playerCount)
+ card.getPcsSecond(this.playerCount) + card.getPcsThird(this.playerCount) + card.getPcsFourth(this.playerCount); i++) {
switch (r.nextInt(3)) {
case 0:
this.pcsFirst.add(new RuleCard(card.getName(), card.getDescription(), new int[0], new int[0]));
break;
case 1:
this.pcsSecond.add(new RuleCard(card.getName(), card.getDescription(), new int[0], new int[0]));
break;
case 2:
this.pcsThird.add(new RuleCard(card.getName(), card.getDescription(), new int[0], new int[0]));
break;
}
}
}

/**
* Adds a card on top of the deck.
*
Expand All @@ -128,6 +150,9 @@ public void addCardOnTop(Card card) {
* @return Card-object from the top of the deck
*/
public Card nextCard() {
if (this.deck.isEmpty()) {
return new EndCard();
}
return this.deck.pollFirst();
}
}
Loading

0 comments on commit 65902e7

Please sign in to comment.