Advertisement
Towl

chkrs12 checkfor win added, fixed bugs

Jan 28th, 2020
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.74 KB | None | 0 0
  1. reset = "\033[0m"
  2. red = "\033[0;31m"
  3. green = "\033[0;32m"
  4. yellow = "\033[0;33m"
  5. blue = "\033[0;34m"
  6. purple = "\033[0;35m"
  7. BRed = "\033[1;31m"
  8. BYellow = "\033[1;33m"
  9. BBlue = "\033[1;34m"
  10. BPurple = "\033[1;35m"
  11. On_Black = "\033[40m"
  12. BIRed = "\033[1;91m"
  13. BIYellow = "\033[1;93m"
  14. BIBlue = "\033[1;94m"
  15. BIPurple = "\033[1;95m"
  16. IWhite = "\033[0;97m"
  17. black = "\033[0;30m"
  18. iblack = "\033[0;90m"
  19.  
  20. # TO-DO:
  21. # force_eat ask, random computer, reselect, undo move, reset board, start game etc.
  22.  
  23. p1 = f"{red}P1{reset}"
  24. p2 = f"{blue}P2{reset}"
  25. p1k = "p1k"
  26. p2k = "p2k"
  27. p1l = [p1, p1k]
  28. p2l = [p2, p2k]
  29.  
  30. # Default board reference
  31. d_board = ["0", "1", "2", "3", "4", "5", "6", "7",
  32.            "8", "9", "10", "11", "12", "13", "14", "15",
  33.            "16", "17", "18", "19", "20", "21", "22", "23",
  34.            "24", "25", "26", "27", "28", "29", "30", "31",
  35.            "32", "33", "34", "35", "36", "37", "38", "39",
  36.            "40", "41", "42", "43", "44", "45", "46", "47",
  37.            "48", "49", "50", "51", "52", "53", "54", "55",
  38.            "56", "57", "58", "59", "60", "61", "62", "63"]
  39.  
  40. available_spots = ["1", "3", "5", "7",
  41.                    "8", "10", "12", "14",
  42.                    "17", "19", "21", "23",
  43.                    "24", "26", "28", "30",
  44.                    "33", "35", "37", "39",
  45.                    "40", "42", "44", "46",
  46.                    "49", "51", "53", "55",
  47.                    "56", "58", "60", "62"]
  48.  
  49. # Realtime board and reset
  50. boardr = ["0", p2, "2", p2, "4", p2, "6", p2,
  51.           p2, "9", p2, "11", p2, "13", p2, "15",
  52.           "16", p2, "18", p2, "20", p2, "22", p2,
  53.           "24", "25", "26", "27", "28", "29", "30", "31",
  54.           "32", "33", "34", "35", "36", "37", "38", "39",
  55.           p1, "41", p1, "43", p1, "45", p1, "47",
  56.           "48", p1, "50", p1, "52", p1, "54", p1,
  57.           p1, "57", p1, "59", p1, "61", p1, "63"]
  58.  
  59. # TESTING BOARD
  60. board = ["0", "1", "2", "3", "4", "5", "6", "7",
  61.            "8", "9", p2, "11", "12", "13", "14", "15",
  62.            "16", "17", "18", "19", "20", "21", "22", "23",
  63.            "24", "25", "26", "27", "28", "29", "30", "31",
  64.            "32", p1, "34", p1, "36", "37", "38", "39",
  65.            p1, "41", p1, "43", p1, "45", "46", "47",
  66.            "48", "49", "50", "51", "52", "53", "54", "55",
  67.            "56", "57", "58", "59", "60", "61", "62", "63"]
  68.  
  69.  
  70. def display_board():
  71.     b = []
  72.     p1_pieces = [each for each in board if each in p1l]
  73.     p1_pieces = len(p1_pieces)
  74.     p2_pieces = [each for each in board if each in p2l]
  75.     p2_pieces = len(p2_pieces)
  76.     spaces = " " * ((p1_pieces < 10) + (p2_pieces < 10))
  77.     for index, each in enumerate(board):
  78.         color2 = purple # available_pieces color
  79.         color3 = yellow # selected piece color
  80.         if each == p1:
  81.             color = red
  82.         elif each == p2:
  83.             color = blue
  84.         elif each == p1k:
  85.             color = BIRed
  86.             color2 = BIPurple
  87.             color3 = BIYellow
  88.         elif each == p2k:
  89.             color = BIBlue
  90.             color2 = BIPurple
  91.             color3 = BIYellow
  92.         elif each.isdigit() and each in available_spots:
  93.             color = iblack  # available squares
  94.         else:
  95.             color = black  # unused squares
  96.         if index < 10:
  97.             findex = f"0{index}"
  98.         else:
  99.             findex = f"{index}"
  100.  
  101.         if selection_process is True:
  102.             if index in available_pieces:
  103.                 b.append(f"{color}[{color2}{findex}{color}]")
  104.             else:
  105.                 b.append(f"{color}[{findex}]")
  106.         elif cord_process is True:
  107.             if index == int(selection):
  108.                 b.append(f"{color}[{color3}{findex}{color}]")
  109.             elif index in available_cords:
  110.                 b.append(f"{iblack}[{green}{findex}{iblack}]")
  111.             else:
  112.                 b.append(f"{color}[{findex}]")
  113.         else:
  114.             b.append(f"{color}[{findex}]")
  115.     print(f"""
  116. {On_Black}                                    {reset}
  117. {On_Black}  {reset} {current_player}{reset}'s turn.              {spaces}{red}{p1_pieces}{reset} - {blue}{p2_pieces}{On_Black}  {reset}                    
  118. {On_Black}                                    {reset}
  119. {On_Black}  {b[0]}{b[1]}{b[2]}{b[3]}{b[4]}{b[5]}{b[6]}{b[7]}{On_Black}  {reset}
  120. {On_Black}  {b[8]}{b[9]}{b[10]}{b[11]}{b[12]}{b[13]}{b[14]}{b[15]}{On_Black}  {reset}
  121. {On_Black}  {b[16]}{b[17]}{b[18]}{b[19]}{b[20]}{b[21]}{b[22]}{b[23]}{On_Black}  {reset}
  122. {On_Black}  {b[24]}{b[25]}{b[26]}{b[27]}{b[28]}{b[29]}{b[30]}{b[31]}{On_Black}  {reset}
  123. {On_Black}  {b[32]}{b[33]}{b[34]}{b[35]}{b[36]}{b[37]}{b[38]}{b[39]}{On_Black}  {reset}
  124. {On_Black}  {b[40]}{b[41]}{b[42]}{b[43]}{b[44]}{b[45]}{b[46]}{b[47]}{On_Black}  {reset}
  125. {On_Black}  {b[48]}{b[49]}{b[50]}{b[51]}{b[52]}{b[53]}{b[54]}{b[55]}{On_Black}  {reset}
  126. {On_Black}  {b[56]}{b[57]}{b[58]}{b[59]}{b[60]}{b[61]}{b[62]}{b[63]}{On_Black}  {reset}
  127. {On_Black}                                    {reset}""")
  128.  
  129.  
  130. def space_empty(direction, pos):
  131.     # is the space corresponding to the pawn empty?
  132.     directions = {"up_right": -7, "up_left": -9, "down_right": 9, "down_left": 7, "2up_right": -14, "2up_left": -18, "2down_right": 18, "2down_left": 14}
  133.     direction_number = directions[direction]
  134.     pos = int(pos)
  135.     if str(pos + direction_number) in available_spots and board[pos + direction_number] in available_spots:
  136.         return True
  137.     return False
  138.  
  139.  
  140. def space_enemy(direction, pos):
  141.     # is the space corresponding to the pawn an enemy?
  142.     directions = {"up_right": -7, "up_left": -9, "down_right": 9, "down_left": 7}
  143.     direction_number = directions[direction]
  144.     pos = int(pos)
  145.     if board[pos] in p1l:
  146.         enemy_list = p2l
  147.     else:
  148.         enemy_list = p1l
  149.     if str(pos + direction_number) in available_spots and board[pos + direction_number] in enemy_list:
  150.         return True
  151.     return False
  152.  
  153.  
  154. def can_eat(pos):
  155.     pos = int(pos)
  156.     if str(pos) not in available_spots:
  157.         return False
  158.     if board[pos] in p1l or is_king(pos):
  159.         if space_enemy("up_right", pos) and space_empty("2up_right", pos):
  160.             return True
  161.         if space_enemy("up_left", pos) and space_empty("2up_left", pos):
  162.             return True
  163.  
  164.     if board[pos] in p2l or is_king(pos):
  165.         if space_enemy("down_left", pos) and space_empty("2down_left", pos):
  166.             return True
  167.         if space_enemy("down_right", pos) and space_empty("2down_right", pos):
  168.             return True
  169.     return False
  170.  
  171.  
  172. def can_move(pos):
  173.     pos = int(pos)
  174.     if str(pos) not in available_spots:
  175.         return False
  176.     if can_eat(pos):
  177.         return True
  178.     if space_empty("up_right", pos) or space_empty("up_left", pos):
  179.         if board[pos] in p1l or is_king(pos):
  180.             return True
  181.     if space_empty("down_right", pos) or space_empty("down_left", pos):
  182.         if board[pos] in p2l or is_king(pos):
  183.             return True
  184.     return False
  185.  
  186.  
  187. def does_list_have_eaters(list_):
  188.     for each in list_:
  189.         if can_eat(each):
  190.             return True
  191.     return False
  192.  
  193.  
  194. selection = ''
  195. selection_process = False
  196. available_pieces = []
  197. def select_pawn():
  198.     global selection_process, available_pieces, selection
  199.     available_pieces = [index for index, each in enumerate(board) if each in current_player_list and can_move(index)]
  200.     if force_eat is True and does_list_have_eaters(available_pieces):
  201.         for each in available_pieces[:]:
  202.             if not can_eat(each):
  203.                 available_pieces.remove(each)
  204.  
  205.     selection_process = True
  206.     display_board()
  207.     selection = input(f"{current_player} select a piece. Available pieces: {purple}{available_pieces}{reset} ")
  208.     while True:
  209.         if not selection.isdigit():
  210.             selection = input(f"Invalid input. Possible options: {purple}{available_pieces}{reset} Try again: ")
  211.             continue
  212.         if int(selection) not in available_pieces:
  213.             selection = input(f"Invalid input. Possible options: {purple}{available_pieces}{reset} Try again: ")
  214.             continue
  215.         else:
  216.             break
  217.     selection = selection.lstrip('0')
  218.     selection_process = False
  219.  
  220.  
  221. cord_process = False
  222. available_cords = []
  223. def select_cord():
  224.     global board, selection, cord_process, available_cords
  225.     # creating a list with the only possible cordinates the selected piece can go:
  226.     double_jump = False
  227.     while True:
  228.         cord = None
  229.         available_cords = []
  230.         if not (force_eat is True and can_eat(selection)) and (double_jump is False):
  231.             if current_player == p1 or is_king(selection):
  232.                 if space_empty("up_right", selection):
  233.                     available_cords.append(int(selection) - 7)
  234.                 if space_empty("up_left", selection):
  235.                     available_cords.append(int(selection) - 9)
  236.             if current_player == p2 or is_king(selection):
  237.                 if space_empty("down_left", selection):
  238.                     available_cords.append(int(selection) + 7)
  239.                 if space_empty("down_right", selection):
  240.                     available_cords.append(int(selection) + 9)
  241.         if can_eat(selection):
  242.             if current_player == p1 or is_king(selection):
  243.                 if space_empty("2up_right", selection) and space_enemy("up_right", selection):
  244.                     available_cords.append(int(selection) - 14)
  245.                 if space_empty("2up_left", selection) and space_enemy("up_left", selection):
  246.                     available_cords.append(int(selection) - 18)
  247.             if current_player == p2 or is_king(selection):
  248.                 if space_empty("2down_left", selection) and space_enemy("down_left", selection):
  249.                     available_cords.append(int(selection) + 14)
  250.                 if space_empty("2down_right", selection) and space_enemy("down_right", selection):
  251.                     available_cords.append(int(selection) + 18)
  252.         available_cords.sort()
  253.  
  254.         # starting the cord_process, choosing a cord the piece will move to.
  255.         cord_process = True
  256.         display_board()
  257.         if double_jump is False:
  258.             print(f"{current_player} selected piece at {yellow}{selection}{reset}.")
  259.         cord = input(f"Select a coordinate to go to: ")
  260.         while True:
  261.             cord = cord.lstrip('0')
  262.             if not cord.isdigit() or cord not in available_spots:
  263.                 cord = input(f"Invalid coordinate. Available coordinates: {green}{available_cords}{reset}. Try again: ")
  264.                 continue
  265.             elif int(cord) not in available_cords:
  266.                 cord = input(f"Invalid coordinate. Available coordinates: {green}{available_cords}{reset}. Try again: ")
  267.                 continue
  268.             else:
  269.                 break
  270.  
  271.         # capturing pieces
  272.         captured_piece = None
  273.         if int(cord) < int(selection) - 10 or int(cord) > int(selection) + 10:
  274.             if current_player == p1 or is_king(selection):
  275.                 if int(cord) == int(selection) - 14:
  276.                     captured_piece = int(selection) - 7
  277.                 elif int(cord) == int(selection) - 18:
  278.                     captured_piece = int(selection) - 9
  279.             if current_player == p2 or is_king(selection):
  280.                 if int(cord) == int(selection) + 14:
  281.                     captured_piece = int(selection) + 7
  282.                 elif int(cord) == int(selection) + 18:
  283.                     captured_piece = int(selection) + 9
  284.  
  285.         if captured_piece is not None:
  286.             board[captured_piece] = d_board[captured_piece]
  287.  
  288.         if current_player == p1 and not is_king(selection) and board[int(cord)] in ["1", "3", "5", "7"]:
  289.             board[int(cord)] = p1k
  290.         elif current_player == p2 and not is_king(selection) and board[int(cord)] in ["56", "58", "60", "62"]:
  291.             board[int(cord)] = p2k
  292.         else:
  293.             board[int(cord)] = board[int(selection)]
  294.  
  295.         board[int(selection)] = d_board[int(selection)]
  296.         if captured_piece is None:
  297.             if current_player == p1:
  298.                 print(f"Pawn {red}{selection}{reset} moved to square {green}{cord}{reset} without capturing any pieces.")
  299.             elif current_player == p2:
  300.                 print(f"Pawn {blue}{selection}{reset} moved to square {green}{cord}{reset} without capturing any pieces.")
  301.         else:
  302.             if current_player == p1:
  303.                 print(
  304.                     f"Pawn {red}{selection}{reset} moved to square {green}{cord}{reset} and captured: {blue}{captured_piece}{reset}")
  305.             elif current_player == p2:
  306.                 print(
  307.                     f"Pawn {blue}{selection}{reset} moved to square {green}{cord}{reset} and captured: {red}{captured_piece}{reset}")
  308.         if can_eat(cord) and captured_piece is not None:
  309.             if force_eat is False:
  310.                 input1 = None
  311.                 selection = cord
  312.                 available_cords = []
  313.                 if current_player == p1 or is_king(selection):
  314.                     if space_empty("2up_right", selection) and space_enemy("up_right", selection):
  315.                         available_cords.append(int(selection) - 14)
  316.                     if space_empty("2up_left", selection) and space_enemy("up_left", selection):
  317.                         available_cords.append(int(selection) - 18)
  318.                 if current_player == p2 or is_king(selection):
  319.                     if space_empty("2down_left", selection) and space_enemy("down_left", selection):
  320.                         available_cords.append(int(selection) + 14)
  321.                     if space_empty("2down_right", selection) and space_enemy("down_right", selection):
  322.                         available_cords.append(int(selection) + 18)
  323.                 available_cords.sort()
  324.                 display_board()
  325.                 while input1 not in ["yes", "no"]:
  326.                     input1 = input("Do you want to continue capturing? (yes, no): ")
  327.                 if input1 == "yes":
  328.                     double_jump = True
  329.                     continue
  330.                 else:
  331.                     break
  332.             else:
  333.                 print("You are forced to capture again.")
  334.                 double_jump = True
  335.                 selection = cord
  336.                 continue
  337.         else:
  338.             break
  339.     cord_process = False
  340.  
  341.  
  342. def is_king(pawn):
  343.     pawn = int(pawn)
  344.     if d_board[pawn] not in available_spots:
  345.         return False
  346.     if board[pawn] == p1k or board[pawn] == p2k:
  347.         return True
  348.     return False
  349.  
  350.  
  351. def check_for_win():
  352.     global winner, game_is_active
  353.     p1_list = []
  354.     p2_list = []
  355.     for index, each in enumerate(board):
  356.         if each in p1l:
  357.             p1_list.append(index)
  358.         if each in p2l:
  359.             p2_list.append(index)
  360.     if p1_list == []:
  361.         winner = p2
  362.         game_is_active = False
  363.     elif p2_list == []:
  364.         winner = p1
  365.         game_is_active = False
  366.     else:
  367.         p1_can_move = 0
  368.         p2_can_move = 0
  369.         for each in p1_list:
  370.             if can_move(each):
  371.                 p1_can_move = 1
  372.         for each in p2_list:
  373.             if can_move(each):
  374.                 p2_can_move = 1
  375.         if p1_can_move == 0:
  376.             winner = p2
  377.             game_is_active = False
  378.         elif p2_can_move == 0:
  379.             winner = p1
  380.             game_is_active = False
  381.  
  382.  
  383. def switch_player():
  384.     global current_player, current_player_list
  385.     if current_player == p1:
  386.         current_player = p2
  387.         current_player_list = p2l
  388.     else:
  389.         current_player = p1
  390.         current_player_list = p1l
  391.  
  392. winner = None
  393. force_eat = False
  394. game_is_active = True
  395. current_player = p1
  396. current_player_list = p1l
  397. while game_is_active:
  398.     select_pawn()
  399.     select_cord()
  400.     check_for_win()
  401.     switch_player()
  402. display_board()
  403. print(f"{winner} {reset}has won the game.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement