Today, my senior brother gave me this question: Use as3 to write it ~ I am familiar with it ~~
There is a 27 cm fine wood pole, each of which has an ant in the five positions 3 cm, 7 cm, 11 cm, 17 cm, 23 cm. The wooden pole is very small and cannot pass through an ant at the same time. At the beginning, the head of the ant financial system is either left or right. They only move forward or turn their heads, but do not move back. When any two ants meet, they both turn their heads to the opposite direction. It is assumed that the ant Financial can walk 1 cm away every second. Write a program to find the minimum and maximum time for all ants to leave the wooden pole.
First, we will write an ant class:
Package {
Import flash. Events .*;
Import flash. display. movieclip;
Public class ant extends movieclip {
Private var totallength: Int = 27; // a total of 27
Public var speedpersecond: Int = 1; // speed of movement
Private var initposition: int; // initialization position
Public var curposition: int; // Current Position
Public var isdone: Boolean = false; // whether to exit
Public Function ant (position: INT) {// Initialization
Init (position );
}
Public Function set direct (value: INT): void {// set to set the direction of movement
This. speedpersecond = value;
}
Public Function get direct (): int {// get or move direction
Return this. speedpersecond;
}
Public Function reset (): void {// reset the initial position of the ant financial, and set it as not going out
Isdone = false;
This. curposition = initposition;
}
Private function Init (position: INT): void {// Initialization
This. curposition = position;
This. initposition = position;
}
Public Function forword (): void {// move
Curposition + = speedpersecond;
If (curposition = 0 | curposition = totallength) {// if it is 0 or 27
Isdone = true; // The setting is out.
}
}
}
}
Then we write a control class that controls ant Mobile:
Package {
Import flash. display. Sprite;
Import flash. Events .*;
Public class controler extends sprite {
Private var antgroup: array = new array (); // store the ant financial
Private var positiongroup: array = new array (); // stores 32 mobile methods, such as: 00100
Private var totalpositions: int; // The total number of moving methods recorded
Private var curposition: Int = 0; // The number of moving methods currently
Private var curpositionstr: string; // The current moving method, for example, 00100
Private var usedframe: Int = 0; // The number of frames it takes
Private var isdonenums: Int = 0; // Number of Records completed
Public Function controler (amount: INT) {// initialize the total number of ants
Totalpositions = math. Pow (2, amount); // The amount power of 2. Here is the 5 power of 2, that is, 32
Addants (); // Add an ant
Addpositions (amount); // Add different locations
}
Private function addants (): void {
Antgroup. push (New Ant (3), new ant (7), new ant (11), new ant (17), new ant (23 )); // 5 ants in different locations
}
Private function addpositions (amount: INT): void {// Add a different location. Here there are 32 types
For (var I: Int = 0; I <totalpositions; I ++ ){
VaR STR: String = I. tostring (2); // 0 is converted to a binary value of 0.
VaR temp: Int = 5-str.length; // Add a few zeros to the front
For (var k: Int = 0; k <temp; k ++ ){
STR = "0" + STR; // Add the first 0
}
Positiongroup. Push (STR); // Add it to the array
}
}
Public Function run (): void {// run
Nextposition (); // The next specific moving Method
Addeventlistener (event. enter_frame, moving); // Add event listening
}
Private function nextposition (): void {// next move Method
For (var I: Int = 0; I <5; I ++ ){
VaR temp: Int = parseint (positiongroup [curposition]. charat (I); // if it is 1, do not do it. Go to the right
If (temp = 0) {// if it is 0, it is-1 and left
Temp =-1;
}
Antgroup [I]. Direct = temp; // sets the direction for each ant to move
}
}
Private function testhit (): void {// test encounter
For (var I: Int = 0; I <5; I ++ ){
Switch (I ){
Case 0:
If (antgroup [I]. curposition = antgroup [I + 1]. curposition) {// The first and second
Antgroup [I]. speedpersecond * =-1;
Antgroup [I + 1]. speedpersecond * =-1;
}
Break;
Case 4:
If (antgroup [I]. curposition = antgroup [I-1]. curposition) {// last and last
Antgroup [I]. speedpersecond * =-1;
Antgroup [I-1]. speedpersecond * =-1;
}
Break;
Default:
If (antgroup [I]. curposition = antgroup [I + 1]. curposition) {// encounter
Antgroup [I]. speedpersecond * =-1;
Antgroup [I + 1]. speedpersecond * =-1;
}
If (antgroup [I]. curposition = antgroup [I-1]. curposition ){
Antgroup [I]. speedpersecond * =-1;
Antgroup [I-1]. speedpersecond * =-1;
}
Break;
}
}
}
Private function moving (Event: Event): void {// move
Usedframe ++; // Add one for each frame
For (var I: Int = 0; I <5; I ++ ){
If (antgroup [I]. isdone) {// skip if it is already out
Continue;
} Else {
Antgroup [I]. forword (); // otherwise move
}
If (antgroup [I]. isdone) {// After moving out, add 1 to the complete number.
Isdonenums ++;
}
}
Testhit (); // test encounter
If (isdonenums = 5) {// if the number of completions is 5, all are returned.
Trace (positiongroup [curposition] + ": Use frames" + usedframe); // output the specific moving method and time used
Curposition ++; // The next specific moving Method
Usedframe = 0; // set the time to 0
Isdonenums = 0; // The number of completed settings is 0.
If (curposition = totalpositions) {// if the current move is the last one, all the moving methods are tried.
Trace ("all done! "); // Move the output
Removeeventlistener (event. enter_frame, moving); // remove the listener
} Else {
Resetants (); // otherwise, reset the ant financial
Nextposition (); // sets the next specific moving method.
}
}
}
Private function resetants (): void {// reset ant financial
For (var I: Int = 0; I <5; I ++ ){
Antgroup [I]. Reset (); // reset each ant
}
}
}
}
Create a controler instance on Main. fla:
VaR controler: controler = new controler (5); // create 5 points
Controler. Run (); // start Operation