Advertisement
Guest User

SmartChess beta 1.5.2 EN

a guest
Nov 4th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 39.88 KB | None | 0 0
  1. ##Inglise tõlkeks vajalikud
  2.  
  3. def typeToEN(letter):
  4.     if letter == "V":
  5.         return "R"
  6.     elif letter == "O":
  7.         return "B"
  8.     elif letter == "E":
  9.         return "P"
  10.     elif letter == "K":
  11.         return "K"
  12.     elif letter == "L":
  13.         return "Q"
  14.     elif letter == "R":
  15.         return "N"
  16.     return " "
  17. def typeToEE(letter):
  18.     if letter == "R":
  19.         return "V"
  20.     elif letter == "B":
  21.         return "O"
  22.     elif letter == "P":
  23.         return "E"
  24.     elif letter == "K":
  25.         return "K"
  26.     elif letter == "Q":
  27.         return "L"
  28.     elif letter == "N":
  29.         return "R"
  30.     return letter
  31.  
  32. def ownerToEN(letter):
  33.     if letter == "V":
  34.         return "W"
  35.     elif letter == "M":
  36.         return "B"
  37.     return " "
  38. def ownerToEE(letter):
  39.     if letter == "W":
  40.         return "V"
  41.     elif letter == "B":
  42.         return "M"
  43.     return letter
  44.  
  45. import time
  46. import random
  47. import operator
  48. import textwrap
  49.  
  50. DIGITS1TO8 = "1 2 3 4 5 6 7 8".split()
  51. LETTERSATOH = "A B C D E F G H".split()
  52.  
  53. def getNewBoardType():
  54.     boardType = [[" "] * 8 for i in range(8)]
  55.     for x in range(8):
  56.         boardType[x][1] = "E"
  57.         boardType[x][6] = "E"
  58.     typeOrder = ["V", "R", "O", "L", "K", "O", "R", "V"]
  59.     for x in range(8):
  60.         boardType[x][0] = typeOrder[x]
  61.         boardType[x][7] = typeOrder[x]
  62.     return boardType
  63.  
  64. def getNewBoardOwner():
  65.     boardOwner = [[" "] * 8 for i in range(8)]
  66.     for x in range(8):
  67.         boardOwner[x][1] = "V"
  68.         boardOwner[x][0] = "V"
  69.         boardOwner[x][6] = "M"
  70.         boardOwner[x][7] = "M"
  71.     return boardOwner
  72.  
  73. def getPrintMode():
  74.     while True:
  75.         printMode = input("Input the size of the board ([S] for large, [K] for medium, [V] for small): ")
  76.         if len(printMode)==0:
  77.             nope = ".avi"
  78.         elif printMode[0].upper()=="S":
  79.             printMode = "S"
  80.             break
  81.         elif printMode[0].upper()=="V":
  82.             printMode = "V"
  83.             break
  84.         elif printMode[0].upper()=="K":
  85.             printMode = "K"
  86.             break
  87.     return printMode
  88.  
  89. def printBoard(printMode): #Lisasin veidi "print("")" et tekitada whitespace'i
  90.     for y in range(8):
  91.         for x in range(8):
  92.             boardType[x][y] = typeToEN(boardType[x][y])
  93.             boardOwner[x][y] = ownerToEN(boardOwner[x][y])
  94.     if printMode == "S":
  95.         print("")
  96.         print("  ", end="")
  97.         for x in range(8):
  98.             print("    %s    " %(LETTERSATOH[x]), end="")
  99.         print("")
  100.         for y in range(8):
  101.             print("  "+"+--------"*8, end="")
  102.             print("+")
  103.             print("  "+"|        "*8+"|")
  104.             print(DIGITS1TO8[7-y], end = " ")
  105.             for x in range(8):
  106.                 print("|   %s%s   " %(boardType[x][7-y], boardOwner[x][7-y]), end = "")
  107.             print("|  ", end = " ")
  108.             print(DIGITS1TO8[7-y])
  109.             print("  ", end="")
  110.             for x in range(8):
  111.                 print("|%s%s      " % (LETTERSATOH[x],DIGITS1TO8[8-y-1]), end = "")
  112.             print("|")
  113.         print("  "+"+--------"*8, end="")
  114.         print("+")
  115.         print("  ", end="")
  116.         for x in range(8):
  117.             print("    %s    " %(LETTERSATOH[x]), end="")
  118.         print("")
  119.         print("")
  120.        
  121.     elif printMode == "V":
  122.         print("")
  123.         print("  ", end="")
  124.         for x in range(8):
  125.             print("  %s " %(LETTERSATOH[x]), end="")
  126.         print("")
  127.         for y in range(8):
  128.             print("  "+"+---"*8, end="")
  129.             print("+")
  130.             print(DIGITS1TO8[7-y], end = " ")
  131.             for x in range(8):
  132.                 print("|%s%s " %(boardType[x][7-y], boardOwner[x][7-y]), end = "")
  133.             print("|", end = " ")
  134.             print(DIGITS1TO8[7-y])
  135.         print("  "+"+---"*8, end="")
  136.         print("+")
  137.         print("  ", end="")
  138.         for x in range(8):
  139.             print("  %s " %(LETTERSATOH[x]), end="")
  140.         print("")
  141.         print("")
  142.        
  143.     elif printMode == "K":
  144.         print("")
  145.         print("  ", end="")
  146.         for x in range(8):
  147.             print("  %s  " %(LETTERSATOH[x]), end="")
  148.         print("")
  149.         for y in range(8):
  150.             print("  "+"+----"*8, end="")
  151.             print("+")
  152.             print(DIGITS1TO8[7-y], end = " ")
  153.             for x in range(8):
  154.                 print("| %s%s " %(boardType[x][7-y], boardOwner[x][7-y]), end = "")
  155.             print("|", end = " ")
  156.             print(DIGITS1TO8[7-y])
  157.         print("  "+"+----"*8, end="")
  158.         print("+")
  159.         print("  ", end="")
  160.         for x in range(8):
  161.             print("  %s  " %(LETTERSATOH[x]), end="")
  162.         print("")
  163.         print("")
  164.     for y in range(8):
  165.         for x in range(8):
  166.             boardType[x][y] = typeToEE(boardType[x][y])
  167.             boardOwner[x][y] = ownerToEE(boardOwner[x][y])
  168.        
  169.  
  170. def turnChange(turn):
  171.     if turn == "V":
  172.         turn = "M"
  173.     else:
  174.         turn = "V"
  175.     return turn
  176.  
  177. def posConvert(position):
  178.     position = ((ord(position[0])-ord("A")), int(position[1])-1)
  179.     return position
  180.  
  181. def getMove(turn):
  182.     while True:
  183.         surrender = 0
  184.         movesTogether = input("Insert your turn: ")
  185.         if movesTogether.lower() == "surrender": #Alla andmine
  186.             surrender = 1
  187.             startPos = (0,0)
  188.             endPos = (0,0)
  189.             return startPos, endPos, surrender
  190.         if len(movesTogether) == 4 and movesTogether[0].upper() in LETTERSATOH and movesTogether[2].upper() in LETTERSATOH and movesTogether[1] in DIGITS1TO8 and movesTogether[3] in DIGITS1TO8:
  191.             startPos = str(movesTogether[0].upper()) + movesTogether[1]
  192.             endPos = str(movesTogether[2].upper()) + movesTogether[3]
  193.             startPos = posConvert(startPos)
  194.             endPos = posConvert(endPos)
  195.             if boardOwner[startPos[0]][startPos[1]] != turn:
  196.                 print("You don't have a piece there.")
  197.             else:
  198.                 return startPos, endPos, surrender
  199.         else:
  200.             print("Input your move in the format [Start location letter][Start location number][Destination letter][Destination number]. For example, A2A3.")
  201.  
  202. def checkCheck(X,boardType,boardOwner):
  203.     Y = X[0] + 1
  204.     while Y < 8:
  205.         if boardOwner[Y][X[1]] == turn:
  206.             break
  207.         elif boardOwner[Y][X[1]] != " ":
  208.             if boardType[Y][X[1]] == "V" or boardType[Y][X[1]] == "L": #paremal
  209.                 return True
  210.             else:
  211.                 break
  212.         Y += 1
  213.        
  214.     Y = X[0] - 1
  215.     while Y > -1:
  216.         if boardOwner[Y][X[1]] == turn:
  217.             break
  218.         elif boardOwner[Y][X[1]] != " ":
  219.             if boardType[Y][X[1]] == "V" or boardType[Y][X[1]] == "L": #vasakul
  220.                 return True
  221.             else:
  222.                 break
  223.         Y -= 1
  224.     Y = X[1] + 1
  225.     while Y < 8:
  226.         if boardOwner[X[0]][Y] == turn:
  227.             break
  228.         elif boardOwner[X[0]][Y] != " ":
  229.             if boardType[X[0]][Y] == "V" or boardType[X[0]][Y] == "L": #üles
  230.                 return True
  231.             else:
  232.                 break
  233.         Y += 1
  234.     Y = X[1] - 1
  235.     while Y > -1:
  236.         if boardOwner[X[0]][Y] == turn:
  237.             break
  238.         elif boardOwner[X[0]][Y] != " ":
  239.             if boardType[X[0]][Y] == "V" or boardType[X[0]][Y] == "L": # alla
  240.                 return True
  241.             else:
  242.                 break
  243.         Y -= 1
  244.        
  245.     Y = X[0] + 1
  246.     Z = X[1] + 1
  247.     while Y < 8 and Z < 8:
  248.         if boardOwner[Y][Z] == turn:
  249.             break
  250.         elif boardOwner[Y][Z] != " ":
  251.             if boardType[Y][Z] == "O" or boardType[Y][Z] == "L": #Asimuut 45
  252.                 return True
  253.             else:
  254.                 break
  255.         Y += 1
  256.         Z += 1
  257.        
  258.     Y = X[0] - 1
  259.     Z = X[1] - 1
  260.     while Y > -1 and Z > -1:
  261.         if boardOwner[Y][Z] == turn:
  262.             break
  263.         elif boardOwner[Y][Z] != " ":
  264.             if boardType[Y][Z] == "O" or boardType[Y][Z] == "L": #asimuut 225
  265.                 return True
  266.             else:
  267.                 break
  268.         Y -= 1
  269.         Z -= 1
  270.        
  271.     Y = X[0] + 1
  272.     Z = X[1] - 1
  273.     while Y < 8 and Z > -1:
  274.         if boardOwner[Y][Z] == turn:
  275.             break
  276.         elif boardOwner[Y][Z] != " ":            
  277.             if boardType[Y][Z] == "O" or boardType[Y][Z] == "L": #asimuut 135
  278.                 return True
  279.             else:
  280.                 break
  281.            
  282.         Y += 1
  283.         Z -= 1
  284.     Y = X[0] - 1
  285.     Z = X[1] + 1
  286.     while Z < 8 and Y > -1:
  287.         if boardOwner[Y][Z] == turn:
  288.             break
  289.         elif boardOwner[Y][Z] != " ":
  290.             if boardType[Y][Z] == "O" or boardType[Y][Z] == "L": #asimuut 315
  291.                 return True
  292.             else:
  293.                 break
  294.         Y -= 1
  295.         Z += 1
  296.                                     #Ratsud
  297.     if X[0]+2 < 8:                  #Paremal
  298.         if X[1]+1 < 8:
  299.             if boardType[X[0]+2][X[1]+1] == "R":
  300.                 if boardOwner[X[0]+2][X[1]+1] != turn:
  301.                     return True
  302.         if X[1]-1 > -1:
  303.             if boardType[X[0]+2][X[1]-1] == "R":
  304.                 if boardOwner[X[0]+2][X[1]-1] != turn:
  305.                     return True
  306.     if X[0]-2 > -1:                 #Vasakul
  307.         if X[1]+1 < 8:
  308.             if boardType[X[0]-2][X[1]+1] == "R":
  309.                 if boardOwner[X[0]-2][X[1]+1] != turn:
  310.                     return True
  311.         if X[1]-1 > -1:
  312.             if boardType[X[0]-2][X[1]-1] == "R":
  313.                 if boardOwner[X[0]-2][X[1]-1] != turn:
  314.                     return True
  315.     if X[1]+2 < 8:                  #Üleval
  316.         if X[0]+1 < 8:
  317.             if boardType[X[0]+1][X[1]+2] == "R":
  318.                 if boardOwner[X[0]+1][X[1]+2] != turn:
  319.                     return True
  320.         if X[0]-1 > -1:
  321.             if boardType[X[0]-1][X[1]+2] == "R":
  322.                 if boardOwner[X[0]-1][X[1]+2] != turn:
  323.                     return True
  324.     if X[1]-2 > -1:                  #All
  325.         if X[0]+1 < 8:
  326.             if boardType[X[0]+1][X[1]-2] == "R":
  327.                 if boardOwner[X[0]+1][X[1]-2] != turn:
  328.                     return True
  329.         if X[0]-1 > -1:
  330.             if boardType[X[0]-1][X[1]-2] == "R":
  331.                 if boardOwner[X[0]-1][X[1]-2] != turn:
  332.                     return True
  333.     if turn == "V":
  334.         if X[1]+1 < 8:
  335.             if X[0]+1 < 8:
  336.                 if boardType[X[0]+1][X[1]+1] == "E" and boardOwner[X[0]+1][X[1]+1] != turn:
  337.                     return True
  338.             if X[0]-1 > -1:
  339.                 if boardType[X[0]-1][X[1]+1] == "E" and boardOwner[X[0]-1][X[1]+1] != turn:
  340.                     return True
  341.     else:
  342.         if X[1]-1 > -1:
  343.             if X[0]+1 < 8:
  344.                 if boardType[X[0]+1][X[1]-1] == "E" and boardOwner[X[0]+1][X[1]-1] != turn:
  345.                     return True
  346.             if X[0]-1 > -1:
  347.                 if boardType[X[0]-1][X[1]-1] == "E" and boardOwner[X[0]-1][X[1]-1] != turn:
  348.                     return True
  349.  
  350.    
  351. def checkValidity(turn, startPos, endPos):
  352.     validMoves = []
  353.     startx = startPos[0]
  354.     starty = startPos[1]
  355.     endx = endPos[0]
  356.     endy = endPos[1]
  357.     if boardType[startx][starty] == "E": #Lisab validMoves listi kõik võimalikud käigud etturi poolt.
  358.         if turn == "V":
  359.             if boardType[startx][starty+1] == " ":
  360.                 validMoves.append((startx,starty+1))
  361.             if startx+1 < 8 and boardOwner[startx+1][starty+1] == "M":
  362.                 validMoves.append((startx+1,startPos[1]+1))
  363.             if startx-1 > -1 and boardOwner[startx-1][starty+1] == "M":
  364.                 validMoves.append((startx-1,starty+1))
  365.             if starty == 1 and boardType[startx][starty+2] == " ":
  366.                 validMoves.append((startx,3))
  367.         if turn == "M":
  368.             if boardType[startx][starty-1] == " ":
  369.                 validMoves.append((startx,starty-1))
  370.             if startx+1 < 8 and boardOwner[startx+1][starty-1] == "V":
  371.                 validMoves.append((startx+1,starty-1))
  372.             if startx-1 > -1 and boardOwner[startx-1][starty-1] == "V":
  373.                 validMoves.append((startx-1,starty-1))
  374.             if starty == 6 and boardType[startx][starty-2] == " ":
  375.                 validMoves.append((startx,4))
  376.                
  377.     if boardType[startx][starty] == "K":
  378.         if startx-1 > -1 and (boardOwner[startx-1][starty] != turn):
  379.             validMoves.append((startx-1,starty))
  380.         if startx+1 < 8 and (boardOwner[startx-1][starty] != turn):
  381.             validMoves.append((startx+1,starty))
  382.         if starty+1 < 8:
  383.             for i in range(3):
  384.                 if startx-1+i < 8 and startx-1+i > - 1:
  385.                     if boardOwner[startx-1+i][starty+1] != turn:
  386.                         validMoves.append((startx-1+i,starty+1))
  387.         if starty-1 > -1:
  388.             for i in range(3):
  389.                 if startx-1+i < 8 and startx-1+i > - 1:
  390.                     if boardOwner[startx-1+i][starty-1] != turn:
  391.                         validMoves.append((startx-1+i,starty-1))
  392.  
  393.     if boardType[startx][starty] == "R":
  394.         if starty+2 < 8:
  395.             if startx-1 > -1:
  396.                 if boardOwner[startx-1][starty+2] != turn:
  397.                     validMoves.append((startx-1,starty+2))
  398.             if startx+1 < 8:
  399.                 if boardOwner[startx+1][starty+2] != turn:
  400.                     validMoves.append((startx+1,starty+2))
  401.         if starty-2 > -1:
  402.             if startx-1 > -1:
  403.                 if boardOwner[startx-1][starty-2] != turn:
  404.                     validMoves.append((startx-1,starty-2))
  405.             if startx+1 < 8:
  406.                 if boardOwner[startx+1][starty-2] != turn:
  407.                     validMoves.append((startx+1,starty-2))
  408.         if startx+2 < 8:
  409.             if starty-1 > -1:
  410.                 if boardOwner[startx+2][starty-1] != turn:
  411.                     validMoves.append((startx+2,starty-1))
  412.             if starty+1 < 8:
  413.                 if boardOwner[startx+2][starty+1] != turn:
  414.                     validMoves.append((startx+2,starty+1))
  415.         if startx-2 > -1 :
  416.             if starty-1 > -1:
  417.                 if boardOwner[startx-2][starty-1] != turn:
  418.                     validMoves.append((startx-2,starty-1))
  419.             if starty+1 < 8:
  420.                 if boardOwner[startx-2][starty+1] != turn:
  421.                     validMoves.append((startx-2,starty+1))
  422.  
  423.     if boardType[startx][starty] == "V" or boardType[startx][starty] == "L":
  424.         for moveLength in range(1,8):
  425.             if starty+moveLength < 8: #1. Üles
  426.                 nextTileNumber = (startx, (starty+moveLength))
  427.                 nextTileOwner = boardOwner[startx][starty+moveLength]
  428.                 if nextTileOwner != turn:
  429.                     if nextTileOwner == " ":
  430.                         validMoves.append(nextTileNumber)
  431.                     else:
  432.                         validMoves.append(nextTileNumber)
  433.                         break
  434.                 else:
  435.                     break
  436.         for moveLength in range(1,8):
  437.             if startx+moveLength < 8: #2. Paremale
  438.                 nextTileNumber = (startx+moveLength, starty)
  439.                 nextTileOwner = boardOwner[startx+moveLength][starty]
  440.                 if nextTileOwner != turn:
  441.                     if nextTileOwner == " ":
  442.                         validMoves.append(nextTileNumber)
  443.                     else:
  444.                         validMoves.append(nextTileNumber)
  445.                         break
  446.                 else:
  447.                     break
  448.         for moveLength in range(1,8):
  449.             if starty-moveLength > (-1): #3. Alla
  450.                 nextTileNumber = (startx, starty-moveLength)
  451.                 nextTileOwner = boardOwner[startx][starty-moveLength]
  452.                 if nextTileOwner != turn:
  453.                     if nextTileOwner == " ":
  454.                         validMoves.append(nextTileNumber)
  455.                     else:
  456.                         validMoves.append(nextTileNumber)
  457.                         break
  458.                 else:
  459.                     break
  460.         for moveLength in range(1,8):
  461.             if startx-moveLength > (-1): #4. Vasakule
  462.                 nextTileNumber = (startx-moveLength, starty)
  463.                 nextTileOwner = boardOwner[startx-moveLength][starty]
  464.                 if nextTileOwner != turn:
  465.                     if nextTileOwner == " ":
  466.                         validMoves.append(nextTileNumber)
  467.                     else:
  468.                         validMoves.append(nextTileNumber)
  469.                         break
  470.                 else:
  471.                     break
  472.     if boardType[startx][starty] == "O" or boardType[startx][starty] == "L":
  473.         for moveLength in range(1,8): #1. asimuut 45
  474.             if startx+moveLength < 8 and starty+moveLength < 8:
  475.                 nextTileNumber = (startx+moveLength, starty+moveLength)
  476.                 nextTileOwner = boardOwner[startx+moveLength][starty+moveLength]
  477.                 if nextTileOwner != turn:
  478.                     if nextTileOwner == " ":
  479.                         validMoves.append(nextTileNumber)
  480.                     else:
  481.                         validMoves.append(nextTileNumber)
  482.                         break
  483.                 else:
  484.                     break
  485.         for moveLength in range(1,8): #2. asimuut 135
  486.             if startx+moveLength < 8 and starty-moveLength > (-1):
  487.                 nextTileNumber = (startx+moveLength, starty-moveLength)
  488.                 nextTileOwner = boardOwner[startx+moveLength][starty-moveLength]
  489.                 if nextTileOwner != turn:
  490.                     if nextTileOwner == " ":
  491.                         validMoves.append(nextTileNumber)
  492.                     else:
  493.                         validMoves.append(nextTileNumber)
  494.                         break
  495.                 else:
  496.                     break
  497.         for moveLength in range(1,8): #1. asimuut 225
  498.             if startx-moveLength > (-1) and starty-moveLength > (-1):
  499.                 nextTileNumber = (startx-moveLength, starty-moveLength)
  500.                 nextTileOwner = boardOwner[startx-moveLength][starty-moveLength]
  501.                 if nextTileOwner != turn:
  502.                     if nextTileOwner == " ":
  503.                         validMoves.append(nextTileNumber)
  504.                     else:
  505.                         validMoves.append(nextTileNumber)
  506.                         break
  507.                 else:
  508.                     break
  509.         for moveLength in range(1,8): #1. asimuut 315
  510.             if startx+moveLength > (-1) and starty+moveLength < 8:
  511.                 nextTileNumber = (startx-moveLength, starty+moveLength)
  512.                 nextTileOwner = boardOwner[startx-moveLength][starty+moveLength]
  513.                 if nextTileOwner != turn:
  514.                     if nextTileOwner == " ":
  515.                         validMoves.append(nextTileNumber)
  516.                     else:
  517.                         validMoves.append(nextTileNumber)
  518.                         break
  519.                 else:
  520.                     break
  521.                
  522.     if check == True:
  523.         boardTypeTemp = [[" "] * 8 for i in range(8)]
  524.         boardOwnerTemp = [[" "] * 8 for i in range(8)]
  525.         for i in range(8):
  526.             for y in range(8):
  527.                 boardTypeTemp[i][y]=(boardType[i][y])
  528.                 boardOwnerTemp[i][y]=(boardOwner[i][y])
  529.         boardTypeTemp[endPos[0]][endPos[1]] = boardType[startPos[0]][startPos[1]]
  530.         boardOwnerTemp[endPos[0]][endPos[1]] = turn
  531.         boardTypeTemp[startPos[0]][startPos[1]] = " "
  532.         boardOwnerTemp[startPos[0]][startPos[1]] = " "
  533.         if boardType[startx][starty] == "K":
  534.                 if checkCheck((endPos[0],endPos[1]),boardTypeTemp,boardOwnerTemp):
  535.                     print("Check! You can't move there.")
  536.                     return False
  537.         elif turn == "V":
  538.             if checkCheck(whiteKingPos,boardTypeTemp,boardOwnerTemp):
  539.                 print("Check! You can't move there.")
  540.                 return False
  541.         else:
  542.             if checkCheck(blackKingPos,boardTypeTemp,boardOwnerTemp):
  543.                 print("Check! You can't move there.")
  544.                 return False    
  545.     if boardType[startx][starty] == "K": ################ Vangerdus
  546.         if turn == "V":
  547.             if (endx, endy) == (6, 0) and "V" not in kingSideCastle:
  548.                 if boardType[5][0] == " " and not checkCheck((5,0),boardType,boardOwner) and boardType[6][0] == " " and not checkCheck((6,0),boardType,boardOwner):
  549.                     return "whiteKingSide"
  550.             elif (endx, endy) == (2, 0) and "V" not in queenSideCastle:
  551.                 if boardType[3][0] == " " and not checkCheck((5,0),boardType,boardOwner) and boardType[2][0] == " " and not checkCheck((2,0),boardType,boardOwner) and boardType[1][0] == " ":
  552.                     return "whiteQueenSide"
  553.         else:
  554.             if (endx, endy) == (6, 7) and "M" not in kingSideCastle:
  555.                 if boardType[5][7] == " " and not checkCheck((5,7),boardType,boardOwner) and boardType[6][7] == " " and not checkCheck((6,7),boardType,boardOwner):
  556.                     return "blackKingSide"
  557.             elif (endx, endy) == (2, 7) and "M" not in queenSideCastle:
  558.                 if boardType[3][7] == " " and not checkCheck((5,7),boardType,boardOwner) and boardType[2][7] == " " and not checkCheck((2,7),boardType,boardOwner) and boardType[1][7] == " ":
  559.                     return "blackQueenSide"
  560.                        
  561.     if (endx, endy) in validMoves:
  562.         return True
  563.     else:
  564.         print("Te ei saa sinna käia.")
  565.         return False
  566.  
  567. def castle(x): ########## Vangerdamise laua muutmine
  568.     if x == "whiteKingSide":
  569.         boardType[5][0] = "V"
  570.         boardOwner[5][0] = "V"
  571.         boardType[7][0] = " "
  572.         boardOwner[7][0] = " "
  573.         boardType[6][0] = "K"
  574.         boardOwner[6][0] = "V"
  575.         boardType[4][0] = " "
  576.         boardOwner[4][0] = " "
  577.         kingSideCastle.append("V")
  578.         queenSideCastle.append("V")
  579.     elif x == "blackKingSide":
  580.         boardType[5][7] = "V"
  581.         boardOwner[5][7] = "M"
  582.         boardType[7][7] = " "
  583.         boardOwner[7][7] = " "
  584.         boardType[6][7] = "K"
  585.         boardOwner[6][7] = "M"
  586.         boardType[4][7] = " "
  587.         boardOwner[4][7] = " "
  588.         kingSideCastle.append("M")
  589.         queenSideCastle.append("M")
  590.     elif x == "whiteQueenSide":
  591.         print("x")
  592.         boardType[3][0] = "V"
  593.         boardOwner[3][0] = "V"
  594.         boardType[0][0] = " "
  595.         boardOwner[0][0] = " "
  596.         boardType[2][0] = "K"
  597.         boardOwner[2][0] = "V"
  598.         boardType[4][0] = " "
  599.         boardOwner[4][0] = " "
  600.         kingSideCastle.append("V")
  601.         queenSideCastle.append("V")
  602.     elif x == "blackQueenSide":
  603.         boardType[3][7] = "V"
  604.         boardOwner[3][7] = "M"
  605.         boardType[0][7] = " "
  606.         boardOwner[0][7] = " "
  607.         boardType[2][7] = "K"
  608.         boardOwner[2][7] = "M"
  609.         boardType[4][7] = " "
  610.         boardOwner[4][7] = " "
  611.         kingSideCastle.append("M")
  612.         queenSideCastle.append("M")
  613.  
  614. def pawnUpgrade(endPos):
  615.     while True:
  616.         command = input("What piece would you like your pawn to be upgraded to [Q/R/B/N]? ")
  617.         if len(command) != 0:
  618.             command = typeToEE(command[0])
  619.         if len(command) == 1 and command[0].upper() in "L O V R".split():
  620.             boardType[endPos[0]][endPos[1]] = command[0].upper()
  621.             return
  622.  
  623. def changeBoard(turn, startPos, endPos):
  624.     if boardType[endPos[0]][endPos[1]] == "K":
  625.         if turn == "V":
  626.             print("White wins.")
  627.         else:
  628.             print("Black wins.")
  629.         return False        
  630.     boardType[endPos[0]][endPos[1]] = boardType[startPos[0]][startPos[1]]
  631.     boardOwner[endPos[0]][endPos[1]] = turn
  632.     boardType[startPos[0]][startPos[1]] = " "
  633.     boardOwner[startPos[0]][startPos[1]] = " "
  634.     # Vankri liikumine
  635.     if boardType[endPos[0]][endPos[1]] == "V":
  636.         if startPos == (0,0):
  637.             queenSideCastle.append("V")
  638.         elif startPos == (0,7):
  639.             queenSideCastle.append("M")
  640.         elif startPos == (7,0):
  641.             kingSideCastle.append("V")
  642.         elif startPos == (7,7):
  643.             kingSideCastle.append("M")
  644.     if turn == "V": #Etturite ülendamine mängulaua lõppu jõudes
  645.         if boardType[endPos[0]][endPos[1]] == "E" and endPos[1] == 7:
  646.             pawnUpgrade(endPos)
  647.     else:
  648.         if boardType[endPos[0]][endPos[1]] == "E" and endPos[1] == 0:
  649.             pawnUpgrade(endPos)
  650.            
  651.     if boardType[endPos[0]][endPos[1]] == "K": #Kuninga liikumine
  652.         if turn == "V":
  653.             kingSideCastle.append("V")
  654.             queenSideCastle.append("V")
  655.             whiteKingPos = (endPos[0],endPos[1])
  656.         else:
  657.             kingSideCastle.append("M")
  658.             queenSideCastle.append("M")
  659.             blackKingPos = (endPos[0],endPos[1])
  660.     return True
  661.  
  662. def moveStage(turn):
  663.     while True:
  664.         startPos, endPos, surrender = getMove(turn)
  665.         if surrender == 1:# alla andmine
  666.             if turn == "V":
  667.                 print("White has surrendered.")
  668.             else:
  669.                 print("Black has surrendered.")
  670.             return False
  671.         temp = checkValidity (turn, startPos, endPos)
  672.         if temp != True and temp != False:
  673.             castle(temp)
  674.             return True
  675.         elif temp:
  676.             if changeBoard(turn, startPos, endPos): #returni false kui kuningas võeti ära
  677.                 return True
  678.             else:
  679.                 return False
  680.  
  681. def playAgain():
  682.     while True:
  683.         command = input("Would you like to play again? (j/e): ") #Jah
  684.         if len(command)==0:
  685.             nope = ".avi"
  686.         elif command[0].lower() == "j":
  687.             return True
  688.         elif command[0].lower() == "e":
  689.             return False
  690.  
  691. def showTurn(turn, pointsWhite, pointsBlack):
  692.     if turn == "V":
  693.         print("White's turn. Points: %s" %(pointsWhite))
  694.     else:
  695.         print("Black's turn. Points: %s" %(pointsBlack))
  696.     print("")
  697.  
  698. def displayTutorial(): #TutorialTexti pikkus ei tohi jaguda selle jagatava arvuga (praegu 70)
  699.     tutorialText = "Only available in the Estonian version."
  700.     print("-------------------------------------------------------------------------")
  701.     for x in range(len(textwrap.wrap(tutorialText))):
  702.         print(textwrap.wrap(tutorialText)[x])
  703.     print("-------------------------------------------------------------------------")
  704.    
  705. def enterToStart():
  706.     input("Press Enter to begin.")
  707.  
  708. def displayStart():
  709.     print("SmartChess(c) 2014 Beta 1.5.1")
  710.     print("Copyright: J.Kirme and A.Saidlo")
  711.  
  712. def displayOptions(printMode, purchasing, placing, queens, check):
  713.     print("{0:30}".format("Board size (S - large, K - medium, V - small): ") + printMode)
  714.     if check:
  715.         print ("{0:30}".format("Check:") + "On")
  716.     else:
  717.         print ("{0:30}".format("Check:") + "Off")
  718.     if purchasing:
  719.         print ("{0:30}".format("Purchasing pieces:") + "On")
  720.     else:
  721.         print ("{0:30}".format("Purchasing pieces:") + "Off")
  722.     if placing == "M":
  723.         print ("{0:30}".format("Purchasing location:") + "Back row and promotion")
  724.     elif placing == "T":
  725.         print ("{0:30}".format("Purchasing location:") + "Back row")
  726.     elif placing == "P":
  727.         print ("{0:30}".format("Purchasing location:") + "Promotion")  
  728.     if queens:
  729.         print ("{0:30}".format("Purchasing queens:") + "On")
  730.     else:
  731.         print ("{0:30}".format("Purchasing queens:") + "Off")      
  732.  
  733. def changeOptions(printMode, purchasing, placing, queens, check):
  734.     while True:
  735.         print("-------------------------------------------------------------------------")
  736.         displayOptions(printMode, purchasing, placing, queens, check)
  737.         print("-------------------------------------------------------------------------")
  738.         print("Which option would you like to change? ")
  739.         command = input("Size[S]/Check[U]/Purchasing[O]/Location[A]/Queens[L]/Back[T] ")
  740.         if len(command)==0: #Miks ma neid ei kirjutanud if len!=0? -J
  741.             nope = ".avi"
  742.         elif command[0].upper() == "S":
  743.             printMode = getPrintMode()
  744.         elif command[0].upper() == "O":
  745.             if purchasing == True:
  746.                 purchasing = False
  747.             else:
  748.                 purchasing = True
  749.         elif command[0].upper() == "A":
  750.             while True:
  751.                 placing = input("What should the possible placement of purchasing be like? (Back row[T]/Promotion[P]/Both[M]) ")
  752.                 if len(placing) != 0:
  753.                     placing = placing[0].upper()
  754.                     if placing in ("T","P","M"):
  755.                         break
  756.                 else:
  757.                     print("That's not a valid input. ")
  758.         elif command[0].upper() == "L":
  759.             if queens == True:
  760.                 queens = False
  761.             else:
  762.                 queens = True
  763.         elif command[0].upper() == "U":
  764.             if check == True:
  765.                 check = False
  766.             else:
  767.                 check = True
  768.         elif command[0].upper() == "T":
  769.             break
  770.     return printMode, purchasing, placing, queens
  771.  
  772. def startMenu(printMode, purchasing, placing, queens, check):
  773.     while True:
  774.         command = input("Would you like to start[A] or open the options menu[V]? ")
  775.         if len(command)==0:
  776.             nope = ".avi"
  777.         elif command[0].upper() == "A":
  778.             return printMode, purchasing, placing, queens
  779.         elif command[0].upper() == "J":
  780.             displayTutorial()
  781.         elif command[0].upper() == "V":
  782.             printMode, purchasing, placing, queens = changeOptions(printMode, purchasing, placing, queens, check)
  783.  
  784. def shop(turn, pointsWhite, pointsBlack, pawnValue, placing):
  785.     R = 0    
  786.     validPlaces = []
  787.     printBoard(printMode)
  788.     if turn == "V":
  789.         points = pointsWhite
  790.     else:
  791.         points = pointsBlack
  792.     while True:
  793.         pood = (input("Would you like to purchase a new piece?[J/E] "))
  794.         if len(pood) != 0:
  795.             pood = pood[0].upper()
  796.             if pood in ("E","J"):
  797.                 break
  798.         else:
  799.             print("Palun jälgige õigeid sisestusreegleid. ")
  800.     if pood == "J":
  801.         print("-------------------------------------------------------------------------")
  802.         print("{0:15}".format("Shop") + "Points:",points,"\n")
  803.         if queens:
  804.             print("{0:15}".format("Queen:"), 9*pawnValue)
  805.         print("{0:15}".format("Rook:"), 5*pawnValue)
  806.         print("{0:15}".format("Bishop/Knight:"), 3*pawnValue)
  807.         print("-------------------------------------------------------------------------")
  808.         while True:
  809.             while True:
  810.                 if turn == "V":
  811.                     points = pointsWhite
  812.                 else:
  813.                     points = pointsBlack
  814.                 if queens:
  815.                     newPiece = input("What kind of piece would you like [Q/R/B/N]? Press [T] to go back. ")
  816.                     if len(newPiece) != 0:
  817.                         newPiece = newPiece[0].upper()
  818.                         newPiece = typeToEE(newPiece[0])
  819.                     if newPiece in ("L","O","R","V"):
  820.                         if newPiece == "L" and points >= 9*pawnValue:
  821.                             points -= 9*pawnValue
  822.                             break
  823.                         if newPiece == "V" and points >= 5*pawnValue:
  824.                             points -= 5*pawnValue
  825.                             break
  826.                         if newPiece == "O" and points >= 3*pawnValue:
  827.                             points -= 3*pawnValue
  828.                             break
  829.                         if newPiece == "R" and points >= 3*pawnValue:
  830.                             points -= 3*pawnValue
  831.                             break
  832.                 else:
  833.                     newPiece = input("What piece would you like [R/B/N]? Press [T] to go back. ")
  834.                     if len(newPiece) != 0:
  835.                         newPiece = newPiece[0].upper()
  836.                         newPiece = typeToEE(newPiece[0])
  837.                     if newPiece in ("O","R","V"):                        
  838.                         if newPiece == "V" and points >= 5*pawnValue:
  839.                             points -= 5*pawnValue
  840.                             break
  841.                         if newPiece == "O" and points >= 3*pawnValue:
  842.                             points -= 3*pawnValue
  843.                             break
  844.                         if newPiece == "R" and points >= 3*pawnValue:
  845.                             points -= 3*pawnValue
  846.                             break
  847.                     elif newPiece == "L":
  848.                         print("Buying queens is disabled.")
  849.                
  850.                 if newPiece == "T":
  851.                     break
  852.                 else:
  853.                     print("Please insert a valid letter.")
  854.             if newPiece == "T":
  855.                 break
  856.  
  857.             if turn == "V":
  858.                 backup = pointsWhite
  859.                 pointsWhite = points
  860.             else:
  861.                 backup = pointsBlack
  862.                 pointsBlack = points
  863.  
  864.            
  865.             if placing == "M" or placing == "T":
  866.                 if turn == "V":
  867.                     for i in range(8):
  868.                         if boardType[i][0] == " ":
  869.                             validPlaces.append((i,0))
  870.                 else:
  871.                     for i in range(8):
  872.                         if boardType[i][7] == " ":
  873.                             validPlaces.append((i,7))
  874.             if placing == "M" or placing == "P":
  875.                 for i in range(8):
  876.                     for j in range(8):
  877.                         if boardOwner[i][j] == turn and boardType[i][j] != "K":
  878.                             validPlaces.append((i,j))
  879.             while True:
  880.                 place = str(input("Where would you like to place your piece? [T] to cancel "))
  881.                 if len(place) != 0:
  882.                    
  883.                     if len(place) == 2 and place[0].upper() in LETTERSATOH and place[1] in DIGITS1TO8:
  884.                         place = str(place[0].upper()) + str(place[1])
  885.                         place = posConvert(place)
  886.                         if place in validPlaces:
  887.                             boardType[place[0]][place[1]] = newPiece
  888.                             boardOwner[place[0]][place[1]] = turn
  889.                             R = 1
  890.                             break
  891.                         if placing == "T" and boardOwner[place[0]][place[1]] == turn:
  892.                             print("Promotion isn't allowed.")
  893.                         elif placing == "P" and boardOwner[place[0]][place[1]] == " " and place[1] == 0:
  894.                             print("Only promotion is allowed.")
  895.                         elif boardType[place[0]][place[1]] == "K":
  896.                             print("You can't promote your king.")
  897.                    
  898.                     elif place[0].upper() == "T":
  899.                         if turn == "V":
  900.                             pointsWhite = backup
  901.                         else:
  902.                             pointsBlack = backup
  903.                         break
  904.                 else:
  905.                     print("Not a valid input.")
  906.             if R == 1 or place == "T":
  907.                 break        
  908.     return boardType, boardOwner,pointsBlack,pointsWhite    
  909.  
  910. def getNewOperationMath():
  911.     x = random.randint(100,999)#Liitmine
  912.     y = random.randint(100,999)
  913.     answerRight = str(x+y)
  914.     operationDisplay = str(x) + "+" + str(y)
  915.     pointsWorth = 1
  916.     return answerRight, operationDisplay, pointsWorth
  917.  
  918. def giveOperationMath(turn, pointsWhite, pointsBlack):
  919.     answerRight, operationDisplay, pointsWorth = getNewOperationMath()
  920.     if turn == "V":
  921.         print("Points: %s" %pointsWhite)
  922.     else:
  923.         print("Points: %s" %pointsBlack)
  924.     answerPlayer = input("%s = " %(operationDisplay))
  925.     return answerRight, answerPlayer, pointsWorth
  926.  
  927. def mathStage(turn, timeLast, pointsWhite, pointsBlack): #Veel whitespace'i
  928.     print("")
  929.     print("You have %s seconds to solve tasks." % (round(timeLast, 1)))
  930.     print("")
  931.     time.sleep(3)
  932.     timeStart = timerStart()
  933.     while True:
  934.         if time.time() - timeStart > timeLast:
  935.             break
  936.         else:
  937.             answerRight, answerPlayer, pointsWorth = giveOperationMath(turn, pointsWhite, pointsBlack)
  938.             print("")
  939.         if turn == "V":
  940.             if answerRight == answerPlayer:
  941.                 pointsWhite += pointsWorth
  942.             else:
  943.                 pointsWhite -= pointsWorth
  944.         else:
  945.             if answerRight == answerPlayer:
  946.                 pointsBlack += pointsWorth
  947.             else:
  948.                 pointsBlack -= pointsWorth
  949.     timeOverLimit = time.time()-timeStart-timeLast
  950.     print("The opponent is given %s seconds for going over your time." %(round(timeOverLimit, 1)))
  951.     return timeOverLimit, pointsWhite, pointsBlack
  952.    
  953.  
  954. def questionStage(turn, timeLast, questionLast, questionsBanned, pointsWhite, pointsBlack): #Lisa siit alates
  955.     timeOverLimit = 0
  956.     if timeLast == 0 and turn == "V":
  957.         print("")
  958.         print("You can't solve tasks on the first turn.")
  959.         print("")
  960.     elif questionLast == 0: #Must saab juba küsimuse 1. käigul
  961.         while questionLast in questionsBanned:
  962.             questionLast = random.randint(1,100)
  963.     elif turn == "V": #Valge käigul tuleb uus voor
  964.         while questionLast in questionsBanned:
  965.             questionLast = random.randint(1,100)
  966.     if questionLast > 0 and questionLast <= 100: #Pranglimine, vaheta tagasi 1-90 (kui EK töötab) #############################
  967.         #print("Pranglimise voor.") ################################### Vaheta tagasi kui teisi voore tuleb
  968.         timeOverLimit, pointsWhite, pointsBlack = mathStage(turn, timeLast, pointsWhite, pointsBlack)
  969.     elif questionLast > 100: #Eesti keel. Vaheta tagasi 91-100 (kui see töötab) ##########################
  970.         print("Eesti keele voor.")
  971.     if turn == "V" and questionLast != 0:
  972.         print("Points: %s" %(pointsWhite))
  973.     elif questionLast != 0:
  974.         print("Points: %s" %(pointsBlack))
  975.     print("Turn change is imminent.")
  976.     time.sleep(5)
  977.     return timeOverLimit, pointsWhite, pointsBlack
  978.  
  979. def timerStart():
  980.     timeStart = time.time()
  981.     return timeStart
  982.  
  983. def timerEnd(timeStart):
  984.     timeCurrent = time.time()-timeStart
  985.     return timeCurrent
  986.    
  987. def showTime(timeCurrent, timeLast):
  988.     print("")
  989.     print("You took %s seconds." %(round(timeCurrent, 1)))
  990.     #if timeLast == 0: ######### Tundub, et see on mõttetu rida
  991.     print("Questions are imminent.")
  992.     time.sleep(2)
  993.  
  994.  
  995.  
  996.  
  997. displayStart()
  998. printMode = "K" #Keskmine suurus
  999. purchasing = True #Nuppude ostmine on sees
  1000. queens = True #Lippe saab osta
  1001. placing = "M"
  1002. check = True
  1003. questionsBanned = [0]
  1004. pawnValue = 1
  1005.  
  1006.  
  1007. while True:
  1008.     turn = "M"
  1009.     kingSideCastle = [] #Vangerduse kontroll
  1010.     queenSideCastle = []
  1011.     printMode, purchasing, placing, queens = startMenu(printMode, purchasing, placing, queens, check)
  1012.     boardType = getNewBoardType()
  1013.     boardOwner = getNewBoardOwner()
  1014.     enterToStart()
  1015.     pointsWhite = 0
  1016.     pointsBlack = 0
  1017.     timeLast = 0
  1018.     questionLast = 0
  1019.     blackKingPos = (4,7)
  1020.     whiteKingPos = (4,0)
  1021.     while True:
  1022.         turn = turnChange(turn)
  1023.         printBoard(printMode)
  1024.         if purchasing:
  1025.             timeStart = timerStart()
  1026.         showTurn(turn, pointsWhite, pointsBlack)
  1027.         if not moveStage(turn): #returni false kui kuningas võeti ära
  1028.             break
  1029.         if purchasing:
  1030.             boardType, BoardOwner, pointsBlack, pointsWhite = shop(turn, pointsWhite, pointsBlack, pawnValue, placing)
  1031.             timeCurrent = timerEnd(timeStart)
  1032.             showTime(timeCurrent, timeLast) #Oodata mõni sekund enne küsimuste esitamist
  1033.             timeOverLimit, pointsWhite, pointsBlack = questionStage(turn, timeLast, questionLast, questionsBanned, pointsWhite, pointsBlack)
  1034.             timeLast = timeCurrent + timeOverLimit
  1035.     if not playAgain():
  1036.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement