Created
June 12, 2017 11:18
-
-
Save landhb/e5b0f6b90b8b405db8eeb643c346c062 to your computer and use it in GitHub Desktop.
Best move calculator for Voltorb Flip: https://bulbapedia.bulbagarden.net/wiki/Voltorb_Flip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, math | |
class Board(object): | |
def __init__(self, row, column): | |
# Creates a list containing 5 lists, each of 5 items, all set to 0 | |
width, height = 5, 5; | |
self.matrix = [[0 for x in range(width)] for y in range(height)] | |
self.known = [[0 for x in range(width)] for y in range(height)] | |
self.row_constraints = row | |
self.col_constraints = column | |
def print_matrix(self): | |
for index, line in enumerate(self.matrix): | |
print("%s : %s" % (line, self.row_constraints[index])) | |
sys.stdout.write(" ") | |
for i in range(2): | |
for j in range(5): | |
sys.stdout.write(str(self.col_constraints[j][i]) + " ") | |
sys.stdout.write("\n ") | |
# Finds the total sum of the row & col a block is in | |
# And the number of still covered blocks in the cross | |
def sum_cross(self, row_index, col_index): | |
result = 0 | |
covered = 0 | |
for i in range(5): | |
result += self.known[i][col_index] | |
result += self.known[row_index][i] | |
if self.known[i][col_index] == 0: | |
covered += 1 | |
if self.known[row_index][i] == 0: | |
covered += 1 | |
# Because we counted it twice, subtract the block before returning | |
return covered, result - self.known[row_index][col_index] | |
def calc_matrix_value(self): | |
#Reward = (# of total coins - # of total uncovered coins) - (# of covered squares - # of total Voltorb squares) | |
for row in range(5): | |
for col in range(5): | |
if (self.known[row][col] == 0): | |
total_coins = self.row_constraints[row][0] + self.col_constraints[col][0] | |
covered_tiles, found_coins = self.sum_cross(row, col) | |
total_votorbs = self.row_constraints[row][1] + self.col_constraints[col][1] | |
# Calculate the reward weight | |
self.matrix[row][col] = (total_coins - found_coins) - (covered_tiles - total_votorbs) | |
# Account for risk (i.e. percentage chance of hitting a voltorb in this cross) | |
self.matrix[row][col] *= (1-float(total_votorbs)/9) | |
self.matrix[row][col] = round(self.matrix[row][col], 2) | |
else: | |
self.matrix[row][col] = 0 | |
def best_move(self): | |
row = 0 | |
col = 0 | |
ans_val = 0.0 | |
for i in range(5): | |
for j in range(5): | |
if self.matrix[i][j] > ans_val: | |
row = i | |
col = j | |
ans_val = self.matrix[i][j] | |
print("\nThe best move is row %s col %s with value %s\n" % (row+1, col+1, ans_val)) | |
def update(self, row, col, value): | |
self.known[row][col] = value | |
self.calc_matrix_value() | |
self.print_matrix() | |
def solve(self): | |
self.calc_matrix_value() | |
self.print_matrix() | |
while True: | |
self.best_move() | |
row, col, value = raw_input("Enter the row, col, and value of the chosen block: ").split() | |
self.update(int(row) -1, int(col)-1, int(value)) | |
def main(): | |
rows=[(4, 3), (7, 2), (5, 3), (5, 2), (4, 3)] | |
columns=[(4, 3), (5, 2), (7, 2), (3, 4), (6, 2)] | |
b = Board(rows, columns) | |
b.solve() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment