Commit 82fd2d9f authored by Felix RABADAN's avatar Felix RABADAN
Browse files

Merge branch 'main' of gitlab.univ-nantes.fr:E188013M/2021-kervestin-rabadan-java into main

parents 59195a5c 35df51e4
......@@ -17,7 +17,8 @@ public class Game {
CardController cardController = CardController.getInstance();
Player p = new Player("Paul Pogba", Position.COURT_FIELD_PLAYER, (float) 5.0);
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);
......@@ -39,17 +40,31 @@ public class Game {
Market market = Market.getInstance();
Admin admin = Admin.getInstance();
admin.putOnMarket(new Pair<Card, Integer>(card1, 1000));
admin.putOnMarket(new Pair<Card, Integer>(card1, 505));
// admin.putOnMarket(new Pair<Card, Integer>(card1, 1000));
market.displayBuyableCards();
felix.addCard(card1);
market.displayBuyableCards();
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();
System.out.println("#### choose cards");
felix.chooseDeck();
// 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 {
// Parser.parseTeam("CSV\\2021_46_Nantes.csv");
......
package game;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import game.Card.Rarity;
public class Market {
private static Market instance = null;
private ArrayList<Pair<Card, Integer>> buyableCards;
private HashMap<Pair<User, Card>, Pair<String, Rarity>> exchangeableCards;
private Market() {
this.buyableCards = new ArrayList<Pair<Card, Integer>>();
this.exchangeableCards = new HashMap<Pair<User, Card>, Pair<String, Rarity>>();
}
public static Market getInstance() {
......@@ -18,15 +24,35 @@ public class Market {
}
public void displayBuyableCards() {
int i = 0;
System.out.println("The market :");
for (Pair<Card, Integer> pair : buyableCards) {
System.out.println(i + " - " + pair.toString());
i++;
System.out.println("Sales :");
if (!buyableCards.isEmpty()) {
int i = 0;
for (Pair<Card, Integer> pair : buyableCards) {
System.out.println(i + " - " + pair.toString());
i++;
}
} else {
System.out.println("no sales");
}
}
public void displayExchangeableCards() {
System.out.println("Exchanges :");
if (!exchangeableCards.isEmpty()) {
for (Map.Entry<Pair<User, Card>, Pair<String, Rarity>> set : exchangeableCards.entrySet()) {
System.out.println("- " + set.getKey().getFst().toString() + " wants to exchange ("
+ set.getKey().getSnd().toString() + ") with " + set.getValue().toString());
}
} else {
System.err.println("no offers");
}
}
public ArrayList<Pair<Card, Integer>> getBuyableCards() {
return this.buyableCards;
}
public HashMap<Pair<User, Card>, Pair<String, Rarity>> getExchangeableCards() {
return this.exchangeableCards;
}
}
......@@ -32,7 +32,6 @@ public class Player {
} else {
System.err.println("[]");
}
}
public String getName() {
......
......@@ -3,6 +3,7 @@ package game;
import java.util.ArrayList;
import java.util.Scanner;
import game.Card.Rarity;
import game.Player.Position;
public class User extends Member {
......@@ -10,6 +11,8 @@ public class User extends Member {
private ArrayList<Card> deck;
private Card[] team;
private Scanner scanner = new Scanner(System.in);
public User(String name) {
super(name);
this.balance = 10000;
......@@ -17,6 +20,10 @@ public class User extends Member {
this.team = new Card[4];
}
public String toString() {
return super.name;
}
public void displayDeck() {
int i = 0;
System.out.println("My deck :");
......@@ -75,8 +82,6 @@ public class User extends Member {
}
}
private Scanner scanner = new Scanner(System.in);
public void sell() {
System.out.println("Which card would you like to sell ?");
displayDeck();
......@@ -87,4 +92,57 @@ public class User extends Member {
System.out.println("Your card is on the market now");
deck.remove(chosenCard);
}
public void buy() {
System.out.println("Which card would you like to buy ?");
Market.getInstance().displayBuyableCards();
int choice = scanner.nextInt();
Pair<Card, Integer> chosenCard = Market.getInstance().getBuyableCards().get(choice);
int price = chosenCard.getSnd();
System.out.println("It will costs " + price + " coins\nAre you sure ? (yes/no)");
String confirm = scanner.next();
if (confirm.equals("yes")) {
if (balance >= price) {
balance -= chosenCard.getSnd();
addCard(chosenCard.getFst());
System.out.println("This card is yours now ! You still have " + balance + " coins");
Market.getInstance().getBuyableCards().remove(chosenCard);
} else {
System.err.println("Operation cancelled : you don't have enough coins to buy this card");
}
} else {
System.err.println("Operation cancelled");
}
}
public void exchange() {
System.out.println("Which card would you like to exchange ?");
displayDeck();
int choice = scanner.nextInt();
Card chosenCard = deck.get(choice);
scanner.nextLine(); // it consumes the \n character
System.out.println("With which card ?\nName of the player :");
String namePlayer = scanner.nextLine();
System.out.println("Rarity (common/less_common/rare) :");
String str_rarity = scanner.nextLine();
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()) {
Market.getInstance().getExchangeableCards().forEach((k, v) -> {
if (k.snd.getPlayer().getName().equals(namePlayer) && k.snd.getRarity().equals(rarity)) {
deck.add(k.snd);
k.fst.getDeck().remove(k.snd);
k.fst.addCard(chosenCard);
deck.remove(chosenCard);
Market.getInstance().getExchangeableCards().remove(k); // bug
} else {
Market.getInstance().getExchangeableCards().put(new Pair<User, Card>(this, chosenCard), mapValue);
System.out.println("Your card is now on exchange");
}
});
} else {
Market.getInstance().getExchangeableCards().put(new Pair<User, Card>(this, chosenCard), mapValue);
System.out.println("Your card is now on exchange");
}
}
}
\ No newline at end of file
......@@ -8,7 +8,7 @@ import game.User;
public final class UserController {
private static UserController instance = null;
private HashMap<String, User> users;
private Member connectedUser;
private Member connectedMember;
private UserController() {
users = new HashMap<String, User>();
......@@ -40,20 +40,16 @@ public final class UserController {
}
public void connect(Member user) {
if (connectedUser == null) {
this.connectedUser = user;
if (connectedMember == null) {
this.connectedMember = user;
System.out.println(user.getName() + " is connected");
} else {
System.out.println("Connexion error : another user is already connected");
}
}
public void disconnect(User user) {
if (connectedUser != null) {
this.connectedUser = null;
System.out.println(user.getName() + " is disconnected");
} else {
System.out.println("Disconnexion error : nobody is connected");
}
public void disconnect() {
System.out.println(connectedMember.getName() + " is disconnected");
connectedMember = null;
}
}
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