Commit aa912e60 authored by Prathisha Kunnumbrath Manden's avatar Prathisha Kunnumbrath Manden
Browse files

Code update for removing matched tiles and shifting the above tiles

parent 84c2d395
import numpy as np
# game_grid = np.array([[0, 1, 0, 2, 1], [1, 2, 0, 1, 2], [2, 1, 2, 0, 0], [0, 0, 1, 2, 2], [1, 0, 1, 0, 2]])
# game_grid = `np.array([[1, 2, 3, 4], [1, 2, 1, 4], [3, 4, 1, 2]])
import collections
def generate_board():
board = np.random.randint(4, size=(5, 5))
board = np.random.randint(1, 4, size=(5, 5))
# check whether the board contains any 3-matches
if check_matches(game_grid):
if check_matches(board):
board = generate_board()
else:
print(board)
return board
return board
def init_board():
# game_grid = generate_board()
global game_grid
game_grid = np.array([[0, 1, 0, 2, 1], [1, 2, 0, 1, 2], [2, 1, 2, 0, 0], [0, 0, 1, 2, 2], [1, 0, 1, 0, 2]])
global possible_move_positions
# generate a 2D array with values between 1 and k
# value 0 is used later to show removed tiles and empty spaces on top of the grid
game_grid = generate_board()
# game_grid = np.array([[1, 2, 1, 3, 2],
# [2, 1, 1, 2, 1],
# [1, 2, 3, 1, 1],
# [1, 1, 2, 3, 3],
# [2, 1, 2, 1, 3]])
print(game_grid)
global possible_move_positions
possible_move_positions = find_moves(game_grid)
print(possible_move_positions)
# print("possible moves = ", possible_move_positions)
if not possible_move_positions:
print("No more moves possible")
print("No more moves possible. Shuffling...")
game_grid = generate_board()
else:
print("Valid board")
......@@ -32,25 +40,25 @@ def init_board():
def input_tiles():
print("Take the swap input")
# TODO: Take the move from user / agent
print("Take the swap co-ordinates")
move = possible_move_positions.pop(0) #pop the last pair of co-ordinates from the possible moves for swap
(coord1, coord2) = move
print("One move taken from possible moves: ", coord1, coord2)
row_1, col_1, row_2, col_2 = 1, 0, 1, 1 #hardcoded swap co-ordinates
print("row_1, col_1, row_2, col_2: ", row_1, col_1, row_2, col_2)
is_valid_move = validate_move(row_1, col_1, row_2, col_2)
is_valid_move = validate_move(coord1, coord2)
if is_valid_move:
print("Valid Move!")
swap_tiles(row_1, col_1, row_2, col_2)
swap_tiles(coord1, coord2)
else:
print("Invalid move!!")
def find_moves(board):
# function for checking if there is any possible move in the board
global co_ords
co_ords = []
# vertical swap and check for matches
for i in range(len(board) - 1):
for j in range(len(board[0])):
......@@ -58,7 +66,8 @@ def find_moves(board):
board_copy[i][j], board_copy[i+1][j] = board_copy[i+1][j], board_copy[i][j]
match = check_matches(board_copy)
if match:
co_ords.append(([i, j], [i+1, j]))
co_ords.append([(i, j), (i+1, j)])
# horizontal swap and check for matches
for i in range(len(board)):
for j in range(len(board[0]) - 1):
......@@ -66,11 +75,13 @@ def find_moves(board):
board_copy[i][j], board_copy[i][j+1] = board_copy[i][j+1], board_copy[i][j]
match = check_matches(board_copy)
if match:
co_ords.append(([i, j], [i, j+1]))
co_ords.append([(i, j), (i, j+1)])
return co_ords
def check_matches(board):
# function for checking all the matches in the board
# check the whole board for vertical matches and return true if match found
for j in range(len(board[0])):
count = 1
for i in range(len(board) - 1):
......@@ -80,6 +91,8 @@ def check_matches(board):
count = 1
if (count >= 3):
return True
# check the whole board for horizontal matches and return true if match found
for i in range(len(board)):
count = 1
for j in range(len(board[0]) - 1):
......@@ -91,64 +104,115 @@ def check_matches(board):
return True
return False
def swap_tiles(row_1, col_1, row_2, col_2):
game_grid[row_1][col_1], game_grid[row_2][col_2] = game_grid[row_2][col_2], game_grid[row_1][col_1]
def swap_tiles(coord1, coord2):
# function for swapping the valid move and checking for the matches in
# corresponding rows and columns
game_grid[coord1[0]][coord1[1]], game_grid[coord2[0]][coord2[1]] = game_grid[coord2[0]][coord2[1]], game_grid[coord1[0]][coord1[1]]
print(game_grid)
if col_1 == col_2:
if row_1 > row_2:
row_1, row_2 = row_2, row_1
count = 1
for i in range(len(game_grid) - 1):
# print("same column")
j = col_1
if (game_grid[i][j] == game_grid[i+1][j]):
count += 1
else:
count = 1
if count >= 3:
print("Match found vertically1111!")
for i in range(row_1, row_2+1):
count = 1
horizontal_matches = []
vertical_matches = []
if coord1[1] == coord2[1]: # if vertical swap is performed
for i in [coord1[0], coord2[0]]: # check the 2 rows
horizontal_chain = []
for j in range(len(game_grid[0])-1):
if (game_grid[i][j] == game_grid[i][j+1]):
count += 1
if((i,j) not in horizontal_chain):
horizontal_chain.append((i, j))
horizontal_chain.append((i, j + 1))
elif not horizontal_chain:
continue
else:
count = 1
if count >= 3:
print("Match found horizontally1111!")
if row_1 == row_2:
if col_1 > col_2:
col_1, col_2 = col_2, col_1
break
if len(horizontal_chain)>=3 and horizontal_chain not in horizontal_matches:
horizontal_matches.append(horizontal_chain)
vertical_chain = []
for i in range(len(game_grid)-1): # check the 1 column
if (game_grid[i][coord1[1]] == game_grid[i+1][coord1[1]]):
if ((i, coord1[1]) not in vertical_chain):
vertical_chain.append((i, coord1[1]))
vertical_chain.append((i+1, coord1[1]))
elif not vertical_chain:
continue
else:
break
if len(vertical_chain) >= 3 and vertical_chain not in vertical_matches:
vertical_matches.append(vertical_chain)
# print("horizontal_matches == ", horizontal_matches)
# print("vertical_matches == ", vertical_matches)
shift_tiles(horizontal_matches, vertical_matches)
if coord1[0] == coord2[0]: # if horizontal swap is performed
for j in [coord1[1], coord2[1]]: #check the 2 columns
vertical_chain = []
for i in range(len(game_grid) - 1):
if (game_grid[i][j] == game_grid[i+1][j]):
if((i,j) not in vertical_chain):
vertical_chain.append((i, j))
vertical_chain.append((i+1, j))
elif not vertical_chain:
continue
else:
break
if len(vertical_chain)>=3 and vertical_chain not in vertical_matches:
vertical_matches.append(vertical_chain)
count = 1
for j in range(len(game_grid[0]) - 1):
# print("same column")
i = row_1
if (game_grid[i][j] == game_grid[i][j+1]):
count += 1
horizontal_chain = []
for j in range(len(game_grid[0])-1): #check the 1 row
if (game_grid[coord1[0]][j] == game_grid[coord1[0]][j+1]):
if ((coord1[0], j) not in horizontal_chain):
horizontal_chain.append((coord1[0], j))
horizontal_chain.append((coord1[0],j+1))
elif not horizontal_chain:
horizontal_chain.clear()
continue
else:
count = 1
if count >= 3:
print("Match found horizontally2222!")
break
if len(horizontal_chain) >= 3 and horizontal_chain not in horizontal_matches:
horizontal_matches.append(horizontal_chain)
print("horizontal_matches : ", horizontal_matches)
shift_tiles(horizontal_matches, vertical_matches)
def shift_tiles(row_matches, column_matches):
# Function for shifting tiles above removed tiles
matched_cells = {}
for match in row_matches:
for (row, col) in match:
game_grid[row][col] = 0
if(row in matched_cells):
if col not in matched_cells[row]:
matched_cells[row].append(col)
else:
matched_cells[row]=[col]
for match in column_matches:
for (row, col) in match:
game_grid[row][col] = 0
if(row in matched_cells):
if col not in matched_cells[row]:
matched_cells[row].append(col)
else:
matched_cells[row]=[col]
print("Matched tiles removed :")
print(game_grid)
matched_cells = collections.OrderedDict(sorted(matched_cells.items()))
for j in range(col_1, col_2+1):
count = 1
for i in range(len(game_grid)-1):
if (game_grid[i][j] == game_grid[i+1][j]):
count += 1
for matched_cell in matched_cells:
for cell in matched_cells.get(matched_cell):
for i in range(matched_cell, -1, -1):
if (i == 0):
game_grid[i][cell] = 0
else:
count = 1
if count >= 3:
print("Match found vertically2222!")
game_grid[i][cell] = game_grid[i - 1][cell]
print("Tiles above removed tiles moved down : \n")
print(game_grid)
def validate_move(row_1, col_1, row_2, col_2):
print("moves : ", possible_move_positions[0][0])
if ([row_1, col_1], [row_2, col_2]) or ([row_2, col_2], [row_1, col_1]) in possible_move_positions:
def validate_move(coord1, coord2):
if ([coord1[0], coord1[1]], [coord2[0], coord2[1]]) or ([coord2[0], coord2[1]], [coord1[0], coord1[1]]) in possible_move_positions:
return True
if __name__ == "__main__":
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment