Commit a408c34f authored by amir's avatar amir

using HashMap for saving possible cards instead of arrayList

parent ed2b30de
......@@ -9,10 +9,9 @@ public class Comp extends Player {
@Override
public Card chooseCardToThrow(ArrayList<Card> cards, Card card) {
Card c = null;
if ((getCounter() == 0 && possibleCards.size() > 0) || (getCounter() > 0 && possibleCards.size() > 1)) {
int number = r.nextInt(getPossibleCards().size());
c = possibleCards.get(number);
possibleCards = null;
if (possibleCards.get(card).size() > 0) {
int number = r.nextInt(possibleCards.get(card).size());
c = possibleCards.get(card).get(number);
getPlayerCards().remove(c);
}
else {
......@@ -36,16 +35,16 @@ public class Comp extends Player {
int color = r.nextInt(4) + 1;
switch (color){
case 1:
s = "red";
s = "Red";
break;
case 2:
s = "green";
s = "Green";
break;
case 3:
s = "yellow";
s = "Yellow";
break;
case 4:
s = "blue";
s = "Blue";
break;
}
}
......@@ -55,14 +54,15 @@ public class Comp extends Player {
@Override
public Card chooseColorToThrow(ArrayList<Card> cards, String Color) {
Card c = null;
if ((getCounter() == 0 && possibleCards.size() > 0) || (getCounter() > 0 && possibleCards.size() > 1)) {
if (possibleColorCards.get(Color).size() > 0) {
int number = r.nextInt(getPossibleCards().size());
c = possibleCards.get(number);
possibleCards = null;
c = possibleColorCards.get(Color).get(number);
getPlayerCards().remove(c);
}
else {
System.out.println("kirekhar");
c = randomPunishmentCard(cards);
System.out.println(c.toString());
getPlayerCards().add(c);
if (c instanceof Colored) {
if (!c.cardColor.equals(Color)) {
......
......@@ -9,16 +9,16 @@ public class Human extends Player {
@Override
public Card chooseCardToThrow(ArrayList<Card> cards, Card c) {
Card card = null;
if (((getCounter() == 0 && possibleCards.size() > 0) || (getCounter() > 0 && possibleCards.size() > 1))) {
if (possibleCards.get(c).size() > 0) {
System.out.println("Enter number of the card you wanna throw it!");
int number = scanner.nextInt();
if (number > 0 && number <= getPlayerCards().size() &&
possibleCards.contains(getPlayerCards().get(number - 1))) {
possibleCards.get(c).contains(getPlayerCards().get(number - 1))) {
//possibleCards = null;
System.out.println("kire khar!");
} else {
while (number <= 0 || number > getPlayerCards().size() ||
!possibleCards.contains(getPlayerCards().get(number - 1))) {
!possibleCards.get(c).contains(getPlayerCards().get(number - 1))) {
System.out.println("Invalid input");
System.out.println("Enter number of the card you wanna throw it!");
number = scanner.nextInt();
......@@ -47,7 +47,7 @@ public class Human extends Player {
public String chooseColorIfBlack(Card card){
String s = null;
if (card instanceof Black){
System.out.println("Choose a color: red, green, yellow, blue");
System.out.println("Choose a color: Red, Green, Yellow, Blue");
s = scanner.next();
}
return s;
......@@ -56,11 +56,11 @@ public class Human extends Player {
@Override
public Card chooseColorToThrow(ArrayList<Card> cards, String Color) {
Card card = null;
if ((getCounter() == 0 && possibleCards.size() > 0) || (getCounter() > 0 && possibleCards.size() > 1)) {
if (possibleColorCards.get(Color).size() > 0) {
System.out.println("Enter number of the card you wanna throw it!");
int number = scanner.nextInt();
if (number > 0 && number <= getPlayerCards().size() &&
possibleCards.contains(getPlayerCards().get(number - 1))) {
possibleColorCards.get(Color).contains(getPlayerCards().get(number - 1))) {
//possibleCards = null;
System.out.println("kire khar!");
card = getPlayerCards().get(number - 1);
......@@ -68,7 +68,7 @@ public class Human extends Player {
return card;
} else {
while (number <= 0 || number > getPlayerCards().size() ||
!possibleCards.contains(getPlayerCards().get(number - 1))) {
!possibleColorCards.get(Color).contains(getPlayerCards().get(number - 1))) {
System.out.println("Invalid input");
System.out.println("Enter number of the card you wanna throw it!");
number = scanner.nextInt();
......
......@@ -128,25 +128,30 @@ public class Main {
System.out.println("Clockwise");
else
System.out.println("CounterClockwise");
if (nextColor == null)
if (nextColor == null) {
players.get(turns).checkAllCards(nextCard);
else
players.get(turns).printAllPossibleCards(turns + 1, nextCard);
}
else {
players.get(turns).checkColors(nextColor);
players.get(turns).printAllPossibleCards(turns + 1);
players.get(turns).printAllPossibleColoredCards(turns + 1, nextColor);
}
System.out.println();
players.get(turns).printAllCards(turns + 1);
System.out.println();
if (nextCard != null)
if (nextColor == null)
card = players.get(turns).chooseCardToThrow(cards, nextCard);
else
card = players.get(turns).chooseColorToThrow(cards, nextColor);
if (card != null) {
System.out.println(card.toString());
if (nextColor != null)
nextColor = null;
nextColor = players.get(turns).chooseColorIfBlack(card);
if (nextColor == null)
nextCard = card;
}
if (nextCard != null)
if (nextColor == null)
System.out.println(nextCard.toString());
else
System.out.println(nextColor);
......
......@@ -4,6 +4,7 @@ import com.sun.javafx.scene.control.skin.VirtualFlow;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
......@@ -13,7 +14,8 @@ public class Player {
// point of the player
private int playerPoint;
protected ArrayList<Card> possibleCards = new ArrayList<>();
protected HashMap<Card, ArrayList<Card>> possibleCards = new HashMap<>();
protected HashMap<String, ArrayList<Card>> possibleColorCards = new HashMap<>();
private int counter = 0;
/**
......@@ -41,7 +43,7 @@ public class Player {
return counter;
}
public ArrayList<Card> getPossibleCards() {
public HashMap<Card, ArrayList<Card>> getPossibleCards() {
return possibleCards;
}
......@@ -55,14 +57,7 @@ public class Player {
public void checkAllCards(Card card){
boolean isColored = false;
if (possibleCards == null)
possibleCards = new ArrayList<>();
if (possibleCards.size() > 1){
for (int i = 1; i < possibleCards.size(); i++){
possibleCards.remove(i);
i--;
}
}
possibleCards.put(card, new ArrayList<Card>());
if (card instanceof Colored)
isColored = true;
Iterator<Card> cardIterator = playerCards.iterator();
......@@ -71,14 +66,14 @@ public class Player {
if (c instanceof Colored) {
if (isColored)
if (((Colored) c).checkEquality((Colored) card))
possibleCards.add(c);
possibleCards.get(card).add(c);
}
else
possibleCards.add(c);
possibleCards.get(card).add(c);
}
if (possibleCards.size() > 0) {
if (possibleCards.size() != searchWildDraws())
deleteWildDraws();
if (possibleCards.get(card).size() > 0) {
if (possibleCards.get(card).size() != searchWildDraws(card))
deleteWildDraws(card);
}
}
......@@ -89,22 +84,28 @@ public class Player {
return cards.get(next);
}
public void printAllPossibleCards(int playerNumber){
public void printAllPossibleCards(int playerNumber, Card cardToPrint){
System.out.println("Player" + playerNumber + ": (Possible cards)");
Iterator<Card> cardIterator = possibleCards.iterator();
if (counter != 0) {
cardIterator.next();
Iterator<Card> cardIterator = possibleCards.get(cardToPrint).iterator();
while (cardIterator.hasNext()){
Card c = cardIterator.next();
System.out.println(c.toString());
}
while (cardIterator.hasNext()) {
}
public void printAllPossibleColoredCards(int playerNumber, String colorToPrint){
System.out.println("Player" + playerNumber + ": (Possible color cards)");
Iterator<Card> cardIterator = possibleColorCards.get(colorToPrint).iterator();
while (cardIterator.hasNext()){
Card c = cardIterator.next();
System.out.println(c.toString());
}
counter++;
}
private int searchWildDraws(){
private int searchWildDraws(Card card){
int counterWildDraw = 0;
Iterator<Card> cardIterator = possibleCards.iterator();
Iterator<Card> cardIterator = possibleCards.get(card).iterator();
while (cardIterator.hasNext()){
Card c = cardIterator.next();
if (c instanceof WildDraw)
......@@ -113,10 +114,10 @@ public class Player {
return counterWildDraw;
}
private void deleteWildDraws(){
for (int i = 0; i < possibleCards.size(); i++){
if (possibleCards.get(i) instanceof WildDraw){
possibleCards.remove(i);
private void deleteWildDraws(Card card){
for (int i = 0; i < possibleCards.get(card).size(); i++){
if (possibleCards.get(card).get(i) instanceof WildDraw){
possibleCards.get(card).remove(i);
i--;
}
}
......@@ -136,21 +137,16 @@ public class Player {
" {playerPoint=" + playerPoint +
" }";
}
public void checkColors(String color){
if (possibleCards == null)
possibleCards = new ArrayList<>();
if (possibleCards.size() > 1){
for (int i = 1; i < possibleCards.size(); i++){
possibleCards.remove(i);
i--;
}
}
System.out.println("color = " + color);
possibleColorCards.put(color, new ArrayList<Card>());
Iterator<Card> cardIterator = playerCards.iterator();
while (cardIterator.hasNext()) {
Card c = cardIterator.next();
if (c instanceof Colored)
if(c.cardColor.equals(color))
possibleCards.add(c);
possibleColorCards.get(color).add(c);
}
}
......
Markdown is supported
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