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