首页 > IT职业 > IT生活 > Java入门第三季,扑克牌游戏
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 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 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 pokers;
public Player(int id,String name){
this.id=id;
this.name=name;
this.pokers = new ArrayList();
}
}
测试类:
定义方法(创建扑克牌、洗牌、创建玩家、发牌、开始游戏){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 pokerList;
public List playerList;
public Scanner console;
public Random random;
private List maxComparePoker;
public BeginGame(){
pokerList = new ArrayList();
playerList = new ArrayList();
console = new Scanner(System.in);
random = new Random();
maxComparePoker = new ArrayList();
}
/**
* 创建扑克牌
*/
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 总之,以上就是我的想法,希望能得到大神的指点(原谅我是小白),其中,在链接:https://pan.baidu.com/s/1boXzBdH 密码:06e4中有完整的项目文件,希望和大家多多学习。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4614/viewspace-2798428/ ,如需转载,请注明出处,否则将追究法律责任。