狼女
2016-06-22 18:39
//Card import java.util.Arrays; import java.util.List; public class Card implements Comparable<Card> { private String colour; private String point; public final static List<String> cardColour = Arrays.asList("方片", "梅花", "红桃", "黑桃"); public final static List<String> cardPoint = Arrays.asList("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "k", "A"); public Card() { } public Card(String colour, String point) { this.colour = colour; this.point = point; } public String getColour() { return colour; } public String getPoint() { return point; } // 不然输出的都是地址 @Override public String toString() { return "[" + colour + " " + point + "]"; } @Override public int compareTo(Card o) { if (cardPoint.indexOf(this.point) == cardPoint.indexOf(o.point)) { return Integer.valueOf(cardColour.indexOf(this.colour)) .compareTo(Integer.valueOf(cardColour.indexOf(o.colour))); } else { return Integer.valueOf(cardPoint.indexOf(this.point)) .compareTo(Integer.valueOf(cardPoint.indexOf(o.point))); } } }
//player import java.util.ArrayList; import java.util.List; public class Player { private int id; private String name; private List<Card> palyCards; public Player() { } public Player(int id, String name) { this.id = id; this.name = name; palyCards = new ArrayList<Card>(); } public int getId() { return id; } public String getName() { return name; } public List<Card> getPalyCards() { return palyCards; } }
//game import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Game { private Scanner scanner = new Scanner(System.in); private int playerNum = 2; private int cardNum = 2; private static List<Card> cardList; private static List<Player> playerList; private Game() { cardList = new ArrayList<Card>(); playerList = new ArrayList<Player>(); } // 顺序的初始牌 private void newCard() { String[] colourGame = { "黑桃", "红桃", "梅花", "方片" }; String[] pointGame = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "k", "A" }; Card newcard; for (int i = 0; i < colourGame.length; i++) { for (int j = 0; j < pointGame.length; j++) { newcard = new Card(colourGame[i], pointGame[j]); cardList.add(newcard); // System.out.println(newcard.getColour() + newcard.getPoint()); } } } // 洗牌 private void Shuffle() { System.out.println("--------洗牌--------"); Collections.shuffle(cardList); } // 获取玩家 private void gamePlayer() { int num = 0; int id = 0; while (num < playerNum) { try { System.out.println("请输入第" + (num + 1) + "个玩家的ID:"); id = scanner.nextInt(); } catch (Exception e) { scanner.next(); System.out.println("输入错误,请输入正整数!!"); continue; } System.out.println("请输入第" + (num + 1) + "个玩家的姓名:"); String name; name = scanner.next(); Player player = new Player(id, name); playerList.add(player); System.out.println("添加了ID:" + player.getId() + "的玩家:" + player.getName()); num++; } } // 发牌 private void cardDeal() { System.out.println("--------开始发牌--------"); for (int i = 0; i < cardNum; i++) { for (int j = 0; j < playerNum; j++) { Card cd = cardList.get(i * playerNum + j); playerList.get(j).getPalyCards().add(cd); // System.out.println(playerList.get(j).getPalyCards()); } } System.out.println("--------发牌结束--------"); } // 游戏进行 private void play() { System.out.println("--------游戏开始--------"); int winPlayer = 0; Card maxCard = playerList.get(0).getPalyCards().get(0); for (int i = 0; i < playerNum; i++) { Collections.sort(playerList.get(i).getPalyCards()); Collections.reverse(playerList.get(i).getPalyCards()); System.out.println("玩家:" + playerList.get(i).getName() + " 的手牌为:" + playerList.get(i).getPalyCards()); if (maxCard.compareTo(playerList.get(i).getPalyCards().get(0)) < 0) { maxCard = playerList.get(i).getPalyCards().get(0); winPlayer = i; } } System.out.println("恭喜!!" + "ID:" + playerList.get(winPlayer).getId() + " 的玩家:" + playerList.get(winPlayer).getName() + " 获得胜利!"); System.out.println("最大的牌为:" + playerList.get(winPlayer).getPalyCards().get(0)); } public static void main(String[] args) { // TODO Auto-generated method stub Game game = new Game(); game.gamePlayer(); game.newCard(); game.Shuffle(); game.cardDeal(); game.play(); } }
已修改id重复不检测
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Game { private Scanner scanner = new Scanner(System.in); private int playerNum = 2; private int cardNum = 2; private static List<Card> cardList; private static List<Player> playerList; private Game() { cardList = new ArrayList<Card>(); playerList = new ArrayList<Player>(); } // 顺序的初始牌 private void newCard() { String[] colourGame = { "黑桃", "红桃", "梅花", "方片" }; String[] pointGame = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "k", "A" }; Card newcard; for (int i = 0; i < colourGame.length; i++) { for (int j = 0; j < pointGame.length; j++) { newcard = new Card(colourGame[i], pointGame[j]); cardList.add(newcard); // System.out.println(newcard.getColour() + newcard.getPoint()); } } } // 洗牌 private void Shuffle() { System.out.println("--------洗牌--------"); Collections.shuffle(cardList); } // 获取玩家 private void gamePlayer() { int num = 0; int id = 0; while (num < playerNum) { try { System.out.println("请输入第" + (num + 1) + "个玩家的ID:"); id = scanner.nextInt(); Player playerID=new Player(); playerID.setId(id); if (!playerList.contains(playerID)) { System.out.println("请输入第" + (num + 1) + "个玩家的姓名:"); String name; name = scanner.next(); Player player = new Player(id, name); playerList.add(player); System.out.println("添加了ID:" + player.getId() + "的玩家:" + player.getName()); num++; }else { System.out.println("您输入的ID已被占用,请重新输入!!"); continue; } } catch (Exception e) { scanner.next(); System.out.println("输入错误,请输入正整数!!"); continue; } } } // 发牌 private void cardDeal() { System.out.println("--------开始发牌--------"); for (int i = 0; i < cardNum; i++) { for (int j = 0; j < playerNum; j++) { Card cd = cardList.get(i * playerNum + j); playerList.get(j).getPalyCards().add(cd); // System.out.println(playerList.get(j).getPalyCards()); } } System.out.println("--------发牌结束--------"); } // 游戏进行 private void play() { System.out.println("--------游戏开始--------"); int winPlayer = 0; Card maxCard = playerList.get(0).getPalyCards().get(0); for (int i = 0; i < playerNum; i++) { Collections.sort(playerList.get(i).getPalyCards()); Collections.reverse(playerList.get(i).getPalyCards()); System.out.println("玩家:" + playerList.get(i).getName() + " 的手牌为:" + playerList.get(i).getPalyCards()); if (maxCard.compareTo(playerList.get(i).getPalyCards().get(0)) < 0) { maxCard = playerList.get(i).getPalyCards().get(0); winPlayer = i; } } System.out.println("恭喜!!" + "ID:" + playerList.get(winPlayer).getId() + " 的玩家:" + playerList.get(winPlayer).getName() + " 获得胜利!"); System.out.println("最大的牌为:" + playerList.get(winPlayer).getPalyCards().get(0)); } public static void main(String[] args) { // TODO Auto-generated method stub Game game = new Game(); game.gamePlayer(); game.newCard(); game.Shuffle(); game.cardDeal(); game.play(); } }
同学,咱输入重复的id也不会被检测。还有就是我们输出扑克牌是换行的。和老师的效果不一样啊。
Java入门第三季
409792 学习 · 4340 问题
相似问题