Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.1 kB
1
Indexable
Never
package sezione15.javaCollections.esercitazione5.pokerChallenge.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

import sezione15.javaCollections.esercitazione3.cardChallenge.model.Card;
import sezione15.javaCollections.esercitazione5.pokerChallenge.exception.PokerException;

public class Player {

	private String name;
	private int setScore;
	private ArrayList<Card> set;
	private int numCardsRemoved;
	
	public Player(String name) {
		this.name = name;
		set = new ArrayList<Card>();
		numCardsRemoved = 0;
	}
	
	public boolean addCard(Card card) {
		return set.add(card);
	}
	
	public void newSet(List<Card> set) {
		this.set = new ArrayList<>(set);
		System.out.println("%s set is: %s".formatted(name, this.set));
	}
	
	public void clearSet() {
		set.clear();
	}
	
	public String getName() {
		return name;
	}

	public ArrayList<Card> getSet() {
		return set;
	}
	
	public int getNumCardsRemoved() {
		return numCardsRemoved;
	}
	
	public int getScore() {
		return setScore;
	}
	
	public void setScore(int score) {
		this.setScore = score;
	}
	
	public boolean removeCard(Card cardToRemove) {
		if(set.remove(cardToRemove)) {
			numCardsRemoved++;
			return true;
		}
		return false;
	}
	
	@Override
	public String toString() {
		return "[ Name = %s,%s Score = %d ]".formatted(name, (set.isEmpty()) ? " No set yet," : " Set = " + set + ",", getScore());
	}
	
	@Override
	public int hashCode() {
		return Objects.hash(name);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Player other = (Player) obj;
		return name.equalsIgnoreCase(other.name);
	}

	public static Player getRandomPlayer(int maxNumCards) {
		String[] names = {
				"Alan",
				"Jhonatan",
				"Geroge",
				"Pier",
				"Sara",
				"Linda"
		};
		
		return new Player(names[new Random().nextInt(0, names.length)]);
	}
	
}