So I was supposed to finish my game this week, but we had exams so we got extension of a week. So I didn’t do much in this week, because of my exams. I made a function, which checks if the queen we place on the board is it thread to any other queen or not.
def queen_safe(i, j, tile_num): global click, rows, columns if (click == 0): for l in possible: for m in l: if(m[i] == tile_num): win.append(m) queen[tile_num].visible = True rows.append(i) columns.append(j) click += 1 else: for k in win: if ((i not in rows) and (j not in columns)): if k[i] == tile_num: queen[tile_num].visible = True click += 1 rows.append(i) columns.append(j) else: if len(win) != 1: win.remove(k)
I called this function in other function ‘on_mouse_press’. I passed 3 argument in this function. Row number, column number and the tile number on which mouse was clicked. My program is working just fine, but their are some flaws that I have to take care of. I may change my logic because of those flaws. Here is my complete code for this week.
# _author_ = "Abdul Junaid" from itertools import permutations import pyglet from pyglet.window import Window, mouse, gl # Display an image in the application window # loading board image board = pyglet.image.load('resources/chess-boards.jpg') click = 0 win =  rows =  columns =  # loading image of queens and storing them in list queen =  a = 1 for i in range(64): if ((i > 7) and (i%8 == 0)): a += 1 queen.append(pyglet.sprite.Sprite(pyglet.image.load("resources/queen"+str(a)+".png"))) queen[i].visible = False # assigning images to tiles, and x, y axis values to the images # starting points of the first tile in each row row = [[317,248],[331,293],[345,335],[358,372],[370,406],[380,437],[391,464],[399,491],[407,516]] width = 91 # width of the tile in first row indx = 0 # to keep record of which row is being used c = 0 # to keep record that 8 queens are placed in one row for i in range(64): if (c == 8): # checking if 8 queens are placed in a row c = 0 if (i > 7 and i % 8 == 0): # checking if the row is changed width -= 3.5 # width of tiles in row changes indx += 1 # row number b = row[indx] # opening the list acoording to the row number queen[i].x = b+(width*c) # assigning x_axis value to the respective queen queen[i].y = b # assigning y_axis value to the respective queen c+=1 # winning conditions of 8x8 (8 Queens) position =  # to store the possiblites column = range(8) for vec in permutations(column): if 8 == len(set(vec[i]+i for i in column)) == len(set(vec[i]-i for i in column)): # converting the tile positon in row to tile positon on board a = 0 temp1 = list(vec) temp2 =  temp2.append(temp1) for i in range(1,8): a += 8 temp2.append(temp1[i]+a) position.append(temp2) # storing winning positions possible =  # to store all the possiblites according to the first row move for i in range(8): temp =  # to temp store the possible lists with same first row move for place in position: if i == place: temp.append(place) possible.append(temp) # creating a window # I set width and height according to the fullscreen resoulutions of my screen width = 1366 height = 715 screen = Window(width, height, resizable=True, caption="8 Queens", config=pyglet.gl.Config(double_buffer=True), vsync=False) @screen.event # checking for the events taking place on screen def on_draw(): screen.clear() # Clearing screen # Drawing board on the center of screen board.blit((width-board.width)//2,(height-board.height)//2) # placing/drawin queens on board for i in range(64): queen[i].draw() @screen.event def on_mouse_press(x, y, button, modifier): if (button == mouse.LEFT): # Check if left button was clicked c = 0 for i in range(8): # for row a = row[i] b = row[i+1] if ((y > a) and (y < b)): for j in range(8): # for column tile_num = c + j if ((x > (queen[(tile_num)].x)) and (x < ((queen[tile_num].width)+queen[tile_num].x))): # checking the tile queen_safe(i, j, tile_num) c += 8 def queen_safe(i, j, tile_num): global click, rows, columns if (click == 0): for l in possible: for m in l: if (m[i] == tile_num): win.append(m) queen[tile_num].visible = True rows.append(i) columns.append(j) click += 1 else: for k in win: if ((i not in rows) and (j not in columns)): if k[i] == tile_num: queen[tile_num].visible = True click += 1 rows.append(i) columns.append(j) else: if len(win) != 1: win.remove(k) def updated(dt): on_draw() pyglet.clock.schedule_interval(updated, 1 / 60) # Launch the application pyglet.app.run()
Next week is the final week, I will complete my game. I might do some customization to make it cool.