交作业了,嗷呜呜。。。。。。
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
public class PlayingCard{
private ArrayList <Card> card = new ArrayList<Card>();
private Scanner scn = new Scanner(System.in);
private String cardType;
private String cardNumber;
private Player player1;
private Player player2;
public static void main(String [] args){
PlayingCard playingCard = new PlayingCard();
playingCard.createCard();
playingCard.shuffleCard();
playingCard.createPlayer();
playingCard.dealCard();
playingCard.compareCard();
}
public void createCard(){
System.out.println("--------------Creating playing card---------------");
for(int i = 0; i < 4; i++){
switch(i){
case 0:
cardType = "Square";
break;
case 1:
cardType = "Plum bolssom";
break;
case 2:
cardType = "Red peach";
break;
case 3:
cardType = "Spade";
}
for(int j = 2; j <=14; j++){
if(j > 10){
switch(j){
case 11:
cardNumber = "J";
break;
case 12:
cardNumber = "Q";
break;
case 13:
cardNumber = "K";
break;
case 14:
cardNumber = "A";
break;
}
}
else{
cardNumber = j+"";
}
card.add( new Card(cardType,cardNumber));
}
}
System.out.println("--------------Playing card created---------------");
System.out.println("Playing card is that:");
for(Card temp : card){
System.out.printf("%s\n",temp.cardType+temp.cardNumber);
}
}
public void shuffleCard(){
System.out.println("--------------Shuffle playing card---------------");
Collections.shuffle(card);
System.out.println("--------------Shuffled---------------");
for(Card temp : card){
System.out.printf("%s\n",temp.cardType+temp.cardNumber);
}
}
public void createPlayer(){
String name1,name2,id1,id2;
System.out.print("Please input the ID of first player: ");
id1 = scn.nextLine();
System.out.print("Please input the name of first player: ");
name1 = scn.nextLine();
System.out.print("Please input the ID of second player: ");
id2 = scn.nextLine();
System.out.print("Please input the name of second player: ");
name2 = scn.nextLine();
player1 = new Player(id1,name1);
player2 = new Player(id2,name2);
System.out.println("Welcome player: "+player1.name);
System.out.println("Welcome player: "+player2.name);
}
public void dealCard(){
System.out.println("--------------Dealing playing card---------------");
player1.haveCard.add(card.get(0));
player2.haveCard.add(card.get(1));
player1.haveCard.add(card.get(2));
player2.haveCard.add(card.get(3));
System.out.println("--------------Dealt playing card---------------");
}
public void compareCard(){
Card card1,card2;
System.out.println("--------------Start game--------------");
card1 = maxCard(player1.haveCard.get(0),player1.haveCard.get(1)) > 0 ? player1.haveCard.get(0) : player1.haveCard.get(1);
card2 = maxCard(player2.haveCard.get(0),player2.haveCard.get(1)) > 0 ? player2.haveCard.get(0) : player2.haveCard.get(1);
System.out.println("Player "+player1.name+"'s max playing card is "+card1.cardType+card1.cardNumber);
System.out.println("Player "+player2.name+"'s max playing card is "+card2.cardType+card2.cardNumber);
System.out.println("--------------Player "+(maxCard(card1,card2) > 0 ? player1.name : player2.name)+" won!--------------");
System.out.println("Each player's own card are:");
System.out.print(player1.name+"'s playing card: ");
System.out.printf(player1.haveCard.get(0).cardType+player1.haveCard.get(0).cardNumber+"\t");
System.out.printf(player1.haveCard.get(1).cardType+player1.haveCard.get(1).cardNumber+"\n");
System.out.print(player2.name+"'s playing card: ");
System.out.printf(player2.haveCard.get(0).cardType+player2.haveCard.get(0).cardNumber+"\t");
System.out.printf(player2.haveCard.get(1).cardType+player2.haveCard.get(1).cardNumber+"\n");
}
public int maxCard(Card card1,Card card2){
if((card1.cardNumber.compareTo("1") > 0 && card1.cardNumber.compareTo("9") <= 0) || (card2.cardNumber.compareTo("1") > 0 && card2.cardNumber.compareTo("9") <= 0)){
if(card1.cardNumber.equals("10")){
return 1;
}
if(card2.cardNumber.equals("10")){
return -1;
}
if(card1.cardNumber.compareTo(card2.cardNumber) != 0){
return card1.cardNumber.compareTo(card2.cardNumber);
}
else{
return compareNumberType(card1,card2);
}
}
int number1=0;
int number2=0;
switch(card1.cardNumber){
case "10":
number1 = 1;
case "J":
number1 = 2;
break;
case "Q":
number1 = 3;
break;
case "K":
number1 = 4;
break;
case "A":
number1 = 5;
break;
default:
System.out.println("Unknow card number1!!!");
}
switch(card2.cardNumber){
case "10":
number2 = 1;
case "J":
number2 = 2;
break;
case "Q":
number2 = 3;
break;
case "K":
number2 = 4;
break;
case "A":
number2 = 5;
break;
default:
System.out.println("Unknow card number2!!!");
}
if(number1-number2 != 0){
return number1-number2;
}
else{
return compareNumberType(card1,card2);
}
}
public int compareNumberType(Card card1,Card card2){
int number3=0;
int number4=0;
switch(card1.cardType){
case "Square":
number3 = 1;
break;
case "Plum bolssom":
number3 = 2;
break;
case "Red peach":
number3 = 3;
break;
case "Spade":
number3 = 4;
break;
default:
System.out.println("Unknow card type1!!!");
}
switch(card2.cardType){
case "Square":
number4 = 1;
break;
case "Plum bolssom":
number4 = 2;
break;
case "Red peach":
number4 = 3;
break;
case "Spade":
number4 = 4;
break;
default:
System.out.println("Unknow card type2!!!");
}
return number3-number4;
}
}
import java.util.ArrayList;
public class Player{
public String id;
public String name;
public ArrayList <Card> haveCard = new ArrayList<Card> ();
public Player(String id, String name){
this.id = id;
this.name = name;
}
}
public class Card{
public String cardType;
public String cardNumber;
public Card(String cardType, String cardNumber){
this.cardType = cardType;
this.cardNumber = cardNumber;
}
}