⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beetlegame.java

📁 Beetle游戏的java实现。支持二个玩家。掷骰子按规则游戏
💻 JAVA
字号:
/*Beetle游戏的java实现
  作者:viMory
  时间:2008.5.3
  游戏规则:参与人数1人以上,谁的甲虫最先完整谁就胜出。初始的甲虫不具有任何器官,而完整的甲虫具有躯干、头部、6条腿、两只眼、两只触角、一条尾巴。 
玩法:轮你掷骰子时,按如下规定采取动作: 
1:如果甲虫已有一处躯干,传骰子给下一位。否则,添加一个躯干,并再掷一次。 
2:如果甲虫已有头部或没有躯干,传骰子。否则,添加一个头部,并再掷一次。 
3:如果甲虫已有6条腿或没有躯干,传骰子,否则,添加两条腿,并再掷一次。 
4:如果甲虫已有两只眼或没有头部,传骰子。否则,添加一只眼睛,并再掷一次。 
5:如果甲虫已有两根触角或没有头部,传骰子,否则,添加一根触角,并再掷一次。 
6:如果甲虫已有一条尾巴或没有躯干,传骰子。否则,添加一条尾巴,并再掷一次。

*/
class Beetle {
	  private boolean body;
	  private int eyes;
	  private int feelers;
	  private boolean head;
	  private int legs;
	  private boolean tail;
	  
	  public Beetle() {
			body = false;
			eyes = 0;
			feelers = 0;
			head = false;
			legs = 0;
			tail = false;
		}
		
		public boolean addBody() {
			if(body) {
				return false;	
			}	
			else {
				body = true;
				return true;	
			}
		}
		
		public boolean addEyes() {
			if(head&&(eyes<2)) {
				eyes++;
				return true;	
			}	
			else {
				return false;
			}
		}
		
		public boolean addFeelers() {
			if(head && (feelers<2)) {
				feelers++;
				return true;	
			}	
			else {
				return false;	
			}
		}
		
		public boolean addHead() {
			if(body && !head) {
				head = true;
				return true;	
			}	
			else {
				return false;	
			}
		}
		
		public boolean addLegs() {
			if(body && (legs<6)) {
				legs++;
				return true;	
			}	
			else {
				return false;	
			}
		}
		
		public boolean addTail() {
			if(body && !tail) {
				tail = true;
				return true;	
			}	
			else {
				return false;	
			}
		}
		
		public boolean isComplete() {
			return body && (eyes == 2) && (feelers == 2) && head
			       && (legs == 6) && tail;	
		}
		
		public String toString() {
			if(body) {
				String result = "";
				if(feelers>0) {
					result += "\\";
					if(feelers == 2) {
						result += " /";	
					}	
					result += "\n";
				}	
			  if(head) {
			  	if(eyes>0) {
			  		result += "o";
			  	}	
			  	else {
			  		result += " ";	
			  	}
			  	result +="O";
			  	if(eyes == 2) {
			  		result += "o";	
			  	}
			  	result +="\n";
			  }	
			  if(legs>0) {
			  	result +="-";	
			  }
			  else {
			  	result +=" ";	
			  }
			    result +="#";
			  
			  if(legs>1) {
			  	result +="-";	
			  }
			    result += "\n";
			  if(legs>2) {
			  	result +="-";	
			  }
			  else {
			  	result +=" ";	
			  }
		   	  result +="#";
			  
			  if(legs>3) {
			  	result +="-";	
			  }
			  result += "\n";
			  if(legs>4) {
			  	result +="-";	
			  }
			  else {
			  	result +=" ";	
			  }
			    result +="#";
			  
			  if(legs>5) {
			  	result += "-";	
			  }
			  if(tail) {
			  	result += "\n v";
        }
			  return result;
			}	
			else {
				return "No Parts Yet";	
			}
		}
}

class Die {
    private int topFace;
    
    public Die() {
    	this.topFace = 1;	
    }
    
    public int getTopFace() {
    	return this.topFace;	
    }
    
    public void setTopFace(int topFace) {
    	this.topFace = topFace;
    }
    
    public void roll() {
    	this.topFace = ((int)(Math.random()*6))+1;
    }
}

class BeetleGame {
	  public static final java.util.Scanner INPUT
	  	= new java.util.Scanner(System.in);
	  	
	  private Beetle bug1;
	  private Beetle bug2;
	  private Die die;
	  
	  public BeetleGame() {
	  	bug1 = new Beetle();
	  	bug2 = new Beetle();
	  	die = new Die();	
	  }
	  
	  public boolean takeTurn(int player,Beetle bug) {
	  	System.out.println("\n现在拿骰子的是玩家"+player+",你目前的甲虫情况是::");
	  	System.out.println(bug);
	  	System.out.println("按回车键掷骰子:");
	  	INPUT.nextLine();
	  	die.roll();
	  	System.out.print("You rolled a"+die.getTopFace());
	  	switch(die.getTopFace()) {
	  		case 1:
	  			System.out.println("(body)");
	  			return bug.addBody();	
	  	  case 2:
	  			System.out.println("(head)");
	  			return bug.addHead();	
	  		case 3:
	  			System.out.println("(legs)");
	  			return bug.addLegs();	
	  		case 4:
	  			System.out.println("(eyes)");
	  			return bug.addEyes();	
	  		case 5:
	  			System.out.println("(feelers)");
	  			return bug.addFeelers();	
	  		default:
	  			System.out.println("(tail)");
	  			return bug.addTail();	
	  	}	
	  }
	  
	  public void play() {
	  	int player = 1;
	  	Beetle bug = bug1;
	  	while(!(bug.isComplete())) {
	  		if(!(takeTurn(player,bug))) {
	  			if(player == 1) {
	  				player =2;
	  				bug = bug2;	
	  			}	
	  			else {
	  				player = 1;
	  				bug = bug1;	
	  			}
	  		}	
	  	}	
	  System.out.println("\n玩家"+player+"获得胜利!");
	  System.out.println(bug);
	  }
	  
	  public static void main(String[] args) {
	  	System.out.println("欢迎玩甲虫游戏!");
	  	BeetleGame game = new BeetleGame();	
	  	game.play();
	  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -