手记

Java入门第三季 简易扑克游戏

建了4个类

 PokerCards用来给Poker类赋值

Player玩家类

Poker扑克牌类(重写比较函数)

Gaming游戏类

-----------------------------------------

public class PokerCards {//用来给Poker类赋值

public String[] decor = {"黑桃","红桃","梅花","方块"};

public String[] point = {"3","4","5","6","7","8","9","10","J","Q","k","A","2"};

}

---------------------------------------

public class Player{//玩家类

public int ID;

public String name;

public Poker[] handCards = new Poker[2];//手牌

}

-------------------------------------------

public class Poker implements Comparable<Poker>{//扑克牌类

public String Decor;//花色

public String Point;//点数

public Poker() {

}

public Poker(String decor,String point) {

this.Decor = decor;

this.Point = point;

}

@Override

public int compareTo(Poker o) {//重写了compareTo()函数,根据PokerCards类中数组的索引来判断大小

// TODO Auto-generated method stub

PokerCards pc = new PokerCards();

int a = 0;

int b = 0;//a,b记录point的索引位置

int c = 0;

int d = 0;//c,d记录decor的索引位置

for(int i = 0;i < pc.point.length;i++) {

if(this.Point == pc.point[i]) {

a = i;

}

}

for(int i = 0;i < pc.point.length;i++) {

if(o.Point == pc.point[i]) {

b = i;

}

}

for(int i = 0;i < pc.decor.length;i++) {

if(this.Decor == pc.decor[i]) {

c = i;

}

}

for(int i = 0;i < pc.decor.length;i++) {

if(o.Decor == pc.decor[i]) {

d = i;

}

}

if(a > b) {//先判断谁的point大

return 1;

}else if(a < b){

return -1;

}else {

if(c < d) {//再判断谁的decor大

return 1;

}else if(c > d) {

return -1;

}else {

return 0;

}

}

}

}

-------------------------

//游戏类

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;


public class Gaming {

public static void main(String[] args) {

// TODO Auto-generated method stub

List<Poker> cards = new ArrayList<Poker>();

Scanner console = new Scanner(System.in);

//1.创建扑克牌

PokerCards pc = new PokerCards();

System.out.println("-----创建扑克牌");

for(int i = 0;i < pc.decor.length;i++) {

for(int j = 0;j < pc.point.length;j++) {

cards.add(new Poker(pc.decor[i],pc.point[j]));

}

}

System.out.println("-----扑克牌创建成功!");

//2.洗牌

System.out.println("-----开始洗牌...");

Collections.shuffle(cards);

System.out.println("-----洗牌结束!");

//3.创建玩家

System.out.println("-----创建玩家");

Player player1 = new Player();

Player player2 = new Player(); 

System.out.println("请输入第1位玩家的ID和姓名");

while(true) {

try {

console = new Scanner(System.in);

System.out.println("输入ID:");

player1.ID = console.nextInt();

break;

}catch(Exception e) {

System.out.println("请输入整数类型的ID!");

}

}

System.out.println("输入姓名:");

player1.name = console.next();

System.out.println("请输入第2位玩家的ID和姓名");

while(true) {

try {

console = new Scanner(System.in);

System.out.println("输入ID:");

player2.ID = console.nextInt();

break;

}catch(Exception e) {

System.out.println("请输入整数类型的ID!");

}

}

System.out.println("输入姓名:");

player2.name = console.next();

System.out.println("---欢迎玩家" + player1.name);

System.out.println("---欢迎玩家" + player2.name);

//4.发牌

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

for(int i = 0; i < 4; i++) {

if(i % 2 == 0) {

player1.handCards[i / 2] = (Poker)cards.get(i);

System.out.println("玩家" + player1.name + "拿牌");

}else {

player2.handCards[i / 2] = (Poker)cards.get(i);

System.out.println("玩家" + player2.name + "拿牌");

}

}

System.out.println("-----发牌结束!");

//5.开始游戏

System.out.println("-----开始游戏");

Poker maxp1 = new Poker();//玩家1最大的手牌

int a = player1.handCards[0].compareTo(player1.handCards[1]);//比较玩家1的两张手牌

if(a >= 0) {

maxp1 = player1.handCards[0];

}else {

maxp1 = player1.handCards[1];

}

System.out.println("玩家" + player1.name + "最大的手牌为:" + maxp1.Decor + maxp1.Point);

Poker maxp2 = new Poker();//玩家2最大的手牌

int b = player2.handCards[0].compareTo(player2.handCards[1]);//比较玩家2的两张手牌

if(b >= 0) {

maxp2 = player2.handCards[0];

}else {

maxp2 = player2.handCards[1];

}

System.out.println("玩家" + player2.name + "最大的手牌为:" + maxp2.Decor + maxp2.Point);

int c = maxp1.compareTo(maxp2);

if(c > 0) {

System.out.println("-----玩家" + player1.name + "获胜!");

}else if(c < 0) {

System.out.println("-----玩家" + player2.name + "获胜!");

}else {

System.out.println("平局!");

}

System.out.println("玩家各自的手牌为:");

System.out.println(player1.name + ":[" + player1.handCards[0].Decor + player1.handCards[0].Point +

"," +player1.handCards[1].Decor + player1.handCards[1].Point + "]");

System.out.println(player2.name + ":[" + player2.handCards[0].Decor + player2.handCards[0].Point +

"," +player2.handCards[1].Decor + player2.handCards[1].Point + "]");

console.close();

}

}


1人推荐
随时随地看视频
慕课网APP