Skip to content

Instantly share code, notes, and snippets.

@ddmitov
Last active May 28, 2020 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddmitov/c2967bd38778b5b807d0b6be78665c00 to your computer and use it in GitHub Desktop.
Save ddmitov/c2967bd38778b5b807d0b6be78665c00 to your computer and use it in GitHub Desktop.
Implementation of the famous Python Turtle Chessboard task
#!/usr/bin/python3
# Turtle Chessboard v.1.0
# by Dimitar D. Mitov
# https://github.com/ddmitov
# This is free and unencumbered software released into the public domain.
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# For more information, please refer to <http://unlicense.org/>
# Homework for the 'Introduction to Python' course by
# Software Academy (https://softwareacademy.bg/)
# 2020-05-27 - 2020-06-17
from turtle import Turtle, Screen
def make_cell(painter, row, column, cell_side):
if (row % 2) > 0 and (column % 2) == 0:
painter.begin_fill()
if (row % 2) == 0 and (column % 2) > 0:
painter.begin_fill()
side = 0
while side < 4:
side += 1
painter.forward(cell_side)
painter.right(90)
if (row % 2) > 0 and (column % 2) == 0:
painter.end_fill()
if (row % 2) == 0 and (column % 2) > 0:
painter.end_fill()
return True
def go_to_next_cell(painter, cell_side):
painter.forward(cell_side)
return True
def make_row(painter, row, columns, cell_side):
for column in range(columns):
real_column = column + 1
make_cell(painter, row, real_column, cell_side)
go_to_next_cell(painter, cell_side)
go_to_next_row(painter, columns, cell_side)
return True
def go_to_next_row(painter, columns, cell_side):
painter.right(180)
painter.forward(columns * cell_side)
painter.left(90)
painter.forward(cell_side)
painter.left(90)
return True
def make_table(painter, rows, columns, cell_side):
for row in range(rows):
real_row = row + 1
make_row(painter, real_row, columns, cell_side)
return True
def main():
screen = Screen()
turtle = Turtle()
turtle.penup()
turtle.goto(-350, 300)
turtle.pendown()
make_table(turtle, 8, 8, 50)
screen.exitonclick()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment