addded homework 9 and 10

This commit is contained in:
Heli-o 2024-01-12 22:06:42 +01:00
parent 0c59a36efb
commit 5afa9e7fcf
5 changed files with 607 additions and 62 deletions

77
ant.py Normal file
View file

@ -0,0 +1,77 @@
import sys
neighbors = lambda p, q: [(p + dp, q + dq) for dp, dq in [(0, -1), (1, -1), (1, 0), (0, 1), (-1, 1), (-1, 0)]]
def crawler(table, visitation_rights, pos):
visitation_rights.add(pos)
resulted_offence = list() # just a result but cause I renamed visited to visitation_rights, I also renamed this.
for i in table[pos][0]:
touching_hive, empty_space = False, False
if not i in table:
continue
if i not in visitation_rights and not table[i][1]:
for j in table[i][0]:
if empty_space and touching_hive:
break
if j in table[pos][0]:
if not j in table:
continue
if not table[j][1] or j in visitation_rights:
empty_space = True
else:
touching_hive = True
if empty_space and touching_hive:
resulted_offence += [i] + crawler(table, visitation_rights, i)
return resulted_offence
def flood(table, visited, pos):
n = 0
for i in table[pos][0]:
if i not in table:
continue
if i not in visited and table[i][1] != 0:
visited.add(i)
n += 1 + flood(table, visited, i)
return n
def seperation_check(table, figs, pos):
for i in table[pos][0]:
if i not in table:
continue
if not table[i][1]:
continue
visitation_rights = set() #orginally visited, but for some reason I decided to name it like this.
visitation_rights.add(pos)
visitation_rights.add(i)
return figs-2 == flood(table, visitation_rights, i)
return False
def main():
with open(sys.argv[1],"r") as f:
lines = f.readlines()
lines = [list(map(int, l.split(" "))) for l in lines]
starting_ant = None
lookup_table = dict()
total_stones_on_board = 0
for l in lines:
lookup_table[(l[0],l[1])] = [neighbors(l[0],l[1]),l[2]]
if l[2] != 0:
total_stones_on_board +=1
if l[2] == 2:
starting_ant = (l[0],l[1])
if not seperation_check(lookup_table, total_stones_on_board, starting_ant):
print([])
return
res = crawler(lookup_table, set(), starting_ant)
res.sort()
print(list(map(list,res)))
if __name__=="__main__":
main()

86
babylon.py Normal file
View file

@ -0,0 +1,86 @@
import sys
from collections import deque
class towersolve:
def __init__(self, matrix):
self.matrix = matrix
self.visited = set()
self.queue = deque([(matrix, [])])
def solve(self):
while self.queue:
c_matrix, steps = self.queue.popleft()
state = self.m2t(c_matrix)
if state in self.visited:
continue
self.visited.add(state)
if self.is_goal_state(c_matrix):
return steps
zero_pos = self.find0(c_matrix)
self.explore(c_matrix, steps, zero_pos)
return None
@staticmethod
def m2t(matrix):
return tuple(tuple(row) for row in matrix)
@staticmethod
def is_goal_state(matrix):
for col in range(len(matrix[0])):
non_zero_numbers = [row[col] for row in matrix if row[col] != 0]
if non_zero_numbers and non_zero_numbers.count(non_zero_numbers[0]) != len(non_zero_numbers):
return False
return True
@staticmethod
def rotate(matrix, row_index, vec):
row = matrix[row_index]
if vec == -1:
row = row[1:] + row[:1]
elif vec == 1:
row = row[-1:] + row[:-1]
matrix[row_index] = row
return matrix
@staticmethod
def move0(matrix, zero_pos, vec):
row, col = zero_pos
if vec == 1 and row > 0: # Swapped the directions
matrix[row][col], matrix[row - 1][col] = matrix[row - 1][col], 0
elif vec == -1 and row < len(matrix) - 1: # Swapped the directions
matrix[row][col], matrix[row + 1][col] = matrix[row + 1][col], 0
return matrix
@staticmethod
def find0(matrix):
for i, row in enumerate(matrix):
for j, value in enumerate(row):
if value == 0:
return i, j
return None
def explore(self, c_mat, steps, zero_pos):
for row_index in range(len(c_mat)):
for vec in [-1, 1]:
new_matrix = self.rotate([row[:] for row in c_mat], row_index, vec)
new_steps = steps + [f"r {row_index} {vec}"]
self.queue.append((new_matrix, new_steps))
for vec in [-1, 1]:
new_matrix = self.move0([row[:] for row in c_mat], zero_pos, vec)
new_steps = steps + [f"m {vec}"]
self.queue.append((new_matrix, new_steps))
def read_mat(file_path):
with open(file_path, "r") as file:
return [[int(num) for num in line.split()] for line in file]
if __name__ == "__main__":
_input = read_mat(sys.argv[1])
solver = towersolve(_input)
sol = solver.solve()
print(",".join(sol))

