Commit f6c456c4 authored by Malo KERVESTIN's avatar Malo KERVESTIN
Browse files

création de getTeamScore() dans User + classement dans Admin

parent a31ecb55
package game;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import game.controllers.UserController;
public class Admin extends Member {
private static Admin instance = null;
......@@ -13,4 +21,26 @@ public class Admin extends Member {
}
return instance;
}
public void rank() {
ArrayList<Float> teamScores = new ArrayList<Float>();
for (User user : UserController.getInstance().getUsers().values()) {
teamScores.add(user.getTeamScore());
}
Collections.sort(teamScores);
LinkedHashMap<User, Float> classification = new LinkedHashMap<User, Float>();
for (Float score : teamScores) {
for (User user : UserController.getInstance().getUsers().values()) {
if (user.getTeamScore() == score) {
classification.put(user, score);
}
}
}
System.out.println("### Classement ###");
int i = 1;
for (Map.Entry<User, Float> set : classification.entrySet()) {
System.out.println(i + " - " + set.getKey().getName() + " : " + set.getValue());
i++;
}
}
}
......@@ -2,68 +2,76 @@ package game;
import game.controllers.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import game.Player.Position;
public class Game {
public static void main(String[] args) {
UserController userController = UserController.getInstance();
User felix = userController.addUser("felix");
User malo = userController.addUser("malo");
userController.addUser("felix");
// UserController userController = UserController.getInstance();
userController.connect(felix);
userController.connect(malo);
// User felix = userController.addUser("felix");
// User malo = userController.addUser("malo");
// userController.addUser("felix");
CardController cardController = CardController.getInstance();
// userController.connect(felix);
// userController.connect(malo);
Player p = new Player("Paul Pogba", Position.COURT_FIELD_PLAYER, (float) 5.0); // pq mettre le score dans le
// constructeur ?
Card card1 = cardController.createCard(1, Card.Rarity.COMMON, p);
cardController.createCard(1, Card.Rarity.COMMON, p);
cardController.removeCard(card1);
// CardController cardController = CardController.getInstance();
cardController.createCard(1, Card.Rarity.RARE, p);
cardController.createCard(2, Card.Rarity.RARE, p);
cardController.createCard(3, Card.Rarity.RARE, p);
cardController.createCard(4, Card.Rarity.RARE, p);
cardController.createCard(5, Card.Rarity.RARE, p);
cardController.createCard(6, Card.Rarity.RARE, p);
cardController.createCard(7, Card.Rarity.RARE, p);
cardController.createCard(8, Card.Rarity.RARE, p);
cardController.createCard(9, Card.Rarity.RARE, p);
cardController.createCard(10, Card.Rarity.RARE, p);
cardController.createCard(11, Card.Rarity.RARE, p);
System.out.println("####");
// p.displayCards();
// Player p = new Player("Paul Pogba", Position.COURT_FIELD_PLAYER, (float)
// 5.0); // pq mettre le score dans le
// // constructeur ?
// Card card1 = cardController.createCard(1, Card.Rarity.COMMON, p);
// cardController.createCard(1, Card.Rarity.COMMON, p);
// cardController.removeCard(card1);
Market market = Market.getInstance();
Admin admin = Admin.getInstance();
// cardController.createCard(1, Card.Rarity.RARE, p);
// cardController.createCard(2, Card.Rarity.RARE, p);
// cardController.createCard(3, Card.Rarity.RARE, p);
// cardController.createCard(4, Card.Rarity.RARE, p);
// cardController.createCard(5, Card.Rarity.RARE, p);
// cardController.createCard(6, Card.Rarity.RARE, p);
// cardController.createCard(7, Card.Rarity.RARE, p);
// cardController.createCard(8, Card.Rarity.RARE, p);
// cardController.createCard(9, Card.Rarity.RARE, p);
// cardController.createCard(10, Card.Rarity.RARE, p);
// cardController.createCard(11, Card.Rarity.RARE, p);
// System.out.println("####");
// // p.displayCards();
// admin.putOnMarket(new Pair<Card, Integer>(card1, 1000));
market.displayBuyableCards();
// Market market = Market.getInstance();
// Admin admin = Admin.getInstance();
Card card2 = cardController.createCard(1, Card.Rarity.RARE,
new Player("Kylian Mbappe", Position.COURT_FIELD_PLAYER, (float) 5.0));
felix.addCard(card2);
// market.displayBuyableCards();
market.displayExchangeableCards();
// felix.sell();
// felix.buy();
felix.exchange();
felix.displayDeck();
// // admin.putOnMarket(new Pair<Card, Integer>(card1, 1000));
// market.displayBuyableCards();
market.displayExchangeableCards();
userController.disconnect();
userController.connect(malo);
malo.addCard(card1);
malo.exchange();
malo.displayDeck();
userController.disconnect();
userController.connect(felix);
felix.displayDeck();
market.displayExchangeableCards();
// Card card2 = cardController.createCard(1, Card.Rarity.RARE,
// new Player("Kylian Mbappe", Position.COURT_FIELD_PLAYER, (float) 5.0));
// felix.addCard(card2);
// // market.displayBuyableCards();
// market.displayExchangeableCards();
// // felix.sell();
// // felix.buy();
// felix.exchange();
// felix.displayDeck();
// // market.displayBuyableCards();
// market.displayExchangeableCards();
// userController.disconnect();
// userController.connect(malo);
// malo.addCard(card1);
// malo.exchange();
// malo.displayDeck();
// userController.disconnect();
// userController.connect(felix);
// felix.displayDeck();
// market.displayExchangeableCards();
// test parser
// try {
......@@ -72,5 +80,6 @@ public class Game {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
......@@ -42,6 +42,10 @@ public class Player {
return this.position;
}
public float getScore() {
return this.score;
}
public HashMap<String, Card> getCards() {
return this.cards;
}
......
......@@ -3,7 +3,6 @@ package game;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;
import game.Card.Rarity;
import game.Player.Position;
......@@ -129,13 +128,19 @@ public class User extends Member {
Rarity rarity = Rarity.valueOf(str_rarity.toUpperCase()); // convert String to enum
Pair<String, Rarity> mapValue = new Pair<String, Rarity>(namePlayer, rarity);
if (!Market.getInstance().getExchangeableCards().isEmpty()) {
for (Pair<User, Card> mapKey : Market.getInstance().getExchangeableCards().keySet()) { // iterate on keys
if (mapKey.snd.getPlayer().getName().equals(namePlayer) && mapKey.snd.getRarity().equals(rarity)) {
deck.add(mapKey.snd);
mapKey.fst.getDeck().remove(mapKey.snd);
mapKey.fst.addCard(chosenCard);
for (Map.Entry<Pair<User, Card>, Pair<String, Rarity>> set : Market.getInstance().getExchangeableCards()
.entrySet()) {
// si 2 utilisateurs veulent chacun ce que l'autre offre alors on réalise
// l'échange sans demander leurs avis
if (set.getKey().snd.getPlayer().getName().equals(namePlayer) && set
.getKey().snd.getRarity().equals(rarity)
&& set.getValue().fst.equals(chosenCard.getPlayer().getName())
&& set.getValue().snd.equals(chosenCard.getRarity())) {
deck.add(set.getKey().snd);
set.getKey().fst.getDeck().remove(set.getKey().snd);
set.getKey().fst.addCard(chosenCard);
deck.remove(chosenCard);
Market.getInstance().getExchangeableCards().remove(mapKey);
Market.getInstance().getExchangeableCards().remove(set.getKey());
} else {
Market.getInstance().getExchangeableCards().put(new Pair<User, Card>(this, chosenCard), mapValue);
System.out.println("Your card is now on exchange");
......@@ -146,4 +151,12 @@ public class User extends Member {
System.out.println("Your card is now on exchange");
}
}
public float getTeamScore() {
float sum = 0;
for (Card c : team) {
sum += c.getPlayer().getScore();
}
return sum / (float) team.length;
}
}
\ No newline at end of file
......@@ -21,6 +21,10 @@ public final class UserController {
return instance;
}
public HashMap<String, User> getUsers() {
return this.users;
}
public User addUser(String name) {
if (!users.containsKey(name)) {
User new_user = new User(name);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment