五月NING
2018-06-27 14:54
public class MainClass {
public static void main(String[] args) {
Game game = Game.newInstance();
game.initUser(2);
game.dealCards(3);
game.playCards();
}
}
public class Game {
private List<Cards> mCaraList;
private List<User> mUserList;
private Game() {
if(mCaraList==null) {
mCaraList = new ArrayList<>();
}
if(mUserList==null) {
mUserList = new ArrayList<>();
}
initCards();
shuffleCards();
}
public static Game newInstance() {
return new Game();
}
private void initCards() {
System.out.println("-----------创建扑克牌--------------");
mCaraList.clear();
for (String colour : Constant.ARRAY_COLOUR) {
for (String point : Constant.ARRAY_POINT) {
mCaraList.add(new Cards(colour, point));
}
}
System.out.println("-----------扑克牌创建成功!--------------");
}
private void shuffleCards() {
System.out.println("-----------开始洗牌!--------------");
Collections.shuffle(mCaraList);
Collections.shuffle(mCaraList);
System.out.println("-----------洗牌结束!--------------");
}
/**
*
* @param userCount 玩家数量
*/
public void initUser(int userCount) {
Scanner scannerIn = new Scanner(System.in);
int i = 1;
do {
System.out.println("请输入第" + i + "位玩家的ID和姓名:");
while (true) {
try {
System.out.println("请输入ID:");
Integer id = scannerIn.nextInt();
User user = new User(id);
if (mUserList.contains(user)) {
throw new Exception();
}
user = null;
System.out.println("请输入姓名:");
String name = scannerIn.next();
mUserList.add(new User(id, name));
break;
} catch (InputMismatchException e) {
scannerIn = new Scanner(System.in);
System.out.println("请输入整数类型的ID!");
} catch (Exception e) {
System.out.println("该ID已存在,请重新输入!");
}
}
i++;
} while (i <= userCount&&userCount<=4);
scannerIn.close();
scannerIn = null;
for (User user : mUserList) {
if (user != null) {
System.out.println("---欢迎玩家:" + user.getName());
}
}
}
/**
*
* @param cardCount 每人发几张牌
*/
public void dealCards(int cardCount) {
System.out.println("-----------开始发牌!--------------");
for (int i = 0; i < cardCount; i++) {
int size = mUserList.size();
for (User user : mUserList) {
if (user != null) {
Cards cards = mCaraList.get(0);
user.addCardsToList(cards);
mCaraList.remove(0);
System.out.println("---玩家" + user.getName() + "---拿牌---"+cards);
}
}
}
System.out.println("-----------发牌结束!--------------");
}
public void playCards() {
System.out.println("-----------游戏开始!--------------");
int size = mUserList.size();
Cards maxCards = null;
User winUser = null;
for(User user:mUserList) {
List<Cards> list = user.getList();
Collections.sort(list);
Cards cards = list.get(0);
if (maxCards == null) {
maxCards = cards;
winUser = user;
} else {
int win = maxCards.compareTo(cards);
if (win > 0) {
maxCards = cards;
winUser = user;
}
}
System.out.println("玩家:" + winUser.getName() + " 最大的手牌为" + maxCards);
}
System.out.println("-----------玩家:" + winUser + "获胜!--------------");
}
}
public class Cards implements Comparable<Cards> {
private String colour;
private String point;
public Cards(String colour, String point) {
super();
this.colour = colour;
this.point = point;
}
@Override
public int compareTo(Cards card) {//用于将 Cards对象与方法的参数进行比较
int pointPosition1 = card.getPointPosition();
int pointPosition2 = this.getPointPosition();
if(pointPosition1==pointPosition2) {
int colourPosition1 = card.getColourPosition();
int colourPosition2 = this.getColourPosition();
return colourPosition1-colourPosition2;
}
return pointPosition1-pointPosition2;
}
private int getPointPosition() {
String point = getPoint();
int length = Constant.ARRAY_POINT.length;
for (int i = 0; i < length; i++) {
if (point.equals( Constant.ARRAY_POINT[i])) {
return i;
}
}
return length - 1;
}
private int getColourPosition() {
String colour = getColour();
int length = Constant.ARRAY_COLOUR.length;
for (int i = 0; i < length; i++) {
if (colour.equals( Constant.ARRAY_COLOUR[i])) {
return i;
}
}
return length - 1;
}
public String getColour() {
return colour == null ? Constant.ARRAY_COLOUR[3] : colour;
}
public String getPoint() {
return point == null ? Constant.ARRAY_POINT[0] : point;
}
public void setColour(String colour) {
this.colour = colour;
}
public void setPoint(String point) {
this.point = point;
}
@Override
public String toString() {
return colour+point;
}
}
package com.itedu.www.entity;
import java.util.ArrayList;
import java.util.List;
public class User {
private int id;
private String name;
private List<Cards> list;
public User(int id, String name) {
super();
this.id = id;
this.name = name;
this.list = new ArrayList<>();
}
public User(int id) {
super();
this.id = id;
}
@Override
public String toString() {
return "昵称: " + name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public List<Cards> getList() {
return list;
}
public void addCardsToList(Cards c) {
list.add(c);
}
public void clearList() {
list.clear();;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id != other.id)
return false;
return true;
}
}
public class Constant {
public static final String[] ARRAY_COLOUR = {"方片", "梅花","红桃","黑桃"};//从小到大排序
public static final String[] ARRAY_POINT = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
}
for (String colour : Constant.ARRAY_COLOUR) {
for (String point : Constant.ARRAY_POINT) {
constant 这儿地方显示错误怎么搞
谢谢大佬
老哥强
6666
Java入门第三季
409792 学习 · 4340 问题
相似问题