Daylight_黎明6
2019-02-12 09:26
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
/**
* 综合练习————洗牌发牌Easy版
* 功能描述:
* 1、创建一副扑克牌
* 包括四种花色:黑桃、红桃、梅花、方片
* 十三种点数:2-10,J,Q,K,A,不考虑大小王
* 2、创建两名玩家
* 玩家至少有ID、姓名、手牌等属性,手牌为扑克牌的集合
* 3、洗牌
* 将之前创建的“一副扑克牌”打乱顺序
* 4、发牌
* 将洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,
* 按照一人一张的方式,每人发两张
* 5、游戏
* 比较两名玩家手中的扑克牌,规则为:取两人各自手中点数最大的牌进行比较,
* 点数大的赢;若两人各自的点数最大的牌相等,则再按花色比较
*/
public class CardGames {
public List<CardSystem> cardToSelect;
public List<CardSystem> userList1;
public List<CardSystem> userList2;
public CardGames(){
cardToSelect=new ArrayList<CardSystem>();
userList1=new ArrayList<CardSystem>();
userList2=new ArrayList<CardSystem>();
}
public void Cards(){
System.out.println("---------创建扑克牌---------");
String[] pkpsign=new String[]{"黑桃","红桃","梅花","方片"};
String[] pkpnum=new String[]{"2","3","4","5","6","7","8","9","10","J",
"Q","K","A"};
System.out.println("---------扑克牌创建成功---------");
System.out.print("为:[");
for(int i=pkpsign.length-1;i>0;i--) {
for(int j=0;j<pkpnum.length;j++){
if(j==pkpnum.length-1&&i==pkpsign.length-1) {
System.out.print(pkpsign[i]+pkpnum[j]);
}else {
System.out.print(pkpsign[i]+pkpnum[j]+",");
}
CardSystem cardSystem=new CardSystem(pkpsign[i],pkpnum[j]);
cardToSelect.add(cardSystem);
}
}
System.out.println("]");
System.out.println("---------开始洗牌---------");
Collections.shuffle(cardToSelect);
//这个foreach循环输出的就是打乱后的扑克牌
/*
for (CardSystem cSystem:cardToSelect) {
System.out.print(cSystem.getSign()+cSystem.getNum()+" ");
}
*/
System.out.println("---------洗牌结束---------");
System.out.println("---------创建玩家---------");
Scanner s=new Scanner(System.in);
int user1ID=inputExpetionID1();
System.out.println("输入姓名");
String user1name=s.next();
int user2ID=inputExpetionID2();
System.out.println("输入姓名");
String user2name=s.next();
System.out.println("---------开始发牌---------");
for(int i=0;i<4;i++) {
if(i%2==0){
System.out.println("----玩家"+user1name+"--拿牌");
userList1.add(cardToSelect.get(i));
//System.out.println(cardToSelect.get(i).getSign()+cardToSelect.get(i).getNum());
}
if(i%2!=0){
System.out.println("----玩家"+user2name+"--拿牌");
userList2.add(cardToSelect.get(i));
//System.out.println(cardToSelect.get(i).getSign()+cardToSelect.get(i).getNum());
}
}
System.out.println("---------发牌结束---------");
System.out.println("---------开始游戏---------");
//玩家1
int[] user1cardsign=new int[2];
for(int i=0;i<2;i++){
for(int j=0;j<pkpsign.length;j++){
if(userList1.get(i).getSign()==pkpsign[j]){
user1cardsign[i]=j;
//System.out.println(j);
}
}
}
int[] user1cardnum=new int[2];
for(int i=0;i<2;i++){
for(int j=0;j<pkpnum.length;j++){
if(userList1.get(i).getNum()==pkpnum[j]){
user1cardnum[i]=j;
//System.out.println(j);
}
}
}
int num1sign=0;
int num1num=0;
if(user1cardsign[0]>user1cardsign[1]){
num1sign=num1num=0;
}
if(user1cardsign[0]<user1cardsign[1]){
num1sign=num1num=1;
}
if(user1cardsign[0]==user1cardsign[1]){
if(user1cardnum[0]>user1cardnum[1]){
num1sign=0;
num1num=0;
}
if(user1cardnum[0]<user1cardnum[1]){
num1sign=0;
num1num=1;
}
if(user1cardsign[0]==user1cardsign[1]){
num1sign=num1num=0;
}
}
System.out.println("玩家:"+user1name+"最大的手牌为"
+userList1.get(num1sign).getSign()+userList1.get(num1num).getNum());
//玩家2
int[] user2cardsign=new int[2];
for(int i=0;i<2;i++){
for(int j=0;j<pkpsign.length;j++){
if(userList2.get(i).getSign()==pkpsign[j]){
user2cardsign[i]=j;
//System.out.println(j);
}
}
}
int[] user2cardnum=new int[2];
for(int i=0;i<2;i++){
for(int j=0;j<pkpnum.length;j++){
if(userList2.get(i).getNum()==pkpnum[j]){
user2cardnum[i]=j;
//System.out.println(j);
}
}
}
int num2sign=0;
int num2num=0;
if(user2cardsign[0]>user2cardsign[1]){
num2sign=num2num=0;
}
if(user2cardsign[0]<user2cardsign[1]){
num2sign=num1num=1;
}
if(user2cardsign[0]==user2cardsign[1]){
if(user2cardnum[0]>user2cardnum[1]){
num2sign=0;
num2num=0;
}
if(user2cardnum[0]<user2cardnum[1]){
num2sign=0;
num2num=1;
}
if(user2cardsign[0]==user2cardsign[1]){
num2sign=num2num=0;
}
}
System.out.println("玩家:"+user2name+"最大的手牌为"
+userList2.get(num2sign).getSign()+userList2.get(num2num).getNum());
//赢家
if(user1cardsign[num1sign]>user2cardsign[num2sign]) {
System.out.println("----------玩家"+user1name+"-------------");
}
if(user1cardsign[num1sign]<user2cardsign[num2sign]) {
System.out.println("----------玩家"+user2name+"-------------");
}
if(user1cardsign[num1sign]==user2cardsign[num2sign]) {
if(user1cardnum[num1num]>user2cardnum[num2num]) {
System.out.println("----------玩家"+user1name+"-------------");
}
if(user1cardnum[num1num]<user2cardnum[num2num]) {
System.out.println("----------玩家"+user2name+"-------------");
}
if(user1cardnum[num1num]==user2cardnum[num2num]) {
System.out.println("----玩家"+user1name+"和玩家"+user2name+"打成了平局----");
}
}
System.out.println("玩家各自的手牌为:");
System.out.print(user1name+":[");
for(int i=0;i<userList1.size();i++){
if(i==userList1.size()-1){
System.out.print(userList1.get(i).getSign()
+userList1.get(i).getNum());
}else {
System.out.print(userList1.get(i).getSign()
+userList1.get(i).getNum()+",");
}
}
System.out.println("]");
System.out.print(user2name+":[");
for(int i=0;i<userList2.size();i++){
if(i==userList2.size()-1){
System.out.print(userList2.get(i).getSign()
+userList2.get(i).getNum());
}else {
System.out.print(userList2.get(i).getSign()
+userList2.get(i).getNum()+",");
}
}
System.out.println("]");
}
/**
*
* 输入异常
*/
public int inputExpetionID1(){
Scanner s=new Scanner(System.in);
try {
System.out.println("请输入第一位玩家的ID");
int user1ID=s.nextInt();
return user1ID;
} catch (Exception e) {
// TODO: handle exception
System.out.println("请输入整数类型的ID");
return inputExpetionID1();
}
}
public int inputExpetionID2(){
Scanner s=new Scanner(System.in);
try {
System.out.println("请输入第二位玩家的ID");
int user2ID=s.nextInt();
return user2ID;
} catch (Exception e) {
// TODO: handle exception
System.out.println("请输入整数类型的ID");
return inputExpetionID2();
}
}
public static void main(String[] args){
CardGames cg=new CardGames();
cg.Cards();
}
}
//创建CardSystem类
public class CardSystem {
private String sign;
public String getSign(){
return sign;
}
private String num;
public String getNum(){
return num;
}
public CardSystem(String sign,String num){
this.sign=sign;
this.num=num;
}
public CardSystem(){
}
}
不是应该先比大小再比花色吗?
Java入门第三季
409792 学习 · 4340 问题
相似问题