在本次练习中对于集合的了解加深。自己对于Java语言面向对象这一特性更深入了解,开始时分好几个类,明确每个类有哪些数据,要做什么。最后一步一步写代码。在编写过程中对于自己思路有模糊的地方,对于不同中数据方法的调用产生迷糊。意识到编写时要加强注释和写一点就运行一下看是否有错误。
package pokergame;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Play {
/**
*
* @param args
*/
public Player player1;
public Player player2;
public static void main(String[] args) {
Pocker p = new Pocker();
//创造牌
p.creatPocker();
//洗牌
p.shufflePoker();
Play play = new Play();
play.playerLoad();
System.out.println("欢迎玩家:"+play.player1.getName());
System.out.println("欢迎玩家:"+play.player2.getName());
//发牌
play.player1.add(p.pockerListRandom.get(0));
play.player2.add(p.pockerListRandom.get(1));
play.player1.add(p.pockerListRandom.get(2));
play.player2.add(p.pockerListRandom.get(3));
System.out.println("开始游戏...");
play.comparePoker();
}
public void playerLoad(){
this.player1=new Player();
this.player2=new Player();
Scanner sc = new Scanner(System.in);
//输入第一位玩家ID和姓名
while (true) {
System.out.println("输入ID:");
try {
String s=sc.next();
player1.setID(s);
break;
} catch (Exception e) {
System.out.println("您输入有误");
}
}
System.out.println("输入姓名:");
String name = sc.next();
player1.setName(name);
//输入第二位玩家ID和姓名
while (true) {
System.out.println("输入ID:");
try {
String s=sc.next();
player2.setID(s);
break;
} catch (Exception e) {
System.out.println("您输入有误");
}
}
System.out.println("输入姓名:");
String name2 = sc.next();
player2.setName(name2);
}
public void comparePoker() {
player1.sort();
player2.sort();
System.out.println("玩家"+this.player1.getName()+"最大牌是:"+
player1.getPokerOfPlayer().get(0).color+
player1.getPokerOfPlayer().get(0).points);
System.out.println("玩家"+this.player2.getName()+"最大牌是:"+
player2.getPokerOfPlayer().get(0).color+
player2.getPokerOfPlayer().get(0).points);
if (this.player1.getPokerOfPlayer().get(0).compareTo(this.player2.getPokerOfPlayer().get(0))>0) {
System.out.println("玩家:"+this.player1.getName()+"获胜");
}else if (this.player1.getPokerOfPlayer().get(0).compareTo(this.player2.getPokerOfPlayer().get(0))==0) {
System.out.println("平局");
}else {
System.out.println("玩家:"+this.player2.getName()+"获胜");
}
System.out.println(this.player1.getName()+"手牌:"+this.player1.getPokerOfPlayer().get(0)+this.player1.getPokerOfPlayer().get(1));
System.out.println(this.player2.getName()+"手牌:"+this.player2.getPokerOfPlayer().get(0)+this.player1.getPokerOfPlayer().get(1));
}
}
package pokergame;
/**
* 单个纸牌包含花色,点数,和每张牌指定的大小属性。 实现Compara接口,重写Comparato方法,来比较每张牌大小的属性。
*
* @author Administrator
*
*/
public class Apoker implements Comparable<Apoker> {
public String color;// 花色属性
public String points;// 点数属性
public int number;// 大小属性
// 无参构造
public Apoker() {
super();
}
// 有参构造
public Apoker(String color, String points, int number) {
super();
this.color = color;
this.points = points;
this.number = number;
}
@Override
public int compareTo(Apoker o) {
if (this.number > o.number) {
return 1;
} else if (this.number == o.number) {
return 0;
} else {
return -1;
}
}
}
package pokergame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 玩家:定义玩家的ID和姓名
* @author Administrator
*
*/
public class Player {
private String ID;//玩家ID
private String name;//玩家name
private List<Apoker> pokerOfPlayer;//玩家手牌
//构造函数
public Player(String iD, String name) {
super();
this.ID = iD;
this.name = name;
this.pokerOfPlayer=new ArrayList<>();
}
public Player() {
super();
}
//Getter和Setter方法
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Apoker> getPokerOfPlayer() {
return pokerOfPlayer;
}
public void setPokerOfPlayer(List<Apoker> pokerOfPlayer) {
this.pokerOfPlayer = pokerOfPlayer;
}
//为玩家添加一张手牌
public void add(Apoker e) {
this.pokerOfPlayer.add(e);
}
//对玩家的手牌进行排序
public void sort() {
Collections.sort(this.pokerOfPlayer);
}
}
package pokergame;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 创建列表存放纸牌
*
* @author Administrator
*
*/
public class Pocker {
// 顺序纸牌序列
List<Apoker> pockerList = new ArrayList<Apoker>();
// 随机纸牌序列
List<Apoker> pockerListRandom = new ArrayList<>();
// 创造纸牌
public void creatPocker() {
System.out.println("----------欢迎来到纸牌游戏----------");
System.out.println("确认纸牌:");
// 花色数组
String[] colorAry = { "黑桃", "红桃", "梅花", "方片" };
// 点数数组
String[] pointsAry = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
for (int i = 0; i < colorAry.length; i++) {
for (int j = 0; j < pointsAry.length; j++) {
int k = 10 * (j + 1) + i;// 每张牌的大小
Apoker apoker = new Apoker(colorAry[i], pointsAry[j], k);// 创造纸牌
this.pockerList.add(apoker);
}
}
System.out.println("纸牌为:" + "[");
// foreach遍历
for (Apoker apoker : pockerList) {
System.out.print(apoker.color + apoker.points + " ");
}
System.out.print("]");
}
// 洗牌
public void shufflePoker() {
System.out.println("开始洗牌...");
Random random = new Random();
// 遍历
for (int i = 0; i < pockerList.size(); i++) {
while (true) {
int index = random.nextInt(pockerList.size());
Apoker newPoker = pockerList.get(index);
if (this.pockerListRandom.contains(newPoker)) {
continue;
} else {
this.pockerListRandom.add(newPoker);
break;
}
}
}
/*
* 用于测试洗牌结果 for (Apoker apoker : pockerListRandom) {
* System.out.print(apoker.color+apoker.points+" "); }
*/
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