java基础作业:猜拳游戏(面向对象思想)

最近上了点Java的课程,有个作业是以面向对象的思想写个猜拳的游戏,代码有些辣鸡,请多包涵

效果:
java基础作业:猜拳游戏(面向对象思想)_第1张图片
有些功能我懒得添加:
1.电脑类的名字我写死= =

电脑类:

package fingGuess;
import java.util.Random;

public class Computer {
public String cname = "人工智障";
private int Score = 0;
private String[]type;	

public Computer() {
	type=new String[] {"拳头","剪刀","布"};
}

public int showFist() {//出拳的方法
	Random rd = new Random();
	int fistType = rd.nextInt(3);
	System.out.println(cname + "出拳为:"+type[fistType]);
	return fistType;
}

public void setScore(int s) {
	Score = s;
}

public int getScore() {
	return Score;
}
}

玩家类:

package fingGuess;
import java.util.Scanner;
public class Person {
private String name;
private int Score;
private String[] type;
public Person() {
	type = new String[] { "拳头", "剪刀", "布" };
}
public int showFist() {
	Scanner sc = new Scanner(System.in);
	System.out.println("请选择0.拳头 1.剪 2.布");
	int fistType = sc.nextInt();
	System.out.println(name + "出拳为:" + type[fistType]);
	return fistType;
}
public void setName(String name) {
	this.name = name;
}
public String getName() {
	return name;
}
public void setScore(int s) {
	Score = s;
}
public int getScore() {
	return Score;
}
}

裁判类:

package fingGuess;
import java.util.Scanner;
public class Referer {
private Computer ai;
private Person per;
private int count;//记录总局数

public void init() {
Scanner sc = new Scanner(System.in);
 per = new Person();
 ai = new Computer();
 System.out.println("请输入姓名:");
 String name =sc.nextLine();
 per.setName(name);
}

public void playing() {// 0=拳头,1=剪刀,2=布
	boolean flag = true;
	while (flag) {
		System.out.println("请"+per.getName()+"出拳:");
		int p = per.showFist();
		int c = ai.showFist();
		
		if((p==0&&c==1)||(p==1&&c==2)||(p==2&&c==0)) {
			System.out.println("**********"+per.getName()+"赢了**********");
			per.setScore(per.getScore()+1);
			ai.setScore(ai.getScore()-1);
		}
		else if(p==c) {
			System.out.println("平局鸭~");
		}
		else {
			System.out.println("**********"+ai.cname+"赢了**********");
			per.setScore(per.getScore()-1);
			ai.setScore(ai.getScore()+1);
		}
		
		count++;
		Scanner sc = new Scanner(System.in);
		System.out.println("是否进行下一局游戏?(Y/N)");
		String answer = sc.next();
		if (answer.equalsIgnoreCase("N"))//equalsIgnoreCase可以忽略大小写
			flag = false;
	}
}

public void scorePanel() {//游戏结束的分数面板
	System.out.println("游戏共进行" + count + "局");
	System.out.println(per.getName()+"共获得了:"+per.getScore()+"分");
	System.out.println(ai.cname+"共获得了:"+ai.getScore()+"分");
}
}

测试类:

package fingGuess;
public class fingGuessTest {
public static void main(String[] args) {

	Referer re = new Referer();

	re.init();
	re.playing();
	re.scorePanel();
}
}

你可能感兴趣的:(总结)