问答详情
源自:7-1 简易扑克牌游戏

来交作业啦

package PlayCards;


public class Colour {

String colour;

int coloreNum;

public Colour(int colourNum) {

this.coloreNum = colourNum;

marryNum(colourNum);//赋值

}

public void marryNum(int colourNum) {//通过颜色匹配大小

switch(colourNum) {

case 1: colour="方块";break;

case 2: colour="梅花";break;

case 3: colour="红桃";break;

case 4: colour="黑桃";break;

}

}

public boolean equals(Colour colour) {//重写equals方法 比较两种牌的花色大小

if(this.coloreNum>colour.coloreNum) {

return true;

}else {

return false;

}

}

}




package PlayCards;


import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;


public class Card {

Colour cardcolour;//花色

CardNum cardnumber;//点数

List<Card> Cards;//牌的集合

public Card() {

Cards = new ArrayList<Card>();//这个构造器是在main方法中用的

}

public  Card(int cardcolour,int cardnumber) {//构造器,是给putCard方法用的

this.cardcolour = new Colour(cardcolour);

this.cardnumber = new CardNum(cardnumber);

Cards = new ArrayList<Card>();

}

public void putCard() {//将牌放在集合中去

for(int i=1;i<14;i++) {

for(int j=1;j<5;j++) {

Card c = new Card(j,i);

Cards.add(c);

}

}

}

public  void showCard(List<Card> Cards) {//展示牌

for(Card c:Cards) {

System.out.print(c.cardcolour.colour+c.cardnumber.numChange+",");

}


}

}



package PlayCards;


public class CardNum {

int  cardnum;

String numChange;

public CardNum(int cardnum) {

this.cardnum = cardnum;

merryNum(cardnum);

}

public void merryNum(int cardnum) {//转换点数

if(cardnum>=2&&cardnum<11) {

this.numChange = cardnum+"";

}else{

String s = cardnum+"";

switch(s) {

case "11": this.numChange = "J";break;

case "12": this.numChange = "Q";break;

case "13": this.numChange = "K";break;

case "1":  this.numChange = "A";break;

}

}

}

public int equals(CardNum cn) {//重写equals方法,判断点数大小

if(cn.cardnum>2&&this.cardnum>2) {

if(this.cardnum>cn.cardnum) {

return 1;

}else if(this.cardnum==cn.cardnum) {

return 0;

}else {

return -1;

}

}else if(this.cardnum>cn.cardnum) {//如果cn 比this 大返回false;

return 1;

}else if(this.cardnum==cn.cardnum){

return 0;

}else {

return -1;

}

}

}




package PlayCards;


import java.util.ArrayList;

import java.util.List;


public class Player {

String ID;

String name;

List<Card> playercard;

public Player(String ID,String name) {//构造器

this.ID=ID;

this.name=name;

playercard = new ArrayList<Card>();

}

public void handCard(Card c) {//手牌

this.playercard.add(c);

}

public void showPlayerCards(List<Card>playercard) {//展示手牌

for(Card c:playercard) {

System.out.println(c.cardcolour.colour+c.cardnumber.numChange);

}

}

}



package PlayCards;


import java.util.Collections;

import java.util.List;


public class Game {

Player p1;

Player p2;

Card[] cardsp1;

Card[] cardsp2;

public Game(Player p1,Player p2) {

this.p1=p1;

this.p2=p2;

}

public void putPlayerCards(List<Card> Cards) {//发牌

int i=0;

while(i<4) {

if(i%2!=0) {

Card c = Cards.get(i);

p1.handCard(c);

i++;

}else {

Card c = Cards.get(i);

p2.handCard(c);

i++;

}

}

}

public Card PlayermaxCard(Player p) {//比较手牌中的大小

Card c0 = p.playercard.get(0);

Card c1 = p.playercard.get(1);

CardNum cardnum1 =c0.cardnumber;

CardNum cardnum2 = c1.cardnumber;

Colour cardcolour1 =c0.cardcolour;

Colour cardcolour2 =c1.cardcolour;

if(cardnum1.equals(cardnum2)==1) {//前面重写了CardNum类的equals方法,

return  c0;//比较点数的大小

}else if(cardnum1.equals(cardnum2)==0){//如果点数相同

if(cardcolour1.equals(cardcolour2)) {//比较颜色

return c0;

}else {

return c1;

}

}else {

return c1;

}

}

public Player winner() {//比较连个玩家最大的手牌

Card cardp1=PlayermaxCard(p1);

Card cardp2=PlayermaxCard(p2);

CardNum cardnum1 =cardp1 .cardnumber;

CardNum cardnum2 =cardp2.cardnumber;

Colour cardcolour1 =cardp1.cardcolour;

Colour cardcolour2 =cardp2.cardcolour;//这里只是转换一下属性

if(cardnum1.equals(cardnum2)==1) {//比较点数

return p1;

}else if(cardnum1.equals(cardnum2)==0) {//点数相同比较颜色

if(cardcolour1.equals(cardcolour2)) {//在colour类中重写了equals方法

return p1;

}else {

return p2;

}

}else {

return p2;

}

}

//



package PlayCards;


import java.util.Collections;

import java.util.Scanner;


public class test {

static Scanner sc = new Scanner(System.in);//将 sc设为静态变量可以全局调用

public static void main(String[] args) {

test t =new test();

System.out.println("欢迎进入游戏");

System.out.println("======================");

System.out.println("展示牌:");

Card c =new Card();

c.putCard();//将牌装进集合里

c.showCard(c.Cards);//展示未打乱的牌

Collections.shuffle(c.Cards);//重点这里。Collections.shuffle方法可以将List集合打乱

  //c.showCard(c.Cards);//展示打乱后的牌

System.out.println();//换行

Player p1=t.setPlayer();

Player p2=t.setPlayer();//设置玩家信息

System.out.println("玩家信息:"+p1.ID+p1.name);

System.out.println("玩家信息:"+p2.ID+p2.name);

System.out.println("开始发牌");

System.out.println("========================");

Game g = new Game(p1,p2);//实例化Game类对象,将两名玩家传进去

g.putPlayerCards(c.Cards);//发牌

Player winner = g.winner();//调用Game类中的比较方法

System.out.println("赢家是:");

System.out.println(winner.ID+winner.name);

System.out.println("展示手牌");

System.out.println(p1.ID+p1.name);

p1.showPlayerCards(p1.playercard);//展示手牌

System.out.println(p2.ID+p2.name);

p1.showPlayerCards(p2.playercard);//展示手牌

}

public Player setPlayer() {//设置玩家信息

System.out.println("请输入玩家信息");

String ID=sc.next();

String name=sc.next();

Player p=new Player(ID,name) ;

return p;

}

}


提问者:小涂啥也不知道 2020-04-12 04:35

个回答

  • 小涂啥也不知道
    2020-04-12 04:40:27

    没有用什么Set集合,用的是Collectians接口的shuffle方法。把List集合打乱。

    我觉得最难的就是比大小。开始准备用重写comopar的方法。但发现代码量也差不多。索性就用重写Cardnuml类和Colouer的equals 方法法。先比数字后,如果数字相同就比颜色。

    代码不怎么规范,各位有什么问题欢迎提出来。


    咦,本以为代码不多的,结果那么多