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