记录一下,自学两个星期JAVA写了个简单的程序。
但是对List和Set掌握还是不是很好,希望大神能指导一下程序如何简化。
//Game 主体
package com.Garry;
import java.util.Scanner;
public class Game {
//创建玩家;
public void CreatePlayer(){
Scanner sr=new Scanner(System.in);
Player player1 = new Player(null);
player1.player_name=sr.next();
//将player1添加到Player中;
Player p = new Player(player1.player_name);
System.out.println("~~~~~~欢迎 "+player1.player_name+"~~~~~~~~~");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎来到Garry的扑克牌小游戏!");
System.out.println("游戏规则如下: ");
System.out.println("扑克牌四种花色,去除大小王,双方玩家轮流随机获得牌,最终通过比较玩家手中最大牌判断胜负。");
System.out.println("注:若双方玩家点数一致,则按照花色:黑桃>梅花>红心>方块判断胜负。");
System.out.println("====================开始游戏======================");
System.out.println("请创建玩家1,输入玩家姓名:");
Game player_1=new Game();
player_1.CreatePlayer();
System.out.println("请创建玩家2,输入玩家姓名:");
Game player_2=new Game();
player_2.CreatePlayer();
System.out.println("正在准备扑克牌~~~洗牌中------------------");
System.out.println("洗牌完毕,现在开始发牌");
PokerForPlayer p=new PokerForPlayer();
String p1_card1=p.addPoker(); // Player1_card1;
String p2_card1=p.addPoker();
String p1_card2=p.addPoker(); // Player1_card2;
String p2_card2=p.addPoker();
//发牌过程中牌不能重复;
System.out.println("----------------发牌结束。开始游戏-----------");
System.out.println("玩家1的卡片有:"+p1_card1+","+p1_card2);
System.out.println("玩家2的卡片有:"+p2_card1+","+p2_card2);
String max_1 =p.Max_card(p1_card1, p1_card2);
System.out.println("玩家1最大的牌是:"+max_1);
String max_2 =p.Max_card(p2_card1, p2_card2);
System.out.println("玩家2最大的牌是:"+max_2);
System.out.println("****************双方开始比较*****************");
p.Winner(max_1, max_2);
System.out.println("游戏结束啦!");
}
}
//保存玩家类
package com.Garry;
import java.util.HashSet;
import java.util.Set;
public class Player {
public String player_name; //玩家昵称
public Set<Poker> pokers; //玩家拥有的卡片; 卡片不重复,用HashSet即可。
public Player(String name){
this.player_name=name;
this.pokers=new HashSet<Poker>();
}
}
package com.Garry;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
//发牌给玩家
public class PokerForPlayer {
public List<Poker> PokerToPlayer;
public PokerForPlayer(){
//初始化PokerToPlayer
this.PokerToPlayer=new ArrayList<Poker>();
}
//随机添加一张牌给玩家;
public String addPoker(){
Random random = new Random();
Poker p_card = new Poker(null);
String card= p_card.Pokersuit[random.nextInt(53)];
Poker pcard = new Poker(card);
PokerToPlayer.add(pcard);
//牌不能重复,取出以后要避免; HashSet();
return card;
}
//输出玩家的卡片
public void DesplayCards(){
PokerForPlayer p =new PokerForPlayer();
System.out.println(p.addPoker());
}
//比较玩家手中的最大牌;
public String Max_card(String card_1, String card_2){
/**
* 1.优先比较数字,大的胜利,
* 2.数字一样比较花色,♠>♣>♥>♦
*/
char [] poker1 = card_1.toCharArray();
char [] poker2 = card_2.toCharArray();
if(poker1[2] == poker2[2])
{
//数字一样比较花色;
if(poker1[0]=='黑')
return card_1;
else if(poker1[0]=='梅' && poker2[0]!='黑' && poker2[0]!='梅')
return card_1;
else if(poker1[0]=='红'&& poker2[0]=='方')
return card_1;
else if(poker1[0]=='方' && poker2[0]!='方')
return card_2;
else
return card_2;
}
if(poker1[2] > poker2[2])
return card_1;
if(poker1[2] < poker2[2])
return card_2;
else
return null;
}
//Win
public void Winner(String maxcard1,String maxcard2){
String winner = null;
PokerForPlayer pfp = new PokerForPlayer();
winner = pfp.Max_card(maxcard1, maxcard2);
if( winner.equals(maxcard1))
System.out.println("玩家1获胜");
else
System.out.println("玩家2获胜");
}
}