Java 面向对象实例:人机猜拳

package com.neusoft.guess;
/**
 * 玩家选择出拳
 */
import java.util.Scanner;

public class Person {
	String name ;
	int pscore;
	int count;

	public int showFirst() {
		Scanner input = new Scanner(System.in);
		System.out.print("请出拳:1.剪刀2.石头 3.布(输入相应数字):");
		int choice = input.nextInt();
		switch (choice) {
		case 1:
			System.out.println("你出拳:剪刀");
			break;
		case 2:
			System.out.println("你出拳:石头");
			break;
		case 3:
			System.out.println("你出拳:布");
			break;
		default:
			System.out.println("选择有误");
			break;
		}
		count++;
		return choice;
	}

}
package com.neusoft.guess;
/**
 * 1.电脑随机出拳
 * 2.选择对战电脑角色
 */
import java.util.Scanner;

public class Computer {
	String name;
	int cscore;
//	public Computer(){
//		this.name = name;
//		this.cscore = cscore;
//	}
	
	public int showFirst() {
		int choice = (int) (Math.random()*3+1);
		switch (choice) {
		case 1:
			System.out.println("电脑出拳:剪刀");
			break;
		case 2:
			System.out.println("电脑出拳:石头");
			break;
		case 3:
			System.out.println("电脑出拳:布");
			break;
		default:
			break;
		}
		return choice;
	}
	
	public void choiceRole(){
		Scanner input = new Scanner(System.in);
		int role = input.nextInt();
		switch (role) {
	case 1:
		name = "刘备";
		break;
	case 2:
		name = "孙权";
		break;
	case 3:
		name = "曹操";
		break;
	default:
		name = "computer";
		break;
	}
	}
	
	
}
package com.neusoft.guess;

import java.util.Scanner;


public class Test {
	public static void main(String[] args) {
		
//		Person person = new Person();
//		int p = person.showFirst();
//		System.out.println(p);
//		
//		Computer computer = new Computer();
//		int c = computer.showFirst();
//		System.out.println(c);
//		移动至Game中引用
		
		Game game = new Game();
		game.starGame();

	}
}

package com.neusoft.guess;
/**
 * 1.游戏规则 
 * 2.判定条件 积分规则
 * 
 * @author zhuzidexter
 *
 */
import java.util.Scanner;

public class Game {
	private Person person;
	private Computer computer;

	public Game() {
		person = new Person();
		computer = new Computer();
	}

	public void starGame() {
		System.out.println("---------------------进入游戏世界---------------------");
		System.out.println("出拳规则:1.剪刀       2.石头       3.布");
		System.out.print("请选择对方的角色  (1.刘备      2.孙权     3.曹操)");
		Scanner input = new Scanner(System.in);
		computer.choiceRole();
		System.out.println("你选择了" + computer.name + "对战");
		System.out.print("要开始吗?(y/n)");
		String yn = input.next();
		String yon;
		if(yn.equals("y")) {
			do {
				int p = 0;
				int c = 0;
				p = person.showFirst();
				c = computer.showFirst();
				if (p == c) {
					System.out.println("结果说:和局,真衰!嘿嘿,等着瞧吧!");
				} else if (p == 1 && c == 2 || p == 2 && c == 3 || p == 3 && c == 1) {
					computer.cscore++;
					System.out.println("^_^,你输了,真笨!");
				} else if (p == 1 && c == 3 || p == 2 && c == 1 || p == 3 && c == 2) {
					person.pscore++;
					System.out.println("恭喜,你赢了!");
				}
				
				System.out.print("是否开始下一轮?(y/n)");
				yon = input.next();
			} while (yon.equals("y"));
		
		System.out.print("请输入您的名字:");
		person.name = input.next();
		System.out.println("-----------------------------------------------------");
		System.out.println(computer.name + "   vs   " + person.name);
		System.out.print("对战次数:" + person.count+'\n');
		System.out.println("姓名"+'\t'+"得分");
		System.out.println(person.name+'\t'+person.pscore);
		System.out.println(computer.name+'\t'+computer.cscore);
		
		if (person.pscore > computer.cscore) {
			System.out.println("结果:不要走决战到天亮!");
		} else if (person.pscore < computer.cscore) {
			System.out.println("结果:呵呵,笨笨,下次加油啊!");
		} else if (person.pscore == computer.cscore) {
			System.out.println("结果:达成平手,下次再和你一分高下!");
		}
		System.out.println("------------------------------------------------------");
		}else{
			System.out.println("再见!");
		}
	}

}
package com.neusoft.guess;

import java.util.Scanner;


public class Test {
	public static void main(String[] args) {
		
//		Person person = new Person();
//		int p = person.showFirst();
//		System.out.println(p);
//		
//		Computer computer = new Computer();
//		int c = computer.showFirst();
//		System.out.println(c);
//		移动至Game中引用
		
		Game game = new Game();
		game.starGame();

	}
}

效果图:

Java 面向对象实例:人机猜拳_第1张图片Java 面向对象实例:人机猜拳_第2张图片


你可能感兴趣的:(JavaHomework)