Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement a simple poker game with Java code

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to use Java code to achieve a simple card game". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Java code to achieve a simple card game" can help you solve the problem.

Function description

1. Create playing cards. Including four kinds of colors (spades, hearts, clubs, diamonds), thirteen points (2-10 JMagQ Q ·K), regardless of the size of the king.

2. Create two players. Including the player id, name, hand, etc., the hand is a collection of playing cards.

3. Shuffle the cards. The created playing cards will be disrupted.

4. Licensing. The shuffled cards will be collected, starting from the first card, issued to two players, installed in the way of one card per person, each issued two cards.

5. Games. Compare the cards in the hands of two players. The rule is: compare the cards with the largest points on the hands of two players, and win if the points are the same, compare them according to the color flowers.

Difficulty analysis:

1. Shuffle cards and how to disrupt playing cards. (main knowledge points Random,list.contains ())

2. How to compare the size of playing cards. (main knowledge points collections.sort (), comparable)

Implementation code

Card.java (playing cards)

Package com.betty.dome2; public class Card implements Comparable {/ / card face private String num; / / pattern private String name; public Card () {} public Card (String num,String name) {this.num = num; this.name = name } public String getNum () {return num;} public void setNum (String num) {this.num = num;} public String getName () {return name;} public void setName (String name) {this.name = name } @ Override public int hashCode () {final int prime = 31; int result = 1; result = prime * result + ((name = = null)? 0: name.hashCode ()); result = prime * result + ((num = = null)? 0: num.hashCode ()); return result } @ Override public boolean equals (Object obj) {if (this = = obj) return true; if (obj = = null) return false; if (getClass ()! = obj.getClass ()) return false Card other = (Card) obj; if (name = = null) {if (other.name! = null) return false;} else if (! name.equals (other.name)) return false If (num = = null) {if (other.num! = null) return false;} else if (! num.equals (other.num)) return false; return true } / / get card size private Integer getNumValue (String num) {switch (num) {case "A": return 12; case "2": return 13; case "3": return 1; case "4": return 2 Case "5": return 3; case "6": return 4; case "7": return 5; case "8": return 6; case "9": return 7; case "10": return 8 Case "J": return 9; case "Q": return 10; case "K": return 11;} return-1 } / / get the color size private Integer getNameValue (String name) {switch (name) {case "spades": return 4; case "hearts": return 3; case "clubs": return 2; case "diamonds": return 1 } return-1;} @ Override public int compareTo (Card card) {/ / if the cards are the same, int numCompare = getNumValue (this.num) .compareto (getNumValue (card.num)) If (numCompare = = 0) {return getNameValue (this.name) .compareto (getNameValue (card.name));} return numCompare;}}

Player.java (role class)

Package com.betty.dome2; import java.util.ArrayList;import java.util.List; public class Player {/ / role id private Integer ID; / / role name private String name; / / card obtained by the role List cardList; public Player () {} public Player (Integer ID,String name) {this.ID = ID This.name = name; this.cardList = new ArrayList ();} public Integer getID () {return ID;} public void setID (Integer iD) {ID = iD;} public String getName () {return name } public void setName (String name) {this.name = name;}} Welcome.java (main program) package com.betty.dome2; import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Random;import java.util.Scanner Public class Welcome {String [] nums = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; String [] names = {"spades", "hearts", "clubs", "diamonds"}; List cardStartList; / sequence List cardEndList when starting to create playing cards / / sequence after shuffle List playerList; / / player list public Welcome () {this.cardStartList= new ArrayList (); this.cardEndList = new ArrayList (); this.playerList = new ArrayList () } / / create playing cards void createCard () {System.out.println ("- create playing cards -") For (String name: names) {for (String num: nums) {cardStartList.add (new Card (num,name)) }} System.out.println ("- Poker created successfully -"); System.out.print ("["); for (int iPlay0polii)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report