Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- board = [
- ['-', 'x', '-', 'x', '-', 'x', '-', 'x'],
- ['x', '-', 'x', '-', 'x', '-', 'x', '-'],
- ['-', 'x', '-', 'x', '-', 'x', '-', 'x'],
- ['-', '-', '-', '-', '-', '-', '-', '-'],
- ['-', '-', '-', '-', '-', '-', '-', '-'],
- ['o', '-', 'o', '-', 'o', '-', 'o', '-'],
- ['-', 'o', '-', 'o', '-', 'o', '-', 'o'],
- ['o', '-', 'o', '-', 'o', '-', 'o', '-']
- ]
- pieces = {0:12,1:12}
- def printboard(board):
- for i in range(len(board)):
- for v in board[i]:
- print(v + " ", end='')
- print(8 - i)
- for i in range(len(board)):
- print(chr(65 + i) + " ", end='')
- print()
- def convertnotationtolistformat(source, dest):
- return [letterindextointegerindex(source[0]) - 65, 8 - int(source[1]), letterindextointegerindex(dest[0]) - 65,
- 8 - int(dest[1])]
- def inputplayer1():
- return input('Light pieces to play. Enter your move: ')
- def inputplayer2():
- return input('Dark pieces to play. Enter your move: ')
- def letterindextointegerindex(character):
- return ord(character.capitalize())
- def piece(location):
- destination_piece = board[location[2]][location[3]]
- source_piece = board[location[1]][location[2]]
- if destination_piece == source_piece:
- return -1
- def swap(board, location):
- piece = board[location[1]][location[0]]
- board[location[1]][location[0]] = board[location[3]][location[2]]
- board[location[3]][location[2]] = piece
- #
- # -1 invalid
- # 1 moved
- # 2 captured
- # 3 Promoted to king
- #
- def updateboard(location, is_player_1):
- destination_piece = board[location[3]][location[2]]
- source_piece = board[location[1]][location[0]]
- rowS = location[1] # 3
- colS = location[0] # A
- rowD = location[3]
- colD = location[2]
- if ((colS + 1) == colD or (colS - 1) == colD) and ((rowS + 1) == rowD or (rowS - 1) == rowD):
- if destination_piece == '-':
- swap(board, location)
- return 1
- else:
- return -1
- else:
- return -1
- # if (colS + 2 == colD or colS - 2 == colD) and (rowS + 2 == rowD or rowS - 2 == rowD):
- # if destination_piece == '-':
- # if source_piece != board[colS + 2][rowS + 2] or
- # elif ~is_player1_turn and destination_piece:
- # return -1
- #
- # else
- def main():
- print("Welcome to Checkers!")
- printboard(board)
- is_player1_turn = True
- while pieces[0] > 0 or pieces[1] > 0:
- if is_player1_turn:
- input_notation = inputplayer1()
- else:
- input_notation = inputplayer2()
- splittedInput = input_notation.split(" ")
- source = splittedInput[0]
- dest = splittedInput[1]
- result = updateboard(convertnotationtolistformat(source, dest), is_player1_turn)
- if result == -1:
- print("Invalid move!")
- continue
- elif result == 1:
- is_player1_turn = ~is_player1_turn
- printboard(board)
- elif result == 2:
- continue
- else:
- continue
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement