Need help with a checkerboard algorythm - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Need help with a checkerboard algorythm (/thread-29941.html) |
Need help with a checkerboard algorythm - giladal - Sep-26-2020 Hey Guys, In the middle of a python course and unfortunately the course is off the air right now so I cant get help from the instructors. Hopefully I can get some help here. The chess() function gets a number in this case n that will determine the size of the canvas. For example: 8 x 8. THe function is supposed to color the pixels either black or white in order to create a checkerboard. As you can see, i tried using (x + y)%2 to determine what pixel to color but it doesnt seem to accept my answer. Plus if I show the image result it prints out very very small... Anyways, would appreciate some help to solve this: ### DO NOT MODIFY THIS CODE ### ### THIS CODE LETS YOU VIEW THE IMAGE YOU MAKE ### ### USING im.show() ### from PIL import Image import urllib import random def display_image(im): fn = str(random.randint(1, 500)) + ".bmp" im.save(fn) print("<img src=\"/resources/{}\">".format(fn)) print("") Image.Image.show = display_image ### YOU CAN MODIFY BELOW THIS LINE ### def chess(n): img = Image.new('L',(n,n),255) mat = img.load() for x in range(n): for y in range(n): if (x + y)%2: mat[x,y] = 0 else: mat[x,y] = 255 img.show() return img ### TESTS ### print("********************") print("Starting the test:") print("********************") print("Checking for n = 2") ans = chess(2) test = [0, 255, 255, 0] if type(ans) == Image.Image and list(ans.getdata()) == test: print("CORRECT: A proper chess board was drawn for n = 2") else: print("WRONG: An improper chess board was drawn for n = 2") print("********************") print("Checking for n = 3") ans = chess(3) test = [0, 255, 0, 255, 0, 255, 0, 255, 0] if type(ans) == Image.Image and list(ans.getdata()) == test: print("CORRECT: A proper chess board was drawn for n = 3") else: print("WRONG: An improper chess board was drawn for n = 3") print("********************") print("Tests concluded, add more tests of your own below!") print("********************")The answers I receive are: ******************** Starting the test: ******************** Checking for n = 2 WRONG: An improper chess board was drawn for n = 2 ******************** Checking for n = 3 WRONG: An improper chess board was drawn for n = 3 ******************** Tests concluded, add more tests of your own below! ******************** RE: Need help with a checkerboard algorythm - bowlofred - Sep-26-2020 Your parity is off. If you change the conditional to (x + y + 1)%2 , it works. Or you could change your tests to be the reverse. That should work as well.
RE: Need help with a checkerboard algorythm - giladal - Sep-27-2020 (Sep-26-2020, 09:55 PM)bowlofred Wrote: Your parity is off. If you change the conditional to Wow, thank you very much for your help. It did indeed solve my issue. Does that mean that changing the color to 255 and leaving it (x+y)%2 the color on the else statement to 255 will also help? for y in range(n): if (x + y)%2: mat[x,y] = 255 else: mat[x,y] = 0 |