Commit 5bcd2be5 authored by kiana's avatar kiana

first attempt

parents
Pipeline #4333 failed with stages
/**
* Card object
*
* @author Kiana Agha Kasiri
*/
public class Card {
/**
* color of the card
*/
private String cardColor;
/**
* value of the card
*/
private String cardValue;
/**
* type of the card
*/
private CardType cardType;
/**
* score of the card
*/
private int cardScore;
/**
* Constructor
* @param cardType type
*/
public Card(CardType cardType) { initialize(cardType); }
/**
* Constructor
* @param cardColor color
* @param cardValue value
*/
public Card(String cardColor, String cardValue) {
for (CardType type : CardType.values())
if (type.name().equals(cardColor + "_" + cardValue)) {
initialize(type);
break;
}
}
/**
* initializing the card type, color, value, ...
* @param cardType type
*/
private void initialize(CardType cardType) {
this.cardType = cardType;
String cardType_string = cardType.toString();
if (cardType == CardType.WILD || cardType == CardType.WILD_DRAW4) { // Wild cards
cardScore = 50;
cardColor = "WILD";
if (cardType == CardType.WILD)
cardValue = "WILD";
else
cardValue = "DRAW4";
} else if (cardType_string.contains("_SKIP") || cardType_string.contains("_REVERSE") || cardType_string.contains("_DRAW2")) { // Action cards
cardScore = 20;
cardColor = cardType_string.split("_")[0];
cardValue = cardType_string.split("_")[1];
} else { // Ordinary cards
cardScore = Integer.parseInt(cardType_string.split("_")[1]);
cardColor = cardType_string.split("_")[0];
cardValue = cardType_string.split("_")[1];
}
// Adjust the card color according to the available Console Color
adjustConsoleColor();
}
/**
* Get card type
* @return type
*/
public CardType getCardType() {
return cardType;
}
/**
* Get card score
* @return score
*/
public int getCardScore() {
return cardScore;
}
/**
* Get card color
* @return color
*/
public String getCardColor() {
return cardColor;
}
/**
* sets the card's color
* @param cardColor
*/
public void setCardColor(String cardColor) {
cardColor = cardColor;
}
/**
* Get card value
* @return value
*/
public String getCardValue() { return cardValue; }
/**
* set back color
*/
private void adjustConsoleColor() {
if (cardColor.equals("RED")) {
cardColor = ConsoleColors.RED_BACKGROUND_BRIGHT;
} else if (this.cardColor.equals("BLUE")) {
cardColor = ConsoleColors.BLUE_BACKGROUND_BRIGHT;
} else if (this.cardColor.equals("GREEN")) {
cardColor = ConsoleColors.GREEN_BACKGROUND_BRIGHT;
} else if (this.cardColor.equals("YELLOW")) {
cardColor = ConsoleColors.YELLOW_BACKGROUND_BRIGHT;
}
}
}
\ No newline at end of file
import java.util.ArrayList;
import java.util.Random;
public class CardList {
private ArrayList <Card> cards = new ArrayList<>();
/**
* constructor
* adding all cards to the list
*/
public CardList(){
for (int j=0; j<4; j++){
cards.add(new Card(CardType.WILD_DRAW4));
}
for (int i=0; i<4; i++){
cards.add(new Card(CardType.WILD));
}
for(int k=0; k<2; k++){
cards.add(new Card(CardType.RED_REVERSE));
cards.add(new Card(CardType.BLUE_REVERSE));
cards.add(new Card(CardType.GREEN_REVERSE));
cards.add(new Card(CardType.YELLOW_REVERSE));
}
for(int k=0; k<2; k++){
cards.add(new Card(CardType.BLUE_SKIP));
cards.add(new Card(CardType.GREEN_SKIP));
cards.add(new Card(CardType.RED_SKIP));
cards.add(new Card(CardType.YELLOW_SKIP));
}
for(int k=0; k<2; k++){
cards.add(new Card(CardType.RED_DRAW2));
cards.add(new Card(CardType.BLUE_DRAW2));
cards.add(new Card(CardType.GREEN_DRAW2));
cards.add(new Card(CardType.YELLOW_DRAW2));
}
for (int z=0; z<2; z++){
cards.add(new Card(CardType.BLUE_1));
cards.add(new Card(CardType.BLUE_2));
cards.add(new Card(CardType.BLUE_3));
cards.add(new Card(CardType.BLUE_4));
cards.add(new Card(CardType.BLUE_5));
cards.add(new Card(CardType.BLUE_6));
cards.add(new Card(CardType.BLUE_7));
cards.add(new Card(CardType.BLUE_8));
cards.add(new Card(CardType.BLUE_9));
cards.add(new Card(CardType.GREEN_1));
cards.add(new Card(CardType.GREEN_2));
cards.add(new Card(CardType.GREEN_3));
cards.add(new Card(CardType.GREEN_4));
cards.add(new Card(CardType.GREEN_5));
cards.add(new Card(CardType.GREEN_6));
cards.add(new Card(CardType.GREEN_7));
cards.add(new Card(CardType.GREEN_8));
cards.add(new Card(CardType.GREEN_9));
cards.add(new Card(CardType.YELLOW_1));
cards.add(new Card(CardType.YELLOW_2));
cards.add(new Card(CardType.YELLOW_3));
cards.add(new Card(CardType.YELLOW_4));
cards.add(new Card(CardType.YELLOW_5));
cards.add(new Card(CardType.YELLOW_6));
cards.add(new Card(CardType.YELLOW_7));
cards.add(new Card(CardType.YELLOW_8));
cards.add(new Card(CardType.YELLOW_9));
cards.add(new Card(CardType.RED_1));
cards.add(new Card(CardType.RED_2));
cards.add(new Card(CardType.RED_3));
cards.add(new Card(CardType.RED_4));
cards.add(new Card(CardType.RED_5));
cards.add(new Card(CardType.RED_6));
cards.add(new Card(CardType.RED_7));
cards.add(new Card(CardType.RED_8));
cards.add(new Card(CardType.RED_9));
}
cards.add(new Card(CardType.GREEN_0));
cards.add(new Card(CardType.BLUE_0));
cards.add(new Card(CardType.RED_0));
cards.add(new Card(CardType.YELLOW_0));
}
/**
* sets the cards
* @param cards
*/
public void setCards(ArrayList<Card> cards) {
this.cards = cards;
}
/**
* gets the cards
* @return
*/
public ArrayList<Card> getCards() {
return cards;
}
/**
* adds card to the list
* @param card
*/
public void addCard(Card card){
cards.add(card);
}
/**
* gets a card from list
* remove the card that has been drawn
* @return
*/
public Card drawCard(){
int color = new Random().nextInt(cards.size());
Card card = cards.get(color);
cards.remove(card);
return card;
}
}
/**
* card types of the game
*
* @author Kiana Agha Kasiri
*/
public enum CardType {
WILD, WILD_DRAW4, YELLOW_0, YELLOW_1, YELLOW_2, YELLOW_3, YELLOW_4, YELLOW_5, YELLOW_6, YELLOW_7, YELLOW_8, YELLOW_9, RED_0, RED_1, RED_2, RED_3, RED_4, RED_5, RED_6, RED_7, RED_8, RED_9,
GREEN_0, GREEN_1, GREEN_2, GREEN_3, GREEN_4, GREEN_5, GREEN_6, GREEN_7, GREEN_8, GREEN_9, BLUE_0, BLUE_1, BLUE_2, BLUE_3, BLUE_4, BLUE_5, BLUE_6, BLUE_7, BLUE_8, BLUE_9,
YELLOW_SKIP, GREEN_SKIP, BLUE_SKIP, RED_SKIP, YELLOW_DRAW2, GREEN_DRAW2, BLUE_DRAW2, RED_DRAW2, YELLOW_REVERSE, GREEN_REVERSE, BLUE_REVERSE, RED_REVERSE,
}
/**
* ConsoleColors unicode
*/
public class ConsoleColors {
public static final String RESET = "\033[0m"; // Text Reset
public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE
public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK
public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED
public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN
public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW
public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE
}
\ No newline at end of file
This diff is collapsed.
import java.util.ArrayList;
import java.util.Random;
public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.println("for human players please enter like cardType");
Game uno = new Game();
Show show = new Show();
//first card
Card table = null;
do {
int color = new Random().nextInt(uno.cardList.getCards().size());
table = uno.cardList.getCards().get(color);
} while (table.getCardType().name().contains("SKIP") || table.getCardType().name().contains("REVERSE") || table.getCardType().name().contains("WILD") || table.getCardType().name().contains("WILD_DRAW") || table.getCardType().name().contains("DRAW2"));
uno.play(table);
}
/**
* checking if the game is over
* if someone finishes his/her cards the game is over
* @param players
* @return true or false
*/
public static boolean isGameOver(ArrayList<Player> players) {
for (Player player : players)
if (player.getCards().size() == 0)
return true;
return false;
}
}
import java.util.ArrayList;
public class Player {
private ArrayList<Card> cards;
private String name;
private int score;
private Card lastCard;
private boolean isAI;
public Player(String name, boolean isAI) {
this.name = name;
this.score = 0;
this.lastCard = null;
cards = new ArrayList<>();
this.isAI = isAI;
}
/**
* gets the last card
* @return
*/
public Card getLastCard() {
return lastCard;
}
/**
* sets the last card
* @param lastCard
*/
public void setLastCard(Card lastCard) {
this.lastCard = lastCard;
}
/**
* gets the player's name
* @return name
*/
public String getName() {
return name;
}
/**
* gets the player's score
* @return score
*/
public int getScore() {
return score;
}
/**
* sets score
* @param score
*/
public void setScore(int score) {
this.score = score;
}
/**
* sets name
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* gets the cards
* @return cards
*/
public ArrayList<Card> getCards() {
return cards;
}
/**
* sets the cards
* @param cards
*/
public void setCards(ArrayList<Card> cards) {
this.cards = cards;
}
/**
* adds a card to the list of player's cards
* @param card
*/
public void addCard(Card card) {
cards.add(card);
}
/**
* removes a card from the list of player's cards
* @param card
*/
public void removeCard(Card card) {
cards.remove(card);
}
/**
* changes the last card
* removes the card from the list of player's cards
* @param card
*/
public void lastCardOut(Card card) {
lastCard = card;
cards.remove(card);
}
public Card play(Card table, String color) {
for (Card card : cards) {
if (color.equals("")) {
if (card.getCardColor().equals(table.getCardColor()))
return decide(card);
if (card.getCardValue().equals(table.getCardValue()))
return decide(card);
} else {
if (card.getCardColor().equals(color))
return decide(card);
}
}
for (Card card : cards)
if (card.getCardType() == CardType.WILD)
return decide(card);
for (Card card : cards)
if (card.getCardType() == CardType.WILD_DRAW4)
return decide(card);
return null;
}
/**
* removing the card
* @param cardToRemove
* @return
*/
public Card decide(Card cardToRemove) {
//changing the last card
lastCardOut(cardToRemove);
// Remove from set
removeCard(cardToRemove);
// Now return the card
return cardToRemove;
}
/**
* if the player is human or computer
* @return
*/
public boolean getIsAI() {
return isAI;
}
/**
* @param userCard
* @return
*/
public boolean contains(String userCard) {
for (Card card : cards)
if (card.getCardType().name().equals(userCard))
return true;
return false;
}
/**
* checks if the cards are the same
* @param userCard
* @return
*/
public Card getEquavalentCard(String userCard) {
for (Card card: cards)
if (card.getCardType().name().equals(userCard))
return card;
return null;
}
/**
* checking if player has a card to draw
* @param table
* @param userCard
* @return true or false
*/
public boolean canDraw(Card table, Card userCard, String color) {
if (userCard.getCardValue().equals(table.getCardValue()) || userCard.getCardColor().equals(table.getCardColor()) || userCard.getCardType() == CardType.WILD)
return true;
else {
boolean isOk = false;
for (Card card: cards) {
if (table.getCardValue().equals(card.getCardValue()) || table.getCardColor().equals(card.getCardColor())) {
isOk = true;
break;
}
}
if (!isOk && userCard.getCardType() == CardType.WILD_DRAW4)
return true;
}
if (userCard.getCardColor().equals(color) || userCard.getCardType() == CardType.WILD)
return true;
else {
boolean isOk = false;
for (Card card: cards) {
if (color.equals(card.getCardColor())) {
isOk = true;
break;
}
}
if (!isOk && userCard.getCardType() == CardType.WILD_DRAW4)
return true;
}
return false;
}
}
\ No newline at end of file
import java.util.ArrayList;
public class PlayerHuman extends Player {
public PlayerHuman(String name, boolean isAI) {
super(name, isAI);
}
@Override
public Card getLastCard() {
return super.getLastCard();
}
@Override
public String getName() {
return super.getName();
}
@Override
public void setScore(int score) {
super.setScore(score);
}
@Override
public ArrayList<Card> getCards() {
return super.getCards();
}
@Override
public void addCard(Card card) {
super.addCard(card);
}
@Override
public void removeCard(Card card) {
super.removeCard(card);
}
@Override
public void setLastCard(Card lastCard) {
super.setLastCard(lastCard);
}
@Override
public int getScore() {
return super.getScore();
}
@Override
public void setName(String name) {
super.setName(name);
}
@Override
public void setCards(ArrayList<Card> cards) {
super.setCards(cards);
}
@Override
public void lastCardOut(Card card) {
super.lastCardOut(card);
}
@Override
public Card play(Card table, String color) {
return super.play(table, color);
}
@Override
public Card decide(Card cardToRemove) {
return super.decide(cardToRemove);
}
}
import java.util.ArrayList;
public class Show {
public void cardsType(Card card){
switch (card.getCardColor()) {
case ConsoleColors.BLUE_BACKGROUND_BRIGHT:
System.out.print(ConsoleColors.BLUE_BACKGROUND_BRIGHT + " " + ConsoleColors.WHITE_BOLD_BRIGHT + card.getCardValue() + " " + ConsoleColors.RESET + " ");
break;
case ConsoleColors.RED_BACKGROUND_BRIGHT:
System.out.print(ConsoleColors.RED_BACKGROUND_BRIGHT + " " + ConsoleColors.WHITE_BOLD_BRIGHT + " " + card.getCardValue() + " " + ConsoleColors.RESET + " ");
break;
case ConsoleColors.YELLOW_BACKGROUND_BRIGHT:
System.out.print(ConsoleColors.YELLOW_BACKGROUND_BRIGHT + " " + ConsoleColors.WHITE_BOLD_BRIGHT + " " + card.getCardValue() + " " + ConsoleColors.RESET + " ");
break;
case ConsoleColors.GREEN_BACKGROUND_BRIGHT:
System.out.print(ConsoleColors.GREEN_BACKGROUND_BRIGHT + " " + ConsoleColors.WHITE_BOLD_BRIGHT + " " + card.getCardValue() + " " + ConsoleColors.RESET + " ");
break;
default:
System.out.print(" " + card.getCardValue() + " ");
break;
}
}
public void show(ArrayList<Player> players, Card table, int turn, int rotation){
if(rotation == +1)
System.out.println("rotation: right");
else if(rotation == -1)
System.out.println("rotation: left");
System.out.println("It is " + players.get(turn).getName() + "'s turn");
System.out.print("THE CARD ON THE TABLE: ");
cardsType(table);
System.out.println();
for (Player player: players){
System.out.println(player.getName() + "'s score: " + player.getScore());
System.out.print(player.getName() + "'s cards: ");
for (Card card: player.getCards()){
cardsType(card);
}
System.out.println();
}
System.out.println("\n");
}
}
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