8 Queens – WEEK 3 (Python)

 

As you guys know, I’m also in learning stage. Till this week I didn’t know how to place one image on other, with transparent background. So I made all those queens for each tile. But now I know how to place one image on other, with transparent background. So there is no need to use those images of queen. These are the updated queens, 8 queens in total each for different row.

This week I placed all the queens on the board. First I opened them in the list.

# 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

And set their visibility to False, because I don’t want them to be shown on the board. Then I drew them on the board, in on_draw function (event of screen).

@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()

Then I made the list of all the winning possibilities. For the eight queens of 8×8 board there are 92 possibilities.

column = range(8)
for vec in permutations(column):
    if n == len(set(vec[i]+i for i in column)) == len(set(vec[i]-i for i in column)):
	print(vec)

I took this code from here. In the above code for all the possibilities, we can use anther nested loop instead of using the loop inside the if condition. The above code will print the possible moves like this:

capture

We can see from the output that it only give us the number of column in each row. But I wanted to convert it in the tile number. So I did changes to this code. And then I added one more event for mouse click. This is the 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')

# 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[0]+(width*c) # assigning x_axis value to the respective queen
    queen[i].y = b[1] # 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[0])
        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[0]:
            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[1]) and (y < b[1])):
                for j in range(8): # for column
                    if ((x > (queen[(c+j)].x)) and (x < ((queen[c+j].width)+queen[c+j].x))): # checking the tile
                        queen[c+j].visible = True # Changing the visibility of the queen on that tile
            c += 8
def updated(dt):
    on_draw()

pyglet.clock.schedule_interval(updated, 1 / 60)

# Launch the application
pyglet.app.run()

In this code I haven’t put the check on wrong move. This is the last part of my game, and I will do it next week.

Advertisements

One Comment Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s