Commit 36ceedab authored by Prathisha Kunnumbrath Manden's avatar Prathisha Kunnumbrath Manden
Browse files

Updated code for shifting tiles

parent aa912e60
import numpy as np
import collections
def generate_board():
board = np.random.randint(1, 4, size=(5, 5))
def validate_board(board):
new_board = board
# check whether the board contains any 3-matches
if check_matches(board):
board = generate_board()
else:
return board
while check_matches(new_board):
new_board = shuffle_board(new_board)
return new_board
def shuffle_board(board):
board = board.ravel()
np.random.shuffle(board)
board = board.reshape(5,5)
return board
def get_possible_move_positions(board):
possible_move_positions = find_moves(board)
while not possible_move_positions:
board = shuffle_board(board)
possible_move_positions = find_moves(board)
return possible_move_positions
def init_board():
global game_grid
......@@ -17,44 +29,26 @@ def init_board():
# 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
init_board = np.random.randint(1, 4, size=(5, 5))
game_grid = validate_board(init_board)
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("Generated Board : ")
print(game_grid)
possible_move_positions = find_moves(game_grid)
# print("possible moves = ", possible_move_positions)
if not possible_move_positions:
print("No more moves possible. Shuffling...")
game_grid = generate_board()
else:
print("Valid board")
input_tiles()
possible_move_positions = get_possible_move_positions(game_grid)
input_tiles()
def input_tiles():
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
move = possible_move_positions.pop() #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)
is_valid_move = validate_move(coord1, coord2)
if is_valid_move:
print("Valid Move!")
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
......@@ -109,6 +103,7 @@ def swap_tiles(coord1, coord2):
# 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("Board after swapping the tiles:")
print(game_grid)
horizontal_matches = []
vertical_matches = []
......@@ -116,67 +111,76 @@ def swap_tiles(coord1, coord2):
for i in [coord1[0], coord2[0]]: # check the 2 rows
horizontal_chain = []
for j in range(len(game_grid[0])-1):
chain_continue_flag = False
if (game_grid[i][j] == game_grid[i][j+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:
break
if len(horizontal_chain)>=3 and horizontal_chain not in horizontal_matches:
horizontal_matches.append(horizontal_chain)
chain_continue_flag = True
if(j == len(game_grid[0]) - 2):
chain_continue_flag = False
if (not chain_continue_flag):
if len(vertical_chain)>=3:
horizontal_matches.append(horizontal_chain)
else:
horizontal_chain.clear()
vertical_chain = []
for i in range(len(game_grid)-1): # check the 1 column
chain_continue_flag = False
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)
chain_continue_flag = True
if(1 == len(game_grid) - 2):
chain_continue_flag = False
if (not chain_continue_flag):
if len(vertical_chain)>=3:
vertical_matches.append(vertical_chain)
else:
vertical_chain.clear()
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):
chain_continue_flag = False
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)
chain_continue_flag = True
if(i == len(game_grid) - 2):
chain_continue_flag = False
if (not chain_continue_flag):
if len(vertical_chain)>=3:
vertical_matches.append(vertical_chain)
else:
vertical_chain.clear()
horizontal_chain = []
for j in range(len(game_grid[0])-1): #check the 1 row
chain_continue_flag = False
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:
break
if len(horizontal_chain) >= 3 and horizontal_chain not in horizontal_matches:
horizontal_matches.append(horizontal_chain)
print("horizontal_matches : ", horizontal_matches)
horizontal_chain.append((coord1[0],j+1))
chain_continue_flag = True
# elif not horizontal_chain:
# horizontal_chain.clear()
# continue
if(j == len(game_grid[0]) - 2):
chain_continue_flag = False
if (not chain_continue_flag):
if len(horizontal_chain)>=3:
horizontal_matches.append(horizontal_chain)
else:
horizontal_chain.clear()
shift_tiles(horizontal_matches, vertical_matches)
def shift_tiles(row_matches, column_matches):
# Function for shifting tiles above removed tiles
matched_cells = {}
......@@ -208,7 +212,7 @@ def shift_tiles(row_matches, column_matches):
else:
game_grid[i][cell] = game_grid[i - 1][cell]
print("Tiles above removed tiles moved down : \n")
print("Tiles above removed tiles moved down :")
print(game_grid)
def validate_move(coord1, coord2):
......
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