In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to fight landlords on the console by Java". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Java how to achieve the console to fight landlords" bar!
To fight the landlord in the console.
Today to share with you, recently went back to learn java basic implementation of a console version of the fight landlord. First of all, let's take a brief look at the requirements:
There are landlords, there are two farmers, and the landlords have three extra cards.
At the beginning of the game, the landlord's card is displayed and the card to be played is received from the console. After the landlord plays the card.
Farmer 1 displays the card and receives the card to be played from the console, and so on.
If the landlord wins, it prompts the landlord to win. If one of the peasants wins, he will be reminded of his victory.
1. Sort out the logic first.
Before we start typing the code, we must not be in a hurry. Instead, we need to analyze how the project we do should be operated and completed step by step. After sorting out the train of thought, in fact, the rest is natural! Beginners can write this project without looking at the following analysis. I'm sure you'll have a problem in the middle.
OK, then let's make a simple analysis after seeing the title.
We need to create a Poke, which has two basic attributes: pokecard and pokecolor. Because these two attributes are fixed, we can modify them with the final modifier when defining them. Second, it needs a method to generate a deck of cards, makepoke (), so that it can be called later. Then we need to have a sorting method for a display from large to small, and it is also convenient to reorder after the license is issued. There is also a custom comparator interface MyComparator.
You need to create a character class (Person), which has a deck of cards (Poke), a person's name (name), and whether a landlord (islandlord) has a method of playing cards (sendpoke).
You need to create a tool class (Fightlandlords) that fights landlords, which has two basic attributes: Poke and person. There is a method of selecting landlords (changelandlord), a method of shuffling cards (shufflecards), a method of dealing cards (Licensing), a method of playing cards (startpoke).
Realize step by step according to the train of thought
Let's first implement the card class. The specific code is as follows:
Package com.dun.palypoke;import java.util.ArrayList;import java.util.Collections;/* card class has two attributes: brand and color; there is a way to generate a deck of cards (54 cards); implement the comparator interface to facilitate sorting from small to large when sorting cards. The constructor is privatized and can only be obtained by generating a deck of cards. * / public class Poke {private static final String [] pokecard = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "Xiao Wang", "King"}; / / Brand private static final String [] pokecolor = {"♠", "♥", "♣", "♦"} / / pattern and color public String [] getPokecard () {return pokecard;} public String [] getPokecolor () {return pokecolor;} private Poke () {} / * method of generating cards, returns a deck of cards * / public static ArrayList makePoke () {ArrayList poke = new ArrayList (); for (int I = 0; I < pokecard.length-2; iTunes +) {for (int j = 0; j < pokecolor.length) ) {Collections.addAll (poke, pokecolor [j] + pokecard [I]);}} / * manually add Xiao Wang * / poke.add (pokecard.length-2); poke.add (pokecard [pokecard.length-1]); return poke;}
In the makepoke () method, I use a two-layer for loop, the first to control the brand and the second to control the color, because the big and small king does not involve color, so I add it manually in the last step. Finally return this deck of cards. Because my constructor is set to private, I add the makepoke method to the static modifier, which can only be called through the class name point.
Create a new Test test class and test it.
When we see that the run is complete, our first class is complete!
Then we write a second class, the character class.
Package com.dun.palypoke;import java.util.ArrayList;import java.util.Iterator;import java.util.Scanner;/* creates character class: 1. There are three attributes: a deck of cards (collection / array), name, and whether it is a landlord; 2. There is a way to play cards, print out all the cards in the console before playing, and then accept the cards from the console and play the cards. 3. The method of rational cards, after finishing, the cards are sorted from small to large. * / public class Person {private ArrayList poke = new ArrayList (); / / A deck of cards private String name; / / name private boolean islandlord; / / is the landlord public Scanner sc = new Scanner (System.in); public ArrayList getPoke () {return poke } public Person () {} public Person (String name) {this.name = name;} public void setPoke (ArrayList poke) {this.poke = poke;} public String getName () {return name;} public void setName (String name) {this.name = name;} public boolean isIslandlord () {return islandlord } public void setIslandlord (boolean islandlord) {this.islandlord = islandlord;} / * the function of the local variable list is to indicate whether all the cards played by the user and those in his hand exist or not when the user plays multiple cards, and if so, delete them all. * / public ArrayList sendpoke (ArrayList poke) {System.out.println (poke); ArrayList list = new ArrayList (); / / temporary variable for (int I = 0; I < poke.size (); iTunes +) {list.add (poke.get (I)) } System.out.println ("Please enter the cards to be played (no need to enter colors, multiple cards, separate, can not afford or please enter N):"); String usersc = sc.next (); if (usersc.equalsIgnoreCase ("n")) {System.out.println ("can't afford"); return this.poke } String [] spoke = usersc.split (","); int n = 0; / / count int I = 0; while (I
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.