Java人机猜拳
package theforthweek;
import java.util.Random;
import java.util.Scanner;
public class Game {
String player_A;
String player_B;
int player_a_grade;
int player_b_grade;
int number;
int andy;
public void judge(int i) {
switch (i) {
case 1:
player_B = "刘备";
break;
case 2:
player_B = "孙权";
break;
case 0:
player_B = "曹操";
break;
}
}
public void initial() {
System.out.println("welcome to the game word:");
System.out.println("************************");
System.out.println("******=magin=start=*****");
System.out.println("************************");
System.out.println("出拳规则:1:剪刀,2:石头,3:布");
System.out.println("please input the name of you");
Scanner scanner_P_A = new Scanner(System.in);
player_A = scanner_P_A.nextLine();
System.out.println("请选择对手:1:刘备,2:孙权,3:曹操");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
judge((i%3));
System.out.println("你选择了" + player_B);
do {
System.out.println("start or not?(y/n)");
Scanner scanner1 = new Scanner(System.in);
String start = scanner1.nextLine();
if (start.equals("y")) {
number++;
startGame();
} else if (start.equals("n")) {
colTheEnd(number, i);
break;
}
} while (true);
}
public void startGame() {
System.out.println("请出拳:1:剪刀,2:石头,3:布");
Scanner scanner = new Scanner(System.in);
int sc = scanner.nextInt();
User.magic((sc%3));
int and = Computer.magic() % 3;
if (sc % 3 < and) {
System.out.println("you win");
player_a_grade++;
} else if (sc % 3 == and) {
} else {
System.out.println("you lost");
player_b_grade++;
}
}
public void colTheEnd(int number, int i) {
System.out.println(player_B + " VS " + player_A);
System.out.println("对战次数:" + number);
showTheEnd();
}
public void showTheEnd() {
System.out.println("姓名" + "\t" + "得分");
System.out.println(player_A + " " + player_a_grade);
System.out.println(player_B + " " + player_b_grade);
System.out.println(player_a_grade>=player_b_grade?"you win":"you lost");
}
public static void main(String[] args) {
Game game = new Game();
game.initial();
}
}
class User {
private String name;
private int grage;
public static void magic(int i) {
switch (i) {
case 1:
System.out.println("剪刀");
break;
case 2:
System.out.println("布");
break;
case 0:
System.out.println("石头");
}
}
}
class Computer {
private String name;
private int grade;
public static int magic() {
Random random = new Random();
int pc = random.nextInt(3);
switch (pc + 1) {
case 1:
System.out.println("剪刀");
break;
case 2:
System.out.println("布");
break;
case 3:
System.out.println("石头");
}
return (pc + 1);
}
}