猜拳的银魂(Java小游戏)

参考:http://wenku.baidu.com/view/52ed94c4aa00b52acfc7ca20.html

part 1:

package 猜拳的银魂;

public class Test {
	public static void main(String arg[]){
		Game game=new Game();
		System.out.println("__________欢迎进入游戏世界__________");
		System.out.println("\n\t*******************\n\t  **  猜拳  开始**  \n\t********************\n");
		System.out.println("游戏规则:1.石头  2.剪刀  3.布");
		game.character();
		game.begin();
		game.result();
	}
}

part 2:

package 猜拳的银魂;

import java.util.Scanner;

public class Game {
	int count=0;
	int countp=0;
	Person person=new Person();
	Computer computer=new Computer();
	Scanner reader=new Scanner(System.in);
	public void character(){
		System.out.println("请选择你的角色:1.银时  2.神乐  3.新八");
		int input=reader.nextInt();
		switch (input){
		case 1:person.name="银时"; break;
		case 2:person.name="神乐"; break;
		case 3:person.name="新八"; break;
		}
		System.out.println("请选择你的对手:1.近藤  2.土方  3.总悟");
		input=reader.nextInt();
		switch (input){
		case 1:computer.name="近藤"; break;
		case 2:computer.name="土方"; break;
		case 3:computer.name="总悟"; break;
		}
	}
	public void begin(){
		System.out.println("开始吗?(y/n)");
		String input =reader.next();
		while(input.equals("y")){
			int a=person.show();
			int b=computer.show();
			if(a==1&&b==3||a==2&&b==1||a==3&&b==2){
				System.out.println("结果:恭喜胜利!");
				person.score++;
			}
			else if(a==1&&b==1||a==2&&b==2||a==3&&b==3){
				System.out.println("结果:平局!");
				countp++;
			}
			else {
				System.out.println("结果:你输了!");
				computer.score++;
			}
			count++;
			System.out.println("是否继续?(y/n)");
			input =reader.next();
		}
	}
	public String partresult(){
		String a;
		if(person.score>computer.score)
			a="恭喜你获得胜利!";
		else if(person.score==computer.score)
			a="平局!";
		else a="真遗憾,你输了!";
		return a;
	}
	public void result(){
		System.out.println("____________________________");
		System.out.println("\t"+person.name+"  VS  "+computer.name);
		System.out.println("对战次数:"+count);
		System.out.println(person.name+"得:"+person.score+"分");
		System.out.println(computer.name+"得:"+computer.score+"分");
		System.out.println("平局:"+countp);
		System.out.println("最终结果:"+partresult());
		System.out.println("____________________________");
	}
}

part 3:

package 猜拳的银魂;

import java.util.Scanner;
public class Person {
	String name;
	int score;
	Scanner reader=new Scanner(System.in);
	public int show(){
		System.out.println("请出拳:1.石头  2.剪刀  3.布");
		int input=reader.nextInt();
		switch (input){
		case 1:System.out.println("你出:石头"); break;
		case 2:System.out.println("你出:剪刀"); break;
		case 3:System.out.println("你出:布"); break;
		}
		return input;
	}
}

part 4:

package 猜拳的银魂;

public class Computer {
	String name;
	int score;
	public int show(){
		int input=(int)(Math.random()*3+1);
		switch (input){
		case 1:System.out.println(name+"出:石头"); break;
		case 2:System.out.println(name+"出:剪刀"); break;
		case 3:System.out.println(name+"出:布"); break;
		}
		return input;
	}
}


你可能感兴趣的:(java,游戏)