Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //write a function printTriangle, accepts
- //a height as parameter.
- //assuming equilateral triangles
- private function printTriangle(height:uint):void
- {
- if (height == 0)
- {
- trace('Nothin');
- return;
- }
- var width:uint = (height - 1) * 2 + 1; //height - 1 because height of 0 is a single x's.
- var outStr:String = "";
- for (var h:uint = 0; h < height; ++h)
- {
- //a line consist of h*2 + 1 number of x's because one x for center, then h number of x's on each side.
- //Rest of space is left for O's. We have Width number spots, take away number of x's we have the o's.
- //write 0s, then write xs, then write rest of 0s.
- var numOfXs:uint = h * 2 + 1;
- var numOfOsHalf:uint = (width - numOfXs) / 2;
- //draw symbol o's
- outStr += drawSymbol('O', numOfOsHalf);
- outStr += drawSymbol('X', numOfXs);
- outStr += drawSymbol('O', numOfOsHalf);
- outStr += "\n";
- }
- trace(outStr);
- function drawSymbol(symbol:String, count:uint):String
- {
- var oStr:String = new String();
- for (var c:uint = 0; c < count; ++c)
- {
- oStr += symbol;
- }
- return oStr;
- }
- }
Add Comment
Please, Sign In to add comment