GAMES TWO: LESSON Thirteen: Cantris: Color Rotation
It would be nice to be able to re-order the color cells in the individual
cans. We will use the "r" key for rotation of colors, "s" to scramble
colors, and "m" to mutate colors. The can will begin
falling in a horizontal orientation so that flipping the can is not
necessary. Try this game and make sure you can figure out how rotation,
mutation, and scrambling work.
Let's examine how mutation works in this game. First of all you should
notice that a can when it is originally spawned cannot have any repeated
colors due to the use of this do loop in the spawn method.
do{
for(int i = 0; i
But during mutation colors can be repeated since there is no code to prevent
repetition of colors. Most of the work for mutation is done in the
keyPressed method in the following lines:
else if(k.getKeyCode() == KeyEvent.VK_M){
int i = Math.abs(r.nextInt()%sel.length);
can.mutate(sel[i], i%3);
}
The mutate method in CAN2 looks like this:
public void mutate(Color c, int i){
cr[i]=c;
}
The rotate and scramble methods look like this:
public void rotate(){
Color hold = cr[0];
cr[0]=cr[1];
cr[1]=cr[2];
cr[2]=hold;
}
public void scramble(){
Color hold = cr[1];
cr[1]=cr[0];
cr[0]=cr[2];
cr[2]=hold;
}
The score method looks like this:
public int score(Color s1,Color s2,Color s3){
int retval=0;
if(s1.equals(cr[0])) retval++;
if(s2.equals(cr[1])) retval++;
if(s3.equals(cr[2])) retval++;
return retval;
}
Notice that the score method takes three color arguments and that it returns
an integer value.
There are several other small accomodations which had to be made for this
updated version of Cantris, but the changes discussed are the most major
changes which were made.
ASSIGNMENT: