package com.lt.wqkpuke;
/*
主游戏类,主要负责输出交互信息并调用其它方法
*简易扑克牌游戏
- @wqk
-
*/
public class GameMain {
PlayGame pg=new PlayGame();//将PlayGame类作为GameMain类的一个属性,以便实现方法的调用
public static void main(String[] args) {
System.out.println("--------@扑克游戏开发(by java)--------");
System.out.println("----------欢迎进入游戏-----------");
GameMain gm=new GameMain();
gm.pg.CrePuke();
gm.pg.CrePlayer();
gm.pg.CreXipai();
gm.pg.CreFapai();
gm.pg.Compare();
gm.pg.CreShow();
System.out.println("------------游戏结束------------");
}
}
package com.lt.wqkpuke;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class PlayGame {
String[] Huase={"黑桃","红桃","梅花","方块"};
String[] Pai={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
public Scanner console;
List<Puke> PukeList=new ArrayList<Puke>();
List<Player> PlayerList=new ArrayList<Player>();
public PlayGame(){ //构造器
console=new Scanner(System.in);
}
/*
* 创建扑克
*/
public void CrePuke(){
System.out.println("---------开始创建扑克牌---------");
for(int i=0;i<4;i++){
for(int j=0;j<13;j++){
PukeList.add(new Puke(Huase[i],Pai[j]));
}
}
int n=1;
for (Puke Puke: PukeList) {
System.out.print(Puke.Huase+Puke.Pai+" ");
if(n%13==0){
System.out.println();
}
n++;
}
System.out.println("----------创建扑克完毕----------");
}
/*
* 创建玩家
*/
public void CrePlayer(){
for(int i=1;i<=2;i++){
System.out.println("请输入第"+i+"个玩家的ID(整数):");
int ID=0;
try{
ID = Integer.parseInt(console.next());
}catch(Exception e){
System.out.println(e.getMessage());
i--;
continue;
}
for(int j=i;j<=2;j++){
System.out.println("请输入第"+j+"个玩家的姓名:");
String Name=console.next();
if(Name==null){
System.out.println("你未输入玩家姓名,请重新输入");
j--;
continue;
}else{
System.out.println("欢迎第"+i+"个玩家"+Name+"加入游戏");
PlayerList.add(new Player(ID,Name));
break;
}
}
}
}
/*
* 洗牌
*/
public void CreXipai(){
System.out.println("-----------开始洗牌------------");
Collections.shuffle(PukeList);
System.out.println("-----------洗牌结束------------");
}
/*
* 发牌
*/
public void CreFapai(){
for(int i=0;i<2;i++){
for (Player p : PlayerList) {
Puke pc=PukeList.get(0);
p.handCards.add(pc);
System.out.println("--------玩家:" + p.Name + "-拿牌-------");
PukeList.remove(0);
}
}
}
/*
* 比较大小
*/
public void Compare(){
List<Puke> Puke1=new ArrayList<Puke>();
List<Puke> Puke2=new ArrayList<Puke>();
Puke1.add(0,PlayerList.get(0).handCards.get(0));
Puke1.add(1,PlayerList.get(0).handCards.get(0));
Puke2.add(0,PlayerList.get(1).handCards.get(0));
Puke2.add(1,PlayerList.get(1).handCards.get(0));
//调用方法比较大小,先确定玩家各自手牌中较大的一张
ComparePuke comparepuke=new ComparePuke();
Puke a=comparepuke.compareTest(Puke1.get(0), Puke1.get(1));
Puke b=comparepuke.compareTest(Puke2.get(0), Puke2.get(1));
int c=comparepuke.compare(a, b);
if(c>0){//玩家1获胜
System.out.println("-----本次获胜玩家"+ PlayerList.get(0).Name+ "获胜!------");
}
else{//玩家2获胜
System.out.println("-----本次获胜玩家"+ PlayerList.get(1).Name+ "获胜!--------");
}
}
/*
* 展示玩家手牌
*/
public void CreShow(){
System.out.println("---------开始展示玩家手牌---------");
for (Player player : PlayerList) {
System.out.print("玩家"+player.Name+"手牌为:");
for (Puke puke : player.handCards) {
System.out.print(puke.Huase+" "+puke.Pai+" ");
}
System.out.println();
}
}
}
package com.lt.wqkpuke;
public class Puke {
public String Huase;
public String Pai;
public Puke(String Huase,String Pai){
this.Huase=Huase;
this.Pai=Pai;
}
public Puke(){
}
}
package com.lt.wqkpuke;
import java.util.ArrayList;
import java.util.List;
public class Player {
public int ID;
public String Name;
List<Puke> handCards=new ArrayList<Puke>();
public Player(int ID,String Name){
this.ID=ID;
this.Name=Name;
}
}
package com.lt.wqkpuke;
import java.util.Comparator;
public class ComparePuke implements Comparator<Puke> {
String[] Huase={"黑桃","红桃","梅花","方块"};
String[] Pai={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
@Override
public int compare(Puke pk1, Puke pk2) {
int a=0,b=0,c=0;
for(int i=0;i<Pai.length;i++){
if(pk1.Pai==Pai[i]){
a=i;
}
if(pk2.Pai==Pai[i]){
b=i;
}
}
if(a>b){
c=1;
}else if(a<b){
c=-1;
}else if(a==b){
int k1 = 0, k2 = 0;
for(int j=0; j<Huase.length; j++){
if(pk1.Huase == Huase[j])
k1 = j ;
if(pk2.Huase == Huase[j])
k2 = j ;
}
if(k1 > k2)
c = -1;
else
c = 1;
}
return c;
}
public Puke compareTest(Puke pk3,Puke pk4){//返回大的扑克牌
int a = 0, b = 0;
Puke c = new Puke();
for(int i=0; i<Pai.length; i++) {
if(pk3.Pai == Pai[i])
a = i;
if(pk4.Pai == Pai[i])
b = i;
}
if(a > b) //比较大小
c = pk3;
else if(a < b)
c = pk4;
else if(a == b){ //数字相等时
int k1 = 0, k2 = 0;
for(int j=0; j<Huase.length; j++){
if(pk3.Huase == Huase[j])
k1 = j ;
if(pk4.Huase == Huase[j])
k2 = j ;
}
if(k1 > k2)//比较花色
c = pk4;
else
c = pk3;
}
return c;
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章