Advertisement
MarLind

Linje-spill

Feb 19th, 2016
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.26 KB | None | 0 0
  1. Players = ("P1", "P2")
  2. Size = (8, 7)  # Remember: (4n, (4m-1))
  3. # Will not be modified through the game
  4.  
  5. def print_matrix(matrix):
  6.     """Prints 2D matricies vertically
  7.  
  8.    Will print the outermost entries below instead of beside one another for a list in a list (with constant width)"""
  9.     l = len(matrix)
  10.     i = 0
  11.     while i < l:
  12.         j = 0
  13.         m = len(matrix[0])
  14.         while j < m:
  15.             print("{:0>2X}".format(matrix[i][j]), end="  ")
  16.             j += 1
  17.         print()
  18.         i += 1
  19.  
  20.  
  21. def make_board(h, w):   # Corners not included
  22.     """Makes the 2D board
  23.  
  24.    Will make a 2D matrix with impassable edges and corners. h: height (4*n), w: with (4*n - 1)"""
  25.     if h % 4 != 0 or (w+1) % 4 != 0:
  26.         """Old:
  27.        line = [0]*w
  28.        line[0] = 124       # 124 = 2**2 + 2**3 + 2**4 + 2**5 + 2**6
  29.        line[-1] = 199      # 199 = 2**0 + 2**1 + 2**2 + 2**6 + 2**7
  30.        board = []
  31.        board.append([31]*w)      # 31 = 2**0 + 2**1 + 2**2 + 2**3 + 2**4
  32.        for i in range(h-2):
  33.            board.append([b for b in line])
  34.        board.append([241]*w)   # 241 = 2**0 + 2**4 + 2**5 + 2**6 + 2**7
  35.        return board"""
  36.  
  37.         print("Passed! The size is off!")
  38.         return None
  39.  
  40.     board_side = [[120 - (i % 2)*60, *[0]*(w-2), 195 - (i % 2)*60] for i in range(h)]
  41.     # [56 + ((i+1) % 2)*64 + (i % 2)*4, 135 + ((i+1) % 2)*64 + (i % 2)*4]
  42.     board_topbottom = [[*[30, 15]*((w-3)//4), 28, 10, 7, *[30, 15]*((w-3)//4)], *[[0]*w]*(h-2),
  43.                        [*[240, 225]*((w-3)//4), 112, 160, 193, *[240, 225]*((w-3)//4)]]
  44.     # Change depending on winning mechanism, going up/down is possible, 30=1+2+3+4, 15=0+1+2+3, 240=4+5+6+7, 225=1+5+6+7
  45.     # 12=2+3+4, 14=1+3, 7=0+1+2,
  46.     board_topbottom[0][0] -= 24  # 24 = 2**3 + 2**4
  47.     board_topbottom[0][-1] -= 3  # 3 = 2**0 + 2**1
  48.     board_topbottom[-1][0] -= 48  # 48 = 2**4 + 2**5
  49.     board_topbottom[-1][-1] -= 129  # 129 = 2**0 + 2**7
  50.     board = [[board_topbottom[i][j] + board_side[i][j] for j in range(w)] for i in range(h)]
  51.     return board
  52.  
  53.  
  54. def get_bit(num, pw):
  55.     """Returns the {pw} bit from {num}
  56.  
  57.    Only returns 0 or 1. Works with floats or integers (pw < 0 allowed).
  58.    If input is float or pw < 0, it will return float"""
  59.  
  60.     return (num % 2**(pw+1)) // 2**pw
  61.  
  62.  
  63. def move(direction, board=None, pos=None, validcheck=False):
  64.     """Modifies Board making a move
  65.  
  66.    Input a direction (0-7, +1 = +pi/4). It will make a move from Pos in the direction chosen and modify values on a
  67.    board = Board.
  68.    If able to move: Both Pos in board and the new Pos will be modified. Returns [board, pos, X] where board is the new
  69.    Board, pos is the new Pos and X will be 0 if illegal move, 1 if only legal, 2 if legal and additional move.
  70.    If validcheck=True will return [board, pos, 0] if invalid move, [None, None, 1] if valid"""
  71.     if board is None:
  72.         board = [b for b in Board]
  73.     if pos is None:
  74.         pos = [b for b in Pos]
  75.  
  76.     if not get_bit(board[pos[0]][pos[1]], direction) == 0: # Checks if valid move
  77.         return [board, pos, 0]
  78.     newpos = [b for b in pos]
  79.  
  80.     if direction in {1, 2, 3}:
  81.         newpos[0] += -1
  82.         # print("123")
  83.     elif direction in {5, 6, 7}:
  84.         newpos[0] += 1
  85.         # print("567")
  86.     if direction in {3, 4, 5}:
  87.         newpos[1] += -1
  88.         # print("345")
  89.     elif direction in {7, 0, 1}:
  90.         newpos[1] += 1
  91.         # print("701")
  92.  
  93.     if get_bit(board[newpos[0]][newpos[1]], ((direction + 4) % 8)) == 0:
  94.         if validcheck:
  95.             return [None, None, 1]
  96.         # print_matrix(board)  ###
  97.         # print("newpos: " + repr(newpos))  ###
  98.         # print("pos:    " + repr(pos))  ###
  99.  
  100.         board[pos[0]][pos[1]] += 2**direction
  101.         # print_matrix(board)  ###
  102.         if board[newpos[0]][newpos[1]] != 0: # Returns 2 if there already is a line going to/from the new position
  103.             board[newpos[0]][newpos[1]] += 2**((direction + 4) % 8)
  104.             pos = [b for b in newpos]
  105.             return [board, pos, 2]
  106.         board[newpos[0]][newpos[1]] += 2**((direction + 4) % 8)
  107.         pos = [b for b in newpos]
  108.         return [board, pos, 1]
  109.  
  110.     return [board, pos, 0]
  111.  
  112.  
  113. def get_direction(players=Players, turn=None):
  114.     """Players input a direction 0-7
  115.  
  116.    Loops until an input from 0-7 is given. Returns the direction as an integer. Will print playername from turn."""
  117.     if turn is None:
  118.         turn = Turn
  119.     while True:
  120.         direction = input("{}s turn, choose direction (0-7)\n".format(players[turn % 2]))
  121.         if direction in {"0", "1", "2", "3", "4", "5", "6", "7"} and len(direction) == 1:
  122.             return int(direction)
  123.         else:
  124.             print("Try again, single numbers, 0-8")
  125.             print_matrix(Board)
  126.  
  127.  
  128. def valid_moves(vm_board=None, vm_pos=None):
  129.     """Checks for valid moves in current position
  130.  
  131.    Will return True if there are valid moves, False if no valid moves from current board or position.
  132.    May specify other boards or positions than Board and Pos"""
  133.     if vm_board is None:
  134.         vm_board = [b for b in Board]
  135.     if vm_pos is None:
  136.         vm_pos = [b for b in Pos]
  137.  
  138.     if vm_board[vm_pos[0]][vm_pos[1]] == 255:
  139.         return False
  140.     for d in range(8):
  141.         vm_movedata = move(d, board=vm_board, pos=vm_pos, validcheck=True)[2]
  142.         if vm_movedata == 0:
  143.             continue
  144.         elif vm_movedata == 1:
  145.             return True
  146.     return False
  147.  
  148. Turn = 0
  149. Board = make_board(*Size)
  150. print_matrix(Board)
  151. Pos = [i//2 for i in Size]  # Den som starter må gå oppover (negativ 1.verdi)
  152. print(Pos)
  153.  
  154.  
  155. while True:  #Main loop
  156.     print_matrix(Board)
  157.     print(Pos)
  158.     if not valid_moves():
  159.         print("No valid moves")
  160.         break
  161.  
  162.     Movedata = move(get_direction())
  163.     if Movedata[2] == 0:
  164.         print("That is not a valid move, try again")
  165.         continue
  166.     else:
  167.         Board = Movedata[0]
  168.         Pos = Movedata[1]
  169.     if Movedata[2] == 2:
  170.         print("Extra move for {}".format(Players[Turn % 2]))
  171.         continue
  172.     elif Movedata[2] == 1:
  173.         Turn += 1
  174.         continue
  175.  
  176. """
  177. What is left:
  178. Winning mechanism
  179. GRAPHICS!!! (Using Pygame)
  180.  
  181. Changes:
  182. Change name and docstring "print_matrix" to "print_board", it does more than just print matricies.
  183. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement