新手一枚,最近刚学了java的集合框架,就拿来用一用(水平有限,欢迎交流)
游戏规则是:取两人各自手中点数最大的牌进行比较,点数大的赢;若两人各自的点数相等,则按花色比较。
感觉主要是以下几个方面花了一些时间,
(打乱扑克顺序的洗牌,通过自定义规则比较大小,一些集合与泛型的使用,通过try..catch捕获异常来限定登录ID为整数。)
首先是Card类:
/**
* 扑克牌类
* @author admin
*
*/
public class Card{
// 花色:suit
//点数:figure
public String suit;
public String figure;
/**
* 形参构造器
* @param suit
* @param figure
*/
public Card(String suit,String figure){
this.suit = suit;
this.figure = figure;
}
}
玩家Player类:
import java.util.ArrayList;
import java.util.List;
/**
* 玩家类
* @author admin
*
*/
public class Player {
public int id;
public String name;
//手牌为扑克牌的集合,泛型设定为Card
public List<Card> cards;
public Player(){
this.cards = new ArrayList<Card>();
}
}
最后是test:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
public Scanner console;
//用于存放所有扑克的集合
public List<Card> AllCards;
Player player1 = new Player();
Player player2 = new Player();
String[] suits = {"方块","梅花","红心","黑桃"};
String[] figures = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
public Test(){
console = new Scanner(System.in);
this.AllCards = new ArrayList<Card>();
}
/**
* 玩家登录
*/
void login(){
System.out.println("这里是登录界面:");
//通过try——catch捕获异常,限制输入ID为整数
while(true){
try{
System.out.println("请输入第一个玩家的ID(限为数字):");
String testid = console.next();
player1.id=Integer.parseInt(testid);
break;
}catch(NumberFormatException e){
System.out.println("请输入正确的ID!");
}
}
System.out.println("请输入第一个玩家的昵称:");
player1.name = console.next();
while(true){
try{
System.out.println("请输入第二个玩家的ID(限为数字):");
String testid = console.next();
player2.id=Integer.parseInt(testid);
break;
}catch(NumberFormatException e){
System.out.println("请输入正确的ID!");
}
}
System.out.println("请输入第二个玩家的昵称:");
player2.name = console.next();
}
/**
* 创建扑克牌
*/
void creatCards(){
System.out.println("----------创建扑克牌-----------");
for(int i=0;i<4;i++){
for(int j=0;j<13;j++){
AllCards.add(new Card(suits[i],figures[j]));
}
}
System.out.println("---------创建成功----------");
}
/**
* 两张扑克牌比较大小的方法(规则是:点数大的为大,点数相同比较花色,大小顺序为:黑桃>红心>梅花>方块),
* 返回1代表ob1>ob2,返回-1代表ob1<ob2。
*/
int compare(Card cd1,Card cd2){
int figure1 = 0;
int figure2 = 0;
//利用数组的下标来比较大小,下标大的就大
for(int i=0;i<13;i++){
//先获取其在数组中的下标
if(cd1.figure == figures[i]){
figure1=i;
}
if(cd2.figure == figures[i]){
figure2=i;
}
}
//比较大小
if(figure1>figure2){
return 1;
}else if(figure1==figure2){
for(int i=0;i<4;i++){
if(cd1.suit == suits[i]){
figure1=i;
}
if(cd2.suit == suits[i]){
figure2=i;
}
}
if(figure1>figure2){
return 1;
}else return -1;
}else{
return -1;
}
}
/**
* 遍历输出所有牌
*/
void forEach(){
System.out.println("有以下扑克牌(通过for each访问):");
int i =1;
for(Object obj:AllCards){
Card cr = (Card) obj;
System.out.print(cr.suit+":"+cr.figure+" ");
//每显示13个牌就换行
if(i == 13 i == 26 i == 39){
System.out.println();
}
i++;
}
System.out.println();
}
/**
* 洗牌
*/
void Shuffle(){
System.out.println("-----------开始洗牌---------------");
for(int i=0;i<52;i++){
int x=(int)(Math.random()*52);
Card temp = AllCards.get(x);
AllCards.set(x,AllCards.get(i));
AllCards.set(i,temp);
}
System.out.println("-----------洗牌结束---------------");
System.out.println("此时的牌顺序为:");
forEach();
}
/**
* 发牌
*/
void deal(){
System.out.println("-----------------开始发牌--------------------");
player1.cards.add(AllCards.get(0));
player2.cards.add(AllCards.get(1));
player1.cards.add(AllCards.get(2));
player2.cards.add(AllCards.get(3));
System.out.println("玩家"+player1.name+"的手牌为:"+player1.cards.get(0).suit+":"+player1.cards.get(0).figure+
" 和:"+player1.cards.get(1).suit+":"+player1.cards.get(1).figure);
System.out.println("玩家"+player2.name+"的手牌为:"+player2.cards.get(0).suit+":"+player2.cards.get(0).figure+
" 和:"+player2.cards.get(1).suit+":"+player2.cards.get(1).figure);
}
/**
* 比较大小,选出获胜玩家
*/
void fight(){
System.out.println("比较大小:");
int e = 0;
//找出玩家1手里最大的牌
int o = compare(player1.cards.get(0),player1.cards.get(1));
//找出玩家2手里最大的牌
int w = compare(player2.cards.get(0),player2.cards.get(1));
if(o == 1&&w == 1){
//玩家1,2用第一张牌比较大小
e = compare(player1.cards.get(0),player2.cards.get(0));
}else if(o == -1&& w == 1){
e = compare(player1.cards.get(1),player2.cards.get(0));
}else if(o == -1&& w == -1){
e = compare(player1.cards.get(1),player2.cards.get(1));
}else{
e = compare(player1.cards.get(0),player2.cards.get(1));
}
if(e>0){
System.out.println("玩家"+player1.name+"获胜!!");
}else{
System.out.println("玩家"+player2.name+"获胜!!");
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t = new Test();
t.login();
t.creatCards();
t.forEach();
t.Shuffle();
t.deal();
t.fight();
}
}
热门评论
零基础的我,刚刚开始学习HTML呢,看的一头雾水,哎呀,我什么时候才能到达这种水平啊。