猜拳游戏项目
项目功能点:
1猜拳游戏,每一把游戏进行三局,可以玩多把,每一局赢者积一分,输者不扣分,平局不计分,三局决胜负。
2两人进行猜拳游戏。可以随机选取要比赛的对手。
考察知识点:
Java类和对象
设计思路:
1有两方,甲方:玩家(Player) 乙方:电脑(Computer)模拟 进行猜拳游戏
2 猜拳开始:赢者积一分,平局不计分,输者不扣分。每次三局决胜负
3 每一把游戏决定最终的胜负,继续下一轮游戏的开始。
代码展示:
package com.finger;
import java.util.Scanner;
/**
* 描述:开始游戏
*
* @Author administrator{GINO ZHANG}
* @Date2018/12/13
*/
public class Game {
private Computer computer;
private Player player;
Game(){
Scanner scanner = new Scanner(System.in);
System.out.println("zs lisi ww");
String name = scanner.next();
computer = new Computer(name);
player = new Player("lll");
}
public static void main(String[] args) {
}{
System.out.println(player.getName() +" VS "+computer.getName());
Scanner scanner = new Scanner(System.in);
int fistPlayer;
int fistComputer;
while(true){
int count = 0;
while(count<3) {
System.out.println("第"+(count+1)+"次出拳");
fistPlayer = player.showFist();
fistComputer = computer.showFist();
count++;
if (fistComputer == Constant.STONE && fistPlayer == Constant.CLOTH || fistComputer == Constant.SCISSOR && fistPlayer == Constant.STONE || fistComputer == Constant.CLOTH && fistPlayer == Constant.SCISSOR) {//player赢
player.addScore(1);
}
if (fistComputer == Constant.CLOTH && fistPlayer == Constant.STONE|| fistComputer == Constant.STONE && fistPlayer == Constant.SCISSOR|| fistComputer == Constant.SCISSOR && fistPlayer == Constant.CLOTH) {//赢
computer.addScore(1);
}
}
int playerScore = player.getScore();
int computerScore = computer.getScore();
System.out.println("最终结果:"+player.getName()+playerScore+" : "+computer.getName()+computerScore);
if(playerScore>computerScore){
System.out.println(player.getName() + "获胜");
}else if(playerScore == computerScore){
System.out.println("平局");
}else{
System.out.println(computer.getName() + "获胜");
}
System.out.println("是否继续下一轮猜拳 是or否");
if(scanner.next().equals("是")){
continue;
}else{
break;
}
}
}
}
package com.finger;
import java.util.Scanner;
/**
* 描述:玩家类
*
* @Author administrator{GINO ZHANG}
* @Date2018/12/13
*/
public class Player {
private String name;
private int score;
public Player(String name){
score = 0;
this.name = name;
}
public int showFist(){//玩家进行出拳操作
System.out.println("请出拳:1.石头,2.剪刀,3.布");
Scanner scanner = new Scanner(System.in);
int f = scanner.nextInt();
return f;
}
public String getName(){
return name;
}
public void addScore(int n){//赢者积一分
score += n;
}
public int getScore(){
return score;
}
}
package com.finger;
import java.util.Random;
/**
* 描述:电脑类
*
* @Author administrator{GINO ZHANG}
* @Date2018/12/13
*/
public class Computer {
private String name;
private int score;
public Computer(String name){
this.score = 0;
this.name = name;
}
public int showFist(){//随机出拳
Random random = new Random();
int fist = random.nextInt(3)+1;
return fist;
}
public String getName(){
return name;
}
/**
* 记录成绩
* @param n
*/
public void addScore(int n){
score += n;
}
public int getScore(){
return score;
}
}
package com.finger;
/**
* 描述:常量类
*
* @Author administrator{GINO ZHANG}
* @Date2018/12/13
*/
public interface Constant {
public static final int STONE = 1;
public static final int SCISSOR = 2;
public static final int CLOTH = 3;
}