Java design pattern from [scissor stone cloth AI Strategy] analysis Strategy (strategy) mode

Source: Internet
Author: User

Strategy mode is a very easy mode.

It defines a series of algorithms, encapsulates them one by one, and enables them to replace each other.

Considering I'm making a scissors and stone cloth game, I can play against the computer.

The difficulty level of a computer is divided into 2 levels: ordinary difficulty and inability to overcome difficulty.

Ordinary difficulty refers to the computer will randomly out of stone, scissors, cloth. The inability to overcome difficulty means that the computer will "cheat". The computer will know beforehand what gesture the player is out of.

Suppose the player is a pair of scissors. Then the computer will be out of stone, the player will never win.

So, these two difficulties represent two kinds of algorithms, in order to make them can be loaded by the main class of the game, they should inherit from the same interface or class. and exposed the way the computer out gestures, code such as the following:

Import java.util.random;import java.util.scanner;interface gamestrategy{int play (int player);}    Class fingerguessing{Scanner Playerscanner = new Scanner (system.in);        Public String toString (int. finger) {switch (finger) {case 1:return "stone";        Case 2:return "Scissors";        Case 3:return "cloth"; Default:return "Error!        ";        }} public void Start (Gamestrategy comstrategy) {Boolean gameover = false; while (!gameover) {System.out.println (press ENTER to confirm): 1, Stone, 2, scissors.            3, cloth ");            int playerchoice = 0;            while (playerchoice <= 0 | | playerchoice > 4) {playerchoice = Playerscanner.nextint ();            } int comchoice = Comstrategy.play (Playerchoice); System.out.printf ("You're out of%s.")            The computer is%s\n ", ToString (Playerchoice), ToString (Comchoice));            if (Playerchoice = = Comchoice) {System.out.println ("draw"); } else if (plAyerchoice + 1 = = Comchoice | | PlayerChoice-2 = = Comchoice) {System.out.println ("Congratulations on your victory!                ");            Gameover = true; } else {System.out.println ("I'm sorry you failed!")                ");            Gameover = true;        }}}}class Normal implements gamestrategy{public int play (int player) {Random rnd = new Random ();    Return Rnd.nextint (3) + 1; }}class hard implements gamestrategy{public int play (int. player) {switch (player) {case 1:r        Eturn 3;        Case 2:return 1;        Case 3:return 2;        Default:return 1; }}}public class strategy{public static void Main (string[] args) {fingerguessing guess = new fingerguessing        ();        System.out.println ("General Difficulty:");        Guess.start (New Normal ());        System.out.println ("You can't win the difficulty:");    Guess.start (New Hard ()); }

The computer's gesture mode inherits from the Gamestrategy interface. The Play method returns a computer-out gesture (stone, scissors, cloth), and the incoming player is a gesture from the player (to cheat in the difficulty of showing). In the game class, the Start method accepts an object that satisfies the Gamestrategy interface, which is equivalent to accepting a different algorithm. In the main method. We pass in a common difficulty algorithm, and an algorithm that can not overcome the difficulty, we can try to execute a bit.

In addition, the advantage of this model is that if you want to design a new difficulty, it is very convenient, just to design a class that inherits from Gamestrategy. and write your own algorithm in the play method.

Java design pattern from [scissor stone cloth AI Strategy] analysis Strategy (strategy) mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.