人機互錘系統

main方法類

package caiquan;

import java.util.Scanner;

public class MyMain {
	public static void main(String[] args) {
		String choose;
		Welcome wel = new Welcome();
		wel.sayHello();
		System.out.println("歡迎進入猜拳遊戲");
		System.out.println("出拳規則:");
		System.out.println("0.石頭,1.剪刀,2.布");
		do {
			System.out.println("要開始嗎?y/n");
			Game game = new Game();
			game.game();
			Scanner input = new Scanner(System.in);
			System.out.println("是否繼續下一局y/n");
			choose = input.next();
			if (choose.equalsIgnoreCase("n"))
				break;

		} while (choose.equalsIgnoreCase("y"));
		System.out.println("遊戲結束");
		// System.out.println("你的分數:"+com.getScore());

	}

}

計算機類

package caiquan;

public class Computer {
	private String comName;
	private int score;
	private int choose;

	public String getComName() {
		return comName;
	}

	public void setComName(String comName) {
		this.comName = comName;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getChoose() {
		return choose;
	}

	public void setChoose(int choose) {
		this.choose = choose;
	}

	public void Computer() {
		choose = (int) (Math.random() * 3);
		// System.out.println(random);
		switch (choose) {
		case 0:
			System.out.println("電腦出拳:石頭");
			break;
		case 1:
			System.out.println("電腦出拳:剪刀");
			break;
		case 2:
			System.out.println("電腦出拳:布");
			break;
		default:
			break;
		}

	}

}

玩家類

package caiquan;

public class User {
	private String userName;
	private int score;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public User(int choose2) {
		switch (choose2) {
		case 0:
			System.out.println("你出拳:石頭");
			break;
		case 1:
			System.out.println("你出拳:剪刀");
			break;
		case 2:
			System.out.println("你出拳:布");
			break;
		default:
			break;
		}

	}

}

遊戲類

package caiquan;

import java.util.Scanner;

public class Game {
	public void game() {

		// Welcome wel=new Welcome();
		// wel.sayHello();
		/*
		 * System.out.println("歡迎進入猜拳遊戲"); System.out.println("出拳規則:");
		 * System.out.println("0.石頭,1.剪刀,2.布"); System.out.println("要開始嗎?y/n");
		 */
		Scanner input = new Scanner(System.in);
		String ch = input.next();
		if (ch.equalsIgnoreCase("y")) {
			System.out.println("請出拳:");
			System.out.println("0.石頭,1.剪刀,2.布(輸入數字)");

			// 人
			int choose2 = input.nextInt();
			User user = new User(choose2);
			// System.out.println(choose2);

			// 電腦
			Computer com = new Computer();
			com.Computer();
			int choose = com.getChoose();
			// System.out.println(choose);

			System.out.println("結果:");
			if ((choose2 == 0 && choose == 1) || (choose2 == 1 && choose == 2) || (choose2 == 2 && choose == 0)) {
				System.out.println("你贏了");
				int score2 = 0;
				score2++;
				user.setScore(score2);
			} else if ((choose2 == 0 && choose == 0) || (choose2 == 1 && choose == 1)
					|| (choose2 == 2 && choose == 2)) {
				System.out.println("平局");
			} else if ((choose2 == 0 && choose == 2) || (choose2 == 1 && choose == 0)
					|| (choose2 == 2 && choose == 1)) {
				System.out.println("你輸了");
				int score = 0;
				score++;
				com.setScore(score);
			}

		}

	}

}

歡迎類

package caiquan;

import java.util.Scanner;

public class Game {
	public void game() {

		// Welcome wel=new Welcome();
		// wel.sayHello();
		/*
		 * System.out.println("歡迎進入猜拳遊戲"); System.out.println("出拳規則:");
		 * System.out.println("0.石頭,1.剪刀,2.布"); System.out.println("要開始嗎?y/n");
		 */
		Scanner input = new Scanner(System.in);
		String ch = input.next();
		if (ch.equalsIgnoreCase("y")) {
			System.out.println("請出拳:");
			System.out.println("0.石頭,1.剪刀,2.布(輸入數字)");

			// 人
			int choose2 = input.nextInt();
			User user = new User(choose2);
			// System.out.println(choose2);

			// 電腦
			Computer com = new Computer();
			com.Computer();
			int choose = com.getChoose();
			// System.out.println(choose);

			System.out.println("結果:");
			if ((choose2 == 0 && choose == 1) || (choose2 == 1 && choose == 2) || (choose2 == 2 && choose == 0)) {
				System.out.println("你贏了");
				int score2 = 0;
				score2++;
				user.setScore(score2);
			} else if ((choose2 == 0 && choose == 0) || (choose2 == 1 && choose == 1)
					|| (choose2 == 2 && choose == 2)) {
				System.out.println("平局");
			} else if ((choose2 == 0 && choose == 2) || (choose2 == 1 && choose == 0)
					|| (choose2 == 2 && choose == 1)) {
				System.out.println("你輸了");
				int score = 0;
				score++;
				com.setScore(score);
			}

		}

	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章