Commit b9897eb4 authored by 9731301's avatar 9731301

implement act and isAvailableToPut methods in Card Class and it subClasses

parent 55d28438
...@@ -3,14 +3,6 @@ ...@@ -3,14 +3,6 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="608d52d9-3dd7-4e55-84b9-421c09e25ee0" name="Default Changelist" comment=""> <list default="true" id="608d52d9-3dd7-4e55-84b9-421c09e25ee0" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Uno/ColoredCard.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Uno/ColoredCard.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Uno/Draw2Card.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Uno/Draw2Card.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Uno/ReverseCard.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Uno/ReverseCard.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Uno/SkipCard.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Uno/SkipCard.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Uno/Uno.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Uno/Uno.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Uno/Wild4.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Uno/Wild4.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Uno/WildCard.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Uno/WildCard.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Uno/WildColor.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Uno/WildColor.class" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
...@@ -20,6 +12,7 @@ ...@@ -20,6 +12,7 @@
<component name="FileTemplateManagerImpl"> <component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES"> <option name="RECENT_TEMPLATES">
<list> <list>
<option value="Interface" />
<option value="Class" /> <option value="Class" />
</list> </list>
</option> </option>
...@@ -68,6 +61,7 @@ ...@@ -68,6 +61,7 @@
<workItem from="1586769548038" duration="8743000" /> <workItem from="1586769548038" duration="8743000" />
<workItem from="1586800273922" duration="8352000" /> <workItem from="1586800273922" duration="8352000" />
<workItem from="1586853542539" duration="15929000" /> <workItem from="1586853542539" duration="15929000" />
<workItem from="1586930969911" duration="22987000" />
</task> </task>
<servers /> <servers />
</component> </component>
...@@ -75,10 +69,10 @@ ...@@ -75,10 +69,10 @@
<option name="version" value="1" /> <option name="version" value="1" />
</component> </component>
<component name="WindowStateProjectService"> <component name="WindowStateProjectService">
<state x="574" y="97" key="#com.intellij.ide.util.MemberChooser" timestamp="1586873194317"> <state x="522" y="245" width="665" height="579" key="#com.intellij.ide.util.MemberChooser" timestamp="1586954939488">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="574" y="97" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1586873194317" /> <state x="522" y="245" width="665" height="579" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1586954939488" />
<state width="1493" height="261" key="GridCell.Tab.0.bottom" timestamp="1586875227765"> <state width="1493" height="261" key="GridCell.Tab.0.bottom" timestamp="1586875227765">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
...@@ -111,10 +105,10 @@ ...@@ -111,10 +105,10 @@
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="261" key="GridCell.Tab.1.right/0.0.1536.824@0.0.1536.824" timestamp="1586875227765" /> <state width="1493" height="261" key="GridCell.Tab.1.right/0.0.1536.824@0.0.1536.824" timestamp="1586875227765" />
<state x="107" y="217" key="com.intellij.ide.util.TipDialog" timestamp="1586853731045"> <state x="107" y="217" key="com.intellij.ide.util.TipDialog" timestamp="1586930975674">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="107" y="217" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1586853731045" /> <state x="107" y="217" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1586930975674" />
<state x="425" y="187" key="run.anything.popup" timestamp="1586862725471"> <state x="425" y="187" key="run.anything.popup" timestamp="1586862725471">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
......
import java.util.ArrayList;
public abstract class Card { public abstract class Card {
protected String name; protected String name;
private int score; private int score;
...@@ -24,8 +26,26 @@ public abstract class Card { ...@@ -24,8 +26,26 @@ public abstract class Card {
} }
public abstract String toString(); public abstract String toString();
public abstract void print(); public abstract void print();
public abstract void 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))
return true;
else
return false;
} else {
WildCard chosenWildCard = (WildCard) chosenUser.getUserCards().get(numOfChosenCard);
if (chosenWildCard.isAvailableToPutAWildCard(numOfChosenCard, chosenUser, upperCard))
return true;
else
return false;
}
}
} }
/** public abstract class ColoredCard extends Card {
*
*/
public class ColoredCard extends Card {
private String color; private String color;
/** /**
...@@ -32,11 +29,11 @@ public class ColoredCard extends Card { ...@@ -32,11 +29,11 @@ public class ColoredCard extends Card {
@Override @Override
public String toString() { public String toString() {
String s ="|$$$$$$$$$$$$$$$|\n"; String s = "|$$$$$$$$$$$$$$$|\n";
s +="| |\n"; s += "| |\n";
s += name+"\n"; s += name + "\n";
s +="| |\n"; s += "| |\n";
s +="|$$$$$$$$$$$$$$$|\n"; s += "|$$$$$$$$$$$$$$$|\n";
return s; return s;
} }
...@@ -50,7 +47,37 @@ public class ColoredCard extends Card { ...@@ -50,7 +47,37 @@ public class ColoredCard extends Card {
} else if (color.equals("blue")) { } else if (color.equals("blue")) {
first = blue; first = blue;
} else if (color.equals("green")) { } else if (color.equals("green")) {
first = green;} first = green;
System.out.println(first + toString() + set); }
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;
}
} }
} }
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner;
public class Draw2Card extends MotiveCard { public class Draw2Card extends MotiveCard {
GivingCard givingCard = new GivingCard();
Scanner scanner = new Scanner(System.in);
int addTwo = 2;
/** /**
* create a Colored card that according to subClasses create a special kind of colored cards * create a Colored card that according to subClasses create a special kind of colored cards
...@@ -14,9 +18,37 @@ public class Draw2Card extends MotiveCard { ...@@ -14,9 +18,37 @@ public class Draw2Card extends MotiveCard {
} }
@Override @Override
public void move(User chosenUser, ArrayList<User> users) { public void act(User chosenUser, ArrayList<User> users , ArrayList<Card> remainingCards , Card upperCard ) {
int numChoice;
chosenUser = users.get(users.indexOf(chosenUser) + 1);
chosenUser.printCards();
numChoice = scanner.nextInt() - 1;
if (numChoice == chosenUser.getUserCards().size() - 1){
givingCard.give_n_Cards(chosenUser , remainingCards, addTwo , upperCard);
chosenUser = users.get(users.indexOf(chosenUser) + 1);
System.out.println("you draw "+addTwo+" cards");
addTwo =2;//we need if another time in game we had this card this method start giving card with 2 cards
return;
} }
else if (isAvailableToPut(numChoice , chosenUser)){
upperCard = chosenUser.getUserCards().get(numChoice);
chosenUser.getUserCards().remove(chosenUser.getUserCards().get(numChoice));
remainingCards.add(chosenUser.getUserCards().get(numChoice));
addTwo += 2;// because when we use recursive function we needs that addTwo not be 2
act(chosenUser , users , remainingCards , upperCard);
}
else {
}
}
@Override
public boolean isAvailableToPut(int numOfChosenCard , User chosenUser ) {
if (chosenUser.getUserCards().get(numOfChosenCard) instanceof Draw2Card){
return true;
}
else return false;
}
} }
...@@ -7,9 +7,10 @@ public class GivingCard { ...@@ -7,9 +7,10 @@ public class GivingCard {
r = new Random(); r = new Random();
} }
public void give_n_Cards(User chosenUser , ArrayList<Card> remainingCards , int n){ public void give_n_Cards(User chosenUser , ArrayList<Card> remainingCards , int n , Card upperCard){
for (int cardNam = 0; cardNam < n ; ){ for (int cardNam = 0; cardNam < n ; ){
int randomNum = r.nextInt(remainingCards.size()); int randomNum = r.nextInt(remainingCards.size());
if (!remainingCards.get(randomNum).equals(upperCard))
chosenUser.addCard( remainingCards.get(randomNum)); chosenUser.addCard( remainingCards.get(randomNum));
remainingCards.remove(randomNum); remainingCards.remove(randomNum);
cardNam++; cardNam++;
......
...@@ -11,5 +11,4 @@ public abstract class MotiveCard extends ColoredCard{ ...@@ -11,5 +11,4 @@ public abstract class MotiveCard extends ColoredCard{
super(color, score); super(color, score);
} }
public abstract void move(User chosenUser, ArrayList<User> users);
} }
\ No newline at end of file
import java.util.ArrayList;
public class NumericalCard extends ColoredCard { public class NumericalCard extends ColoredCard {
private int number; private int number;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public NumericalCard(String color, int score, int number) { public NumericalCard(String color, int score, int number) {
super(color, score); super(color, score);
...@@ -8,4 +17,13 @@ public class NumericalCard extends ColoredCard { ...@@ -8,4 +17,13 @@ public class NumericalCard extends ColoredCard {
name="| "+ Integer.toString(number) +" |"; name="| "+ Integer.toString(number) +" |";
} }
@Override
public void 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 = chosenUser.pass(users,chosenUser);
}
} }
...@@ -14,14 +14,15 @@ public class ReverseCard extends MotiveCard { ...@@ -14,14 +14,15 @@ public class ReverseCard extends MotiveCard {
} }
@Override @Override
public void move(User chosenUser, ArrayList<User> users) { public void act(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
chosenUser = users.get(users.indexOf(chosenUser) - 1); upperCard = chosenUser.getUserCards().get(numOfChosenCard);
chosenUser.getUserCards().remove(numOfChosenCard);
remainingCards.add(upperCard);
ArrayList<User> reverseUsers = new ArrayList<>(); ArrayList<User> reverseUsers = new ArrayList<>();
for (int i = users.size() ; i > -1 ; i++){ for (int i = users.size() ; i > -1 ; i++){
reverseUsers.add(users.get(i)); reverseUsers.add(users.get(i));
} }
users = reverseUsers; users = reverseUsers;
chosenUser = users.get(users.indexOf(chosenUser) - 1);
} }
} }
...@@ -14,9 +14,11 @@ public class SkipCard extends MotiveCard { ...@@ -14,9 +14,11 @@ public class SkipCard extends MotiveCard {
name = "| skip |"; name = "| skip |";
} }
@Override @Override
public void move(User chosenUser , ArrayList<User> users) { public void 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 = users.get(users.indexOf(chosenUser) + 2);
} }
} }
...@@ -95,10 +95,27 @@ public class Uno { ...@@ -95,10 +95,27 @@ public class Uno {
System.out.println("TURN IS FOR "+chosenUser.getName() +"YOUR CARDS ARE"); System.out.println("TURN IS FOR "+chosenUser.getName() +"YOUR CARDS ARE");
chosenUser.printCards(); chosenUser.printCards();
userChosenCardNum = scanner.nextInt() -1; userChosenCardNum = scanner.nextInt() -1;
upperCard = chosenUser.getUserCards().get(userChosenCardNum); if (userChosenCardNum == chosenUser.getUserCards().size()){//if user chose draw card
chosenUser.removeCard(upperCard); givingCard.give_n_Cards(chosenUser , remainingCards , 1 ,upperCard);
remainingCards.add(upperCard); if (chosenUser.getUserCards().get(chosenUser.getUserCards().size() - 1).isAvailableToPut(userChosenCardNum, chosenUser ,upperCard)){
chosenUser = chosenUser.pass(users,chosenUser); continue;
}
else {
System.out.println("pass");
}
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);
}
chosenUser.removeCard(upperCard);
remainingCards.add(upperCard);
////// chosenUser = chosenUser.pass(users,chosenUser);
}
} }
} }
} }
...@@ -7,9 +7,8 @@ public class Wild4 extends WildCard { ...@@ -7,9 +7,8 @@ public class Wild4 extends WildCard {
name = "| +4 |\n"; name = "| +4 |\n";
} }
@Override
public void act(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
public void giveCard(User chosenUser, ArrayList<Card> cards , Card chosenCard , ArrayList<User> users) {
System.out.println("is it a bluff؟"); System.out.println("is it a bluff؟");
//if (.equals("yes")) //if (.equals("yes"))
......
...@@ -2,35 +2,53 @@ import java.util.Scanner; ...@@ -2,35 +2,53 @@ import java.util.Scanner;
public abstract class WildCard extends Card { public abstract class WildCard extends Card {
String nextColor;
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
public WildCard(int score){ public WildCard(int score) {
super(score); super(score);
} }
public void chooseColor() {
System.out.println("plz choose a color (just choose the number)\n1:blue 2:red 3:yellow 4:green"); public void chooseColor() {
switch (scanner.nextInt()) { int num = 0;
while (num < 1 && num > 4) {
System.out.println("plz choose a color (just choose the number)\n1:blue 2:red 3:yellow 4:green");
num = scanner.nextInt();
}
switch (num) {
case 1: { case 1: {
System.out.println("next:" + blue + "blue" + set); System.out.println("next:" + blue + "blue" + set);
break; setNextColor("blue");
return;
} }
case 2: { case 2: {
System.out.println("next:" + red + "red" + set); System.out.println("next:" + red + "red" + set);
break; setNextColor("red");
return;
} }
case 3: { case 3: {
System.out.println("next:" + yellow + "yellow" + set); System.out.println("next:" + yellow + "yellow" + set);
break; setNextColor("yellow");
return;
} }
case 4: { case 4: {
System.out.println("next:" + green + "green" + set); System.out.println("next:" + green + "green" + set);
break; setNextColor("green");
return;
} }
} }
} }
public void setNextColor(String nextColor) {
this.nextColor = nextColor;
}
public String getNextColor() {
return nextColor;
}
@Override @Override
public String toString(){ public String toString() {
String s = "|$$$$$$$$$$$$$$$|\n"; String s = "|$$$$$$$$$$$$$$$|\n";
s += "| |\n"; s += "| |\n";
s += name; s += name;
...@@ -40,7 +58,18 @@ public abstract class WildCard extends Card { ...@@ -40,7 +58,18 @@ public abstract class WildCard extends Card {
} }
@Override @Override
public void print(){ public void print() {
System.out.println(toString()); System.out.println(toString());
} }
public boolean isAvailableToPutAWildCard(int numOfChosenCard, User chosenUser, Card upperCard) {//if there is another card to choose you can not choose wild card
if (upperCard instanceof WildCard) // you can not put a wild card if upper card is wild
return false;
for (Card card : chosenUser.getUserCards()) {
if (card.isAvailableToPut(numOfChosenCard, chosenUser, upperCard) && !card.equals(chosenUser.getUserCards().get(numOfChosenCard))) {
return false;
}
}
return false;
}
} }
import java.util.ArrayList;
import java.util.Scanner;
public class WildColor extends WildCard { public class WildColor extends WildCard {
Scanner scanner = new Scanner(System.in);
String color ;
public WildColor(int score) { public WildColor(int score) {
super(score); super(score);
name = "| setColor |\n"; name = "| setColor |\n";
} }
@Override
public void act(User chosenUser, int numOfChosenCard, ArrayList<User> users, ArrayList<Card> remainingCards, Card upperCard) {
chooseColor();
upperCard = chosenUser.getUserCards().get(numOfChosenCard);
chosenUser.getUserCards().remove(numOfChosenCard);
remainingCards.add(upperCard);
chosenUser = chosenUser.pass(users,chosenUser);
}
public void setColor(String color) {
this.color = color;
}
} }
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