aboutsummaryrefslogtreecommitdiff
path: root/voerbak
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-04-11 17:50:58 +0200
committerlonkaars <l.leblansch@gmail.com>2021-04-11 17:50:58 +0200
commit28f104de9ae9abe4b42abafbf3865ede5687996c (patch)
tree65e651f09d8fbf81380384692e45803cb4f9d61c /voerbak
parent7b4859059b3bbabf4139ccdf3270a82c094f5d8e (diff)
dprint yapf python formatting
Diffstat (limited to 'voerbak')
-rw-r--r--voerbak/main.py149
-rw-r--r--voerbak/test.py8
-rw-r--r--voerbak/v2.py175
3 files changed, 171 insertions, 161 deletions
diff --git a/voerbak/main.py b/voerbak/main.py
index 433f130..d802603 100644
--- a/voerbak/main.py
+++ b/voerbak/main.py
@@ -6,85 +6,92 @@ DISC_A = Fore.RED + DISC_SHAPE + Fore.RESET
DISC_B = Fore.BLUE + DISC_SHAPE + Fore.RESET
EMPTY = Fore.LIGHTBLACK_EX + "_" + Fore.RESET
+
class bord:
- def __init__(self, w, h):
- self.width = w
- self.height = h
- self.board = [[EMPTY for x in range(self.width)] for u in range(self.height)]
+ def __init__(self, w, h):
+ self.width = w
+ self.height = h
+ self.board = [
+ [EMPTY for x in range(self.width)] for u in range(self.height)
+ ]
+
+ def print(self):
+ print(
+ "\n".join(
+ [
+ " ".join(self.board[y])
+ for y in range(len(self.board) - 1, -1, -1)
+ ]
+ )
+ )
- def print(self):
- print("\n".join([" ".join(self.board[y]) for y in range(len(self.board) -1, -1, -1)]))
+ def outside_board(self, coords):
+ return coords[0] < 0 or \
+ coords[1] < 0 or \
+ coords[0] > self.height - 1 or \
+ coords[1] > self.width - 1
- def outside_board(self, coords):
- return coords[0] < 0 or \
- coords[1] < 0 or \
- coords[0] > self.height - 1 or \
- coords[1] > self.width - 1
+ def recursive_solve(self, coords, check_for, direction, current_length):
+ new_position = (coords[0] + direction[0], coords[1] + direction[1])
+ if self.outside_board(new_position) or self.board[new_position[0]][
+ new_position[1]] != check_for:
+ return current_length
+ else:
+ return self.recursive_solve(
+ new_position, check_for, direction, current_length + 1
+ )
- def recursive_solve(self, coords, check_for, direction, current_length):
- new_position = (
- coords[0] + direction[0],
- coords[1] + direction[1]
- )
- if self.outside_board(new_position) or self.board[new_position[0]][new_position[1]] != check_for:
- return current_length
- else:
- return self.recursive_solve(new_position, check_for, direction, current_length + 1)
+ def check_win(self, coords):
+ directions = [
+ (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1),
+ (1, -1)
+ ]
+ values = list()
+ for direction in directions:
+ values.append(
+ self.recursive_solve(
+ coords, self.board[coords[0]][coords[1]], direction, 0
+ )
+ )
+ joined_directions = [
+ values[0] + values[4], values[1] + values[5],
+ values[2] + values[6], values[3] + values[7]
+ ]
+ won = any(i >= 3 for i in joined_directions)
+ if won:
+ for i, value in enumerate(joined_directions):
+ if value >= 3:
+ start_pos = (
+ coords[0] + directions[i][0] * values[i],
+ coords[1] + directions[i][1] * values[i],
+ )
+ end_pos = (
+ coords[0] + directions[i + 4][0] * values[i + 4],
+ coords[1] + directions[i + 4][1] * values[i + 4],
+ )
+ print(start_pos, end_pos)
- def check_win(self, coords):
- directions = [
- ( 1, 0),
- ( 1, 1),
- ( 0, 1),
- (-1, 1),
- (-1, 0),
- (-1, -1),
- ( 0, -1),
- ( 1, -1)
- ]
- values = list()
- for direction in directions:
- values.append(self.recursive_solve(coords, self.board[coords[0]][coords[1]], direction, 0))
- joined_directions = [
- values[0] + values[4],
- values[1] + values[5],
- values[2] + values[6],
- values[3] + values[7]
- ]
- won = any(i >= 3 for i in joined_directions)
- if won:
- for i, value in enumerate(joined_directions):
- if value >= 3:
- start_pos = (
- coords[0] + directions[i][0] * values[i],
- coords[1] + directions[i][1] * values[i],
- )
- end_pos = (
- coords[0] + directions[i+4][0] * values[i+4],
- coords[1] + directions[i+4][1] * values[i+4],
- )
- print(start_pos, end_pos)
+ return won
- return won
+ def drop_fisje(self, column, disc):
+ for row, value in enumerate(self.board):
+ if self.board[row][column] == EMPTY:
+ self.board[row][column] = disc
+ won = self.check_win((row, column))
+ print(won)
+ return
- def drop_fisje(self, column, disc):
- for row, value in enumerate(self.board):
- if self.board[row][column] == EMPTY:
- self.board[row][column] = disc
- won = self.check_win((row, column))
- print(won)
- return
def main():
- disc_a = True
- gert = bord(11, 8)
- while True:
- gert.print()
- column = int(input("column?: ")) - 1
- os.system("clear")
- gert.drop_fisje(column, DISC_A if disc_a else DISC_B)
- disc_a = not disc_a
+ disc_a = True
+ gert = bord(11, 8)
+ while True:
+ gert.print()
+ column = int(input("column?: ")) - 1
+ os.system("clear")
+ gert.drop_fisje(column, DISC_A if disc_a else DISC_B)
+ disc_a = not disc_a
-if __name__ == "__main__":
- main()
+if __name__ == "__main__":
+ main()
diff --git a/voerbak/test.py b/voerbak/test.py
index 8cc1db9..6481817 100644
--- a/voerbak/test.py
+++ b/voerbak/test.py
@@ -4,10 +4,9 @@ w = 7
h = 6
column = 3
-process = subprocess.Popen(["./voerbak"],
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- stderr=None)
+process = subprocess.Popen(
+ ["./voerbak"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None
+)
process.stdin.write(bytearray(f"{w} {h}\n", "utf-8"))
process.stdin.flush()
@@ -21,4 +20,3 @@ process.stdin.flush()
# sys.stdout.write(c)
print(process.stdout.readlines(5))
-
diff --git a/voerbak/v2.py b/voerbak/v2.py
index 69e61aa..26030f5 100644
--- a/voerbak/v2.py
+++ b/voerbak/v2.py
@@ -7,99 +7,104 @@ DISC_A = Fore.RED + DISC_SHAPE + Fore.RESET
DISC_B = Fore.BLUE + DISC_SHAPE + Fore.RESET
EMPTY = Fore.LIGHTBLACK_EX + "_" + Fore.RESET
+
class bord:
- def __init__(self, w, h):
- self.width = w
- self.height = h
- self.board = [EMPTY] * (w * h)
+ def __init__(self, w, h):
+ self.width = w
+ self.height = h
+ self.board = [EMPTY] * (w * h)
+
+ def print(self):
+ for y in range(self.height - 1, -1, -1):
+ for x in range(self.width):
+ print(self.board[x + y * self.width], end=" ")
+ print("\n", end="")
- def print(self):
- for y in range(self.height -1, -1, -1):
- for x in range(self.width):
- print(self.board[x + y * self.width], end=" ")
- print("\n", end="")
+ def recursive_solve(self, pos, check_for, direction, current_length):
+ overflow = (pos % self.width) + direction
+ if overflow == self.width or overflow == -1: # horizontal overflow
+ return current_length
+ new_position = pos + direction
+ if new_position < 0 or new_position > self.width * self.height - 1: # vertical overflow
+ return current_length
+ if self.board[new_position] != check_for:
+ return current_length
+ return self.recursive_solve(
+ new_position, check_for, direction, current_length + 1
+ )
- def recursive_solve(self, pos, check_for, direction, current_length):
- overflow = (pos % self.width) + direction
- if overflow == self.width or overflow == -1: # horizontal overflow
- return current_length
- new_position = pos + direction
- if new_position < 0 or new_position > self.width * self.height - 1: # vertical overflow
- return current_length
- if self.board[new_position] != check_for:
- return current_length
- return self.recursive_solve(new_position, check_for, direction, current_length + 1)
+ def check_win(self, pos):
+ directions = [
+ self.width, # north
+ self.width + 1, # northeast
+ 1, # east
+ -self.width + 1, # southeast
+ -self.width, # south
+ -self.width - 1, # southwest
+ -1, # west
+ self.width - 1, # northwest
+ ]
+ values = list()
+ for direction in directions:
+ values.append(
+ self.recursive_solve(pos, self.board[pos], direction, 0)
+ )
+ joined_directions = [
+ values[0] + values[4], values[1] + values[5],
+ values[2] + values[6], values[3] + values[7]
+ ]
+ won = any(i >= 3 for i in joined_directions)
+ if won:
+ for i, value in enumerate(joined_directions):
+ if value >= 3:
+ start_pos = pos + directions[i] * values[i]
+ end_pos = pos + directions[i + 4] * values[i + 4]
+ print(start_pos, end_pos)
+ self.board[start_pos] = "x"
+ self.board[end_pos] = "x"
+ return won
- def check_win(self, pos):
- directions = [
- self.width, # north
- self.width + 1, # northeast
- 1, # east
- -self.width + 1, # southeast
- -self.width, # south
- -self.width - 1, # southwest
- -1, # west
- self.width - 1, # northwest
- ]
- values = list()
- for direction in directions:
- values.append(self.recursive_solve(pos, self.board[pos], direction, 0))
- joined_directions = [
- values[0] + values[4],
- values[1] + values[5],
- values[2] + values[6],
- values[3] + values[7]
- ]
- won = any(i >= 3 for i in joined_directions)
- if won:
- for i, value in enumerate(joined_directions):
- if value >= 3:
- start_pos = pos + directions[i] * values[i]
- end_pos = pos + directions[i+4] * values[i+4]
- print(start_pos, end_pos)
- self.board[start_pos] = "x"
- self.board[end_pos] = "x"
- return won
+ def debug(self, pos):
+ self.board[pos] = "x"
+ directions = [
+ self.width, # 0: north
+ self.width + 1, # 1: northeast
+ 1, # 2: east
+ -self.width + 1, # 3: southeast
+ -self.width, # 4: south
+ -self.width - 1, # 5: southwest
+ -1, # 6: west
+ self.width - 1, # 7: northwest
+ ]
- def debug(self, pos):
- self.board[pos] = "x"
- directions = [
- self.width, # 0: north
- self.width + 1, # 1: northeast
- 1, # 2: east
- -self.width + 1, # 3: southeast
- -self.width, # 4: south
- -self.width - 1, # 5: southwest
- -1, # 6: west
- self.width - 1, # 7: northwest
- ]
+ for index, direction in enumerate(directions):
+ new_position = pos + direction
+ if new_position > len(self.board) - 1 or new_position < 0: continue
+ if index in range(1, 4) and pos % self.width == self.width - 1:
+ continue
+ if index in range(5, 8) and pos % self.width == 0: continue
+ self.board[new_position] = "o"
- for index, direction in enumerate(directions):
- new_position = pos + direction
- if new_position > len(self.board) - 1 or new_position < 0: continue
- if index in range(1, 4) and pos % self.width == self.width -1: continue
- if index in range(5, 8) and pos % self.width == 0: continue
- self.board[new_position] = "o"
+ def drop_fisje(self, column, disc):
+ for row in range(self.height):
+ pos = column + row * self.width
+ if self.board[pos] == EMPTY:
+ self.board[pos] = disc
+ won = self.check_win(pos)
+ print(won)
+ return
- def drop_fisje(self, column, disc):
- for row in range(self.height):
- pos = column + row * self.width
- if self.board[pos] == EMPTY:
- self.board[pos] = disc
- won = self.check_win(pos)
- print(won)
- return
def main():
- disc_a = True
- gert = bord(7, 6)
- for x in range(len(gert.board)):
- gert = bord(7, 6)
- gert.debug(x)
- gert.print()
- print("\n\n", end='')
- time.sleep(0.1)
+ disc_a = True
+ gert = bord(7, 6)
+ for x in range(len(gert.board)):
+ gert = bord(7, 6)
+ gert.debug(x)
+ gert.print()
+ print("\n\n", end='')
+ time.sleep(0.1)
-if __name__ == "__main__":
- main()
+if __name__ == "__main__":
+ main()