I am trying to make a GUI for an othello game that i made in Python. The for loops below only display pieces in the first row. Once it iterates through the nested for loop a second time, no more pieces are displayed on any of the next rows. What is preventing these other pieces from being drawn?
def _draw_pieces(self) -> None:
self._canvas.delete(tkinter.ALL)
rows = int(self._width/RADIUS)
columns = int(self._height/RADIUS)
x_frac = 0
y_frac = 0
for row in self._board:
for column in row:
if column == NONE:
self._draw_neutral_piece((x_frac/rows),(y_frac/columns))
x_frac += 1
elif column == BLACK:
self._draw_black_piece((x_frac/rows),(y_frac/columns))
x_frac += 1
elif column == WHITE:
self._draw_white_piece((x_frac/rows),(y_frac/columns))
x_frac += 1
else:
x_frac = 0
y_frac+= 1
The functions _draw_black_piece, _draw_white_piece, etc. are all basically written the same. They take a fractional coordinate and draw a piece of that color. The coordinates are determined by the size of the board after it is resized. The default size of the board is the number of pieces in each column and row times the RADIUS constant, which is set to 100.
def _draw_black_piece(self, x_frac: float, y_frac: float) -> None:
x_radius = self._canvas.winfo_width()/len(self._board[0])
y_radius = self._canvas.winfo_height()/len(self._board)
x1 = self._canvas.winfo_width() * x_frac
x2 = self._canvas.winfo_width() * x_frac + x_radius
y1 = self._canvas.winfo_height() * y_frac
y2 = self._canvas.winfo_height() * y_frac + y_radius
self._canvas.create_oval(x1, y1, x2, y2, fill = 'black')
Here is the the board as well:
self._board = [[NONE,BLACK,NONE,WHITE],[NONE,BLACK,WHITE,NONE],[NONE,WHITE,BLACK,NONE] [NONE,NONE,NONE,NONE]]
Related
I've trying to implement transition from an amount of space to another which is similar to acceleration and deceleration, except i failed and the only thing that i got from this was this infinite stack of mess, here is a screenshot showing this in action:
you can see a very black circle here, which are in reality something like 100 or 200 circles stacked on top of each other
and i reached this result using this piece of code:
def Place_circles(curve, circle_space, cs, draw=True, screen=None):
curve_acceleration = []
if type(curve) == tuple:
curve_acceleration = curve[1][0]
curve_intensity = curve[1][1]
curve = curve[0]
#print(curve_intensity)
#print(curve_acceleration)
Circle_list = []
idx = [0,0]
for c in reversed(range(0,len(curve))):
for p in reversed(range(0,len(curve[c]))):
user_dist = circle_space[curve_intensity[c]] + curve_acceleration[c] * p
dist = math.sqrt(math.pow(curve[c][p][0] - curve[idx[0]][idx[1]][0],2)+math.pow(curve [c][p][1] - curve[idx[0]][idx[1]][1],2))
if dist > user_dist:
idx = [c,p]
Circle_list.append(circles.circles(round(curve[c][p][0]), round(curve[c][p][1]), cs, draw, screen))
This place circles depending on the intensity (a number between 0 and 2, random) of the current curve, which equal to an amount of space (let's say between 20 and 30 here, 20 being index 0, 30 being index 2 and a number between these 2 being index 1).
This create the stack you see above and isn't what i want, i also came to the conclusion that i cannot use acceleration since the amount of time to move between 2 points depend on the amount of circles i need to click on, knowing that there are multiple circles between each points, but not being able to determine how many lead to me being unable to the the classic acceleration formula.
So I'm running out of options here and ideas on how to transition from an amount of space to another.
any idea?
PS: i scrapped the idea above and switched back to my master branch but the code for this is still available in the branch i created here https://github.com/Mrcubix/Osu-StreamGenerator/tree/acceleration .
So now I'm back with my normal code that don't possess acceleration or deceleration.
TL:DR i can't use acceleration since i don't know the amount of circles that are going to be placed between the 2 points and make the time of travel vary (i need for exemple to click circles at 180 bpm of one circle every 0.333s) so I'm looking for another way to generate gradually changing space.
First, i took my function that was generating the intensity for each curves in [0 ; 2]
Then i scrapped the acceleration formula as it's unusable.
Now i'm using a basic algorithm to determine the maximum amount of circles i can place on a curve.
Now the way my script work is the following:
i first generate a stream (multiple circles that need to be clicked at high bpm)
this way i obtain the length of each curves (or segments) of the polyline.
i generate an intensity for each curve using the following function:
def generate_intensity(Circle_list: list = None, circle_space: int = None, Args: list = None):
curve_intensity = []
if not Args or Args[0] == "NewProfile":
prompt = True
while prompt:
max_duration_intensity = input("Choose the maximum amount of curve the change in intensity will occur for: ")
if max_duration_intensity.isdigit():
max_duration_intensity = int(max_duration_intensity)
prompt = False
prompt = True
while prompt:
intensity_change_odds = input("Choose the odds of occurence for changes in intensity (1-100): ")
if intensity_change_odds.isdigit():
intensity_change_odds = int(intensity_change_odds)
if 0 < intensity_change_odds <= 100:
prompt = False
prompt = True
while prompt:
min_intensity = input("Choose the lowest amount of spacing a circle will have: ")
if min_intensity.isdigit():
min_intensity = float(min_intensity)
if min_intensity < circle_space:
prompt = False
prompt = True
while prompt:
max_intensity = input("Choose the highest amount of spacing a circle will have: ")
if max_intensity.isdigit():
max_intensity = float(max_intensity)
if max_intensity > circle_space:
prompt = False
prompt = True
if Args:
if Args[0] == "NewProfile":
return [max_duration_intensity, intensity_change_odds, min_intensity, max_intensity]
elif Args[0] == "GenMap":
max_duration_intensity = Args[1]
intensity_change_odds = Args[2]
min_intensity = Args[3]
max_intensity = Args[4]
circle_space = ([min_intensity, circle_space, max_intensity] if not Args else [Args[0][3],circle_space,Args[0][4]])
count = 0
for idx, i in enumerate(Circle_list):
if idx == len(Circle_list) - 1:
if random.randint(0,100) < intensity_change_odds:
if random.randint(0,100) > 50:
curve_intensity.append(2)
else:
curve_intensity.append(0)
else:
curve_intensity.append(1)
if random.randint(0,100) < intensity_change_odds:
if random.randint(0,100) > 50:
curve_intensity.append(2)
count += 1
else:
curve_intensity.append(0)
count += 1
else:
if curve_intensity:
if curve_intensity[-1] == 2 and not count+1 > max_duration_intensity:
curve_intensity.append(2)
count += 1
continue
elif curve_intensity[-1] == 0 and not count+1 > max_duration_intensity:
curve_intensity.append(0)
count += 1
continue
elif count+1 > 2:
curve_intensity.append(1)
count = 0
continue
else:
curve_intensity.append(1)
else:
curve_intensity.append(1)
curve_intensity.reverse()
if curve_intensity.count(curve_intensity[0]) == len(curve_intensity):
print("Intensity didn't change")
return circle_space[1]
print("\n")
return [circle_space, curve_intensity]
with this, i obtain 2 list, one with the spacing i specified, and the second one is the list of randomly generated intensity.
from there i call another function taking into argument the polyline, the previously specified spacings and the generated intensity:
def acceleration_algorithm(polyline, circle_space, curve_intensity):
new_circle_spacing = []
for idx in range(len(polyline)): #repeat 4 times
spacing = []
Length = 0
best_spacing = 0
for p_idx in range(len(polyline[idx])-1): #repeat 1000 times / p_idx in [0 ; 1000]
# Create multiple list containing spacing going from circle_space[curve_intensity[idx-1]] to circle_space[curve_intensity[idx]]
spacing.append(np.linspace(circle_space[curve_intensity[idx]],circle_space[curve_intensity[idx+1]], p_idx).tolist())
# Sum distance to find length of curve
Length += abs(math.sqrt((polyline[idx][p_idx+1][0] - polyline[idx][p_idx][0]) ** 2 + (polyline [idx][p_idx+1][1] - polyline[idx][p_idx][1]) ** 2))
for s in range(len(spacing)): # probably has 1000 list in 1 list
length_left = Length # Make sure to reset length for each iteration
for dist in spacing[s]: # substract the specified int in spacing[s]
length_left -= dist
if length_left > 0:
best_spacing = s
else: # Since length < 0, use previous working index (best_spacing), could also jsut do `s-1`
if spacing[best_spacing] == []:
new_circle_spacing.append([circle_space[1]])
continue
new_circle_spacing.append(spacing[best_spacing])
break
return new_circle_spacing
with this, i obtain a list with the space between each circles that are going to be placed,
from there, i can Call Place_circles() again, and obtain the new stream:
def Place_circles(polyline, circle_space, cs, DoDrawCircle=True, surface=None):
Circle_list = []
curve = []
next_circle_space = None
dist = 0
for c in reversed(range(0, len(polyline))):
curve = []
if type(circle_space) == list:
iter_circle_space = iter(circle_space[c])
next_circle_space = next(iter_circle_space, circle_space[c][-1])
for p in reversed(range(len(polyline[c])-1)):
dist += math.sqrt((polyline[c][p+1][0] - polyline[c][p][0]) ** 2 + (polyline [c][p+1][1] - polyline[c][p][1]) ** 2)
if dist > (circle_space if type(circle_space) == int else next_circle_space):
dist = 0
curve.append(circles.circles(round(polyline[c][p][0]), round(polyline[c][p][1]), cs, DoDrawCircle, surface))
if type(circle_space) == list:
next_circle_space = next(iter_circle_space, circle_space[c][-1])
Circle_list.append(curve)
return Circle_list
the result is a stream with varying space between circles (so accelerating or decelerating), the only issue left to be fixed is pygame not updating the screen with the new set of circle after i call Place_circles(), but that's an issue i'm either going to try to fix myself or ask in another post
the final code for this feature can be found on my repo : https://github.com/Mrcubix/Osu-StreamGenerator/tree/Acceleration_v02
I'm new at Python programming and I'm doing my best to fully understand this code. Here we are printing the path for the First Search Program - Artificial Intelligence for Robotics algorithm. I know how the basic of these lines are working in general, but how they work here in this code. Could I get some clarification for this piece of code below, please?.
This is below the piece of code:
policy=[[' ' for row in range(len(grid[0]))] for col in range(len(grid))]
x=goal[0]
y=goal[1]
policy[x][y]='*'
while x !=init[0] or y !=init[1]:
x2=x-delta[action[x][y]][0]
y2=y-delta[action[x][y]][1]
policy[x2][y2]= delta_name[action[x][y]]
x=x2
y=y2
for i in range(len(policy)):
print(policy[i])
This is the code:
#grid format
# 0 = navigable space
# 1 = occupied space
grid = [[0,0,1,0,0,0],
[0,0,1,0,0,0],
[0,0,0,0,1,0],
[0,0,1,1,1,0],
[0,0,0,0,1,0]]
init = [0,0] #Start location is (0,0) which we put it in open list.
goal = [len(grid)-1,len(grid[0])-1] #Our goal in (4,5) and here are the coordinates of the cell.
#Below the four potential actions to the single field
delta = [[-1 , 0], #up by subtracting one from the first dimention, I mean the demension of (0,0)
[ 0 ,-1], #left
[ 1 , 0], #down
[ 0 , 1]] #right
delta_name = ['^','<','V','>'] #The name of above actions
cost = 1 #Each step costs you one
def search():
#open list elements are of the type [g,x,y]
#To check cells once they expanded and don't expand them again. We defined an array called closed
#and its size as our grid. It has two values 0 & 1. 0 means open and 1 means closed.
closed = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
action=[[-1 for row in range(len(grid[0]))] for col in range(len(grid))]
#We initialize the starting location as checked
closed[init[0]][init[1]] = 1
# we assigned the cordinates and g value
x = init[0]
y = init[1]
g = 0
#our open list will contain our initial value
open = [[g, x, y]]
found = False #flag that is set when search complete
resign = False #Flag set if we can't find expand
#print('initial open list:')
#for i in range(len(open)):
#print(' ', open[i])
#print('----')
while found is False and resign is False:
#Check if we still have elements in the open list
if len(open) == 0: #If our open list is empty, there is nothing to expand.
resign = True
print('Fail')
print('############# Search terminated without success')
else:
#if there is still elements on our list
#remove node from list
open.sort() #sort elements in an increasing order from the smallest g value up
open.reverse() #reverse the list
next = open.pop() #remove the element with the smallest g value from the list
#print('list item')
#print('next')
#Then we assign the three values to x,y and g. Which is our expantion.
x = next[1]
y = next[2]
g = next[0]
#Check if we are done
if x == goal[0] and y == goal[1]:
found = True
print(next) #The three elements above this "if".
print('############## Search is success')
else:
#expand winning element and add to new open list
for i in range(len(delta)): #going through all our actions the four actions
#We apply the actions to x and y with additional delta to construct x2 and y2
x2 = x + delta[i][0]
y2 = y + delta[i][1]
#if x2 and y2 falls into the grid
if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 <= len(grid[0])-1:
#if x2 and y2 not checked yet and there is not obstacles
if closed[x2][y2] == 0 and grid[x2][y2] == 0:
g2 = g + cost #we increment the cose
open.append([g2,x2,y2]) #we add them to our open list
#print('append list item')
#print([g2,x2,y2])
#Then we check them to never expand again
closed[x2][y2] = 1
action[x2][y2]=i
policy=[[' ' for row in range(len(grid[0]))] for col in range(len(grid))]
x=goal[0]
y=goal[1]
policy[x][y]='*'
while x !=init[0] or y !=init[1]:
x2=x-delta[action[x][y]][0]
y2=y-delta[action[x][y]][1]
policy[x2][y2]= delta_name[action[x][y]]
x=x2
y=y2
for i in range(len(policy)):
print(policy[i])
search()
I'm reading this First Search Program - Artificial Intelligence for Robotics algorithm and I'm reading a python code of it. Here we have created a closed array to check cells once they expanded and don't expand them again. We defined an array called closed and its size as our grid. The author said it has two values 0 & 1. 0 means open and 1 means closed, but I saw it just zeros.
He marked the starting point 0,0 by 1 until not to check them, but he put the coordinate as 0 and 1 in this line closed[init[0]][init[1]] = 1. Why he put 0 and 1 instead of 0,0?
The python code is here:
#grid format
# 0 = navigable space
# 1 = occupied space
grid=[[0,0,1,0,0,0],
[0,0,1,0,0,0],
[0,0,0,0,1,0],
[0,0,1,1,1,0],
[0,0,0,0,1,0]]
init = [0,0]
goal = [len(grid)-1,len(grid[0])-1]
delta=[[-1, 0], #up
[ 0,-1], #left
[ 1, 0], #down
[ 0, 1]] #right
delta_name = ['^','<','V','>'] #The name of above actions
cost = 1
def search():
#open list elements are of the type [g,x,y]
closed = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
#We initialize the starting location as checked
closed[init[0]][init[1]] = 1
# we assigned the cordinates and g value
x = init[0]
y = init[1]
g = 0
#our open list will contain our initial value
open = [[g,x,y]]
found = False #flag that is set when search complete
resign= False #Flag set if we can't find expand
#print('initial open list:')
#for i in range(len(open)):
#print(' ', open[i])
#print('----')
while found is False and resign is False:
#Check if we still have elements in the open list
if len(open)==0: #If our open list is empty
resign=True
print('Fail')
print('############# Search terminated without success')
else:
#if there is still elements on our list
#remove node from list
open.sort()
open.reverse() #reverse the list
next = open.pop()
#print('list item')
#print('next')
#Then we assign the three values to x,y and g. Which is our expantion
x = next[1]
y = next[2]
g = next[0]
#Check if we are done
if x == goal[0] and y == goal[1]:
found = True
print(next) #The three elements above this if
print('############## Search is success')
else:
#expand winning element and add to new open list
for i in range(len(delta)):
x2 = x+delta[i][0]
y2 = y+delta[i][1]
#if x2 and y2 falls into the grid
if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 <= len(grid[0])-1:
#if x2 and y2 not checked yet and there is not obstacles
if closed[x2][y2] == 0 and grid[x2][y2] == 0:
g2 = g+cost #we increment the cose
open.append([g2,x2,y2])#we add them to our open list
#print('append list item')
#print([g2,x2,y2])
#Then we check them to never expand again
closed[x2][y2] = 1
search()
he put the coordinate as 0 and 1 in this line closed[init[0]][init[1]] = 1
closed[init[0]][init[1]] does not mean "set the value at coordinates (0,1) to 1". It means "using init[0] as the x coordinate and init[1] at the y coordinate, set the value to 1". init[0] is 0, and init[1] is 0, so closed[init[0]][init[1]] = 1 sets closed[0][0] to 1.
Suppose the starting coordinate was init = [2,5]. It would be incorrect to change the line to closed[init[2]][init[5]] = 1. This would crash with an IndexError, because init only has two elements, so you can only index it with 0 or 1.
So I'm working on a homework assignment regarding using image objects in python. I'm using python 3.4.1 for this assignment. I feel like I have everything done, but it doesn't want to work correctly. Basically, I'm trying to get it to look like the picture that I've attached, but it only shows as 1 red line across, and 1 red line top to bottom on a white background.
Any help would be much appreciated.
The attached image:
http://imgur.com/TMho41w
import cImage as image
width = 500
height = 500
img = image.EmptyImage(width, height)
win = image.ImageWin("Exercise 3", width, height)
img.draw(win)
for row in range(height):
for col in range(width):
p = img.getPixel(col, row)
if row == 0 or col == 0:
p = image.Pixel(255, 0, 0)
else:
Sum = 0
temppixel = img.getPixel(col-1, row)
if temppixel.getRed() == 255:
Sum = Sum + 1
elif temppixel.getBlue() == 255:
Sum = Sum + 2
temppixel = img.getPixel(col-1, row-1)
if temppixel.getRed() == 255:
Sum = Sum + 1
elif temppixel.getBlue() == 255:
Sum = Sum + 2
temppixel = img.getPixel(col, row-1)
if temppixel.getRed() == 255:
Sum = Sum + 1
elif temppixel.getBlue() == 255:
Sum = Sum + 2
if Sum % 3 == 1:
p = image.Pixel(255, 0, 0)
elif Sum % 3 == 2:
p = image.Pixel(0, 0, 255)
else:
p = image.Pixel(255, 255, 255)
img.setPixel(col, row, p)
img.draw(win)
img.draw(win)
# uncomment this to save the image as a file
#img.saveTk("gradient.gif")
win.exitonclick()
Unfortunately, your code does exactly what you have written it to do. Let's name the three first if ... elif condition1, 2 and 3 :
The first pixel is set to red
Then we progress through the first line, so row = 0 which means condition 2 and 3 are using invalid coordinates (because of row-1). So there's only condition at play here, and it will always increment by 1 sum which means it'll add a new red pixel.
So you have now your first red line.
For the first column, starting from the second line : conditions 1 & 2 are using invalid coordinates. Condition 3 will always return sum = 1 which means a new red pixel. And you have your red line from top to bottom
And then from row = 1 and col = 1, all neighbors are red, which leads to a new white pixel. Unfortunately, white does contain some red, so it'll always be the sames conditions that are met, and you have your white background.
I haven't been able to find the complete algorithm for this method to build a Sierpinski carpet, so I can't really correct it. But you should be extra careful with these edges situations : what should be the three neighbors if you are on the first line or first row ?
I'm working on a 2-player board game (e.g. connect 4), with parametric board size h, w. I want to check for winning condition using hw-sized bitboards.
In game like chess, where board size is fixed, bitboards are usually represented with some sort of 64-bit integer. When h and w are not constant and maybe very big (let's suppose 30*30) are bitboards a good idea? If so, are the any data types in C/C++ to deal with big bitboards keeping their performances?
Since I'm currently working on python a solution in this language is appreciated too! :)
Thanks in advance
I wrote this code while ago just to play around with the game concept. There is no intelligence behaviour involve. just random moves to demonstrate the game. I guess this is not important for you since you are only looking for a fast check of winning conditions. This implementation is fast since I did my best to avoid for loops and use only built-in python/numpy functions (with some tricks).
import numpy as np
row_size = 6
col_size = 7
symbols = {1:'A', -1:'B', 0:' '}
def was_winning_move(S, P, current_row_idx,current_col_idx):
#****** Column Win ******
current_col = S[:,current_col_idx]
P_idx= np.where(current_col== P)[0]
#if the difference between indexes are one, that means they are consecutive.
#we need at least 4 consecutive index. So 3 Ture value
is_idx_consecutive = sum(np.diff(P_idx)==1)>=3
if is_idx_consecutive:
return True
#****** Column Win ******
current_row = S[current_row_idx,:]
P_idx= np.where(current_row== P)[0]
is_idx_consecutive = sum(np.diff(P_idx)==1)>=3
if is_idx_consecutive:
return True
#****** Diag Win ******
offeset_from_diag = current_col_idx - current_row_idx
current_diag = S.diagonal(offeset_from_diag)
P_idx= np.where(current_diag== P)[0]
is_idx_consecutive = sum(np.diff(P_idx)==1)>=3
if is_idx_consecutive:
return True
#****** off-Diag Win ******
#here 1) reverse rows, 2)find new index, 3)find offest and proceed as diag
reversed_rows = S[::-1,:] #1
new_row_idx = row_size - 1 - current_row_idx #2
offeset_from_diag = current_col_idx - new_row_idx #3
current_off_diag = reversed_rows.diagonal(offeset_from_diag)
P_idx= np.where(current_off_diag== P)[0]
is_idx_consecutive = sum(np.diff(P_idx)==1)>=3
if is_idx_consecutive:
return True
return False
def move_at_random(S,P):
selected_col_idx = np.random.permutation(range(col_size))[0]
#print selected_col_idx
#we should fill in matrix from bottom to top. So find the last filled row in col and fill the upper row
last_filled_row = np.where(S[:,selected_col_idx] != 0)[0]
#it is possible that there is no filled array. like the begining of the game
#in this case we start with last row e.g row : -1
if last_filled_row.size != 0:
current_row_idx = last_filled_row[0] - 1
else:
current_row_idx = -1
#print 'col[{0}], row[{1}]'.format(selected_col,current_row)
S[current_row_idx, selected_col_idx] = P
return (S,current_row_idx,selected_col_idx)
def move_still_possible(S):
return not (S[S==0].size == 0)
def print_game_state(S):
B = np.copy(S).astype(object)
for n in [-1, 0, 1]:
B[B==n] = symbols[n]
print B
def play_game():
#initiate game state
game_state = np.zeros((6,7),dtype=int)
player = 1
mvcntr = 1
no_winner_yet = True
while no_winner_yet and move_still_possible(game_state):
#get player symbol
name = symbols[player]
game_state, current_row, current_col = move_at_random(game_state, player)
#print '******',player,(current_row, current_col)
#print current game state
print_game_state(game_state)
#check if the move was a winning move
if was_winning_move(game_state,player,current_row, current_col):
print 'player %s wins after %d moves' % (name, mvcntr)
no_winner_yet = False
# switch player and increase move counter
player *= -1
mvcntr += 1
if no_winner_yet:
print 'game ended in a draw'
player = 0
return game_state,player,mvcntr
if __name__ == '__main__':
S, P, mvcntr = play_game()
let me know if you have any question
UPDATE: Explanation:
At each move, look at column, row, diagonal and secondary diagonal that goes through the current cell and find consecutive cells with the current symbol. avoid scanning the whole board.
extracting cells in each direction:
column:
current_col = S[:,current_col_idx]
row:
current_row = S[current_row_idx,:]
Diagonal:
Find the offset of the desired diagonal from the
main diagonal:
diag_offset = current_col_idx - current_row_idx
current_diag = S.diagonal(offset)
off-diagonal:
Reverse the rows of matrix:
S_reversed_rows = S[::-1,:]
Find the row index in the new matrix
new_row_idx = row_size - 1 - current_row_idx
current_offdiag = S.diagonal(offset)