none
SmallBasic chessboard code RRS feed

  • Question

  • Hi

    I am just looking at SmallBasic and my helloworld program equivalent is a chessboard!

    So I have an 8x8 board but all of the same color with my code.

    How do I get alternating square colors?

    rows = 8
    columns = 8
    size = 40
    For r = 1 To rows
    For c = 1 To columns
    GraphicsWindow.BrushColor = "red"
    boxes[r][c] = Shapes.AddRectangle(size, size)
    Shapes.Move(boxes[r][c], c * size, r * size)
    EndFor
    EndFor

    I guess it is some sort of nested for loop with loop counter as in java/c etc? But what?

    Grateful for helpful replies-thanks


    My blog - http://www.richard-dickinson.com

    Wednesday, August 1, 2012 9:37 PM

Answers

  • hi - I'm very much a novice so my solution may be a little clumsy but it does work and borrows your code for drawing the chessboard.

    rows = 8
     columns = 8
     size = 40
     For r = 1 To rows
       For c = 1 To columns
         row_remainder = Math.Remainder(r,2)
         col_remainder = Math.Remainder(c,2)
         If row_remainder = col_remainder then
           GraphicsWindow.BrushColor = "red"
         Else
           GraphicsWindow.BrushColor = "gold"
           EndIf
     boxes[r][c] = Shapes.AddRectangle(size, size)
     Shapes.Move(boxes[r][c], c * size, r * size)
     EndFor
     EndFor

    cheers - Paul

    slightly improved i think - someone needs to stop me from writing subs  if it's bad... part of me wanted to have colours as an array...but i'm not sure if that would have helped :)

    rows = 8
     columns = 8
     size = 40
     colour1 = "red"
     colour2 = "gold"
     For r = 1 To rows
       For c = 1 To columns
    square_colours()
     boxes[r][c] = Shapes.AddRectangle(size, size)
     Shapes.Move(boxes[r][c], c * size, r * size)
     EndFor
    EndFor
    
    'subroutine to colour the squares on the chessboard
    
    Sub square_colours
         row_remainder = Math.Remainder(r,2)
         col_remainder = Math.Remainder(c,2)
         If row_remainder = col_remainder then
           GraphicsWindow.BrushColor = colour1
         Else
           GraphicsWindow.BrushColor = colour2
         EndIf
       endsub


    Wednesday, August 1, 2012 10:33 PM

All replies

  • hi - I'm very much a novice so my solution may be a little clumsy but it does work and borrows your code for drawing the chessboard.

    rows = 8
     columns = 8
     size = 40
     For r = 1 To rows
       For c = 1 To columns
         row_remainder = Math.Remainder(r,2)
         col_remainder = Math.Remainder(c,2)
         If row_remainder = col_remainder then
           GraphicsWindow.BrushColor = "red"
         Else
           GraphicsWindow.BrushColor = "gold"
           EndIf
     boxes[r][c] = Shapes.AddRectangle(size, size)
     Shapes.Move(boxes[r][c], c * size, r * size)
     EndFor
     EndFor

    cheers - Paul

    slightly improved i think - someone needs to stop me from writing subs  if it's bad... part of me wanted to have colours as an array...but i'm not sure if that would have helped :)

    rows = 8
     columns = 8
     size = 40
     colour1 = "red"
     colour2 = "gold"
     For r = 1 To rows
       For c = 1 To columns
    square_colours()
     boxes[r][c] = Shapes.AddRectangle(size, size)
     Shapes.Move(boxes[r][c], c * size, r * size)
     EndFor
    EndFor
    
    'subroutine to colour the squares on the chessboard
    
    Sub square_colours
         row_remainder = Math.Remainder(r,2)
         col_remainder = Math.Remainder(c,2)
         If row_remainder = col_remainder then
           GraphicsWindow.BrushColor = colour1
         Else
           GraphicsWindow.BrushColor = colour2
         EndIf
       endsub


    Wednesday, August 1, 2012 10:33 PM
  • Hi Paul

    Many thanks for your prompt & very helpful reply.

    I like your code model/logic. As at the moment, I know nothing of Basic or even SmallBasic, then I cannot comment on code mechanics/methods (other than general principles)?!

    As always I guess there are many ways to code even basic programs!

    I am intrigued by Basic (here SmallBasic) now. I am more expert (experienced!) in web coding & design but along the way have dabbled with many languages eg Java, Perl, C, C++, C# and libraries such as GTK/GTK+/ FLTK/ Gambas and very very recently (last couple of days) QT & MS VisualBasic in an attempt to create some GUI & exe programs (on Windows!).

    This first stab at Basic for me is interesting- I think I can see the prospect of reasonable results without getting too bogged down with technicalities (installing SDK's IDE's etc etc).

    Would you think it worth my while to look more at Basic/SmallBasic or just take the bull by the horns & learn VisualBasic?

    I think I probably ought to look more at VisualBasic (& even other .Net languages eg VisualC++). I'll see!

    Thanks & best wishes


    My blog - http://www.richard-dickinson.com


    • Edited by rpd Thursday, August 2, 2012 12:17 AM
    Thursday, August 2, 2012 12:13 AM