-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.py
More file actions
89 lines (77 loc) · 2.52 KB
/
Copy pathboard.py
File metadata and controls
89 lines (77 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from os import system, name
from math import floor
import random
def list_moves(board:list[list[int]])->list[tuple[int, int]]:
size = len(board)
moves = []
for i in range(0, size):
for j in range(0, size):
if board[i][j] == 0:
moves.append((i, j))
random.shuffle(moves) #gives a random ordering
return moves
#initializes board
def initialize_board(player1, player2, size=8)->list[list[int]]:
board = [[0 for i in range(size)] for j in range(size)]
mid = floor(size/2)
board[mid][mid-1] = player1
board[mid-1][mid] = player1
board[mid][mid] = player2
board[mid-1][mid-1] = player2
return board
def count(board:list[list[int]], Player:int)->int: #count of a players pieces on board
size = len(board)
count = 0
for i in range(size):
for j in range(size):
if board[i][j] == Player:
count += 1
return count
def print_board(board:list[list[int]]): #print board
size = len(board)
print(" ", end="")
for i in range(size):
print(i, end=" ")
print()
for i in range(size):
print(i, end=" ")
for j in range(size):
print(board[i][j], end=" ")
print()
def check_valid(board, x, y)->bool: #check if a move is valid (empty cell + in boundaries)
if x<0 or y<0 or x>=len(board) or y>=len(board):
return False
if board[x][y] != 0:
return False
return True
def check_over(board)->bool: #checks if game is over
size = len(board)
for i in range(size):
for j in range(size):
if board[i][j] == 0:
return False
return True
def check_modify(board, x, y)->bool: #util function for step. if its an occupied cell it has a chance of being flipped over after a move.
if x<0 or y<0 or x>=len(board) or y>=len(board):
return False
if board[x][y] == 0:
return False
return True
def step(board : list[list[int]], Player:int, row:int, col:int): #modifies the board after a move. flips pieces accordingly.
board[row][col] = Player
directions = [[1, 0], [0, 1], [1, 1], [-1, 0], [0, -1], [-1, -1], [-1, 1], [1, -1]]
for dir in directions:
dr = dir[0]
dc = dir[1]
tr = row + dr
tc = col + dc
while check_modify(board, tr, tc):
if board[tr][tc] == Player:
while tc != col or tr != row:
board[tr][tc] = Player
tr -= dr
tc -= dc
break
tr += dr
tc += dc
return board