适用人群:java基础刚入门的人群
项目需求:
1.搞一副新的扑克牌
2.对新的扑克牌进行洗牌过程
3.有三个人创建三个Player对象(Player类当中String nickname;ArrayList<String>pockers);
三个玩家模拟真实场景情况进行抓牌(记住保留三颗底牌)
4.叫牌(在控制台输入想要叫牌玩家昵称) 对应昵称玩家获得剩余三颗底牌
5.要对三个玩家手中最终的扑克牌进行一个码牌(从小到大进行排序3,45678910J,Q..A,2小王大王)
6.最终打印出三个玩家排好序后的扑克牌
代码详解:
PokerGame类:
搞一副新的扑克牌 :
package com.yunma.pokergame;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.w3c.dom.ls.LSOutput;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class PokerGame {
//搞一副新的扑克牌 该方法一定有返回值
public ArrayList<String> getNewPoker(){
String[] huase={"红桃","梅花","方片","黑桃"};
String[] MianZhi={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
ArrayList<String> list = new ArrayList<>();
for(int i=0;i<MianZhi.length;i++){
for(int j=0;j< huase.length;j++){
list.add(huase[j]+MianZhi[i]);
}
}
list.add("小王");
list.add("大王");
return list;
}
洗牌:
//洗牌操作:返回值是洗后的扑克牌;在洗牌的时候 要给我一副新的扑克牌
public ArrayList<String> washPoker(ArrayList<String> newpoker){
ArrayList<String> washedPoker=new ArrayList();//用于装洗完后的扑克牌
Random random=new Random();
while(newpoker.size()>0){
//洗好的牌 添加 remove删除的元素
//remove删除的元素就是random随机产生的(括号内是0~元素数)
washedPoker.add(newpoker.remove(random.nextInt(newpoker.size())));
}
return washedPoker;
}
抓牌+叫牌:
//抓牌操作:先给我三个玩家以及一副洗好的牌
public ArrayList<String> grabPoker(player p1,player p2,player p3,ArrayList<String>washedCard){
while(washedCard.size()>3){//剩余三张底牌
p1.getPokers().add(washedCard.remove(0));
p2.getPokers().add(washedCard.remove(0));
p3.getPokers().add(washedCard.remove(0));
}
//叫牌操作:
Scanner scanner=new Scanner(System.in);//读取输入内容
System.out.println("开始叫牌,请输入叫牌玩家昵称:");
String callplayer=scanner.next();
if(p1.getNickname().equals(callplayer)){ //字符串比较用equals
p1.getPokers().addAll(washedCard);
}
else if(p2.getNickname().equals(callplayer))
{
p2.getPokers().addAll(washedCard);
}
else if(p3.getNickname().equals(callplayer))
{
p3.getPokers().addAll(washedCard);
}
else{
System.out.println("请输入正确的玩家昵称");
}
//完成叫牌后,对每个玩家手里的牌进行排序(码牌)
orderPoker(p1.getPokers());
orderPoker(p2.getPokers());
orderPoker(p3.getPokers());
//打印玩家手中的牌
System.out.println(p1.getNickname());
printPoker(p1.getPokers());
System.out.println();
System.out.println(p2.getNickname());
printPoker(p2.getPokers());
System.out.println();
System.out.println(p3.getNickname());
printPoker(p3.getPokers());
System.out.println();
System.out.println("剩余三张底牌");
printPoker(washedCard);
return washedCard;
}
码牌:
//码牌操作:先排好牌面值;遍历手中所有牌,与取出的每个牌面值看是否相符
public void orderPoker(ArrayList<String> pokers){
String[] orderMianzhi={"3","4","5","6","7","8","9","10","J","Q","K","A","2","小王","大王"};
ArrayList<String> orderedlist=new ArrayList<>();
for(int i=0;i<orderMianzhi.length;i++){ //遍历牌面值
for(String card:pokers) //遍历玩家手中的牌
{
if(card.indexOf(orderMianzhi[i])>-1){
orderedlist.add(card);
}
}
}
pokers.clear();//清空列表
pokers.addAll(orderedlist);//把排好的加进去
}
观察:
//专门打印扑克牌内容,方便观察
public void printPoker(ArrayList<String> list){
for(String poker:list){
System.out.print(poker+" ");
}
}
PokerGame类完整代码:
package com.yunma.pokergame;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.w3c.dom.ls.LSOutput;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class PokerGame {
//搞一副新的扑克牌 该方法一定有返回值
public ArrayList<String> getNewPoker(){
String[] huase={"红桃","梅花","方片","黑桃"};
String[] MianZhi={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
ArrayList<String> list = new ArrayList<>();
for(int i=0;i<MianZhi.length;i++){
for(int j=0;j< huase.length;j++){
list.add(huase[j]+MianZhi[i]);
}
}
list.add("小王");
list.add("大王");
return list;
}
//洗牌操作:返回值是洗后的扑克牌;在洗牌的时候 要给我一副新的扑克牌
public ArrayList<String> washPoker(ArrayList<String> newpoker){
ArrayList<String> washedPoker=new ArrayList();//用于装洗完后的扑克牌
Random random=new Random();
while(newpoker.size()>0){
//洗好的牌 添加 remove删除的元素
//remove删除的元素就是random随机产生的(括号内是0~元素数)
washedPoker.add(newpoker.remove(random.nextInt(newpoker.size())));
}
return washedPoker;
}
//抓牌操作:先给我三个玩家以及一副洗好的牌
public ArrayList<String> grabPoker(player p1,player p2,player p3,ArrayList<String>washedCard){
while(washedCard.size()>3){//剩余三张底牌
p1.getPokers().add(washedCard.remove(0));
p2.getPokers().add(washedCard.remove(0));
p3.getPokers().add(washedCard.remove(0));
}
//叫牌操作:
Scanner scanner=new Scanner(System.in);//读取输入内容
System.out.println("开始叫牌,请输入叫牌玩家昵称:");
String callplayer=scanner.next();
if(p1.getNickname().equals(callplayer)){ //字符串比较用equals
p1.getPokers().addAll(washedCard);
}
else if(p2.getNickname().equals(callplayer))
{
p2.getPokers().addAll(washedCard);
}
else if(p3.getNickname().equals(callplayer))
{
p3.getPokers().addAll(washedCard);
}
else{
System.out.println("请输入正确的玩家昵称");
}
//完成叫牌后,对每个玩家手里的牌进行排序(码牌)
orderPoker(p1.getPokers());
orderPoker(p2.getPokers());
orderPoker(p3.getPokers());
//打印玩家手中的牌
System.out.println(p1.getNickname());
printPoker(p1.getPokers());
System.out.println();
System.out.println(p2.getNickname());
printPoker(p2.getPokers());
System.out.println();
System.out.println(p3.getNickname());
printPoker(p3.getPokers());
System.out.println();
System.out.println("剩余三张底牌");
printPoker(washedCard);
return washedCard;
}
//码牌操作:先排好牌面值;遍历手中所有牌,与取出的每个牌面值看是否相符
public void orderPoker(ArrayList<String> pokers){
String[] orderMianzhi={"3","4","5","6","7","8","9","10","J","Q","K","A","2","小王","大王"};
ArrayList<String> orderedlist=new ArrayList<>();
for(int i=0;i<orderMianzhi.length;i++){ //遍历牌面值
for(String card:pokers) //遍历玩家手中的牌
{
if(card.indexOf(orderMianzhi[i])>-1){
orderedlist.add(card);
}
}
}
pokers.clear();//清空列表
pokers.addAll(orderedlist);//把排好的加进去
}
//专门打印扑克牌内容,方便观察
public void printPoker(ArrayList<String> list){
for(String poker:list){
System.out.print(poker+" ");
}
}
}
player类:
package com.yunma.pokergame;
import java.util.ArrayList;
public class player {
private String nickname;//玩家昵称
private ArrayList<String> pokers;
//封装
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public ArrayList<String> getPokers() {
return pokers;
}
public void setPokers(ArrayList<String> pokers) {
this.pokers = pokers;
}
}
Test类:
package com.yunma.pokergame;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
PokerGame p =new PokerGame();
//调用 洗牌
//p.printPoker(p.washPoker(p.getNewPoker()));
//调用 创建三个玩家
player player1=new player();
player player2=new player();
player player3=new player();
player1.setNickname("小红");
player1.setPokers(new ArrayList<String>());
player2.setNickname("小兰");
player2.setPokers(new ArrayList<String>());
player3.setNickname("小花");
player3.setPokers(new ArrayList<String>());
//调用 抓牌(含叫牌、码牌)
p.grabPoker(player1,player2,player3,p.washPoker(p.getNewPoker()));
}
}