zoukankan      html  css  js  c++  java
  • Android学习-简单的猜拳游戏

    今天是2016728

    Java制作简单的猜拳游戏,

    游戏界面

    简单分析:

    猜拳比赛需要两个选手参加,一个是自己,一个是敌人,每个选手都有两个属性,分别是分数和角色;哪一方获胜,该方的分数自动加1,平局都不加,并记录平局数,每比完一次赛,计算结果并输出,然后询问是否继续玩,如果继续,则重复以上动作,如果不想继续玩,则计算最终结果并输出退出.

    Person.java

    package person;

    public  class Person {

    int score;

    String role;

    public  int attack(){return 0;};

    public int getScore() {

    return score;

    }

    public void setScore(int score) {

    this.score = score;

    }

    public String getRole() {

    return role;

    }

    public void setRole(String role) {

    this.role = role;

    }

    public void addScore() {

    this.score++;

    }

    }

     

    Player.java

     

    package person;

    import java.util.Scanner;

    public class Playerextends Person {

    public Player() {

    // TODO Auto-generated constructor stub

    }

    @Override

    public int attack() {

    // TODO Auto-generated method stub

    System.out.println("请出拳:1.石头2.剪子3.2");

    Scanner input = new Scanner(System.in);

    int index = input.nextInt();

    return index;

    }

    @Override

    public

    void addScore() {

    super.addScore();

    System.out.println("结果:运气真好,你赢了!");

    }

    }

    Enemy.java

    package person;

    import java.util.Random;

    public class Enemyextends Person {

    public Enemy() {

    // TODO Auto-generated constructor stub

    }

    @Override

    public int attack() {

    // TODO Auto-generated method stub

    Random random = new Random();

    return random.nextInt(2) + 1;

    }

    @Override

    public

    void addScore() {

    super.addScore();

    System.out.println("结果:啊!你输了!");

    }

    }

    Game.java

    package gameTest;

    import java.util.Scanner;

    import person.Enemy;

    import person.Player;

    public class Game {

    private Player player;

    private Enemy enemy;

    private String roles[];

    private String enemyRoles[];

    private String attacks[];

    private int peace = 0;

    final int stone = 1;

    final int cut = 2;

    final int cloth = 3;

    public Game() {

    // TODO Auto-generated constructor stub

    player = new Player();

    enemy = new Enemy();

    roles = new String[] { "null", "沸羊羊", "暖羊羊", "美羊羊" };

    enemyRoles = new String[] { "null", "喜羊羊", "慢羊羊", "懒羊羊" };

    attacks = new String[] { "null", "石头", "剪子", "" };

    }

    private void setRole() {

    Scanner input = new Scanner(System.in);

    System.out.print(" 请选择你的角色(1.沸羊羊  2.暖羊羊  3.美羊羊):");

    player.setRole(roles[input.nextInt()]);

    System.out.print(" 请选择对手角色(1.喜羊羊  2.慢羊羊 3.懒羊羊):");

    enemy.setRole(enemyRoles[input.nextInt()]);

    }

    public void start() {

    System.out.println("-----------------欢迎进入羊村游戏世界--------------------");

    System.out.println(" ******************");

    System.out.println(" **  游戏开始  **");

    System.out.println(" ******************");

    System.out.println(" 出拳规则:1.石头2.剪子3.");

    this.setRole();

    System.out.print("开始游戏吗? (y/n)");

    Scanner input = new Scanner(System.in);

    if (input.next().equals("y"))

    this.play();

    else

    System.out.println("退出游戏");

    input.close();

    }

    public void play() {

    int enemyAttack;

    while (true) {

    System.out.print("请出拳:1.石头2.剪子3.");

    Scanner input = new Scanner(System.in);

    switch (input.nextInt()) {

    case stone:

    System.out.println("你出拳:" + attacks[stone]);

    enemyAttack = enemy.attack();

    System.out.println(enemy.getRole() + "出拳:" + attacks[enemyAttack]);

    if (enemyAttack == this.cut)

    player.addScore();

    else if (enemyAttack == this.cloth)

    enemy.addScore();

    else if (enemyAttack == this.stone) {

    peace++;

    System.out.println("平局");

    }

    break;

    case cut:

    System.out.println("你出拳:" + attacks[cut]);

    enemyAttack = enemy.attack();

    System.out.println(enemy.getRole() + "出拳:" + attacks[enemyAttack]);

    if (enemyAttack == this.cloth)

    player.addScore();

    else if (enemyAttack == this.stone)

    enemy.addScore();

    else if (enemyAttack == this.cut) {

    peace++;

    System.out.println("平局");

    }

    break;

    case cloth:

    System.out.println("你出拳:" + attacks[cloth]);

    enemyAttack = enemy.attack();

    System.out.println(enemy.getRole() + "出拳:" + attacks[enemyAttack]);

    if (enemyAttack == this.stone)

    player.addScore();

    else if (enemyAttack == this.cut)

    enemy.addScore();

    else if (enemyAttack == this.cloth) {

    peace++;

    System.out.println("平局");

    }

    break;

    default:

    break;

    }

    System.out.print("是否开始下一轮?  (y/n)");

    if (input.next().equals("n")) {

    this.Over();

    break;

    }

    System.out.println("--------------------------------");

    }

    }

    public void Over() {

    System.out.println(player.getRole() + " 对决 " + enemy.getRole());

    int total = player.getScore() + enemy.getScore() + peace;

    System.out.println("次数 :" + total);

    System.out.println("平局 :" + peace);

    System.out.println(player.getRole() + " :" + player.getScore());

    System.out.println(enemy.getRole() + " :" + enemy.getScore());

    if (player.getScore() > enemy.getScore())

    System.out.println("最终结果 :" + player.getRole() + "获胜");

    else if (player.getScore() < enemy.getScore())

    System.out.println("最终结果 :" + enemy.getRole() + "获胜");

    else

    System.out.println("最终结果 :");

    System.out.println("--------------------------------");

    }

    }

    Test.java

    package gameTest;

    public class Test {

    public static void main(String[] args) {

    // TODO Auto-generated method stub

    Game game = new Game();

    game.start();

    }

    }

    二 用swing做的登录界面

    import javax.swing.JButton;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JTextField;

    public class Login extends JFrame {

    /**

     * @param args

     */

    public Login()

    {

    setLayout(null);

    JLabel l1 = new JLabel("用户名:");

    JTextField t1 =new JTextField();

    JLabel l2 = new JLabel("密码:");

    JTextField t2 =new JTextField();

    JButton b1 = new JButton("登录");

    JButton b2 = new JButton("重置");

    l1.setBounds(30, 10,50,20);

    t1.setBounds(100, 10,150,20);

    l2.setBounds(30, 50,50,20);

    t2.setBounds(100, 50,150,20);

    b1.setBounds(40,80,80,20);

    b2.setBounds(140,80,80,20);

    add(l1);

    add(t1);

    add(l2);

    add(t2);

    add(b1);

    add(b2);

    }

    public static void run(JFrame f,final int width,final int height)

    {

    f.setTitle(f.getClass().getName());

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    f.setSize(width,height);

    f.setVisible(true);

    }

    public static void main(String[] args) {

    // TODO Auto-generated method stub

    run(new Login(),300,400);

    }

    }

  • 相关阅读:
    Appium+Python移动端(Android)自动化测试环境搭建
    我的python笔记06
    我的python笔记05
    Monkey如何使用
    android studio 虚拟机adb.exe已停止工作的处理
    完整的整车开发流程
    汽车行业项目管理
    分析几种常见的汽车电子技术应用与发展
    打印流
    序列化流
  • 原文地址:https://www.cnblogs.com/WK-can-do-anything/p/5792226.html
Copyright © 2011-2022 走看看