407
player - Copy.txt Normal file
View file

@ -0,0 +1,407 @@
import base as Base
import copy
import random
import time
import math
from PIL import Image, ImageDraw
from collections import deque
# Player template for HIVE --- ALP semestral work
# Vojta Vonasek, 2023
# PUT ALL YOUR IMPLEMENTATION INTO THIS FILE
def inBoard(p, q, size):
""" return True if (p,q) is valid coordinate """
return (q >= 0) and (q < size) and (p >= -(q//2)) and (p < (size - q//2))
def get_neighbors(p, q):
directions = [(0, -1), (1, -1), (1, 0), (0, 1), (-1, 1), (-1, 0)]
return [(p + dp, q + dq) for dp, dq in directions]
def has_neighbors(p, q, board, pp, pq):
# Check if the position (p, q) has at least one neighbor
board = copy.deepcopy(board)
board[pp][pq] = ""
for dp, dq in get_neighbors(0, 0):
neighbor_p, neighbor_q = p + dp, q + dq
if inBoard(neighbor_p, neighbor_q, 13) and board[neighbor_p][neighbor_q] != "":
return True
return False
def connection_crawler(spots, s, visited):
n = 0
for i in spots[s][0]:
if i not in visited and spots[i][1]:
visited.add(i)
n += 1
n += connection_crawler(spots, i, visited)
return n
class Piece:
def __init__(self, p, q, team):
self.p = p
self.q = q
self.team = team
def get_piece_info(self, myisupper):
class_to_letter = {
"Bee": "Q",
"Beetle": "B",
"Spider": "S",
"Grasshopper": "G",
"Ant": "A",
}
letter = class_to_letter[self.__class__.__name__]
if not myisupper:
letter = letter.lower()
return [letter, self.p, self.q]
def get_valid_jumps() -> list:
raise NotImplementedError
class Bee(Piece):
def get_valid_jumps(self, spots, s) -> list:
result = []
for i in spots[s][0]:
if spots[i][1] is None:
hive = False
empty = False
for j in spots[i][0]:
if hive and empty:
break
if j in spots[s][0]:
if spots[j][1] is None:
empty = True
else:
hive = True
if (hive and empty):
result.append(i)
return result
class Beetle(Piece):
def __init__(self, p, q, team, stack=""):
super().__init__(p, q, team)
self.stack = stack
def get_valid_jumps(self, spots, s):
if len(spots[s][1].stack) > 0:
return spots[s][0]
result = []
for i in spots[s][0]:
cs = copy.deepcopy(spots)
cs[s][1] = None
for j in spots[i][0]:
if j in spots.keys() and cs[j][1]:
result.append(i)
break
return result
class Spider(Piece):
def get_valid_jumps(self, spots: dict, s: tuple):
return self.crawl(spots, s, 0, set())
def crawl(self, spots, s, n, visited):
n += 1
visited.add(s)
if n == 4:
return [s]
result = []
for i in spots[s][0]:
hive = False
empty = False
if i not in visited and spots[i][1] is None:
for j in spots[i][0]:
if empty and hive:
break
if j in spots[s][0]:
if spots[j][1] is None or j in visited:
empty = True
else:
hive = True
if (empty and hive):
result += self.crawl(spots, i, n, visited.copy())
return result
class Grasshopper(Piece):
def get_valid_jumps(self, spots, s):
result = []
for i in get_neighbors(0, 0):
q = (s[0]+i[0], s[1]+i[1])
if q in spots.keys() and spots[q][1]:
while True:
q = (q[0]+i[0], q[1]+i[1])
if q in spots.keys() and spots[q][1] is None:
nei = [1 for n in spots[q][0] if n!=s and spots[n][1]]
if sum(nei)>0:
result.append(q)
break
if q not in spots.keys():
break
return result
class Ant(Piece):
def get_valid_jumps(self, spots: dict, s: tuple):
return self.crawl(spots, s, set())
def crawl(self, spots: dict, s: tuple, visited: set):
visited.add(s)
result = []
for i in spots[s][0]:
hive = False
empty = False
if i not in visited and spots[i][1] is None:
for j in spots[i][0]:
if empty and hive:
break
if j in spots[s][0]:
if spots[j][1] is None or j in visited:
empty = True
else:
hive = True
if empty and hive:
result += [i]
result += self.crawl(spots, i, visited)
return result
class Player(Base.Board):
def __init__(
self, playerName, myIsUpper, size, myPieces, rivalPieces
): # do not change this line
Base.Board.__init__(
self, myIsUpper, size, myPieces, rivalPieces
) # do not change this line
self.playerName = playerName
self.algorithmName = "just roll the dice, eh?"
self.spots = dict()
self.figures_player = list()
self.bee = None
self.total_piece_count = 18
self.all_placed = list()
@property
def all_remaining(self):
return self.total_piece_count - sum(list(self.myPieces.values())+list(self.rivalPieces.values()))
@property
def remaining_pieces(self):
return sum(self.myPieces.values())
def connections_checker(self, spots, s):
if isinstance(spots[s][1], Beetle):
if len(spots[s][1].stack) > 0:
return True
for i in spots[s][0]:
if spots[i][1] is None:
continue
visited = set()
visited.add(s)
visited.add(i)
return len(self.all_placed)-2 == connection_crawler(spots, i, visited)
return False
def getAllEmptyCells(self):
result = []
for p in self.board:
for q in self.board[p]:
if self.isEmpty(p, q, self.board):
result.append([p, q])
return result
def getAllNonemptyCells(self):
result = []
for p in self.board:
for q in self.board[p]:
if not self.isEmpty(p, q, self.board):
result.append([p, q])
return result
def translate_board(self):
# mapper dict
piece_class_mapping = {
"Q": Bee,
"B": Beetle,
"S": Spider,
"G": Grasshopper,
"A": Ant,
}
spots = dict()
self.all_placed = list()
figures_player = list()
for p, r in self.board.items():
for q, a in r.items():
surr = [s for s in get_neighbors(
p, q) if self.inBoard(s[0], s[1])]
if a:
if len(a) > 1 and a[-1].lower == "b":
spots[(p,q)] = [surr, piece_class_mapping["B"](p, q, self.myColorIsUpper == a[-1].isupper(), a[:-1])]
else:
spots[(p, q)] = [surr, piece_class_mapping[a[-1].upper()](
p, q, self.myColorIsUpper == a[-1].isupper())]
else:
spots[(p,q)] = [surr, None]
if a and self.myColorIsUpper == a[-1].isupper():
figures_player.append((p, q))
if "Q" in list(map(lambda x: x.upper(),a)):
self.bee = Bee(p, q, True)
if a != "":
self.all_placed.append(piece_class_mapping[a[-1].upper()](
p, q, self.myColorIsUpper == a[-1].isupper()))
self.spots = spots
self.figures_player = figures_player
def get_movable_and_empties(self):
moveable = {}
empty_spots = []
for fig in self.figures_player:
# Empty spots
if self.remaining_pieces != 0:
for i in get_neighbors(fig[0], fig[1]):
if i in self.spots.keys():
if self.spots[i][1] is None:
enemy = False
for j in get_neighbors(i[0], i[1]):
if j in self.spots.keys():
if self.spots[j][1] and self.spots[j][1].team == False:
enemy = True
break
if not enemy:
empty_spots.append(i)
# actual move calc
if self.myMove > 3:
var = self.connections_checker(self.spots, fig)
if var:
animal = self.spots[fig][1]
arr = animal.get_valid_jumps(self.spots, fig)
if (len(arr) > 0):
moveable[fig] = arr
return moveable, empty_spots
def placer(self, empty):
animal = random.choice([a for a, v in self.myPieces.items() if v != 0])
spot = random.choice(empty)
return [animal, None, None, spot[0], spot[1]]
def mover(self, moveable):
animal = random.choice([a for a in moveable.keys()])
jump = random.choice(moveable[animal])
return self.spots[animal][1].get_piece_info(self.myColorIsUpper) + [jump[0], jump[1]]
def move(self):
if self.myMove > 80:
return []
self.translate_board()
free = [6]
if self.bee:
free = [1 for n in get_neighbors(
self.bee.p, self.bee.q) if n in self.spots.keys() and self.spots[n][1] == None]
if sum(free) <= 0:
return []
if self.myMove == 0:
animal = random.choice(list(self.myPieces.keys()))
if self.all_remaining == 0:
return [animal, None, None, 3, 6]
else:
choice = random.choice(get_neighbors(3, 6))
return [animal, None, None, choice[0], choice[1]]
moveable, empty = self.get_movable_and_empties()
if len(moveable) == 0 and len(empty) == 0: # movement impossible
return []
if self.bee == None and (self.myMove == 3 or random.choice([True, False])) and self.myMove<=3:
spot = random.choice(empty)
return ["Q" if self.myColorIsUpper else 'q', None, None, spot[0], spot[1]]
if self.myMove <= 3:
return self.placer(empty)
if self.bee:
if (random.choice([True, False]) or len(moveable) == 0) and self.remaining_pieces:
return self.placer(empty)
else:
return self.mover(moveable)
return moveable
def updatePlayers(move, activePlayer, passivePlayer):
"""write move made by activePlayer player
this method assumes that all moves are correct, no checking is made
"""
if len(move) == 0:
return
animal, p, q, newp, newq = move
if p == None and q == None:
# placing new animal
activePlayer.myPieces[animal] -= 1
passivePlayer.rivalPieces = activePlayer.myPieces.copy()
else:
# just moving animal
# delete its old position
activePlayer.board[p][q] = activePlayer.board[p][q][:-1]
passivePlayer.board[p][q] = passivePlayer.board[p][q][:-1]
activePlayer.board[newp][newq] += animal
passivePlayer.board[newp][newq] += animal
if __name__ == "__main__":
boardSize = 13
smallFigures = {
"q": 1,
"a": 2,
"b": 2,
"s": 2,
"g": 2,
} # key is animal, value is how many is available for placing
bigFigures = {
figure.upper(): smallFigures[figure] for figure in smallFigures
} # same, but with upper case
P1 = Player("player1", False, 13, smallFigures, bigFigures)
P2 = Player("player2", True, 13, bigFigures, smallFigures)
filename = "begin.png"
P1.saveImage(filename)
moveIdx = 0
while True:
move = P1.move()
print("P1 returned", move)
updatePlayers(move, P1, P2) # update P1 and P2 according to the move
filename = "moves/move-{:03d}-player1.png".format(moveIdx)
P1.saveImage(filename)
move = P2.move()
print("P2 returned", move)
updatePlayers(move, P2, P1) # update P2 and P1 according to the move
filename = "moves/move-{:03d}-player2.png".format(moveIdx)
P1.saveImage(filename)
moveIdx += 1
P1.myMove = moveIdx
P2.myMove = moveIdx
if moveIdx > 50:
print("End of the test game")
break

View file

@ -48,10 +48,6 @@ class Piece:
self.q = q
self.team = team
@property
def pos(self):
return (self.p, self.q)
def get_piece_info(self, myisupper):
class_to_letter = {
"Bee": "Q",
@ -65,53 +61,6 @@ class Piece:
letter = letter.lower()
return [letter, self.p, self.q]
def __repr__(self):
return ",".join(map(str, self.get_piece_info(True)+[self.team]))
def __str__(self):
return self.__repr__()
def validate_jumps(self, board):
valid_jumps = self.get_valid_jumps(board)
validated_jumps = []
for jump in valid_jumps:
if self.hive_integrity_check(board):
validated_jumps.append(jump)
return validated_jumps
def hive_integrity_check(self, board):
board_copy = {p: {q: board[p][q] for q in board[p]} for p in board}
# See if the hive falls apart without the piece (if so, then the move wasnt valid)
# Remove the piece from its current position
board_copy[self.p][self.q] = None
# Get all remaining pieces on the board
remaining_pieces = [
piece for row in board_copy.values() for piece in row.values() if piece
]
# Start BFS from a random piece
start_piece = remaining_pieces[0]
visited = set()
queue = deque([start_piece])
while queue:
current_piece = queue.popleft()
visited.add(current_piece)
# Get all neighbors of the current piece
for neighbor_p, neighbor_q in get_neighbors(
current_piece.p, current_piece.q
):
neighbor_piece = board_copy.get(neighbor_p, {}).get(neighbor_q)
if neighbor_piece and neighbor_piece not in visited:
queue.append(neighbor_piece)
# Check if all pieces are connected
return len(visited) == len(remaining_pieces)
def get_valid_jumps() -> list:
raise NotImplementedError
@ -147,15 +96,12 @@ class Beetle(Piece):
return spots[s][0]
result = []
for i in spots[s][0]:
if spots[i][1] is None:
cs = copy.deepcopy(spots)
cs[s][1] = None
for j in spots[i][0]:
if j in spots.keys() and cs[j][1]:
result.append(i)
break
else:
result.append(i)
cs = copy.deepcopy(spots)
cs[s][1] = None
for j in spots[i][0]:
if j in spots.keys() and cs[j][1]:
result.append(i)
break
return result
@ -254,6 +200,9 @@ class Player(Base.Board):
return sum(self.myPieces.values())
def connections_checker(self, spots, s):
if isinstance(spots[s][1], Beetle):
if len(spots[s][1].stack) > 0:
return True
for i in spots[s][0]:
if spots[i][1] is None:
continue
@ -368,9 +317,9 @@ class Player(Base.Board):
if sum(free) <= 0:
return []
if (self.myMove == 0):
if self.myMove == 0:
animal = random.choice(list(self.myPieces.keys()))
if (self.all_remaining == 0):
if self.all_remaining == 0:
return [animal, None, None, 3, 6]
else:
choice = random.choice(get_neighbors(3, 6))
@ -392,6 +341,7 @@ class Player(Base.Board):
return self.placer(empty)
else:
return self.mover(moveable)
return moveable
def updatePlayers(move, activePlayer, passivePlayer):
"""write move made by activePlayer player

25
test.txt Normal file
View file

@ -0,0 +1,25 @@
-2 4 0
-1 2 2
-1 3 0
-1 4 0
0 0 0
0 1 1
0 2 0
0 3 0
0 4 0
1 0 1
1 1 1
1 2 1
1 3 1
1 4 0
2 0 1
2 1 1
2 2 0
2 3 1
2 4 0
3 0 1
3 1 1
3 2 0
3 3 0
4 0 0
4 1 0