challenge #6 display a chess board
I do not know but i know dear reader that you have seen a programming problem like this one ;
*** ***
***
*** ***
The program displays something in the form of a chess board where the * are the black boxes and the spaces are the white boxes.
To solve this using Ruby, we need two loops. One for the outer loop and other loop for the inner loop. Horizontally, number the boxes as number in increasing order. We achieve that by adding the variables of the two loops. If the result of the addition divided by 2 gives us 0 then the loop we print spaces to represent a white space else we print *.
In the code, the second loop is used duplicate the print out. Remove it and see how it work.
1 outer = ARGV[0].to_i
2 inner = ARGV[1].to_i
3 outer.times do |j|
4 3.times do
5 1.upto(inner) do |i|
6 if (j+i)%2 == 0
7 print “ “
8 else
9 print “****”
10 end
11 end
12 puts
13 end
14 end
About this entry
You’re currently reading “challenge #6 display a chess board,” an entry on Connecting the dots
- Published:
- 3.20.08 / 5pm
- Category:
- challenges
No comments
Jump to comment form | comments rss [?] | trackback uri [?]