+/HIP

hexagonal iso-path game

git clone https://git.julia.blue/+/HIP

Joseph RyanAdd replay saving, loading, and viewingb9df0d7

v1.0
3.7 KiB131 linesraw
1from engine import *
2from config import *
3
4
5class Move(collections.namedtuple("Move", ["start", "end"])):
6    __slots__ = ()
7
8    def __repr__(self):
9        return str(self.start) + '->' + str(self.end)
10
11
12def createBoard(boardSize):
13    board = {}
14    for q in range(-boardSize, boardSize + 1):
15        for r in range(-boardSize, boardSize + 1):
16            for s in range(-boardSize, boardSize + 1):
17                if q + r + s == 0:
18                    if abs(r) == boardSize:
19                        board[Hex(q, r, s)] = 2 * sign(r)
20                    else:
21                        board[Hex(q, r, s)] = 0
22    return board
23
24
25def getNeighbors(hex):
26    temp = [i for i in hexNeighbors(hex) if hexDistance(i, Hex(0, 0, 0)) <= BOARD_SIZE]
27    teleports = (Hex(-BOARD_SIZE, BOARD_SIZE, 0), Hex(0, BOARD_SIZE, -BOARD_SIZE), Hex(0, -BOARD_SIZE, BOARD_SIZE), Hex(BOARD_SIZE, -BOARD_SIZE, 0), Hex(BOARD_SIZE, 0, -BOARD_SIZE), Hex(-BOARD_SIZE, 0, BOARD_SIZE))
28    for i in range(6):
29        if hex == teleports[i] and teleports[i] not in temp:
30            if i % 2 == 0:
31                temp.append(teleports[i + 1])
32            else:
33                temp.append(teleports[i - 1])
34    return temp
35
36
37def inHomeRow(hex, turn):
38    return hex.r == BOARD_SIZE * turn
39
40
41def getPieceMoves(board, hex, turn):
42    moves = []
43    for i in getNeighbors(hex):
44        if board[i] == turn:
45            moves.append(i)
46    return moves
47
48
49def getMoves(board, turn, pieces):
50    moves = []
51    for i in pieces[(turn + 1) / 2]:
52        moves += [Move(i, j) for j in getPieceMoves(board, i, turn)]
53    return moves
54
55
56def canMovePiece(board, hex, turn):
57    return len(getPieceMoves(board, hex, turn)) > 0
58
59
60def canMove(board, turn, pieces):
61    return len(getMoves(board, turn, pieces)) > 0
62
63
64def piecePickup(board, hex, turn, changeBoard=True):
65    if hex in board and board[hex] == 2 * turn and canMovePiece(board, hex, turn):
66        if changeBoard:
67            board[hex] -= sign(turn)
68        return True
69    return False
70
71
72# assumes that move.start is valid
73def piecePlace(board, move, turn, changeBoard=True):
74    if move.end in getPieceMoves(board, move.start, turn):
75        if changeBoard:
76            board[move.end] = 2 * sign(turn)
77        return True
78    if changeBoard:
79        board[move.start] += turn
80    return False
81
82
83def hexPickup(board, hex, turn, movedPiece, pieces, changeBoard=True):
84    if hex in board and -1 < board[hex] <= 1 and not inHomeRow(hex, turn):
85        board[hex] -= 1
86        if movedPiece or turn == 1 or canMove(board, turn, pieces):
87            if not changeBoard:
88                board[hex] += 1
89            return True
90        board[hex] += 1
91    return False
92
93
94# assumes that move.start is valid
95def hexPlace(board, move, turn, movedPiece, pieces, changeBoard=True):
96    if move.end in board and -1 <= board[move.end] < 1 and not inHomeRow(move.end, turn) and move.start != move.end:
97        board[move.end] += 1
98        if movedPiece or canMove(board, turn, pieces):
99            if not changeBoard:
100                board[move.end] -= 1
101            return True
102        board[move.end] -= 1
103    if changeBoard:
104        board[move.start] += 1
105    return False
106
107
108def getPieces(board):
109    pieces = ([], [])
110    for h in board:
111        if abs(board[h]) == 2:
112            pieces[(board[h] + 2) / 4].append(h)
113    return pieces
114
115
116def getThreatened(turn, pieces):
117    temp = []
118    for i in pieces[(-turn + 1) / 2]:
119        if [j in pieces[(turn + 1) / 2] for j in getNeighbors(i)].count(True) >= 2:
120            temp.append(i)
121    return temp
122
123
124def hasWon(pieces):
125    for i in range(2):
126        if pieces[i] == []:
127            return i * 2 - 1
128        for j in pieces[i]:
129            if inHomeRow(j, -(i * 2 - 1)):
130                return i * 2 - 1
131    return 0