In the program below, the hands are randomly displayed, but change them a little.
Paper, rock, scissors, paper, rock, scissors...
How can I get my hands on regularity as shown in ?
**Regular rock-paper-scissors computer player*/
public class NormalComputerPlayer extensionsPlayer{
protected Hand goo, choki, par;
protected java.util.Random random;// random number used to make a decision
/** constructor, name of player*/
publicNormalComputerPlayer(String name){
super(name);
// set one's hands
goo=new Goo();
choki=new Choki();
par = new Par();
random = new java.util.Random();
}
/** touch the num player of the match
* Override Player showHand
*/
public Hand showHand(int match, int num) {
int hand = random.nextInt(3);
if(hand==0){
return goo;
} else if(hand==1){
return choki;
} else{
return par;
}
}
}
public abstract class Player {
protected String name;// Name of the player
protected int matches; // matches (the winner wins first matches)
protected int wins; // player wins
protected int losses;// Number of player losses
/** constructor, name of player*/
public Player (String name) {
this.name = name; // Name of the player
wins=losts=0;// Initialize win/loss
}
/** Returns the name of the player*/
public String getName() {
return name;
}
/** a method of determining how many games to play
*/
public void setMatches(int matches) {
this.matches = matches;
}
/**
* method of being informed that someone has won
*/
public void youWon() {
wins++;
}
/**
* method of being informed of a defeat
*/
public void youLost() {
losts++;
}
/** touch the num player of the match
* It's an abstract method, so it overrides in subclasses.
*/
public abstract Hand showHand (int match, int num);
}
To display a particular number of pattern arrays repeatedly, the use of remainder calculations is a common solution.
For example, a continuous display of days of the week is a regular pattern that may also be useful for third parties.
To display consecutive days of the week, have seven arrays: Month, Tuesday, ... Day, followed by Month.
This is just a 7 array of paper, goo, and scissors, so if you know how it works, you can apply it to rock-paper-scissors games.
In the sample code below, i
can be obtained by looping the remainder from 0 to 6 by i%7
in the for statement incrementing 100 times.
This allows you to display the array "Mon", "Tuesday", ..., followed by "Day" and "Month".
0%7==0
1%7==1
2%7==2
...
7%7==0
8%7==1
9%7==2
sample code
public class Test{
public static void main(String[]args) {
String dayOfWeeks="Monday, Tuesday, Wednesday, Thursday, Friday, Saturday;
for(inti=0;i<100;i++){
// Find 0, 1, 2, 3, 4, 5, 6, 0, 1, 2...
int joyo=i%7;
// Display char array patterns for dayOfWeeks
System.out.println (dayOfWeeks.charAt(joyo));
}
}
}
The code you asked can also be solved by replacing the "random hand" part with the remaining code.
The sample code uses CharSequence#charAt
to arrange each character, but if you want to convert it to rock-paper-scissors games, you can implement it in any way you like, whether it's an array or a switch statement.
© 2024 OneMinuteCode. All rights reserved.