Commit 187d44b9 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 4b68be73 ea9e95dc
......@@ -2,6 +2,8 @@ package game;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import game.Card.Rarity;
import game.controllers.CardController;
import game.controllers.UserController;
......@@ -21,9 +23,13 @@ public class Admin extends Member {
return instance;
}
//TODO rajouter mettre en vente une carte en utilisant createCard
public void putOnMarket() { // the administrator can only creat new random cards with random prices
Card new_card = CardController.getInstance().createRandomCard();
Market.getInstance().getBuyableCards().add(new Triple<Card, Integer, User>(new_card,
new Random().nextInt(1000) + 1, null));
}
public void newWeek(String pathname){
public void newWeek(String pathname) {
/**
* Launch a next week, and make every actions necessary
*/
......
......@@ -2,13 +2,13 @@ package game;
public class Card {
public enum Rarity {
COMMON("1f"),
LESS_COMMON("1.05f"),
RARE("1.1f");
COMMON(1f),
LESS_COMMON(1.05f),
RARE(1.1f);
public final String factor;
public final float factor;
private Rarity(String factor) {
private Rarity(float factor) {
this.factor = factor;
}
}
......@@ -25,7 +25,6 @@ public class Card {
this.player = player;
}
public Object getId() {
return this.id;
}
......@@ -42,10 +41,11 @@ public class Card {
return this.player;
}
@Override
public String toString() {
return "Card [id=" + id + ", number=" + number + ", player=" + player.getName() + ", rarity=" + rarity + ", score=" + player.getScore() + "]";
return "Card [id=" + id + ", number=" + number + ", player=" + player.getName() + ", position = "
+ player.getPosition() + ", rarity=" + rarity
+ ", score=" + player.getScore() + "]";
}
}
......@@ -4,7 +4,6 @@ import game.controllers.*;
import java.io.FileNotFoundException;
import game.Card.Rarity;
import game.Player.Position;
public class Game {
......@@ -45,9 +44,9 @@ public class Game {
malo.addCard(cardController.createCard(2, Card.Rarity.RARE, axel));
prof.addCard(cardController.createCard(3, Card.Rarity.RARE, axel));
// connect test
felix.chooseDeck();
malo.chooseDeck();
prof.chooseDeck();
felix.chooseTeam();
malo.chooseTeam();
prof.chooseTeam();
admin.newWeek("CSV\\2021_42\\2021_42_Nantes.csv");// first week
felix.displayDeck();//on voit bien la nouvelle carte rare en plus
......
......@@ -8,11 +8,11 @@ import game.Card.Rarity;
public class Market {
private static Market instance = null;
private ArrayList<Pair<Card, Integer>> buyableCards;
private ArrayList<Triple<Card, Integer, User>> buyableCards;
private HashMap<Pair<User, Card>, Pair<String, Rarity>> exchangeableCards;
private Market() {
this.buyableCards = new ArrayList<Pair<Card, Integer>>();
this.buyableCards = new ArrayList<Triple<Card, Integer, User>>();
this.exchangeableCards = new HashMap<Pair<User, Card>, Pair<String, Rarity>>();
}
......@@ -27,8 +27,8 @@ public class Market {
System.out.println("Sales :");
if (!buyableCards.isEmpty()) {
int i = 0;
for (Pair<Card, Integer> pair : buyableCards) {
System.out.println(i + " - " + pair.toString());
for (Triple<Card, Integer, User> triple : buyableCards) {
System.out.println(i + " - " + triple);
i++;
}
} else {
......@@ -48,7 +48,7 @@ public class Market {
}
}
public ArrayList<Pair<Card, Integer>> getBuyableCards() {
public ArrayList<Triple<Card, Integer, User>> getBuyableCards() {
return this.buyableCards;
}
......
package game;
public class Member {
public abstract class Member {
protected String name;
public Member(String name) {
this.name = name;
}
public void putOnMarket(Pair<Card, Integer> pair) {
Market.getInstance().getBuyableCards().add(pair);
}
public String getName() {
return this.name;
}
......
package game;
public class Triple<T, U, V> {
private final T first;
private final U second;
private final V third;
public Triple(T first, U second, V third) {
this.first = first;
this.second = second;
this.third = third;
}
public T getFst() {
return first;
}
public U getSnd() {
return second;
}
public V getTrd() {
return third;
}
public String toString() {
return "(" + getFst() + ", " + getSnd() + ", " + getTrd() + ")";
}
}
......@@ -30,7 +30,7 @@ public class User extends Member {
System.out.println("My deck :");
if (!deck.isEmpty()) {
for (Card card : deck) {
System.out.println(i + " - " + card.toString());
System.out.println(i + " - " + card);
i++;
}
} else {
......@@ -38,10 +38,14 @@ public class User extends Member {
}
}
public void getBalance() {
public void displayBalance() {
System.out.println("Your balance is " + balance + " coins");
}
public void setBalance(int val) {
this.balance += val;
}
public ArrayList<Card> getDeck() {
return this.deck;
}
......@@ -53,10 +57,19 @@ public class User extends Member {
}
private Card chooseCard() {
int choice;
boolean error = true;
int choice = 0;
do {
choice = scanner.nextInt();
} while (choice >= deck.size());
try {
do {
choice = scanner.nextInt();
} while (choice >= deck.size());
error = false;
} catch (Exception e) {
System.err.println("Error : enter a number");
scanner.next();
}
} while (error);
return deck.get(choice);
}
......@@ -79,60 +92,71 @@ public class User extends Member {
return deck.get(choice);
}
public void chooseDeck() {
public void chooseTeam() {
if (deck.size() < 4) {
System.out.println("You don't have enough cards to create a team");
} else {
System.out.println("Which goalkeeper do you want to add to your deck");
System.out.println("Chose your goalkeeper in your deck :");
displayDeck();
team[0] = chooseCardToDeck(Position.GOAL_KEEPER);
for (int i = 1; i < team.length; i++) {
System.out.printf("Choose your nb%d court field player\n", i);
System.out.printf("Choose your court field player number %d : \n", i);
displayDeck();
team[i] = chooseCardToDeck(Position.COURT_FIELD_PLAYER);
}
}
}
public void putOnMarket(Triple<Card, Integer, User> triple) {
Market.getInstance().getBuyableCards().add(triple);
}
public void sell() {
System.out.println("Which card would you like to sell ?");
displayDeck();
Card chosenCard = chooseCard();
System.out.println("At which price ?");
int price = scanner.nextInt();
putOnMarket(new Pair<Card, Integer>(chosenCard, price));
System.out.println("Your card is on the market now");
deck.remove(chosenCard);
if (!deck.isEmpty()) {
System.out.println("Which card would you like to sell ?");
displayDeck();
Card chosenCard = chooseCard();
System.out.println("At which price ?");
int price = scanner.nextInt();
putOnMarket(new Triple<Card, Integer, User>(chosenCard, price, this));
System.out.println("Your card is on the market now");
deck.remove(chosenCard);
} else {
System.err.println("Your deck is empty");
}
}
public void buy() {
// TODO donner largent au vendeur et faire un stockage avec un tuple
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);
if (!Market.getInstance().getBuyableCards().isEmpty()) {
System.out.println("Which card would you like to buy ?");
Market.getInstance().displayBuyableCards();
int choice = scanner.nextInt();
Triple<Card, Integer, User> 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();
chosenCard.getTrd().setBalance(price);
chosenCard.getTrd().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 : you don't have enough coins to buy this card");
System.err.println("Operation cancelled");
}
} else {
System.err.println("Operation cancelled");
System.err.println("There is no card to sell");
}
}
public void exchange() {
System.out.println("Which card would you like to exchange ?");
displayDeck();
int choice = scanner.nextInt();
Card chosenCard = deck.get(choice);
Card chosenCard = chooseCard();
scanner.nextLine(); // it consumes the \n character
System.out.println("With which card ?\nName of the player :");
String namePlayer = scanner.nextLine();
......@@ -145,9 +169,9 @@ public class User extends Member {
.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
if (set.getKey().snd.getPlayer().getName().equalsIgnoreCase(namePlayer) && set
.getKey().snd.getRarity().equals(rarity)
&& set.getValue().fst.equals(chosenCard.getPlayer().getName())
&& set.getValue().fst.equalsIgnoreCase(chosenCard.getPlayer().getName())
&& set.getValue().snd.equals(chosenCard.getRarity())) {
deck.add(set.getKey().snd);
set.getKey().fst.getDeck().remove(set.getKey().snd);
......@@ -169,9 +193,8 @@ public class User extends Member {
float sum = 0;
for (Card c : team) {
if (c != null) {
sum += c.getPlayer().getScore();
sum += c.getPlayer().getScore() * c.getRarity().factor;
}
// TODO prendre en compte la rarete genre 1.05?
}
teamScore = sum / (float) team.length;
}
......
......@@ -17,43 +17,42 @@ public class CardController {
return instance;
}
public Card createCard(int number, Card.Rarity rarity, Player player) {
String new_key = "" + number + rarity;
if (!player.getCards().containsKey(new_key)) {
public Card controlNbMaxCards(String new_key, int number, Player player, Rarity rarity) {
int maxBound = 0;
switch (rarity) {
case COMMON:
maxBound = 1000;
break;
case LESS_COMMON:
maxBound = 100;
break;
case RARE:
maxBound = 10;
break;
default:
break;
}
if (0 < number && number < maxBound) {
Card new_card = new Card(number, rarity, player);
switch (rarity) {
case COMMON:
// TODO moyen de faire des sous fonctions pour eviter la repition du code
if (player.getNbCommonCards() < 1000 && 0 < number && number < 1000) {
player.getCards().put(new_key, new_card);
player.setNbCommonCards();
} else {
System.err.println("The player " + player.getName() + " has too many common cards");
return null;
}
break;
case LESS_COMMON:
if (player.getNbLessCommonCards() < 100 && 0 < number && number < 100) {
player.getCards().put(new_key, new_card);
player.setNbLessCommonCards();
} else {
System.err.println("The player " + player.getName() + " has too many less common cards");
return null;
}
break;
case RARE:
if (player.getNbRareCards() < 10 && 0 < number && number < 10) {
player.getCards().put(new_key, new_card);
player.setNbRareCards();
} else {
System.err.println("The player " + player.getName() + " has too many rare cards");
return null;
}
break;
default:
break;
if (player.getNbCommonCards() < maxBound) {
player.getCards().put(new_key, new_card);
player.setNbCommonCards();
} else {
System.err.println("The player " + player.getName() + " has too many " + rarity + " cards");
return null;
}
return new_card;
} else {
System.err.println("Error : card's number incorrect");
return null;
}
}
public Card createCard(int number, Card.Rarity rarity, Player player) {
String new_key = "" + number + rarity;
if (!player.getCards().containsKey(new_key)) {
return controlNbMaxCards(new_key, number, player, rarity);
} else {
System.err.println("This card already exists");
return 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