Poker类:
定义扑克牌的花色,大小属性;
定义如何判断两张扑克牌的大小的方法(先判断扑克牌的ID,然后判断花色,具体实现过程是在colourSize()和idSize()两个方法中);
package com.imooc.pokergame;
public class Poker {
public String colour;
public String id;
public Poker(String colour,String id){
this.colour=colour;
this.id=id;
}
/**
* 通过for循环遍历当前扑克牌的ID值,记录他们在colours中的索引位置
* 通过比较索引位置,得出牌的ID大小,如果当前对象的ID ①大,返回1; ②相同,返回0; ③ 小,返回-1
* @return
*/
public int idSize(Poker poker){
int thisIdLocation = 0;
int parameterIdLocation = 0;
String [] id = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i = 0;i < id.length;i++){
if(this.id.equals(id[i])&&poker.id.equals(id[i])){
thisIdLocation=parameterIdLocation=i;
}else{
if(this.id.equals(id[i])){
thisIdLocation = i;
} else {
if(poker.id.equals(id[i])){
parameterIdLocation = i;
} else {
continue;
}
}
}
}
if(thisIdLocation > parameterIdLocation){
return 1;
} else {
if(thisIdLocation == parameterIdLocation){
return 0;
} else {
return -1;
}
}
}
/**
* 用语idSize()返回0之后,再执行此方法
* 在for循环中遍历当前扑克牌和参数扑克牌的花色,记录它们在colours中的索引位置
* 如果当前扑克牌>参数扑克牌,返回1;否则返回-1;
* @return
*/
public int colourSize(Poker poker){
int thisColourLoction = 0;
int parameterColourLocation = 0;
String [] colours ={"黑桃","红桃","梅花","方片"};
for(int i = 0;i < colours.length;i++){
if(this.colour.equals(colours[i])){
thisColourLoction = i;
} else {
if(poker.colour.equals(colours[i])){
parameterColourLocation = i;
} else {
continue;
}
}
}
if(thisColourLoction < parameterColourLocation)
return 1;
else
return -1;
}
}
Player类:
定义玩家的ID,姓名,和他们手中的扑克牌序列(List类型)
package com.imooc.pokergame;
import java.util.ArrayList;
import java.util.List;
public class Player {
public int id;
public String name;
public List<Poker> pokers;
public Player(int id,String name){
this.id=id;
this.name=name;
this.pokers = new ArrayList<Poker>();
}
}
测试类:
定义方法(创建扑克牌、洗牌、创建玩家、发牌、开始游戏){PS:老实的说,其实最艰难的还是(开始游戏)这个方法比较难写,原来最开始在测试类中把玩家player的类型写成了Set类型,在写(开始游戏)这个方法的时候,因为Set的实现类没有get方法,不知道如何把玩家的name属性读出来,所以换成了List类型,在此,请教各位大神如何编写源码?}
package com.imooc.pokergame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class BeginGame {
public List<Poker> pokerList;
public List<Player> playerList;
public Scanner console;
public Random random;
private List<Poker> maxComparePoker;
public BeginGame(){
pokerList = new ArrayList<Poker>();
playerList = new ArrayList<Player>();
console = new Scanner(System.in);
random = new Random();
maxComparePoker = new ArrayList<Poker>();
}
/**
* 创建扑克牌
*/
public void creatPoker(){
System.out.println("\t**********创建扑克牌***********");
String [] colours = {"黑桃","红桃","梅花","方片"};
String [] id = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i = 0;i < colours.length;i++){
for(int j =0;j < id.length;j++){
Poker pokers = new Poker(colours[i],id[j]);
pokerList.add(pokers);
}
}
System.out.println("\t*********成功创建扑克牌**********");
for (Poker pokers : pokerList) {
System.out.print("\t" + pokers.colour+pokers.id);
}
}
/**
* 洗牌
* @param args
*/
public void shuffle(){
System.out.println("\n\t*********开始洗牌**********");
Collections.shuffle(pokerList);//Collections的乱序方法,API中可查
System.out.println("\t*********成功洗牌**********");
for (Poker newPokers : pokerList) {
System.out.print("\t" + newPokers.colour+newPokers.id);
}
}
/**
* 创建玩家
* 关键点:异常处理
* @param args
*/
public void addPlayer(){
System.out.println("*********创建玩家**********");
for(int i =0;i < 2; i++){
int id = 0;
do{
System.out.println("\n\t请输入第" + (i+1) + "位玩家的ID: ");
try {
id = console.nextInt();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("\t请输入整形的ID!");
id = -1;
console.next();
}
}while(id == -1);
System.out.println("\t请输入第" + (i+1) + "位玩家的姓名: ");
String name = console.next();
playerList.add(new Player(id,name));
}
for (Player player : playerList) {
System.out.println("\t欢迎玩家:" + player.name);
}
}
/**
* 发牌
*/
public void distributePoker(){
System.out.println("\t*********开始发牌**********");
for(int i = 0;i < 2;i++){
for (Player player : playerList) {
int pokerListSize = pokerList.size();
int randomLocaiton = random.nextInt(pokerListSize);
Poker poker = pokerList.get(randomLocaiton);
System.out.println("\t" + player.name + "拿牌.");
player.pokers.add(poker);
pokerList.remove(poker);//选过牌之后把牌从中删去,防止出现拿相同的牌
}
}
System.out.println("\t*********发牌结束**********");
}
/**
* 开始游戏
* @param args
*/
public void beginGame(){
System.out.println("\t*********开始游戏**********");
for (Player player : playerList) {
Poker maxPoker = null;
Poker poker1 = player.pokers.get(0);
Poker poker2 = player.pokers.get(1);
switch(poker1.idSize(poker2)){
case 1:
maxPoker = poker1;
break;
case -1:
maxPoker = poker2;
break;
case 0:{
switch(poker1.colourSize(poker2)){
case 1:
maxPoker = poker1;
break;
case -1:
maxPoker = poker2;
break;
}
}
}
System.out.println("\t玩家: " + player.name + "最大的手牌为: " +maxPoker.colour+maxPoker.id);
maxComparePoker.add(maxPoker);//将最大的两张牌装进一个List序列中,比较那个玩家赢
}
/**
* 上述程序已经把各个玩家的最大的一张牌洗出来了,并且把两张最大的牌装进了一个叫做maxComparePoker的List集合中
* 接下来关键的是如何进行比较,并且输出谁赢
*
*/
if(maxComparePoker.get(0).idSize(maxComparePoker.get(1))==1){
System.out.println("\t玩家: " + playerList.get(0).name +"获胜.");
} else {
if(maxComparePoker.get(0).idSize(maxComparePoker.get(1))==-1){
System.out.println("\t玩家: " + playerList.get(1).name +"获胜.");
}else{
switch(maxComparePoker.get(0).colourSize(maxComparePoker.get(1))){
case 1:
System.out.println("\t玩家: " + playerList.get(0).name +"获胜.");
case -1:
System.out.println("\t玩家: " + playerList.get(1).name +"获胜.");
}
}
}
System.out.println("\t玩家各自的手牌为: ");
for (Player player1 : playerList) {
System.out.print("\t" + player1.name + ": [");
for (Poker pokers1 : player1.pokers) {
System.out.print(pokers1.colour + pokers1.id +" ");
}
System.out.print("]\n");
}
}
public static void main(String[] args) {
BeginGame bg = new BeginGame();
bg.creatPoker();
bg.shuffle();
bg.addPlayer();
bg.distributePoker();
bg.beginGame();
}
}
总之,以上就是我的想法,希望能得到大神的指点(原谅我是小白),其中,在链接:https://pan.baidu.com/s/1boXzBdH 密码:06e4中有完整的项目文件,希望和大家多多学习。
热门评论
为什么方法的参数是Poker poker