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