📄 history.htm
字号:
for( int y = 0; y < m_Grid.Width; ++y ) {<br>
//initial Ant position<br>
if( x == m_Position.X && y == m_Position.Y ) {<br>
gBmp.FillRectangle( Brushes.Crimson, x*m_CellDim +
1, y*m_CellDim + 1, m_CellDim,</p>
<p style="margin-top: 0; margin-bottom: 0">
m_CellDim );<br>
}<br>
</p>
<p style="margin-top: 0; margin-bottom: 0">
//food<br>
else if( m_Grid[x,y] == 1 ) {<br>
gBmp.FillEllipse( Brushes.Wheat, x*m_CellDim + 1, y*m_CellDim
+ 1, m_CellDim,</p>
<p style="margin-top: 0; margin-bottom: 0">
m_CellDim );</p>
<p style="margin-top: 0; margin-bottom: 0">
}<br>
<br>
//horizontal lines<br>
if( x == 0 && y != 0 ) gBmp.DrawLine( Pens.Black, 0,
y*m_CellDim + 1, dim, y*m_CellDim +</p>
<p style="margin-top: 0; margin-bottom: 0">
1 );<br>
}</p>
<p style="margin-top: 0; margin-bottom: 0">
//vertical lines<br>
if( x != 0 ) gBmp.DrawLine( Pens.Black, x*m_CellDim
+ 1, 0, x*m_CellDim + 1, dim );</p>
<p style="margin-top: 0; margin-bottom: 0">
}<br>
}<br>
}<br>
//otherwise, draw next move<br>
else if( m_MovesIndex < m_FinishedMoves.Length ) {<br>
using( Graphics gBmp =
Graphics.FromImage( m_bmp ) ) {<br>
//draw over
old Ant position<br>
int addX = (m_Position.X
== 0 ? 1 : 2);<br>
int addY = (m_Position.Y
== 0 ? 1 : 2);<br>
// int cellX
= addX - 1;<br>
// int cellY
= addY - 1;<br>
// if(
m_Position.X > 0 ) {<br>
// ++addX;<br>
// if(
m_Position.X + 1 == m_Position.Width ) {<br>
// ++addX;<br>
// }<br>
// }<br>
// if(
m_Position.Y > 0 ) {<br>
// ++addY;<br>
// if(
m_Position.Y + 1 == m_Position.Height ) {<br>
// ++addY;<br>
// }<br>
// }<br>
gBmp.FillRectangle( Brushes.CornflowerBlue,
m_Position.X*m_CellDim + addX,</p>
<p style="margin-top: 0; margin-bottom: 0">
m_Position.Y*m_CellDim + addY, m_CellDim - (addX -
1), m_CellDim - (addY - 1) );<br>
<br>
//move
appropriately and draw new Ant position<br>
switch(
m_FinishedMoves[m_MovesIndex] ) {<br>
case Direction.Left: --m_Position.X; break;<br>
case Direction.Right: ++m_Position.X; break;<br>
case Direction.Up: --m_Position.Y; break;<br>
case Direction.Down: ++m_Position.Y; break;<br>
}<br>
addX = (m_Position.X
== 0 ? 1 : 2);<br>
addY = (m_Position.Y
== 0 ? 1 : 2);<br>
gBmp.FillRectangle( Brushes.Crimson, m_Position.X*m_CellDim
+ addX,</p>
<p style="margin-top: 0; margin-bottom: 0">
m_Position.Y*m_CellDim + addY, m_CellDim - (addX -
1), m_CellDim - (addY - 1) );<br>
}<br>
}<br>
<br>
//draw bitmap on graphics object, scaled to current size<br>
SizeF sizef = new SizeF( m_bmp.Width/m_bmp.HorizontalResolution,
</p>
<p style="margin-top: 0; margin-bottom: 0">
m_bmp.Height/m_bmp.VerticalResolution );<br>
float scale = Math.Min( g.ClipBounds.Width/sizef.Width,
g.ClipBounds.Height/sizef.Height );<br>
sizef.Width *= scale;<br>
sizef.Height *= scale;<br>
g.DrawImage( m_bmp, 0, 0, sizef.Width, sizef.Height );<br>
<br>
++m_MovesIndex;<br>
return m_MovesIndex < m_FinishedMoves.Length;<br>
}</td>
</tr>
</table>
<p style="margin-top: 0; margin-bottom: 0"> </p>
<p style="margin-top: 0; margin-bottom: 0" align="left">
<font face="Arial">The Reset method returns the graphical
instance to the first frame. In other words, it resets
the picture to its initial state.</font></p>
<p style="margin-top: 0; margin-bottom: 0" align="left"> </p>
<div align="center">
<table border="0" width="90%" id="table8">
<tr>
<td><font face="Courier New" size="2">public
void Reset() {<br>
m_MovesIndex = -1;<br>
m_Grid.Reset();<br>
m_Position = null;<br>
<br>
if( m_bmp != null ) m_bmp.Dispose();<br>
m_bmp = null;<br>
}</font></td>
</tr>
</table>
<p align="left"><font face="Arial">The Artificial Ant's
Finish method is called when the <span style="font-weight: 400">
IIndividual </span>is finished
being tested. This prevents more moves from being
made by the Ant and converts the moves into an array of
movement directions.</font></p>
<div align="center">
<table border="0" width="90%" id="table9">
<tr>
<td><font face="Courier New" size="2">public
void Finish() {<br>
m_FinishedMoves = new Direction[m_Moves.Count];<br>
m_Moves.CopyTo( m_FinishedMoves );<br>
m_Moves = null;<br>
}</font></td>
</tr>
</table>
<p align="left"><font face="Arial">Finally, the
direction enumeration specifies the directions the
Ant can move.</font></p>
<div align="center">
<table border="0" width="90%" id="table10">
<tr>
<td><font face="Courier New" size="2">
public enum Direction {<br>
/// <summary>Move up in the grid.</summary><br>
Up = 0, <br>
/// <summary>Move to the right in the grid.</summary><br>
Right, <br>
/// <summary>Move down in the grid.</summary><br>
Down, <br>
/// <summary>Move to the left in the grid.</summary><br>
Left, <br>
};</font></td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<h4 class="dtH4"><font face="Arial">See Also</font></h4>
<p><font face="Arial"><a href="Environment.htm">IEnvironment</a> |
<a href="Individual.htm">IIndividual</a></font></p>
<hr>
<div id="footer">
<font face="Arial">
<a href="http://www.opensource.org/" target="_top">
<img style="padding-bottom: 5px" src="http://opensource.org/trademarks/open_source_button.png" align="left" border="0"></a>
This software licensed under the
<a href="http://opensource.org/licenses/afl-2.1.php" target="_top">Academic
Free License version 2.1</a>. (<a href="../../afl-2.1.txt">Plain
text</a>)</font></div>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -