Skip to content

Commit

Permalink
improvement of the choice system and javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
profumato4 committed Jun 17, 2024
1 parent 077acd7 commit 2f1a7bf
Showing 1 changed file with 68 additions and 5 deletions.
73 changes: 68 additions & 5 deletions src/utils/SelectCardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,37 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

/**
* The SelectCardManager class manages the selection of cards for a player in the game of Briscola.
* It determines the best card to play based on the current game state and the opponent's actions.
*/

public class SelectCardManager {

private final String briscola;
private final JButton[] cardsButtons;

/**
* Constructor for the SelectCardManager class.
*
* @param briscola the briscola suit for the current game
* @param cardsButtons the array of buttons representing the player's cards
*/

public SelectCardManager(String briscola, JButton[] cardsButtons) {
this.cardsButtons = cardsButtons;
this.briscola = briscola;
}

/**
* Selects the best card to play for the current player.
*
* @param g2 the current player
* @param g1 the opponent player
* @return the index of the selected card
*/

public int selectCard(Giocatore g2, Giocatore g1) {
if (g1.isLanciata()) {
int thrownCardIndex = findThrownCard();
Expand All @@ -29,13 +48,20 @@ public int selectCard(Giocatore g2, Giocatore g1) {
return selectBestStartingCard(g2, g1);
}

/**
* Selects the best response card for the current player based on the opponent's thrown card.
*
* @param g2 the current player
* @param thrownCard the card thrown by the opponent
* @return the index of the selected card
*/

private int selectBestResponseCard(Giocatore g2, Carta thrownCard) {
ArrayList<Integer> punti = new ArrayList<>(Arrays.asList(0, 0, 0));

for (int i = 0; i < g2.getMano().size(); i++) {
Carta cartaG2 = g2.getMano().get(i).getCarta();
if (!thrownCard.comparaCarte(thrownCard, cartaG2, briscola)) {
System.out.println(cartaG2 + " vince : " + thrownCard);
punti.set(i, thrownCard.getValore() + cartaG2.getValore());
}
}
Expand All @@ -49,18 +75,40 @@ private int selectBestResponseCard(Giocatore g2, Carta thrownCard) {
return maxIndex;
}

/**
* Selects the best starting card for the current player.
*
* @param g2 the current player
* @param g1 the opponent player
* @return the index of the selected card
*/

private int selectBestStartingCard(Giocatore g2, Giocatore g1) {
int maxPunti = -1;
int bestIndex = -1;

for (int i = 0; i < g2.getMano().size(); i++) {
int puntiAttuali = 0;
Carta cartaG2 = g2.getMano().get(i).getCarta();
boolean cartaG2Briscola = cartaG2.isBriscola(briscola);

for (int j = 0; j < g1.getMano().size(); j++) {
Carta cartaG1 = g1.getMano().get(j).getCarta();
if (cartaG2.comparaCarte(cartaG2, cartaG1, briscola)) {
puntiAttuali += cartaG1.getValore() + cartaG2.getValore();
boolean cartaG1Briscola = cartaG1.isBriscola(briscola);

//if both are briscole or if neither is a briscola
if (cartaG2Briscola == cartaG1Briscola) {
if (cartaG2.comparaCarte(cartaG2, cartaG1, briscola)) {
puntiAttuali += cartaG1.getValore() + cartaG2.getValore();
}
}
// If only the opponent's card is a briscola
else if (cartaG1Briscola) {
puntiAttuali -= cartaG1.getValore();
}
// If only the bot's card is a trump
else if (cartaG2Briscola) {
puntiAttuali += cartaG1.getValore();
}
}

Expand All @@ -71,12 +119,19 @@ private int selectBestStartingCard(Giocatore g2, Giocatore g1) {
}

if (bestIndex == -1) {
return new Random().nextInt(g2.getMano().size());
return selectLowestValueCard(g2);
}

return bestIndex;
}

/**
* Selects the card with the lowest value from the current player's hand.
*
* @param g2 the current player
* @return the index of the selected card
*/

private int selectLowestValueCard(Giocatore g2) {
int minPunti = Integer.MAX_VALUE;
int minIndex = -1;
Expand All @@ -100,6 +155,14 @@ private int selectLowestValueCard(Giocatore g2) {
return minIndex;
}



/**
* Finds the index of the thrown card based on the location of the card buttons.
*
* @return the index of the thrown card or -1 if no card is found
*/

private int findThrownCard() {
for (int i = 0; i < cardsButtons.length; i++) {
if (cardsButtons[i].getLocation().x == 548 && cardsButtons[i].getLocation().y == 300) {
Expand Down

0 comments on commit 2f1a7bf

Please sign in to comment.