How to write a fist guessing Mini Game with Java
This article mainly introduces "how to use Java to write a fist-guessing Mini Game". In daily operation, I believe many people have doubts about how to use Java to write a fist-guessing Mini Game. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to write a fist-guessing Mini Game with Java". Next, please follow the editor to study!
Let's take a look at what classes I wrote:
Player: player class
ComputerPlayer: robot player class, mainly used to realize robot random punching
Game: game class, which mainly implements the logic of game rules, as well as the logic of formal games.
TestGuessBox: code test class
Player class:
/ / player class public class Player {private String name; / / player nickname private int score; / / player integral private String box; / / player constructor, pass the player nickname and the player's initial score Player (String name,int score) {this.name=name; this.score=score;} public String getName () {return name;} public void setName (String name) {this.name=name;} public int getScore () {return score } public void setScore (int score) {this.score = score;} public String getBox () {return box;} public void setBox (String box) {this.box = box;}}
ComputerPlayer class
Public class ComputerPlayer extends Player {/ / robot player constructor, pass in robot nickname and initial integral ComputerPlayer (String name, int score) {super ("robot" + name, score); / / TODO Auto-generated constructor stub} / * to realize the logic of robot players randomly punching * / void punch () {String [] box = {"scissors", "stone", "paper"}; int index = (int) (Math.random () * 3) This.setBox (box index);}
Game class
Import java.util.Scanner; public class Game {Player p; / player ComputerPlayer cp; / / robot player / / constructor to get a player and a robot player Game (Player p, ComputerPlayer cp) {this.p = p; this.cp = cp;} / / start the game void start () {System.out.println ("player" + p.getName () + "and" + cp.getName () + "start the game") System.out.println ("your initial points are:" + p.getScore () + "\ n" + cp.getName () + "+ cp.getScore ()); Scanner sc=new Scanner (System.in); while (true) {System.out.println (" Please punch (scissors stone cloth, exit quit the game): "); String pbox=sc.next (); if (filter (pbox)) {/ / filter if (pbox.equals (" exit ")) {/ / quit the game break } else {p.setBox (pbox); cp.punch (); System.out.println ("you are out:" + p.getBox ()); System.out.println (cp.getName () + "out:" + cp.getBox ()); int result = ruler (pForce cp); if (result > 0) {System.out.println ("you win, win 10 points"); p.setScore (p.getScore () + 10); cp.setScore (cp.getScore ()-10);} else if (result)