• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Dama

a guest Sep 21st, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import math
2.
3. board = [
4.     ['-', 'x', '-', 'x', '-', 'x', '-', 'x'],
5.     ['x', '-', 'x', '-', 'x', '-', 'x', '-'],
6.     ['-', 'x', '-', 'x', '-', 'x', '-', 'x'],
7.     ['-', '-', '-', '-', '-', '-', '-', '-'],
8.     ['-', '-', '-', '-', '-', '-', '-', '-'],
9.     ['o', '-', 'o', '-', 'o', '-', 'o', '-'],
10.     ['-', 'o', '-', 'o', '-', 'o', '-', 'o'],
11.     ['o', '-', 'o', '-', 'o', '-', 'o', '-']
12. ]
13. pieces = {0:12,1:12}
14.
15.
16. def printboard(board):
17.     for i in range(len(board)):
18.         for v in board[i]:
19.             print(v + " ", end='')
20.         print(8 - i)
21.     for i in range(len(board)):
22.         print(chr(65 + i) + " ", end='')
23.
24.     print()
25.
26.
27. def convertnotationtolistformat(source, dest):
28.     return [letterindextointegerindex(source[0]) - 65, 8 - int(source[1]), letterindextointegerindex(dest[0]) - 65,
29.             8 - int(dest[1])]
30.
31.
32. def inputplayer1():
33.     return input('Light pieces to play. Enter your move: ')
34.
35.
36. def inputplayer2():
37.     return input('Dark pieces to play. Enter your move: ')
38.
39.
40. def letterindextointegerindex(character):
41.     return ord(character.capitalize())
42.
43.
44. def piece(location):
45.     destination_piece = board[location[2]][location[3]]
46.     source_piece = board[location[1]][location[2]]
47.     if destination_piece == source_piece:
48.         return -1
49.
50.
51.
52.
53.
54.
55.
56. def swap(board, location):
57.     piece = board[location[1]][location[0]]
58.     board[location[1]][location[0]] = board[location[3]][location[2]]
59.     board[location[3]][location[2]] = piece
60.
61. #
62. # -1 invalid
63. # 1 moved
64. # 2 captured
65. # 3 Promoted to king
66. #
67.
68.
69. def updateboard(location, is_player_1):
70.     destination_piece = board[location[3]][location[2]]
71.     source_piece = board[location[1]][location[0]]
72.
73.     rowS = location[1] # 3
74.     colS = location[0] # A
75.     rowD = location[3]
76.     colD = location[2]
77.
78.     if ((colS + 1) == colD or (colS - 1) == colD) and ((rowS + 1) == rowD or (rowS - 1) == rowD):
79.         if destination_piece == '-':
80.             swap(board, location)
81.             return 1
82.         else:
83.             return -1
84.     else:
85.         return -1
86.
87.     # if (colS + 2 == colD or colS - 2 == colD) and (rowS + 2 == rowD or rowS - 2 == rowD):
88.     #     if destination_piece == '-':
89.     #         if source_piece != board[colS + 2][rowS + 2] or
90.     #     elif ~is_player1_turn and destination_piece:
91.     #         return -1
92.     #
93.     # else
94.
95.
96. def main():
97.     print("Welcome to Checkers!")
98.     printboard(board)
99.     is_player1_turn = True
100.     while pieces[0] > 0 or pieces[1] > 0:
101.
102.         if is_player1_turn:
103.             input_notation = inputplayer1()
104.         else:
105.             input_notation = inputplayer2()
106.
107.         splittedInput = input_notation.split(" ")
108.         source = splittedInput[0]
109.         dest = splittedInput[1]
110.
111.         result = updateboard(convertnotationtolistformat(source, dest), is_player1_turn)
112.         if result == -1:
113.             print("Invalid move!")
114.             continue
115.         elif result == 1:
116.             is_player1_turn = ~is_player1_turn
117.             printboard(board)
118.         elif result == 2:
119.             continue
120.         else:
121.             continue
122.
123.
124.
125. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top