提交作业 - 小弟是新手,请各方大佬指点指点
//////////////////////Poker.java///////////////////////////////
/**
*
*/
package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
/**
* @author zhouhan
*
*/
public class Poker implements Comparable<Poker> {
public String suit;
public String num;
final public static String[] suits = {"Diamand", "Club", "Heart", "Spade"}; // increase
final public static String[] nums = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; // increase
public static ArrayList<Poker> PokersInOrder = new ArrayList<Poker>();
public static ArrayList<Poker> PokersUnordered = new ArrayList<Poker>();
static{
for(int i=0; i<nums.length; i++){
for(int j=0; j<suits.length; j++){
PokersInOrder.add(new Poker(suits[j],nums[i]));
}
}
}
public static void shuffle(){
PokersUnordered.clear();
PokersUnordered.addAll(PokersInOrder);
Collections.shuffle(PokersUnordered);
System.out.println("洗牌完毕");
}
public static void showPokersInOrder(){
if(PokersInOrder.isEmpty()){
System.out.println("PokersInOrder is empty!");
}
else{
System.out.println("PokersInOrder is as blow: ");
System.out.println("************************************");
for(Poker poker : PokersInOrder)
System.out.println(poker.suit + " " + poker.num);
System.out.println("************************************");
}
}
public static void showPokersUnordered(){
if(PokersUnordered.isEmpty()){
System.out.println("PokersUnordered is empty!");
}
else{
System.out.println("PokersUnordered is as blow!");
System.out.println("************************************");
for(Poker poker : PokersUnordered)
System.out.println(poker.suit + " " + poker.num);
System.out.println("************************************");
}
}
public static int getSuitIndex(String suit){
for(int i=0; i<Poker.suits.length; i++){
if(Poker.suits[i].equals(suit)){
return i;
}
}
return -1;
}
public static int getNumIndex(String num){
for(int i=0; i<Poker.nums.length; i++){
if(Poker.nums[i].equals(num)){
return i;
}
}
return -1;
}
public Poker() {
}
public Poker(Poker o) {
this.suit = o.suit;
this.num = o.num;
}
public Poker(String suit, String num) {
this.suit = suit;
this.num = num;
}
public void setPoker(Poker o) {
this.suit = o.suit;
this.num = o.num;
}
public void setPoker(String suit, String num) {
this.suit = suit;
this.num = num;
}
@Override
public int compareTo(Poker o) {
// TODO Auto-generated method stub
if(this == o) //equal
return 0;
//牌面大小比较
if(Poker.getNumIndex(this.num)!=-1 && Poker.getNumIndex(o.num)!=-1){
if(Poker.getNumIndex(this.num) > Poker.getNumIndex(o.num)){
return 1;
}
else if(Poker.getNumIndex(this.num) < Poker.getNumIndex(o.num)){
return -1;
}
else{
//花色比较
if(Poker.getSuitIndex(this.suit)!=-1 && Poker.getSuitIndex(o.suit)!=-1){
if(Poker.getSuitIndex(this.suit) > Poker.getSuitIndex(o.suit)){
return 1;
}
else if(Poker.getSuitIndex(this.suit) < Poker.getSuitIndex(o.suit)){
return -1;
}
else{
return 0;
}
}
}
}
return 0;
}
}
///////////////////////////////Player.java///////////////////////////////
/**
*
*/
package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
/**
* @author zhouhan
*
*/
public class Player {
public String playName;
public ArrayList<Poker> pokerInHand = new ArrayList<Poker>();
public Player(){
}
public Player(String playName){
this.playName = playName;
}
public void drawPoker(Poker poker){
this.pokerInHand.add(poker);
}
public void sortPokers(){
Collections.sort(pokerInHand);
}
public void maxPoker(){
Poker poker = pokerInHand.get(pokerInHand.size()-1);
System.out.println(this.playName + "的最大牌是:" + poker.suit + " " + poker.num);
}
public void showPokersInHand(){
if(pokerInHand.isEmpty()){
System.out.println("No pokers in hand!");
}
else{
System.out.println(this.playName + "手上的牌是:");
for(Poker poker : pokerInHand){
System.out.println(poker.suit + " " + poker.num);
}
}
}
public int comparePokers(Player player2){
if(this == player2){
System.out.println("相同玩家不能比较!");
return 0;
}
else{
int i = this.pokerInHand.size()-1;
return this.pokerInHand.get(i).compareTo(player2.pokerInHand.get(i));
}
}
}
///////////////////////////////PlayGame.java///////////////////////////////
package com.imooc;
import java.util.Scanner;
public class PlayGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//1 创建选手
System.out.println("1 请输入两位选手的姓名:");
System.out.println("第一位玩家:");
Player player1 = new Player(input.next());
System.out.println("第二位玩家:");
Player player2 = new Player(input.next());
//2 洗牌,发牌
System.out.println("2 开始洗牌...");
Poker.shuffle();
Poker.showPokersUnordered();
System.out.println("开始发牌...");
for(int i=0; i<2; i++){
player1.drawPoker(Poker.PokersUnordered.get(2*i+0)); //随机发牌
player2.drawPoker(Poker.PokersUnordered.get(2*i+1)); //随机发牌
}
//3 亮牌,比较大小
System.out.println("3 两位玩家开始亮牌:");
player1.sortPokers(); player2.sortPokers();
player1.showPokersInHand(); player1.maxPoker();
player2.showPokersInHand(); player2.maxPoker();
//4 公布游戏结果
System.out.println("4 两位玩家开始比牌:");
Player winner = player1.comparePokers(player2)==1? player1 : player2;
System.out.println("本局游戏的赢家是:" + winner.playName);
}
}