mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-05-15 17:11:48 +00:00
test(DevWeb): Add unit tests for Card and Deck
This commit is contained in:
@@ -4,55 +4,68 @@ import java.util.Calendar;
|
||||
import uppa.project.dao.DAO;
|
||||
import uppa.project.dao.DAOException;
|
||||
import uppa.project.dao.jpa.Game_JPA_DAO_Factory;
|
||||
import uppa.project.pojo.Card;
|
||||
import uppa.project.pojo.Deck;
|
||||
import uppa.project.pojo.User;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws DAOException {
|
||||
try {
|
||||
Game_JPA_DAO_Factory jpaDaoFactory = new Game_JPA_DAO_Factory();
|
||||
DAO<User> daoJpaUser = jpaDaoFactory.getDAOUser();
|
||||
// DAO<Game> daoJpaGame = jpaDaoFactory.getDAOGame();
|
||||
// DAO<Player> daoJpaPlayer = jpaDaoFactory.getDAOPlayer();
|
||||
|
||||
// // Contenu de la BD au début
|
||||
// User[] users = daoJpaUser.findAll();
|
||||
// for (User u : users) {
|
||||
// System.out.println(u.toString());
|
||||
// }
|
||||
// System.out.println();
|
||||
//
|
||||
// Ajout d'User :
|
||||
Calendar cal1 = Calendar.getInstance();
|
||||
cal1.set(1996, Calendar.FEBRUARY, 20);
|
||||
User user1 = new User("Kevin","kmitresse@gmail.com", "Mitresse", cal1.getTime(), User.Gender.MALE);
|
||||
|
||||
Calendar cal2 = Calendar.getInstance();
|
||||
cal2.set(2002, Calendar.JUNE, 28);
|
||||
User user2 = new User("Lucàs", "lucas@gmail.com" ,"Vabre", cal2.getTime(), User.Gender.MALE);
|
||||
|
||||
daoJpaUser.create(user1);
|
||||
daoJpaUser.create(user2);
|
||||
|
||||
// System.out.println("test récupération user");
|
||||
// User[] users2 = daoJpaUser.findByField("username", "Kevin");
|
||||
// for (User user : users2) {
|
||||
// System.out.println(user.toString());
|
||||
// }
|
||||
// System.out.println("fin test");
|
||||
} catch (DAOException e) {
|
||||
throw new RuntimeException(e);
|
||||
Deck deck = new Deck(3,10);
|
||||
for (Card card : deck.getCards()){
|
||||
System.out.println(card.toString());
|
||||
}
|
||||
// Contenu de la BD après ajout
|
||||
// users = daoJpaUser.findAll();
|
||||
// for (User u : users) {
|
||||
// System.out.println(u.toString());
|
||||
// }
|
||||
// System.out.println();
|
||||
|
||||
// for (int index = 0; index < 4* 3; index++) {
|
||||
// int i = index % 4;
|
||||
// int j = index / 3;
|
||||
//
|
||||
// EntityManagerProvider.close();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// System.out.println("i= " + i + " j = " + j);
|
||||
// }
|
||||
// try {
|
||||
// Game_JPA_DAO_Factory jpaDaoFactory = new Game_JPA_DAO_Factory();
|
||||
// DAO<User> daoJpaUser = jpaDaoFactory.getDAOUser();
|
||||
//// DAO<Game> daoJpaGame = jpaDaoFactory.getDAOGame();
|
||||
//// DAO<Player> daoJpaPlayer = jpaDaoFactory.getDAOPlayer();
|
||||
//
|
||||
//// // Contenu de la BD au début
|
||||
//// User[] users = daoJpaUser.findAll();
|
||||
//// for (User u : users) {
|
||||
//// System.out.println(u.toString());
|
||||
//// }
|
||||
//// System.out.println();
|
||||
////
|
||||
// // Ajout d'User :
|
||||
// Calendar cal1 = Calendar.getInstance();
|
||||
// cal1.set(1996, Calendar.FEBRUARY, 20);
|
||||
// User user1 = new User("Kevin","kmitresse@gmail.com", "Mitresse", cal1.getTime(), User.Gender.MALE);
|
||||
//
|
||||
// Calendar cal2 = Calendar.getInstance();
|
||||
// cal2.set(2002, Calendar.JUNE, 28);
|
||||
// User user2 = new User("Lucàs", "lucas@gmail.com" ,"Vabre", cal2.getTime(), User.Gender.MALE);
|
||||
//
|
||||
// daoJpaUser.create(user1);
|
||||
// daoJpaUser.create(user2);
|
||||
//
|
||||
//// System.out.println("test récupération user");
|
||||
//// User[] users2 = daoJpaUser.findByField("username", "Kevin");
|
||||
//// for (User user : users2) {
|
||||
//// System.out.println(user.toString());
|
||||
//// }
|
||||
//// System.out.println("fin test");
|
||||
// } catch (DAOException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
// // Contenu de la BD après ajout
|
||||
//// users = daoJpaUser.findAll();
|
||||
//// for (User u : users) {
|
||||
//// System.out.println(u.toString());
|
||||
//// }
|
||||
//// System.out.println();
|
||||
////
|
||||
//// EntityManagerProvider.close();
|
||||
//// } catch (Exception e) {
|
||||
//// e.printStackTrace();
|
||||
//// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ public class Card {
|
||||
/**
|
||||
* Valeurs disponibles pour les cartes
|
||||
*/
|
||||
public enum Value{ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING}
|
||||
public enum Value{ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING}
|
||||
|
||||
/**
|
||||
* Couleur de la carte
|
||||
|
||||
@@ -32,7 +32,30 @@ public class Deck {
|
||||
* @see Card.Value
|
||||
*/
|
||||
public Deck(int nbColors, int nbValues) {
|
||||
cards = initializeDeck(nbColors, nbValues);
|
||||
if (!Deck.isDeckValid(nbColors, nbValues)) {
|
||||
throw new IllegalArgumentException("Nombre de couleurs et/ou nombre de valeurs invalide(s)");
|
||||
}
|
||||
|
||||
this.cards = new ArrayList<>(nbColors * nbValues);
|
||||
|
||||
for (int nbCard = 0; nbCard < nbColors * nbValues; nbCard++) {
|
||||
int color = nbCard % nbColors;
|
||||
int value = nbCard / nbColors;
|
||||
|
||||
cards.add(new Card(Card.Color.values()[color], Card.Value.values()[value]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prédicat qui vérifie si un Deck est correctement initialisé
|
||||
*
|
||||
* @param nbColors Nombre de couleurs {@link Card.Color}
|
||||
* @param nbValues Nombre de valeurs {@link Card.Value}
|
||||
* @return true si le prédicat est vérifié, false sinon
|
||||
*/
|
||||
public static boolean isDeckValid(int nbColors, int nbValues) {
|
||||
return 1 <= nbColors && nbColors <= Card.Color.values().length
|
||||
&& 1 <= nbValues && nbValues <= Card.Value.values().length;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,57 +65,14 @@ public class Deck {
|
||||
return cards;
|
||||
}
|
||||
|
||||
/**
|
||||
* Créé un paquet de cartes mélangé avec un nombre de couleurs et de valeurs donné
|
||||
*
|
||||
* @param nbColors nombre de couleurs (doit être compris entre 1 et le nombre de couleurs de {@link Card.Color})
|
||||
* @param nbValues nombre de valeurs (doit être compris entre 1 et le nombre de valeurs de {@link Card.Value})
|
||||
* @return un ensemble de cartes mélangées
|
||||
*/
|
||||
private static ArrayList<Card> initializeDeck(int nbColors, int nbValues) {
|
||||
ArrayList<Card> cards = createSetOfCard(nbColors, nbValues);
|
||||
shuffleSetOfCard(cards);
|
||||
return cards;
|
||||
}
|
||||
|
||||
/**
|
||||
* Créé un ensemble de cartes avec un nombre de couleurs et de valeurs donné
|
||||
*
|
||||
* @param nbColors nombre de couleurs à utiliser pour créer les cartes (doit être compris entre 1 et le nombre de couleurs de {@link Card.Color})
|
||||
* @param nbValues nombre de valeurs à utiliser pour créer les cartes (doit être compris entre 1 et le nombre de valeurs de {@link Card.Value})
|
||||
* @see Card.Color
|
||||
* @see Card.Value
|
||||
* @throws IllegalArgumentException si le nombre de couleurs ou de valeurs est incorrect
|
||||
* @return un ensemble de cartes
|
||||
*/
|
||||
public static ArrayList<Card> createSetOfCard(int nbColors, int nbValues) throws IllegalArgumentException {
|
||||
ArrayList<Card> cards = new ArrayList<>(nbColors*nbValues);
|
||||
|
||||
if (nbColors < 1 || nbColors > Card.Color.values().length) {
|
||||
throw new IllegalArgumentException("Le nombre de couleurs doit être compris entre 1 et " + Card.Color.values().length);
|
||||
}
|
||||
if (nbValues < 1 || nbValues > Card.Value.values().length) {
|
||||
throw new IllegalArgumentException("Le nombre de valeurs doit être compris entre 1 et " + Card.Value.values().length);
|
||||
}
|
||||
|
||||
for (int i = 0; i < nbColors; i++) {
|
||||
for (int j = 0; j < nbValues; j++) {
|
||||
cards.add(new Card(Card.Color.values()[i], Card.Value.values()[j]));
|
||||
}
|
||||
}
|
||||
return cards;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mélange les cartes d'un paquet
|
||||
*
|
||||
* @param cards ensemble de cartes à mélanger
|
||||
* @return un ensemble de cartes mélangées
|
||||
*/
|
||||
public static void shuffleSetOfCard(ArrayList<Card> cards) {
|
||||
Collections.shuffle(cards);
|
||||
public void shuffle() {
|
||||
Collections.shuffle(this.cards);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
@@ -104,7 +84,7 @@ public class Deck {
|
||||
if (cards.get(i).getColor() == deck.cards.get(j).getColor()
|
||||
&& cards.get(i).getValue() == deck.cards.get(j).getValue()) {
|
||||
counter++;
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,6 +94,7 @@ public class Game implements Serializable {
|
||||
this.nbColors = nbColors;
|
||||
this.nbValuesPerColor = nbValuesPerColor;
|
||||
this.deck = new Deck(nbColors, nbValuesPerColor);
|
||||
this.deck.shuffle();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -80,6 +80,7 @@ public class Player implements Serializable {
|
||||
this.rightClickCount = 0;
|
||||
this.rapidClickCount = 0;
|
||||
this.deck = new Deck(game.getNbColors(), game.getNbValuesPerColor());
|
||||
this.deck.shuffle();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -345,7 +345,13 @@ public class User implements Serializable {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("User{username='%s', birth=%s, gender=%s}", username, birth.toString(), gender.toString());
|
||||
return String.format(
|
||||
"User{id = '%s', username='%s', email='%s', birth=%s, gender=%s}",
|
||||
id != null ? id.toString() : "null",
|
||||
username,
|
||||
birth.toString(),
|
||||
gender.toString()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -353,8 +359,14 @@ public class User implements Serializable {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof User)) return false;
|
||||
User user = (User) o;
|
||||
return
|
||||
Objects.equals(getId(), user.getId()) && Objects.equals(getUsername(), user.getUsername()) && Objects.equals(getEmail(), user.getEmail()) && Objects.equals(getPassword(), user.getPassword()) && Objects.equals(getBirth(), user.getBirth()) && getGender() == user.getGender();
|
||||
return Objects.equals(id, user.id)
|
||||
&& Objects.equals(username, user.username)
|
||||
&& Objects.equals(email, user.email)
|
||||
&& Objects.equals(password, user.password)
|
||||
&& Objects.equals(birth, user.birth)
|
||||
&& gender == user.gender
|
||||
&& Objects.equals(playedGames, user.playedGames)
|
||||
&& Objects.equals(recoveryPasswordTokens, user.recoveryPasswordTokens);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user