Commit e5f353eb authored by amir's avatar amir

Checking cards and some new methods

parent 4aeeddde
......@@ -7,6 +7,7 @@ public class Card {
public Card(String color){
this.cardColor = color;
}
public int turnSkip(){
return 1;
}
......
package com.company;
public class Colored extends Card {
private int colorType; // 0 for numeric, 1 == movement
private int colorType; // 0 for numeric, 1 == movement
public Colored(String color, int colorType) {
super(color);
if (colorType == 1 || colorType == 0)
......@@ -9,7 +9,7 @@ public class Colored extends Card {
}
public boolean checkEquality(Colored coloredToCompare){
if (cardColor.equals(coloredToCompare.cardColor) || colorType == coloredToCompare.colorType)
if (cardColor.equals(coloredToCompare.cardColor))
return true;
return false;
}
......
......@@ -7,6 +7,13 @@ public class Draw extends Movemention {
super(color, colorType, moveType);
}
@Override
public boolean checkEquality(Colored coloredToCompare) {
if (!super.checkEquality(coloredToCompare) && coloredToCompare instanceof Draw)
return true;
return false;
}
@Override
public String toString() {
return "Draw{ " + super.toString();
......
package com.company;
public class Human extends Player {
}
......@@ -7,10 +7,11 @@ import java.util.Random;
public class Main {
public static void main(String[] args) {
ArrayList <Card> cards = new ArrayList<>();
String Color; String Color1;
for (int i = 1; i < 5; i++){
switch (i){
ArrayList<Card> cards = new ArrayList<>();
String Color;
String Color1;
for (int i = 1; i < 5; i++) {
switch (i) {
case 1:
Color = "Red";
break;
......@@ -39,8 +40,8 @@ public class Main {
Black black = new Black("Black", 0);
cards.add(black);
}
for (int i = 1; i < 5; i++){
switch(i){
for (int i = 1; i < 5; i++) {
switch (i) {
case 1:
Color1 = "Red";
break;
......@@ -73,63 +74,73 @@ public class Main {
/*for (int i = 0; i < 108; i++){
System.out.println(cards.get(i).toString());
}*/
Random r = new Random(); int next;
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);
for (int j = 1; j < 8; j++){
next = r.nextInt(cards.size());
p.getPlayerCards().add(cards.get(next));
cards.remove(next);
for (int j = 1; j < 8; j++) {
nextCardNumber = r.nextInt(cards.size());
p.getPlayerCards().add(cards.get(nextCardNumber));
cards.remove(nextCardNumber);
//System.out.println("cardNumber = " + next);
}
//p.printAllCards(i);
}
/*for (int i = 0; i < 4; i++) {
for (int i = 0; i < 4; i++) {
players.get(i).printAllCards(i + 1);
System.out.println(players.get(i).getPlayerPoint());
}*/
System.out.println();
}
// first random card from the storage of cards (most be a colored card)
next = r.nextInt(cards.size());
if (cards.get(next) instanceof Black){
while (cards.get(next) instanceof Black) {
next = r.nextInt(cards.size());
nextCardNumber = r.nextInt(cards.size());
if (cards.get(nextCardNumber) instanceof Black) {
while (cards.get(nextCardNumber) instanceof Black) {
nextCardNumber = r.nextInt(cards.size());
}
}
System.out.println("Player" + r.nextInt(5) + " starts the game");
int move = 1; Player p = null;
int nextPlayerNumber = r.nextInt(4);
System.out.println("Player" + (nextPlayerNumber + 1) + " starts the game");
int move = 1;
Player p = null;
int turns = nextPlayerNumber;
// starts a round
while (true) {
Iterator<Player> playerIterator = players.iterator();
while (playerIterator.hasNext()){
while (playerIterator.hasNext()) {
p = playerIterator.next();
if (p.getPlayerCards().size() == 0)
break;
}
if (p.getPlayerCards().size() == 0)
break;
break;
if (move == 1)
System.out.println("Clockwise");
else
System.out.println("CounterClockwise");
if (turns != 2){
}
}
// sort players by each player's point (Acsending sort)
Iterator<Player> players1 = players.iterator(); int min = -1;
/*Iterator<Player> players1 = players.iterator(); int min = 350;
while (players1.hasNext()){
p = players1.next();
if (p.getPlayerPoint() > min)
if (p.getPlayerPoint() < min)
min = p.getPlayerPoint();
}
}*/
// end the round by showing each player's point
Iterator<Player> players2 = players.iterator(); int c = 0;
/*Iterator<Player> players2 = players.iterator(); int c = 0;
while (players2.hasNext()){
c++;
players2.next().toString(c);
}
System.out.println(players2.next().toString(c));
}*/
// unicode of black color: "\033[0;30m"
// unicode of red color: "\033[0;31m"
// unicode of green color: "\033[0;32m"
......
......@@ -13,6 +13,14 @@ public class Numeric extends Colored {
", number=" + number + '}';
}
@Override
public boolean checkEquality(Colored coloredToCompare) {
if (!super.checkEquality(coloredToCompare) && coloredToCompare instanceof Numeric
&& ((Numeric) coloredToCompare).number == number)
return true;
return false;
}
@Override
public int getCardPoint() {
return number;
......
......@@ -4,13 +4,25 @@ import java.util.ArrayList;
import java.util.Iterator;
public class Player {
// List of player's cards
private ArrayList<Card> playerCards = new ArrayList<>();
// point of the player
private int playerPoint;
ArrayList<Card> possibleCards;
/**
* returns the list of player's cards
* @return the list of player's cards
*/
public ArrayList<Card> getPlayerCards() {
return playerCards;
}
/**
*
* @param playerNumber
*/
public void printAllCards(int playerNumber){
System.out.println("Player" + playerNumber + ":");
Iterator<Card> cardIterator = playerCards.iterator();
......@@ -18,7 +30,6 @@ public class Player {
Card c = cardIterator.next();
System.out.println(c.toString());
}
System.out.println();
}
public int getPlayerPoint() {
......@@ -29,6 +40,54 @@ public class Player {
return playerPoint;
}
public void checkAllCards(Card card){
possibleCards = new ArrayList<>();
boolean isColored;
if (card instanceof Colored)
isColored = true;
else
isColored = false;
Iterator<Card> cardIterator = playerCards.iterator();
while (cardIterator.hasNext()){
Card c = cardIterator.next();
if (c instanceof Colored) {
if (isColored)
if (((Colored) c).checkEquality((Colored) card))
possibleCards.add(c);
}
else
possibleCards.add(c);
}
if (possibleCards.size() > 0) {
if (possibleCards.size() != searchWildDraws())
deleteWildDraws();
}
}
public int searchWildDraws(){
int counterWildDraw = 0;
Iterator<Card> cardIterator = possibleCards.iterator();
while (cardIterator.hasNext()){
Card c = cardIterator.next();
if (c instanceof WildDraw)
counterWildDraw++;
}
return counterWildDraw;
}
public void deleteWildDraws(){
Iterator<Card> cardIterator = possibleCards.iterator();
while (cardIterator.hasNext()) {
Card c = cardIterator.next();
if (c instanceof WildDraw)
possibleCards.remove(c);
}
}
public void whichCard(Card card){
}
public String toString(int playerNumber) {
return "Player" + playerNumber +
" {playerPoint=" + playerPoint +
......
......@@ -12,6 +12,13 @@ public class Reverse extends Movemention {
return -1;
}
@Override
public boolean checkEquality(Colored coloredToCompare) {
if (!super.checkEquality(coloredToCompare) && coloredToCompare instanceof Reverse)
return true;
return false;
}
@Override
public String toString() {
return "Reverse{ " + super.toString();
......
......@@ -6,6 +6,18 @@ public class Skip extends Movemention {
super(color, colorType, moveType);
}
@Override
public boolean checkEquality(Colored coloredToCompare) {
if (!super.checkEquality(coloredToCompare) && coloredToCompare instanceof Skip)
return true;
return false;
}
@Override
public int turnSkip() {
return super.turnSkip();
}
@Override
public String toString() {
return "Skip{ " + super.toString();
......
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