• day7_finger-guessGame_update


    项目需求:人机猜拳游戏

    改进

    1. 更改Player类中的importName方法的代码,同时更改Judge类中askName方法,使两者耦合性减弱,使得importName和getName方法不再重复.
    2. 将Player类和Computer类中的fingerGuess方法修改使其只返回猜拳数字,在Judge类中用数组代替switch进行出拳判断,解决之前编译器提醒的代码重复问题.
    3. 在Judge类的judged方法中加入各个游戏成员获胜次数显示.

    小想法(待实现)

    • 增加一般人机和困难人机 ,一般人机必胜利 一局,困难人机必胜利两局

    代码:

    Player类

    package org.zzz1.www;
    public class Player {
    	protected String name;
    	private int flag;
    
    	{
    		name = "未命名";
    		flag = 0;
    	}
    
    	protected String importName() {
    
    		return Game.input.next();
    	}
    
    	protected int fingerGuess() {
    		int finger;
    		do{
    			System.out.println("输入1-3之间的数!");
    		 	finger= Game.input.nextInt();
    		}while(!(finger > 0 && finger < 4) );
    
    		return finger;
    	}//fingerGuess
    
    	protected void setFlag() {
    		this.flag++;
    	}
    
    	protected int getFlag() {
    		return flag;
    	}
    
    	protected void setName(String name) {
    		this.name = name;
    	}
    
    	protected String getName() {
    		return name;
    	}
    
    }
    

    Computer类

    package org.zzz1.www;
    import java.util.Random;
    
    public class Computer extends Player {
    	public Computer() {
    		name = "简单人机";
    	}
    
    	@Override
    	protected String importName() {
    
    		return this.name;
    	}
    
    	@Override
    	protected int fingerGuess() {
    		int finger;
    		Random random = new Random();
    		finger = random.nextInt(3) + 1;
    		return finger;
    	}// fingerGuess
    }
    
    

    Person类

    package org.zzz1.www;
    
    public class Person extends Player {
    
    }
    
    

    Judge类

    package org.zzz1.www;
    public class Judge {
    
    	public void welcome() {
    		System.out.println("欢迎来到猜拳游戏
    游戏规则:输入1,2,3分别代表剪刀,石头,布,与电脑进行对决,五局三胜");
    	}
    
    	public void askName(Player person,Player computer) {
    		if(person instanceof Person) {
    
    			System.out.println("玩家1说出你的名字");
    			String personName = person.importName();
    			person.setName(personName);
    			System.out.println("玩家2说出你的名字");
    			System.out.println(computer.importName());
    		}
    	}
    
    	public void startGame(Player person, Player computer){
    		int personPlay;
    		int computerPlay;
    		String[] play = {"","剪刀","石头","布"};
    
    		System.out.println(person.getName() + " 与 " + computer.getName() + "猜拳正式开始");
    
    		for(int i = 0; i < 5; i++) {
    			System.out.println("第"+ (i+1) + "局 " + person.getName() + " 要出什么?1.剪刀 2.石头 3.布");
    			personPlay = person.fingerGuess();
    			System.out.println(person.getName() + " 出了" + play[personPlay]);
    			computerPlay = computer.fingerGuess();
    			System.out.println(computer.getName() + " 出了" + play[computerPlay]);
    
    			if ((personPlay == 1 && computerPlay == 3) || (personPlay == 2 && computerPlay == 1) || (personPlay == 3 && computerPlay == 2)) {
    				System.out.println(person.getName() + "获胜");
    				person.setFlag();
    			} else if ((computerPlay == 1 && personPlay == 3) || (computerPlay == 2 && personPlay == 1) || (computerPlay == 3 && personPlay == 2)) {
    				System.out.println(computer.getName() + "获胜");
    				computer.setFlag();
    			} else {
    				System.out.println("平局");
    			}
    		}// for
    	}// startGame
    
    	public void judged(Player person,Player computer){
    		System.out.println();
    		System.out.println(person.getName() + " 获胜局数为" +person.getFlag());
    		System.out.println(computer.getName() + " 获胜局数为" +computer.getFlag());
    		if(person.getFlag() < computer.getFlag()){
    			System.out.println(computer.getName() + " 恭喜你获得游戏胜利");
    		}else if(person.getFlag() > computer.getFlag()) {
    			System.out.println(person.getName() + " 恭喜你获得游戏胜利");
    		}else{
    			System.out.println(" 太巧了,竟然平局");
    		}
    	}
    
    }// class
    
    

    Game类

    package org.zzz1.www;
    import java.util.Scanner;
    
    public class Game {
    	public static Scanner input = new Scanner(System.in);
    	public static void main(String[] args) {
    		Judge judge1 = new Judge();
    		Player person = new Person();
    		Player computer = new Computer();
    		judge1.welcome();
    		judge1.askName(person);
    		judge1.startGame(person,computer);
    		judge1.judged(person,computer);
    		input.close();
    	}
    }
    

    类图

    运行结果



  • 相关阅读:
    进阶学习项目实战链接
    Django与数据库操作
    xadmin的使用
    Django 常见的异常
    网站 安全 ---- 常见的 web 攻击
    nodejs--vue
    ECMAScript 6 简介
    Django -- DRF 认证流程
    Django -- 缓存
    Django --- celery异步任务与RabbitMQ模块
  • 原文地址:https://www.cnblogs.com/zzz1zzz/p/14268851.html
Copyright © 2020-2023  润新知