欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简易扑克牌游戏JAVA源码_新手上路

程序员文章站 2024-02-26 16:05:58
...

简易扑克牌游戏JAVA源码_新手上路


简易扑克牌游戏JAVA源码_新手上路

简易扑克牌游戏JAVA源码_新手上路

简易扑克牌游戏JAVA源码_新手上路

简易扑克牌游戏JAVA源码_新手上路


下面来看看具体代码的实现:

一、entity包

1.裁判类

package entity;
/**
 * 	裁判
 * 	@author Geek
 *	@version 1.0
 * 	@date 2018年6月22日 上午9:38:47
 *	@TextDemo 简易poker游戏
 *	@copyright 
 *	@remark
 */

import java.util.List;

public class Judgement {
	/**
	 * 洗牌
	 * @param list
	 * @return
	 */
	public List<Poker> washPoke(List<Poker> list){
		System.out.println("--------开始洗牌...-------");
		Poker poker = null;
		for (int i = 0; i < list.size(); i++) {
			int randrom = (int)(Math.random()*52);
			poker = list.get(randrom);
			list.set(randrom, list.get(i));
			list.set(i, poker);
		}
		System.out.println("--------洗牌结束!--------");
		return list;
	}
	/**
	 * 判断谁赢得了游戏
	 * @return
	 */
	public void whoWin(Player player,Player player2) {
		if(player.putPoker().compareDianShu(player2.putPoker())==0){
			if(player.putPoker().compare(player2.putPoker())==0) {
				System.out.println("--------平局---------");
				return;
			}else if(player.putPoker().compare(player2.putPoker())==1){
				System.out.println("--------玩家:"+player.getName()+"获胜!---------");
				return;
			}
			else {
				System.out.println("--------玩家:"+player2.getName()+"获胜!---------");
				return;
			}
		}else if(player.putPoker().compareDianShu(player2.putPoker())==1) {
			System.out.println("--------玩家:"+player.getName()+"获胜!---------");
			return;
		}	
		System.out.println("--------玩家:"+player2.getName()+"获胜!---------");
	}
	
}

2.玩家类

package entity;
/**
 * 	玩家
 * 	@author Geek
 *	@version 1.0
 * 	@date 2018年6月22日 上午9:39:08
 *	@TextDemo 简易poker游戏
 *	@copyright 
 *	@remark
 */

import java.util.ArrayList;
import java.util.List;

public class Player {
	private int id;
	private String name;
	private List<Poker> pokers = new ArrayList<>();
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<Poker> getPokers() {
		return pokers;
	}
	public void setPokers(List<Poker> pokers) {
		this.pokers = pokers;
	}
	
	public void getPoker(Poker poker) {
		this.pokers.add(poker);
	}
	/**
	 * 出最大的牌
	 * @return
	 */
	public Poker putPoker() {
		Poker poker = this.pokers.get(0);;
		if(this.pokers.get(0).compareDianShu(this.pokers.get(1))==1) {
			poker = this.pokers.get(0);
		}
		else if(this.pokers.get(0).compareDianShu(this.pokers.get(1))==-1){
			poker = this.pokers.get(1);
		}
		return poker;
	}
	

	
	
	public Player() {
		
	}
	
	public Player(int id,String name) {
		setId(id);
		setName(name);
	}
	
}

3.扑克类

package entity;
/**
 * 	poke
 * 	@author Geek
 *	@version 1.0
 * 	@date 2018年6月22日 上午9:38:58
 *	@TextDemo 简易poker游戏
 *	@copyright 
 *	@remark
 */

import java.util.ArrayList;
import java.util.List;

public class Poker {
	private String hs;
	private String dianshu;
	private String[] hss = new String[] {"方片","梅花","红心","黑桃"};
	private String[] dianshus =  new String [] {
			"3","4",
			"5","6","7","8","9",
			"10","J","Q","K","A","2"};

	public List<Poker> createPoker(){
		List<Poker> list = new ArrayList<>();
		System.out.println("---------创建扑克牌---------");
		for(int i=0;i<hss.length;i++) {
			for (int j = 0; j < dianshus.length; j++) {
				list.add(new Poker(hss[i],dianshus[j]));
			}
		}
		System.out.println("---------扑克牌创建成功---------");	
		return list;
	}

