Commit 875d98dd authored by amir's avatar amir

Concurrent Modification exception handling!

parent e5f353eb
package com.company;
import java.util.Random;
public class Comp extends Player {
Random r = new Random();
@Override
public Card chooseCardToThrow() {
Card c = null;
if (possibleCards.size() > 0) {
int number = r.nextInt(getPossibleCards().size());
c = possibleCards.get(number);
possibleCards = null;
getPlayerCards().remove(c);
}
return c;
}
}
...@@ -9,6 +9,8 @@ public class Draw extends Movemention { ...@@ -9,6 +9,8 @@ public class Draw extends Movemention {
@Override @Override
public boolean checkEquality(Colored coloredToCompare) { public boolean checkEquality(Colored coloredToCompare) {
if (super.checkEquality(coloredToCompare))
return true;
if (!super.checkEquality(coloredToCompare) && coloredToCompare instanceof Draw) if (!super.checkEquality(coloredToCompare) && coloredToCompare instanceof Draw)
return true; return true;
return false; return false;
......
package com.company; package com.company;
import java.util.Scanner;
public class Human extends Player { public class Human extends Player {
Scanner scanner = new Scanner(System.in);
@Override
public Card chooseCardToThrow() {
Card card = null;
if (possibleCards.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 = null;
card = getPlayerCards().get(number - 1);
getPlayerCards().remove(number - 1);
} else {
while (true){
System.out.println("Invalid input");
chooseCardToThrow();
}
}
}
return card;
}
} }
...@@ -77,12 +77,14 @@ public class Main { ...@@ -77,12 +77,14 @@ public class Main {
Random r = new Random(); Random r = new Random();
int nextCardNumber; int nextCardNumber;
ArrayList<Player> players = new ArrayList<>(); ArrayList<Player> players = new ArrayList<>();
for (int i = 1; i < 5; i++) { Comp comp1 = new Comp(); players.add(comp1);
Player p = new Player(); Comp comp2 = new Comp(); players.add(comp2);
players.add(p); Human human1 = new Human(); players.add(human1);
Comp comp3 = new Comp(); players.add(comp3);
for (int i = 0; i < 4; i++) {
for (int j = 1; j < 8; j++) { for (int j = 1; j < 8; j++) {
nextCardNumber = r.nextInt(cards.size()); nextCardNumber = r.nextInt(cards.size());
p.getPlayerCards().add(cards.get(nextCardNumber)); players.get(i).getPlayerCards().add(cards.get(nextCardNumber));
cards.remove(nextCardNumber); cards.remove(nextCardNumber);
//System.out.println("cardNumber = " + next); //System.out.println("cardNumber = " + next);
} }
...@@ -101,14 +103,17 @@ public class Main { ...@@ -101,14 +103,17 @@ public class Main {
nextCardNumber = r.nextInt(cards.size()); nextCardNumber = r.nextInt(cards.size());
} }
} }
System.out.println(cards.get(nextCardNumber).toString());
System.out.println();
int nextPlayerNumber = r.nextInt(4); int nextPlayerNumber = r.nextInt(4);
System.out.println("Player" + (nextPlayerNumber + 1) + " starts the game"); System.out.println("Player" + (nextPlayerNumber + 1) + " starts the game");
int move = 1; int move = 1;
Player p = null; Player p = null;
int turns = nextPlayerNumber; int turns = nextPlayerNumber; int c = 0;
// starts a round // starts a round
while (true) { while (c < 5) {
turns %= 4;
c++;
Iterator<Player> playerIterator = players.iterator(); Iterator<Player> playerIterator = players.iterator();
while (playerIterator.hasNext()) { while (playerIterator.hasNext()) {
p = playerIterator.next(); p = playerIterator.next();
...@@ -121,11 +126,9 @@ public class Main { ...@@ -121,11 +126,9 @@ public class Main {
System.out.println("Clockwise"); System.out.println("Clockwise");
else else
System.out.println("CounterClockwise"); System.out.println("CounterClockwise");
if (turns != 2){ players.get(turns).checkAllCards(cards.get(nextCardNumber));
players.get(turns).printAllPossibleCards(turns + 1);
} turns += move;
} }
// sort players by each player's point (Acsending sort) // sort players by each player's point (Acsending sort)
/*Iterator<Player> players1 = players.iterator(); int min = 350; /*Iterator<Player> players1 = players.iterator(); int min = 350;
......
...@@ -15,6 +15,8 @@ public class Numeric extends Colored { ...@@ -15,6 +15,8 @@ public class Numeric extends Colored {
@Override @Override
public boolean checkEquality(Colored coloredToCompare) { public boolean checkEquality(Colored coloredToCompare) {
if (super.checkEquality(coloredToCompare))
return true;
if (!super.checkEquality(coloredToCompare) && coloredToCompare instanceof Numeric if (!super.checkEquality(coloredToCompare) && coloredToCompare instanceof Numeric
&& ((Numeric) coloredToCompare).number == number) && ((Numeric) coloredToCompare).number == number)
return true; return true;
......
package com.company; package com.company;
import com.sun.javafx.scene.control.skin.VirtualFlow;
import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
...@@ -9,7 +12,7 @@ public class Player { ...@@ -9,7 +12,7 @@ public class Player {
// point of the player // point of the player
private int playerPoint; private int playerPoint;
ArrayList<Card> possibleCards; protected ArrayList<Card> possibleCards;
/** /**
* returns the list of player's cards * returns the list of player's cards
...@@ -32,6 +35,10 @@ public class Player { ...@@ -32,6 +35,10 @@ public class Player {
} }
} }
public ArrayList<Card> getPossibleCards() {
return possibleCards;
}
public int getPlayerPoint() { public int getPlayerPoint() {
Iterator<Card> cardIterator = playerCards.iterator(); Iterator<Card> cardIterator = playerCards.iterator();
while (cardIterator.hasNext()){ while (cardIterator.hasNext()){
...@@ -48,7 +55,7 @@ public class Player { ...@@ -48,7 +55,7 @@ public class Player {
else else
isColored = false; isColored = false;
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 (isColored) if (isColored)
...@@ -62,6 +69,17 @@ public class Player { ...@@ -62,6 +69,17 @@ public class Player {
if (possibleCards.size() != searchWildDraws()) if (possibleCards.size() != searchWildDraws())
deleteWildDraws(); deleteWildDraws();
} }
}
public void printAllPossibleCards(int playerNumber){
System.out.println("Player" + playerNumber + ": (Possible cards)");
Iterator<Card> cardIterator = possibleCards.iterator();
while (cardIterator.hasNext()){
Card c = cardIterator.next();
System.out.println(c.toString());
}
} }
public int searchWildDraws(){ public int searchWildDraws(){
...@@ -76,16 +94,16 @@ public class Player { ...@@ -76,16 +94,16 @@ public class Player {
} }
public void deleteWildDraws(){ public void deleteWildDraws(){
Iterator<Card> cardIterator = possibleCards.iterator(); for (int i = 0; i < possibleCards.size(); i++){
while (cardIterator.hasNext()) { if (possibleCards.get(i) instanceof WildDraw){
Card c = cardIterator.next(); possibleCards.remove(i);
if (c instanceof WildDraw) i--;
possibleCards.remove(c); }
} }
} }
public void whichCard(Card card){ public Card chooseCardToThrow(){
return null;
} }
public String toString(int playerNumber) { public String toString(int playerNumber) {
......
...@@ -14,6 +14,8 @@ public class Reverse extends Movemention { ...@@ -14,6 +14,8 @@ public class Reverse extends Movemention {
@Override @Override
public boolean checkEquality(Colored coloredToCompare) { public boolean checkEquality(Colored coloredToCompare) {
if (super.checkEquality(coloredToCompare))
return true;
if (!super.checkEquality(coloredToCompare) && coloredToCompare instanceof Reverse) if (!super.checkEquality(coloredToCompare) && coloredToCompare instanceof Reverse)
return true; return true;
return false; return false;
......
...@@ -8,6 +8,8 @@ public class Skip extends Movemention { ...@@ -8,6 +8,8 @@ public class Skip extends Movemention {
@Override @Override
public boolean checkEquality(Colored coloredToCompare) { public boolean checkEquality(Colored coloredToCompare) {
if (super.checkEquality(coloredToCompare))
return true;
if (!super.checkEquality(coloredToCompare) && coloredToCompare instanceof Skip) if (!super.checkEquality(coloredToCompare) && coloredToCompare instanceof Skip)
return true; return true;
return false; return false;
......
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