Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Players = ("P1", "P2")
- Size = (8, 7) # Remember: (4n, (4m-1))
- # Will not be modified through the game
- def print_matrix(matrix):
- """Prints 2D matricies vertically
- Will print the outermost entries below instead of beside one another for a list in a list (with constant width)"""
- l = len(matrix)
- i = 0
- while i < l:
- j = 0
- m = len(matrix[0])
- while j < m:
- print("{:0>2X}".format(matrix[i][j]), end=" ")
- j += 1
- print()
- i += 1
- def make_board(h, w): # Corners not included
- """Makes the 2D board
- Will make a 2D matrix with impassable edges and corners. h: height (4*n), w: with (4*n - 1)"""
- if h % 4 != 0 or (w+1) % 4 != 0:
- """Old:
- line = [0]*w
- line[0] = 124 # 124 = 2**2 + 2**3 + 2**4 + 2**5 + 2**6
- line[-1] = 199 # 199 = 2**0 + 2**1 + 2**2 + 2**6 + 2**7
- board = []
- board.append([31]*w) # 31 = 2**0 + 2**1 + 2**2 + 2**3 + 2**4
- for i in range(h-2):
- board.append([b for b in line])
- board.append([241]*w) # 241 = 2**0 + 2**4 + 2**5 + 2**6 + 2**7
- return board"""
- print("Passed! The size is off!")
- return None
- board_side = [[120 - (i % 2)*60, *[0]*(w-2), 195 - (i % 2)*60] for i in range(h)]
- # [56 + ((i+1) % 2)*64 + (i % 2)*4, 135 + ((i+1) % 2)*64 + (i % 2)*4]
- board_topbottom = [[*[30, 15]*((w-3)//4), 28, 10, 7, *[30, 15]*((w-3)//4)], *[[0]*w]*(h-2),
- [*[240, 225]*((w-3)//4), 112, 160, 193, *[240, 225]*((w-3)//4)]]
- # 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
- # 12=2+3+4, 14=1+3, 7=0+1+2,
- board_topbottom[0][0] -= 24 # 24 = 2**3 + 2**4
- board_topbottom[0][-1] -= 3 # 3 = 2**0 + 2**1
- board_topbottom[-1][0] -= 48 # 48 = 2**4 + 2**5
- board_topbottom[-1][-1] -= 129 # 129 = 2**0 + 2**7
- board = [[board_topbottom[i][j] + board_side[i][j] for j in range(w)] for i in range(h)]
- return board
- def get_bit(num, pw):
- """Returns the {pw} bit from {num}
- Only returns 0 or 1. Works with floats or integers (pw < 0 allowed).
- If input is float or pw < 0, it will return float"""
- return (num % 2**(pw+1)) // 2**pw
- def move(direction, board=None, pos=None, validcheck=False):
- """Modifies Board making a move
- Input a direction (0-7, +1 = +pi/4). It will make a move from Pos in the direction chosen and modify values on a
- board = Board.
- If able to move: Both Pos in board and the new Pos will be modified. Returns [board, pos, X] where board is the new
- Board, pos is the new Pos and X will be 0 if illegal move, 1 if only legal, 2 if legal and additional move.
- If validcheck=True will return [board, pos, 0] if invalid move, [None, None, 1] if valid"""
- if board is None:
- board = [b for b in Board]
- if pos is None:
- pos = [b for b in Pos]
- if not get_bit(board[pos[0]][pos[1]], direction) == 0: # Checks if valid move
- return [board, pos, 0]
- newpos = [b for b in pos]
- if direction in {1, 2, 3}:
- newpos[0] += -1
- # print("123")
- elif direction in {5, 6, 7}:
- newpos[0] += 1
- # print("567")
- if direction in {3, 4, 5}:
- newpos[1] += -1
- # print("345")
- elif direction in {7, 0, 1}:
- newpos[1] += 1
- # print("701")
- if get_bit(board[newpos[0]][newpos[1]], ((direction + 4) % 8)) == 0:
- if validcheck:
- return [None, None, 1]
- # print_matrix(board) ###
- # print("newpos: " + repr(newpos)) ###
- # print("pos: " + repr(pos)) ###
- board[pos[0]][pos[1]] += 2**direction
- # print_matrix(board) ###
- if board[newpos[0]][newpos[1]] != 0: # Returns 2 if there already is a line going to/from the new position
- board[newpos[0]][newpos[1]] += 2**((direction + 4) % 8)
- pos = [b for b in newpos]
- return [board, pos, 2]
- board[newpos[0]][newpos[1]] += 2**((direction + 4) % 8)
- pos = [b for b in newpos]
- return [board, pos, 1]
- return [board, pos, 0]
- def get_direction(players=Players, turn=None):
- """Players input a direction 0-7
- Loops until an input from 0-7 is given. Returns the direction as an integer. Will print playername from turn."""
- if turn is None:
- turn = Turn
- while True:
- direction = input("{}s turn, choose direction (0-7)\n".format(players[turn % 2]))
- if direction in {"0", "1", "2", "3", "4", "5", "6", "7"} and len(direction) == 1:
- return int(direction)
- else:
- print("Try again, single numbers, 0-8")
- print_matrix(Board)
- def valid_moves(vm_board=None, vm_pos=None):
- """Checks for valid moves in current position
- Will return True if there are valid moves, False if no valid moves from current board or position.
- May specify other boards or positions than Board and Pos"""
- if vm_board is None:
- vm_board = [b for b in Board]
- if vm_pos is None:
- vm_pos = [b for b in Pos]
- if vm_board[vm_pos[0]][vm_pos[1]] == 255:
- return False
- for d in range(8):
- vm_movedata = move(d, board=vm_board, pos=vm_pos, validcheck=True)[2]
- if vm_movedata == 0:
- continue
- elif vm_movedata == 1:
- return True
- return False
- Turn = 0
- Board = make_board(*Size)
- print_matrix(Board)
- Pos = [i//2 for i in Size] # Den som starter må gå oppover (negativ 1.verdi)
- print(Pos)
- while True: #Main loop
- print_matrix(Board)
- print(Pos)
- if not valid_moves():
- print("No valid moves")
- break
- Movedata = move(get_direction())
- if Movedata[2] == 0:
- print("That is not a valid move, try again")
- continue
- else:
- Board = Movedata[0]
- Pos = Movedata[1]
- if Movedata[2] == 2:
- print("Extra move for {}".format(Players[Turn % 2]))
- continue
- elif Movedata[2] == 1:
- Turn += 1
- continue
- """
- What is left:
- Winning mechanism
- GRAPHICS!!! (Using Pygame)
- Changes:
- Change name and docstring "print_matrix" to "print_board", it does more than just print matricies.
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement