Commit a567d846 authored by 9731301's avatar 9731301

Add some changes in whole project and debugging

parent 7732ec50
/**
* Card Class represents A class
*/
import java.util.ArrayList;
public abstract class Card {
protected String name;
protected String color;
private int score;
public static final String set = "\u001B[0m";
public static final String green = "\u001B[32m";
......@@ -13,6 +18,14 @@ public abstract class Card {
public Card() {
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Card(int score) {
this.score = score;
}
......@@ -30,21 +43,42 @@ public abstract class Card {
public abstract void print();
public abstract void act(User chosenUser, int numOfChosenCard ,ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard);
public abstract User act(User chosenUser, int numOfChosenCard ,ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard);
public boolean isAvailableToPut(int numOfChosenCard, User chosenUser, Card upperCard) {
if (chosenUser.getUserCards().get(numOfChosenCard) instanceof ColoredCard) {
ColoredCard chosenColoredCard = (ColoredCard) chosenUser.getUserCards().get(numOfChosenCard);
if (chosenColoredCard.isAvailableToPutAColoredCard(numOfChosenCard, chosenUser, upperCard))
public boolean isAvailableToPut( User chosenUser, Card upperCard) {
if (this.getScore() < 10 && upperCard.getScore() < 10){
NumericalCard upper = (NumericalCard)upperCard;
NumericalCard chosen = (NumericalCard)this;
if (color.equals(upper.getColor()) || chosen.getNumber() == upper.getNumber()){
return true;
}
else
return false;
} else {
WildCard chosenWildCard = (WildCard) chosenUser.getUserCards().get(numOfChosenCard);
if (chosenWildCard.isAvailableToPutAWildCard(numOfChosenCard, chosenUser, upperCard))
}
else if (this.getScore() < 50 && upperCard.getScore() < 50) {
if ((this instanceof SkipCard && upperCard instanceof SkipCard) ||(this instanceof ReverseCard && upperCard instanceof ReverseCard)){
//you can put a reverse card after a reverse card
//you can put a skip card after a skip card
return true;
}
ColoredCard upper = (ColoredCard) upperCard;
if (this.getColor().equals(upper.getColor()))
return true;
else
return false;
} else {
for (Card card : chosenUser.getUserCards()) {
if (card instanceof WildCard){
continue;
}
else {//if there is another card to choose you can not choose wild card
if (card.isAvailableToPut( chosenUser , upperCard)) {
return false;
}
}
}
return true;
}
}
......
public abstract class ColoredCard extends Card {
private String color;
/**
* create a Colored card that according to subClasses create a special kind of colored cards
......@@ -18,14 +17,6 @@ public abstract class ColoredCard extends Card {
*
* @return
*/
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
......@@ -51,33 +42,4 @@ public abstract class ColoredCard extends Card {
}
System.out.println(first + toString() + set);
}
public boolean isAvailableToPutAColoredCard(int numOfChosenCard, User chosenUser, Card upperCard) {
ColoredCard chosen = (ColoredCard) chosenUser.getUserCards().get(numOfChosenCard);
if (upperCard instanceof ColoredCard) {
ColoredCard upper = (ColoredCard) upperCard;
if (chosen instanceof NumericalCard && upper instanceof NumericalCard) {
NumericalCard chosenNum = (NumericalCard) chosen;
NumericalCard upperNum = (NumericalCard) upper;
if (upper.getColor().equals(chosen.getColor()) || upperNum.getNumber() == chosenNum.getNumber()) {
return true;
} else {
return false;
}
} else {
if (chosen.getColor().equals(upper.getColor())) {
return true;
} else {
return false;
}
}
}
else {
WildCard upper = (WildCard) upperCard;
if (upper.getNextColor().equals(chosen.getColor()))
return true;
else
return false;
}
}
}
......@@ -4,7 +4,8 @@ import java.util.Scanner;
public class Draw2Card extends MotiveCard {
GivingCard givingCard = new GivingCard();
Scanner scanner = new Scanner(System.in);
int addTwo = 2;
private int addTwo = 2;
private int flag = 0;
/**
* create a Colored card that according to subClasses create a special kind of colored cards
......@@ -18,8 +19,7 @@ public class Draw2Card extends MotiveCard {
}
@Override
public void act(User chosenUser, int numOfChosenCard, ArrayList<User> users , ArrayList<Card> remainingCards , Card upperCard ) {
int flag = 0;
public User act(User chosenUser, int numOfChosenCard, ArrayList<User> users , ArrayList<Card> remainingCards , Card upperCard ) {
upperCard = chosenUser.getUserCards().get(numOfChosenCard);
chosenUser.getUserCards().remove(numOfChosenCard);
remainingCards.add(upperCard);
......@@ -28,20 +28,23 @@ public class Draw2Card extends MotiveCard {
for (Card card : chosenUser.getUserCards()){
if (card instanceof Draw2Card){
flag = 1;
break;
}
}
if (flag == 0){
givingCard.give_n_Cards(chosenUser , remainingCards, addTwo , upperCard);
chosenUser = chosenUser.pass(users,chosenUser);
addTwo =2;//we need if another time in game we had this card this method start giving card with 2 cards
return;
flag = 0;
return chosenUser;
}
else {
addTwo += 2;// because when we use recursive function we needs that addTwo not be 2
chosenUser.printCards();
chosenUser.printCards(users);
numOfChosenCard = scanner.nextInt() - 1;
act(chosenUser ,numOfChosenCard , users , remainingCards , upperCard);
}
return chosenUser;
}
}
......@@ -19,7 +19,11 @@ public class GivingCard {
}
public Card chooseUpperCardRandomly (ArrayList<Card> remainingCards ){
int randomNum = r.nextInt(remainingCards.size());
return remainingCards.get(randomNum);
while (true) {
int randomNum = r.nextInt(remainingCards.size());
if (!(remainingCards.get(randomNum) instanceof WildCard) && !(remainingCards.get(randomNum) instanceof Draw2Card)) {
return remainingCards.get(randomNum);
}
}
}
}
......@@ -19,11 +19,13 @@ public class NumericalCard extends ColoredCard {
}
@Override
public void act(User chosenUser,int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
public User act(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
upperCard = chosenUser.getUserCards().get(numOfChosenCard);
chosenUser.getUserCards().remove(numOfChosenCard);
System.out.println(chosenUser.getUserCards().size());
remainingCards.add(upperCard);
chosenUser = chosenUser.pass(users,chosenUser);
return chosenUser;
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class ReverseCard extends MotiveCard {
......@@ -14,15 +16,13 @@ public class ReverseCard extends MotiveCard {
}
@Override
public void act(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
public User act(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
upperCard = chosenUser.getUserCards().get(numOfChosenCard);
chosenUser.getUserCards().remove(numOfChosenCard);
remainingCards.add(upperCard);
ArrayList<User> reverseUsers = new ArrayList<>();
for (int i = users.size() ; i > -1 ; i++){
reverseUsers.add(users.get(i));
}
users = reverseUsers;
chosenUser = users.get(users.indexOf(chosenUser) - 1);
Collections.reverse(users);
System.out.println(" توتابع"+ users.get(0).getName() + users.get(1).getName() + users.get(2).getName());
chosenUser = chosenUser.pass(users , chosenUser);
return chosenUser;
}
}
......@@ -15,10 +15,12 @@ public class SkipCard extends MotiveCard {
}
@Override
public void act(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
public User act(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
upperCard = chosenUser.getUserCards().get(numOfChosenCard);
chosenUser.getUserCards().remove(numOfChosenCard);
remainingCards.add(upperCard);
chosenUser = users.get(users.indexOf(chosenUser) + 2);
chosenUser = chosenUser.pass(users , chosenUser);
chosenUser = chosenUser.pass(users , chosenUser);
return chosenUser;
}
}
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.*;
public class Uno {
public static Card upperCard;
public static void main(String [] args){
int userChosenCardNum ;
int numOfPlayers = 0;
......@@ -72,7 +71,7 @@ public class Uno {
while (numOfPlayers == 0) {//set num of players and their names at first
System.out.println("plz enter number of users");
numOfPlayers = scanner.nextInt();
if (!(numOfPlayers >=3 && numOfPlayers <= 5)) {
if (!(numOfPlayers >=2 && numOfPlayers <= 5)) {
numOfPlayers = 0;
continue;
}
......@@ -81,7 +80,7 @@ public class Uno {
System.out.println("plz enter your name");
String name = scanner.next();
User user = new User(name);
// givingCard.give_n_Cards(user , remainingCards , 7);
givingCard.give_n_Cards(user , remainingCards ,7 , upperCard);
users.add(user);
}
chosenUser = users.get(0);
......@@ -90,31 +89,47 @@ public class Uno {
while (true){
for (int i = 0; i < numOfPlayers ; i++){
System.out.println(users.get(i).getName() + " " + users.get(i).getScore());
if (users.get(i).getUserCards().size() == 0){
for (int j = 0 ; j < numOfPlayers ; j++){
System.out.println(users.get(j).getName() + " " + users.get(j).getScore());
}
System.out.println(users.get(i).getName() + " won\n THE END");
return;
}
}
int flag = 0;
System.out.println("UPPER CARD IS");
upperCard.print();
System.out.println("TURN IS FOR "+chosenUser.getName() +"YOUR CARDS ARE");
chosenUser.printCards();
userChosenCardNum = scanner.nextInt() -1;
if (userChosenCardNum == chosenUser.getUserCards().size()){//if user chose draw card
givingCard.give_n_Cards(chosenUser , remainingCards , 1 ,upperCard);
if (chosenUser.getUserCards().get(chosenUser.getUserCards().size() - 1).isAvailableToPut(userChosenCardNum, chosenUser ,upperCard)){
continue;
chosenUser.printCards(users);
for (Card card : chosenUser.getUserCards()){
if (card.isAvailableToPut( chosenUser , upperCard)) {
flag = 1;
break;
}
}
if (flag == 0){//if user chose draw card
givingCard.give_n_Cards(chosenUser , remainingCards , 1 ,upperCard);
if (chosenUser.getUserCards().get(chosenUser.getUserCards().size() - 1).isAvailableToPut( chosenUser , upperCard)){
continue;// if
}
else {
System.out.println("pass");
chosenUser.printCards(users);
System.out.println("you can not choose \n pass");
chosenUser = chosenUser.pass(users , chosenUser);
continue;
}
chosenUser = chosenUser.pass(users , chosenUser);
continue;
}
else {
if (chosenUser.getUserCards().get(userChosenCardNum).isAvailableToPut(userChosenCardNum,chosenUser,upperCard)){
// chosenUser.getUserCards().get(userChosenCardNum).act(chosenUser , users ,remainingCards ,upperCard);
userChosenCardNum = scanner.nextInt() -1;
if (chosenUser.getUserCards().get(userChosenCardNum).isAvailableToPut(chosenUser,upperCard)){
chosenUser = chosenUser.getUserCards().get(userChosenCardNum).act(chosenUser , userChosenCardNum , users ,remainingCards ,upperCard);
upperCard = remainingCards.get(remainingCards.size() - 1);
}
else {
System.out.println("you can not choose this card");
}
chosenUser.removeCard(upperCard);
remainingCards.add(upperCard);
////// chosenUser = chosenUser.pass(users,chosenUser);
}
}
}
......
......@@ -47,6 +47,7 @@ public class User {
* @return score field
*/
public int getScore() {
score = 0;
for (Card card : userCards ){
score += card.getScore();
}
......@@ -54,6 +55,10 @@ public class User {
}
public User pass(ArrayList<User> users , User chosenUser){
if (users.indexOf(chosenUser) + 1 == users.size()){
return users.get(0);
}
else
return users.get(users.indexOf(chosenUser) + 1);
}
......@@ -61,7 +66,11 @@ public class User {
return userCards;
}
public void printCards(){
public void printCards(ArrayList<User> users){
for (int i = 0 ; i < users.size() ; i++){
System.out.println(users.get(i).getName()+ "=====>>"+users.get(i).getUserCards().size());
}
System.out.println("TURN IS FOR "+ name +" YOUR CARDS ARE");
System.out.println("enter the number of your card for example : 3");
int counter = 1;
for (Card card : userCards){
......
import java.util.ArrayList;
public class Wild4 extends WildCard {
int addFour = 4;
private int addFour = 4;
private int flag = 0;
public Wild4(int score) {
super(score);
name = "| +4 |\n";
}
@Override
public void act(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
int flag = 0;
public User act(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
upperCard = chosenUser.getUserCards().get(numOfChosenCard);
chosenUser.getUserCards().remove(numOfChosenCard);
remainingCards.add(upperCard);
......@@ -21,24 +21,22 @@ public class Wild4 extends WildCard {
}
}
if (flag == 0){
System.out.println(users.get(users.indexOf(chosenUser)-1).getName()+" choose a color");
chooseColor();
givingCard.give_n_Cards(chosenUser , remainingCards, addFour , upperCard);
chosenUser = chosenUser.pass(users,chosenUser);
putChosenColor(chosenUser, numOfChosenCard, users, remainingCards, upperCard);//we need this kind of putting card just for wild cards
addFour =4;//we need if another time in game we had this card this method start giving card with 2 cards
return;
flag = 0 ;
return chosenUser;
}
else {
addFour += 4;// because when we use recursive function we needs that addTwo not be 2
chosenUser.printCards();
chosenUser.printCards(users);
numOfChosenCard = scanner.nextInt() - 1;
act(chosenUser ,numOfChosenCard , users , remainingCards , upperCard);
}
putChosenColor(chosenUser, numOfChosenCard, users, remainingCards, upperCard);//we need this kind of putting card just for wild cards
chosenUser.getUserCards().remove(numOfChosenCard);
remainingCards.add(upperCard);
chosenUser = chosenUser.pass(users,chosenUser);
return chosenUser;
}
}
/**
* this Class represents wild cards
*/
import java.util.ArrayList;
import java.util.Scanner;
public abstract class WildCard extends Card {
/**
*
*/
String nextColor;
private String nextColor;
Scanner scanner = new Scanner(System.in);
GivingCard givingCard = new GivingCard();
public WildCard(int score) {
super(score);
}
public void chooseColor() {
int num = 0;
while (num < 1 && num > 4) {
do {
System.out.println("plz choose a color (just choose the number)\n1:blue 2:red 3:yellow 4:green");
num = scanner.nextInt();
}
}while (num < 1 && num > 4);
switch (num) {
case 1: {
System.out.println("next:" + blue + "blue" + set);
......@@ -44,46 +45,40 @@ public abstract class WildCard extends Card {
}
}
public void putChosenColor(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard){
public User putChosenColor(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
while (true) {
int flag = 0;
System.out.println("next card color should be" + nextColor);
System.out.println("TURN IS FOR " + chosenUser.getName() + "YOUR CARDS ARE");
chosenUser.printCards();
numOfChosenCard = scanner.nextInt() - 1;
for (Card card : chosenUser.getUserCards()){
if (card.isAvailableToPut(numOfChosenCard ,chosenUser,upperCard)){
flag = 1;// there is another card to put
System.out.println("next card color should be " + nextColor);
chosenUser.printCards(users);
for (Card c : chosenUser.getUserCards()){
if ( c.color.equals(nextColor)){
flag = 1;
break;
}
}
if (flag == 0) {//if there is not any card to put user should chose draw card
givingCard.give_n_Cards(chosenUser, remainingCards, 1, upperCard);
if (chosenUser.getUserCards().get(chosenUser.getUserCards().size() - 1).isAvailableToPut(numOfChosenCard, chosenUser, upperCard)) {
continue;
} else {
System.out.println("pass");
if (flag == 0){
System.out.println("there is not ant choice for "+ chosenUser.getName()+"\nPASS");
chosenUser = chosenUser.pass(users , chosenUser );
continue;
}
numOfChosenCard = scanner.nextInt() - 1;
if (chosenUser.getUserCards().get(numOfChosenCard) instanceof ColoredCard){
ColoredCard coloredCard = (ColoredCard) chosenUser.getUserCards().get(numOfChosenCard);
if (coloredCard.getColor().equals(nextColor)) {
upperCard = chosenUser.getUserCards().get(numOfChosenCard);
chosenUser.getUserCards().remove(numOfChosenCard);
remainingCards.add(upperCard);
chosenUser = chosenUser.pass(users, chosenUser);
continue;
}
} else {
if (chosenUser.getUserCards().get(numOfChosenCard) instanceof WildCard) {
return chosenUser;
} else {
System.out.println("you can not choose it");
continue;
} else {
ColoredCard coloredCard = (ColoredCard) chosenUser.getUserCards().get(numOfChosenCard);
if (coloredCard.getColor().equals(nextColor)) {
upperCard = chosenUser.getUserCards().get(numOfChosenCard);
chosenUser.getUserCards().remove(numOfChosenCard);
remainingCards.add(upperCard);
chosenUser = chosenUser.pass(users, chosenUser);
return;
} else {
System.out.println("you can not choose it");
continue;
}
}
}
else {// you can not put a wild card after a wild card
System.out.println("you can not choose it");
continue;
}
}
}
......@@ -91,9 +86,6 @@ public abstract class WildCard extends Card {
this.nextColor = nextColor;
}
public String getNextColor() {
return nextColor;
}
@Override
public String toString() {
......@@ -110,14 +102,4 @@ public abstract class WildCard extends Card {
System.out.println(toString());
}
public boolean isAvailableToPutAWildCard(int numOfChosenCard, User chosenUser, Card upperCard) {
if ( chosenUser.getUserCards().size() == 1) // you can not put a wild card when the wild card is the last card
return false;
for (Card card : chosenUser.getUserCards()) {
if (card.isAvailableToPut(numOfChosenCard, chosenUser, upperCard) && !card.equals(chosenUser.getUserCards().get(numOfChosenCard))) {//if there is another card to choose you can not choose wild card
return false;
}
}
return true;
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class WildColor extends WildCard {
Scanner scanner = new Scanner(System.in);
public WildColor(int score) {
super(score);
name = "| setColor |\n";
}
@Override
public void act(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
public User act(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
upperCard = chosenUser.getUserCards().get(numOfChosenCard);
chosenUser.getUserCards().remove(numOfChosenCard);
upperCard.print();
chooseColor();
chosenUser.getUserCards().remove(numOfChosenCard);
remainingCards.add(upperCard);
chosenUser = chosenUser.pass(users,chosenUser);
putChosenColor(chosenUser, numOfChosenCard, users, remainingCards, upperCard);//we need this kind of putting card just for wild cards
chosenUser = putChosenColor(chosenUser, numOfChosenCard, users, remainingCards, upperCard);//we need this kind of putting card just for wild cards
return chosenUser;
}
}
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