Java simplifies the implementation of rock-paper-scissors game

Written before the first game on rock paper scissors, the code is too large, post-maintenance difficult, complex logic (as detailed: https://www.cnblogs.com/1355861728--qqcom/p/11959565.html )

So, this time to write a version of the interface and method calls, easy to maintain and expand later.

First of all, before you write a program you must first understand the needs of the game:

1. Computer generation and people of all "rock, paper, scissors" any one of the results.

2. Comparison of computers and their respective "value", and outputs the result (value representing: rock, paper, scissors).


I began to write the code:

Demand [1]

/*

Person Computer Interface and the new interfaces, and wherein generating (random number value: 0 ~ 2) own random number:

*/

public interface Computer {
int computerIntResult = new Random().nextInt(3);
}

public interface Person {
int personIntResult = new Random().nextInt(3);
}
/ * 

1. Because computers and human-generated numbers, whereas our final output is "rock-paper-scissors" in Chinese characters, so we need to have a virtual int and String conversion process, that change class.
2. In the change created class changeShow methods, 
3. name value is used to distinguish the results of human and computer
4. changeNumber value for receiving the random number and the computer.
5. Create a
stringRandomNumber variable for virtual conversion result output, = null if the write cycle after the order can clear the previous value.
6. In the switch expression we changeNumber value (person computer or random number) is 0, the time when the pass over, stringRandomNumber values: scissors (1 and 2 is not explained). 
7. The final output: Person / Computer out are: scissors / rock / cloth
* /
public class Change {
public void changeShow(String name,int changeNumber){
String stringRandomNumber = null;
switch (changeNumber){
case 0:
stringRandomNumber="剪刀";
break;
case 1:
stringRandomNumber="布";
break;
case 2:
stringRandomNumber="石头";
break;
default:
break;
}
System.out.println (name + "that is:" + stringRandomNumber);
}

}

Demand [2]
/ *
1. Write a class Compare implement and Person Computer interface, this class is used to compare the results of the computer and the respective values.
2. Write a show method in class after convenient to call the test in the test class.
3. Write two output statements prompt the respective human and computer generated random number is how much.
4. The
call class Change inside changeShow method and a computer, respectively, and assign the random number to the person inside changeShow method changeNumber, to achieve the output of the random number characters.
The
random number and a computer human final result of the determination.
*/
public class Compare implements Computer,Person{
public static void show(){
System.out.println("电脑随机数为:"+computerIntResult);
System.out.println("人的随机数为:"+personIntResult);
Change change= new Change();
change.changeShow("Computer",computerIntResult);
change.changeShow("Person",personIntResult);
if(computerIntResult>personIntResult){
System.out.println("最终结果是:电脑获胜!");
}else if(computerIntResult==personIntResult){
System.out.println("最终结果是:平局!");
}else{
System.out.println("最终结果是:人获胜!");
}
}
}

/*
最后编写测试类TestMain直接调用Compare类中的show方法
*/
public class TestMain {
Compare compare = new Compare();
public static void main(String [] args){
Compare.show();
}
}
/*
输出结果(例举一种):
*/

  电脑随机数为:0
  人的随机数为:2
  Computer出的是:剪刀
  Person出的是:石头
  最终结果是:人获胜!

 

【以下为完整代码】

 

 

 

 

 

 

 

 

 

 











Guess you like

Origin www.cnblogs.com/1355861728--qqcom/p/12452637.html