簡易撲克牌遊戲JAVA原始碼_新手上路

acDream_發表於2018-06-23







下面來看看具體程式碼的實現:

一、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();
	}

}

效果圖:



新手上路,還請大牛提意見~~~~



相關文章