簡(jiǎn)易撲克牌游戲
功能描述
1、創(chuàng)建一副撲克牌
包括四種花色
十三個(gè)點(diǎn)數(shù),不考慮大小王
2、創(chuàng)建兩名玩家
玩家至少要有ID、姓名、手牌等屬性,手牌為撲克牌的集合
3、洗牌
將之前創(chuàng)建的一副撲克牌打亂順序
4、發(fā)牌
將洗牌之后的撲克牌集合,從第一張開始,發(fā)給兩名玩家,按照一人一張的方式,沒(méi)人發(fā)兩張
5、游戲
比較兩名玩家手中的撲克牌,規(guī)則為:取兩人各自手中點(diǎn)數(shù)最大的牌進(jìn)行比較,點(diǎn)數(shù)大的贏,若兩人各自的點(diǎn)數(shù)最大的牌相等,則按花色比較
package poker;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Player {
public int id;
public String name;
public List<Poker> myPo = new ArrayList<Poker>();
public Player(int id,String name)
{
this.id = id;
this.name = name;
}
}
package poker;
public class Poker implements Comparable<Poker> {
public String flower;
public String num;
public Integer k;//定義點(diǎn)數(shù)大小
public Integer f;//定義花色大小
public Poker(String flower,String num){
this.flower = flower;
this.num = num;
if(num.equals("J"))
{
this.k = 11;
}
else if(num.equals("Q"))
{
this.k = 12;
}
else if(num.equals("K"))
{
this.k = 13;
}
else if(num.equals("A"))
{
this.k = 14;
}
else
{
this.k = Integer.parseInt(num);
}
if(flower.equals("方塊"))
{
this.f = 1;
}
else if(flower.equals("梅花"))
{
this.f = 2;
}
else if(flower.equals("紅心"))
{
this.f = 3;
}
else
{
this.f = 4;
}
}
public int compareTo(Poker arg0){
if(!(this.k==arg0.k))
{
return this.k.compareTo(arg0.k);
}
else
{
return this.f.compareTo(arg0.f);
}
}
public String toString(){
return (this.flower+this.num);
}
}
package poker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class PlayGame {
List<Poker> allPokers = new ArrayList<Poker>();
List<Player> players = new ArrayList<Player>();
public void createPokers(){
System.out.println("----------創(chuàng)建撲克牌----------");
String[] flowers = {"方塊","梅花","紅心","黑桃"};
String[] nums = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i=0;i<4;i++)
{
for(int j=0;j<13;j++)
{
allPokers.add(new Poker(flowers[i],nums[j]));
}
}
System.out.println("----------撲克牌創(chuàng)建成功!----------");
System.out.print("為:"+allPokers.toString());
}
public void shuf(){
System.out.println("----------開始洗牌...----------");
Collections.shuffle(allPokers);
System.out.println("----------洗牌結(jié)束!----------");
}
public void createPlayers(int num){
System.out.println("----------創(chuàng)建玩家...----------");
Scanner console = new Scanner(System.in);
int id = 0;
boolean isError = true;
for(int i=0;i<num;i++)
{
System.out.println("請(qǐng)輸入第"+(i+1)+"位玩家id和姓名:");
while(isError)
{
System.out.println("輸入id:");
try{
console = new Scanner(System.in); //所輸入的非整型一直在掃描器,因此會(huì)陷入死循環(huán)
id = console.nextInt();
break;
}catch(Exception e){
System.out.println("請(qǐng)輸入整數(shù)類型的id!");
// String s = console.next();
}
}
System.out.println("輸入姓名:");
String name = console.next();
players.add(new Player(id,name));
}
for(Player p:players)
{
System.out.println("歡迎玩家:"+p.name);
}
}
public void deal(int numOfPokes){
System.out.println("----------開始發(fā)牌--------------");
int j = 0;
for(int i =0;i<numOfPokes;i++)
{
for(Player p:players)
{
System.out.println("玩家:"+p.name+"-拿牌");
p.myPo.add(allPokers.get(j++));
}
}
System.out.println("----------發(fā)牌結(jié)束!----------");
}
public void startGame(){
System.out.println("----------開始游戲----------");
for(Player p:players)
{
// Collections.sort(p.myPo);
System.out.println("玩家:"+p.name+
"最大的手牌為:"+Collections.max(p.myPo).toString());
}
}
public void winOrLose(){
Player winner = players.get(0);
for(int i=1; i<players.size()-1;i++)
{
Poker p = Collections.max(players.get(i).myPo);
if(p.compareTo(Collections.max(winner.myPo))>1)
{
winner = players.get(i);
}
}
System.out.println("----------玩家:"+winner.name+"獲勝!----------");
}
public void pokersOfPlayer(){
System.out.println("玩家各自的手牌為:");
for(Player p:players)
{
System.out.println(p.name+":"+p.myPo.toString());
}
}
public static void main(String[] args) {
PlayGame pg = new PlayGame();
pg.createPokers();
pg.shuf();
pg.createPlayers(3);
pg.deal(3);
pg.startGame();
pg.winOrLose();
pg.pokersOfPlayer();
}
}