	public String getHs() {
		return hs;
	}

	public void setHs(String hs) {
		this.hs = hs;
	}

	public String getDianshu() {
		return dianshu;
	}

	public void setDianshu(String dianshu) {
		this.dianshu = dianshu;
	}
	
	public Poker() {
		
	}
	
	public Poker(String hs,String dianshu) {
		setDianshu(dianshu);
		setHs(hs);
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return getHs()+getDianshu();
	}
	/**
	 * 比较花色
	 * @param poker
	 * @return
	 */
	public int compare(Poker poker){


		if(this.getHs().equals(poker.getHs())) {
			return 0;
		}
		for(int i=0;i<this.hss.length;i++) {
			if(this.getHs().equals(hss[i])) {
				return -1;
			}
			if(poker.getHs().equals(hss[i])) {
				return 1;
			}
		}
		
		
		return 0;
	}
	
	/**
	 * 比较点数
	 * @param poker
	 * @return
	 */
	public int compareDianShu(Poker poker){
		boolean boolean1 = true;
		if(this.getDianshu().equals(poker.getDianshu())) {
			return 0;
		}
		for(int i=0;i<this.dianshus.length;i++) {
			if(this.getDianshu().equals(dianshus[i])) {
				boolean1 = false;
				break;
			}
			if(poker.getDianshu().equals(dianshus[i])) {
				boolean1 = true;
				break;
			}
		}
		
		if(boolean1) {
			return 1;
		}
		return -1;
		
	}
}

二、mange包

1.mange类

package mange;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import entity.Judgement;
import entity.Player;
import entity.Poker;

public class Mange {
	public void startGame() {
		Scanner in = new Scanner(System.in);
		List<Poker> list =new Poker().createPoker();
		showPork(list.iterator());
		Judgement judgement = new Judgement();
		list = judgement.washPoke(list);
		System.out.println("------创建玩家...--------");
		int id1;
		String name1;
		System.out.println("请输入第1位玩家的ID和姓名:");
		System.out.print("输入ID:");
		id1 = in.nextInt();
		System.out.println("输入姓名: ");
		in.nextLine();
		name1 = in.nextLine();
		int id2;
		String name2;
		System.out.println("请输入第2位玩家的ID和姓名:");
		System.out.print("输入ID:");
		id2 = in.nextInt();
		System.out.println("输入姓名: ");
		in.nextLine();
		name2 = in.nextLine();
		Player player = new Player(id1, name1);
		Player player2 = new Player(id2, name2);
		System.out.println("------欢迎玩家: "+player.getName());
		System.out.println("------欢迎玩家: "+player2.getName());
		System.out.println("--------开始发牌...--------");
		for (int i = 0; i < 4; i+=2) {
			System.out.println("----玩家:"+player.getName()+"-拿牌");
			player.getPoker(list.get(i));
			System.out.println("----玩家:"+player2.getName()+"-拿牌");
			player2.getPoker(list.get(i+1));
		}
		System.out.println("--------发牌结束!------------");
		System.out.println("----------开始游戏...---------");
		System.out.println("玩家:"+player.getName()+"最大的手牌为:"+player.putPoker());
		System.out.println("玩家:"+player2.getName()+"最大的手牌为:"+player2.putPoker());
		judgement.whoWin(player, player2);
		System.out.println("玩家各自的手牌为: ");
		System.out.println(player.getName()+":"+player.getPokers());
		System.out.println(player2.getName()+":"+player2.getPokers());
	}
	
	
	public void showPork(Iterator<Poker> iterator) {
		System.out.print("为:[");
		while(iterator.hasNext()) {
			System.out.print(iterator.next()+",");
		}
		System.out.println("]");
	}
}

三、text包

1.text类

package text;

import mange.Mange;

public class TextDemo {
	public static void main(String[] args) {
		Mange mange = new Mange();
		mange.startGame();
	}

}

效果图:

简易扑克牌游戏JAVA源码_新手上路

简易扑克牌游戏JAVA源码_新手上路

新手上路,还请大牛提意见~~~~