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 write a fist guessing Mini Game with Java

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

Share

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

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)

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