Guest User

SmartChess beta 1.4

a guest
Oct 9th, 2014
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 38.55 KB | None | 0 0
  1. import time
  2. import random
  3. import operator
  4.  
  5. DIGITS1TO8 = "1 2 3 4 5 6 7 8".split()
  6. LETTERSATOH = "A B C D E F G H".split()
  7.  
  8. def getNewBoardType():
  9.     boardType = [[" "] * 8 for i in range(8)]
  10.     for x in range(8):
  11.         boardType[x][1] = "E"
  12.         boardType[x][6] = "E"
  13.     typeOrder = ["V", "R", "O", "L", "K", "O", "R", "V"]
  14.     for x in range(8):
  15.         boardType[x][0] = typeOrder[x]
  16.         boardType[x][7] = typeOrder[x]
  17.     return boardType
  18.  
  19. def getNewBoardOwner():
  20.     boardOwner = [[" "] * 8 for i in range(8)]
  21.     for x in range(8):
  22.         boardOwner[x][1] = "V"
  23.         boardOwner[x][0] = "V"
  24.         boardOwner[x][6] = "M"
  25.         boardOwner[x][7] = "M"
  26.     return boardOwner
  27.  
  28. def getPrintMode():
  29.     while True:
  30.         printMode = input("Sisestage malelaua kuvamise suurus (suur, keskmine, väike): ")
  31.         if len(printMode)==0:
  32.             nope = ".avi"
  33.         elif printMode[0].upper()=="S":
  34.             printMode = "S"
  35.             break
  36.         elif printMode[0].upper()=="V":
  37.             printMode = "V"
  38.             break
  39.         elif printMode[0].upper()=="K":
  40.             printMode = "K"
  41.             break
  42.     return printMode
  43.  
  44. def printBoard(printMode):
  45.     if printMode == "S":
  46.         print("  ", end="")
  47.         for x in range(8):
  48.             print("    %s    " %(LETTERSATOH[x]), end="")
  49.         print("")
  50.         for y in range(8):
  51.             print("  "+"+--------"*8, end="")
  52.             print("+")
  53.             print("  "+"|        "*8+"|")
  54.             print(DIGITS1TO8[7-y], end = " ")
  55.             for x in range(8):
  56.                 print("|   %s%s   " %(boardType[x][7-y], boardOwner[x][7-y]), end = "")
  57.             print("|  ", end = " ")
  58.             print(DIGITS1TO8[7-y])
  59.             print("  ", end="")
  60.             for x in range(8):
  61.                 print("|%s%s      " % (LETTERSATOH[x],DIGITS1TO8[8-y-1]), end = "")
  62.             print("|")
  63.         print("  "+"+--------"*8, end="")
  64.         print("+")
  65.         print("  ", end="")
  66.         for x in range(8):
  67.             print("    %s    " %(LETTERSATOH[x]), end="")
  68.         print("")
  69.        
  70.     elif printMode == "V":
  71.         print("  ", end="")
  72.         for x in range(8):
  73.             print("  %s " %(LETTERSATOH[x]), end="")
  74.         print("")
  75.         for y in range(8):
  76.             print("  "+"+---"*8, end="")
  77.             print("+")
  78.             print(DIGITS1TO8[7-y], end = " ")
  79.             for x in range(8):
  80.                 print("|%s%s " %(boardType[x][7-y], boardOwner[x][7-y]), end = "")
  81.             print("|", end = " ")
  82.             print(DIGITS1TO8[7-y])
  83.         print("  "+"+---"*8, end="")
  84.         print("+")
  85.         print("  ", end="")
  86.         for x in range(8):
  87.             print("  %s " %(LETTERSATOH[x]), end="")
  88.         print("")
  89.        
  90.     elif printMode == "K":
  91.         print("  ", end="")
  92.         for x in range(8):
  93.             print("  %s  " %(LETTERSATOH[x]), end="")
  94.         print("")
  95.         for y in range(8):
  96.             print("  "+"+----"*8, end="")
  97.             print("+")
  98.             print(DIGITS1TO8[7-y], end = " ")
  99.             for x in range(8):
  100.                 print("| %s%s " %(boardType[x][7-y], boardOwner[x][7-y]), end = "")
  101.             print("|", end = " ")
  102.             print(DIGITS1TO8[7-y])
  103.         print("  "+"+----"*8, end="")
  104.         print("+")
  105.         print("  ", end="")
  106.         for x in range(8):
  107.             print("  %s  " %(LETTERSATOH[x]), end="")
  108.         print("")
  109.        
  110.  
  111. def turnChange(turn):
  112.     if turn == "V":
  113.         turn = "M"
  114.     else:
  115.         turn = "V"
  116.     return turn
  117.  
  118. def posConvert(position):
  119.     position = ((ord(position[0])-ord("A")), int(position[1])-1)
  120.     return position
  121.  
  122. def getMove(turn):
  123.     while True:
  124.         surrender = 0
  125.         movesTogether = input("Sisestage oma käik: ")
  126.         if movesTogether.lower() == "surrender": #Alla andmine
  127.             surrender = 1
  128.             startPos = (0,0)
  129.             endPos = (0,0)
  130.             return startPos, endPos, surrender
  131.         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:
  132.             startPos = str(movesTogether[0].upper()) + movesTogether[1]
  133.             endPos = str(movesTogether[2].upper()) + movesTogether[3]
  134.             startPos = posConvert(startPos)
  135.             endPos = posConvert(endPos)
  136.             if boardOwner[startPos[0]][startPos[1]] != turn:
  137.                 print("Seal asukohas ei ole teie nuppu.")
  138.             else:
  139.                 return startPos, endPos, surrender
  140.         else:
  141.             print("Sisestage oma käik formaadis [algasukoha täht][algasukoha number][sihtpunkti täht][sihtpunkti number]. Näiteks A2A3.")
  142.  
  143. def checkCheck(X,boardType,boardOwner):
  144.     Y = X[0] + 1
  145.     while Y < 8:
  146.         if boardOwner[Y][X[1]] == turn:
  147.             break
  148.         elif boardType[Y][X[1]] == "V" or boardType[Y][X[1]] == "L": #paremal
  149.             return True
  150.         Y += 1
  151.     Y = X[0] - 1
  152.     while Y > -1:
  153.         if boardOwner[Y][X[1]] == turn:
  154.             break
  155.         elif boardType[Y][X[1]] == "V" or boardType[Y][X[1]] == "L": #vasakul
  156.             return True
  157.         Y -= 1
  158.     Y = X[1] + 1
  159.     while Y < 8:
  160.         if boardOwner[X[0]][Y] == turn:
  161.             break
  162.         elif boardType[X[0]][Y] == "V" or boardType[X[0]][Y] == "L": #üles
  163.             return True
  164.         Y += 1
  165.     Y = X[1] - 1
  166.     while Y > -1:
  167.         if boardOwner[X[0]][Y] == turn:
  168.             break
  169.         elif boardType[X[0]][Y] == "V" or boardType[X[0]][Y] == "L": # alla
  170.             return True
  171.         Y -= 1
  172.        
  173.     Y = X[0] + 1
  174.     Z = X[1] + 1
  175.     while Y < 8 and Z < 8:
  176.         if boardOwner[Y][Z] == turn:
  177.             break
  178.         elif boardType[Y][Z] == "O" or boardType[Y][Z] == "L": #Asimuut 45
  179.             return True
  180.         Y += 1
  181.         Z += 1
  182.        
  183.     Y = X[0] - 1
  184.     Z = X[1] - 1
  185.     while Y > -1 and Z > -1:
  186.         if boardOwner[Y][Z] == turn:
  187.             break
  188.         elif boardType[Y][Z] == "O" or boardType[Y][Z] == "L": #asimuut 225
  189.             return True
  190.         Y -= 1
  191.         Z -= 1
  192.     Y = X[0] + 1
  193.     Z = X[1] - 1
  194.     while Y < 8 and Z > -1:
  195.         if boardOwner[Y][Z] == turn:
  196.             break
  197.         elif boardType[Y][Z] == "O" or boardType[Y][Z] == "L": #asimuut 135
  198.             return True
  199.         Y += 1
  200.         Z -= 1
  201.     Y = X[0] - 1
  202.     Z = X[1] + 1
  203.     while Z < 8 and Y > -1:
  204.         if boardOwner[Y][Z] == turn:
  205.             break
  206.         elif boardType[Y][Z] == "O" or boardType[Y][Z] == "L": #asimuut 135
  207.             return True
  208.         Y -= 1
  209.         Z += 1
  210.                                     #Ratsud
  211.     if X[0]+2 < 8:                  #Paremal
  212.         if X[1]+1 < 8:
  213.             if boardType[X[0]+2][X[1]+1] == "R":
  214.                 if boardOwner[X[0]+2][X[1]+1] != turn:
  215.                     return True
  216.         if X[1]-1 > -1:
  217.             if boardType[X[0]+2][X[1]-1] == "R":
  218.                 if boardOwner[X[0]+2][X[1]-1] != turn:
  219.                     return True
  220.     if X[0]-2 > -1:                 #Vasakul
  221.         if X[1]+1 < 8:
  222.             if boardType[X[0]-2][X[1]+1] == "R":
  223.                 if boardOwner[X[0]-2][X[1]+1] != turn:
  224.                     return True
  225.         if X[1]-1 > -1:
  226.             if boardType[X[0]-2][X[1]-1] == "R":
  227.                 if boardOwner[X[0]-2][X[1]-1] != turn:
  228.                     return True
  229.     if X[1]+2 < 8:                  #Üleval
  230.         if X[0]+1 < 8:
  231.             if boardType[X[0]+1][X[1]+2] == "R":
  232.                 if boardOwner[X[0]+1][X[1]+2] != turn:
  233.                     return True
  234.         if X[0]-1 > -1:
  235.             if boardType[X[0]-1][X[1]+2] == "R":
  236.                 if boardOwner[X[0]-1][X[1]+2] != turn:
  237.                     return True
  238.     if X[1]-2 > -1:                  #All
  239.         if X[0]+1 < 8:
  240.             if boardType[X[0]+1][X[1]-2] == "R":
  241.                 if boardOwner[X[0]+1][X[1]-2] != turn:
  242.                     return True
  243.         if X[0]-1 > -1:
  244.             if boardType[X[0]-1][X[1]-2] == "R":
  245.                 if boardOwner[X[0]-1][X[1]-2] != turn:
  246.                     return True
  247.     if turn == "V":
  248.         if X[1]+1 < 8:
  249.             if X[0]+1 < 8:
  250.                 if boardType[X[0]+1][X[1]+1] == "E" and boardOwner[X[0]+1][X[1]+1] != turn:
  251.                     return True
  252.             if X[0]-1 > -1:
  253.                 if boardType[X[0]-1][X[1]+1] == "E" and boardOwner[X[0]-1][X[1]+1] != turn:
  254.                     return True
  255.     else:
  256.         if X[1]-1 > -1:
  257.             if X[0]+1 < 8:
  258.                 if boardType[X[0]+1][X[1]-1] == "E" and boardOwner[X[0]+1][X[1]-1] != turn:
  259.                     return True
  260.             if X[0]-1 > -1:
  261.                 if boardType[X[0]-1][X[1]-1] == "E" and boardOwner[X[0]-1][X[1]-1] != turn:
  262.                     return True
  263.  
  264.    
  265. def checkValidity(turn, startPos, endPos):
  266.     validMoves = []
  267.     startx = startPos[0]
  268.     starty = startPos[1]
  269.     endx = endPos[0]
  270.     endy = endPos[1]
  271.     if boardType[startx][starty] == "E": #Lisab validMoves listi kõik võimalikud käigud etturi poolt.
  272.         if turn == "V":
  273.             if boardType[startx][starty+1] == " ":
  274.                 validMoves.append((startx,starty+1))
  275.             if startx+1 < 8 and boardOwner[startx+1][starty+1] == "M":
  276.                 validMoves.append((startx+1,startPos[1]+1))
  277.             if startx-1 > -1 and boardOwner[startx-1][starty+1] == "M":
  278.                 validMoves.append((startx-1,starty+1))
  279.             if starty == 1 and boardType[startx][starty+2] == " ":
  280.                 validMoves.append((startx,3))
  281.         if turn == "M":
  282.             if boardType[startx][starty-1] == " ":
  283.                 validMoves.append((startx,starty-1))
  284.             if startx+1 < 8 and boardOwner[startx+1][starty-1] == "V":
  285.                 validMoves.append((startx+1,starty-1))
  286.             if startx-1 > -1 and boardOwner[startx-1][starty-1] == "V":
  287.                 validMoves.append((startx-1,starty-1))
  288.             if starty == 6 and boardType[startx][starty-2] == " ":
  289.                 validMoves.append((startx,4))
  290.                
  291.     if boardType[startx][starty] == "K":
  292.         if startx-1 > -1 and (boardOwner[startx-1][starty] != turn):
  293.             validMoves.append((startx-1,starty))
  294.         if startx+1 < 8 and (boardOwner[startx-1][starty] != turn):
  295.             validMoves.append((startx+1,starty))
  296.         if starty+1 < 8:
  297.             for i in range(3):
  298.                 if startx-1+i < 8 and startx-1+i > - 1:
  299.                     if boardOwner[startx-1+i][starty+1] != turn:
  300.                         validMoves.append((startx-1+i,starty+1))
  301.         if starty-1 > -1:
  302.             for i in range(3):
  303.                 if startx-1+i < 8 and startx-1+i > - 1:
  304.                     if boardOwner[startx-1+i][starty-1] != turn:
  305.                         validMoves.append((startx-1+i,starty-1))
  306.  
  307.     if boardType[startx][starty] == "R":
  308.         if starty+2 < 8:
  309.             if startx-1 > -1:
  310.                 if boardOwner[startx-1][starty+2] != turn:
  311.                     validMoves.append((startx-1,starty+2))
  312.             if startx+1 < 8:
  313.                 if boardOwner[startx+1][starty+2] != turn:
  314.                     validMoves.append((startx+1,starty+2))
  315.         if starty-2 > -1:
  316.             if startx-1 > -1:
  317.                 if boardOwner[startx-1][starty-2] != turn:
  318.                     validMoves.append((startx-1,starty-2))
  319.             if startx+1 < 8:
  320.                 if boardOwner[startx+1][starty-2] != turn:
  321.                     validMoves.append((startx+1,starty-2))
  322.         if startx+2 < 8:
  323.             if starty-1 > -1:
  324.                 if boardOwner[startx+2][starty-1] != turn:
  325.                     validMoves.append((startx+2,starty-1))
  326.             if starty+1 < 8:
  327.                 if boardOwner[startx+2][starty+1] != turn:
  328.                     validMoves.append((startx+2,starty+1))
  329.         if startx-2 > -1 :
  330.             if starty-1 > -1:
  331.                 if boardOwner[startx-2][starty-1] != turn:
  332.                     validMoves.append((startx-2,starty-1))
  333.             if starty+1 < 8:
  334.                 if boardOwner[startx-2][starty+1] != turn:
  335.                     validMoves.append((startx-2,starty+1))
  336.  
  337.     if boardType[startx][starty] == "V" or boardType[startx][starty] == "L":
  338.         for moveLength in range(1,8):
  339.             if starty+moveLength < 8: #1. Üles
  340.                 nextTileNumber = (startx, (starty+moveLength))
  341.                 nextTileOwner = boardOwner[startx][starty+moveLength]
  342.                 if nextTileOwner != turn:
  343.                     if nextTileOwner == " ":
  344.                         validMoves.append(nextTileNumber)
  345.                     else:
  346.                         validMoves.append(nextTileNumber)
  347.                         break
  348.                 else:
  349.                     break
  350.         for moveLength in range(1,8):
  351.             if startx+moveLength < 8: #2. Paremale
  352.                 nextTileNumber = (startx+moveLength, starty)
  353.                 nextTileOwner = boardOwner[startx+moveLength][starty]
  354.                 if nextTileOwner != turn:
  355.                     if nextTileOwner == " ":
  356.                         validMoves.append(nextTileNumber)
  357.                     else:
  358.                         validMoves.append(nextTileNumber)
  359.                         break
  360.                 else:
  361.                     break
  362.         for moveLength in range(1,8):
  363.             if starty-moveLength > (-1): #3. Alla
  364.                 nextTileNumber = (startx, starty-moveLength)
  365.                 nextTileOwner = boardOwner[startx][starty-moveLength]
  366.                 if nextTileOwner != turn:
  367.                     if nextTileOwner == " ":
  368.                         validMoves.append(nextTileNumber)
  369.                     else:
  370.                         validMoves.append(nextTileNumber)
  371.                         break
  372.                 else:
  373.                     break
  374.         for moveLength in range(1,8):
  375.             if startx-moveLength > (-1): #4. Vasakule
  376.                 nextTileNumber = (startx-moveLength, starty)
  377.                 nextTileOwner = boardOwner[startx-moveLength][starty]
  378.                 if nextTileOwner != turn:
  379.                     if nextTileOwner == " ":
  380.                         validMoves.append(nextTileNumber)
  381.                     else:
  382.                         validMoves.append(nextTileNumber)
  383.                         break
  384.                 else:
  385.                     break
  386.     if boardType[startx][starty] == "O" or boardType[startx][starty] == "L":
  387.         for moveLength in range(1,8): #1. asimuut 45
  388.             if startx+moveLength < 8 and starty+moveLength < 8:
  389.                 nextTileNumber = (startx+moveLength, starty+moveLength)
  390.                 nextTileOwner = boardOwner[startx+moveLength][starty+moveLength]
  391.                 if nextTileOwner != turn:
  392.                     if nextTileOwner == " ":
  393.                         validMoves.append(nextTileNumber)
  394.                     else:
  395.                         validMoves.append(nextTileNumber)
  396.                         break
  397.                 else:
  398.                     break
  399.         for moveLength in range(1,8): #2. asimuut 135
  400.             if startx+moveLength < 8 and starty-moveLength > (-1):
  401.                 nextTileNumber = (startx+moveLength, starty-moveLength)
  402.                 nextTileOwner = boardOwner[startx+moveLength][starty-moveLength]
  403.                 if nextTileOwner != turn:
  404.                     if nextTileOwner == " ":
  405.                         validMoves.append(nextTileNumber)
  406.                     else:
  407.                         validMoves.append(nextTileNumber)
  408.                         break
  409.                 else:
  410.                     break
  411.         for moveLength in range(1,8): #1. asimuut 225
  412.             if startx-moveLength > (-1) and starty-moveLength > (-1):
  413.                 nextTileNumber = (startx-moveLength, starty-moveLength)
  414.                 nextTileOwner = boardOwner[startx-moveLength][starty-moveLength]
  415.                 if nextTileOwner != turn:
  416.                     if nextTileOwner == " ":
  417.                         validMoves.append(nextTileNumber)
  418.                     else:
  419.                         validMoves.append(nextTileNumber)
  420.                         break
  421.                 else:
  422.                     break
  423.         for moveLength in range(1,8): #1. asimuut 315
  424.             if startx+moveLength > (-1) and starty+moveLength < 8:
  425.                 nextTileNumber = (startx-moveLength, starty+moveLength)
  426.                 nextTileOwner = boardOwner[startx-moveLength][starty+moveLength]
  427.                 if nextTileOwner != turn:
  428.                     if nextTileOwner == " ":
  429.                         validMoves.append(nextTileNumber)
  430.                     else:
  431.                         validMoves.append(nextTileNumber)
  432.                         break
  433.                 else:
  434.                     break
  435.                
  436.     if check == True:
  437.         boardTypeTemp = [[" "] * 8 for i in range(8)]
  438.         boardOwnerTemp = [[" "] * 8 for i in range(8)]
  439.         for i in range(8):
  440.             for y in range(8):
  441.                 boardTypeTemp[i][y]=(boardType[i][y])
  442.                 boardOwnerTemp[i][y]=(boardOwner[i][y])
  443.         boardTypeTemp[endPos[0]][endPos[1]] = boardType[startPos[0]][startPos[1]]
  444.         boardOwnerTemp[endPos[0]][endPos[1]] = turn
  445.         boardTypeTemp[startPos[0]][startPos[1]] = " "
  446.         boardOwnerTemp[startPos[0]][startPos[1]] = " "
  447.         if boardType[startx][starty] == "K":
  448.                 if checkCheck((endPos[0],endPos[1]),boardTypeTemp,boardOwnerTemp):
  449.                     print("Tuli! Te ei saa sinna käia.")
  450.                     return False
  451.         elif turn == "V":
  452.             if checkCheck(whiteKingPos,boardTypeTemp,boardOwnerTemp):
  453.                 print("Tuli! Te ei saa sinna käia.")
  454.                 return False
  455.         else:
  456.             if checkCheck(blackKingPos,boardTypeTemp,boardOwnerTemp):
  457.                 print("Tuli! Te ei saa sinna käia.")
  458.                 return False    
  459.     if boardType[startx][starty] == "K": ################ Vangerdus
  460.         if turn == "V":
  461.             if (endx, endy) == (6, 0) and "V" not in kingSideCastle:
  462.                 if boardType[5][0] == " " and not checkCheck((5,0),boardType,boardOwner) and boardType[6][0] == " " and not checkCheck((6,0),boardType,boardOwner):
  463.                     return "whiteKingSide"
  464.             elif (endx, endy) == (2, 0) and "V" not in queenSideCastle:
  465.                 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] == " ":
  466.                     return "whiteQueenSide"
  467.         else:
  468.             if (endx, endy) == (6, 7) and "M" not in kingSideCastle:
  469.                 if boardType[5][7] == " " and not checkCheck((5,7),boardType,boardOwner) and boardType[6][7] == " " and not checkCheck((6,7),boardType,boardOwner):
  470.                     return "blackKingSide"
  471.             elif (endx, endy) == (2, 7) and "M" not in queenSideCastle:
  472.                 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] == " ":
  473.                     return "blackQueenSide"
  474.                        
  475.     if (endx, endy) in validMoves:
  476.         return True
  477.     else:
  478.         print("Te ei saa sinna käia.")
  479.         return False
  480.  
  481. def castle(x): ########## Vangerdamise laua muutmine
  482.     if x == "whiteKingSide":
  483.         boardType[5][0] = "V"
  484.         boardOwner[5][0] = "V"
  485.         boardType[7][0] = " "
  486.         boardOwner[7][0] = " "
  487.         boardType[6][0] = "K"
  488.         boardOwner[6][0] = "V"
  489.         boardType[4][0] = " "
  490.         boardOwner[4][0] = " "
  491.         kingSideCastle.append("V")
  492.         queenSideCastle.append("V")
  493.     elif x == "blackKingSide":
  494.         boardType[5][7] = "V"
  495.         boardOwner[5][7] = "M"
  496.         boardType[7][7] = " "
  497.         boardOwner[7][7] = " "
  498.         boardType[6][7] = "K"
  499.         boardOwner[6][7] = "M"
  500.         boardType[4][7] = " "
  501.         boardOwner[4][7] = " "
  502.         kingSideCastle.append("M")
  503.         queenSideCastle.append("M")
  504.     elif x == "whiteQueenSide":
  505.         print("x")
  506.         boardType[3][0] = "V"
  507.         boardOwner[3][0] = "V"
  508.         boardType[0][0] = " "
  509.         boardOwner[0][0] = " "
  510.         boardType[2][0] = "K"
  511.         boardOwner[2][0] = "V"
  512.         boardType[4][0] = " "
  513.         boardOwner[4][0] = " "
  514.         kingSideCastle.append("V")
  515.         queenSideCastle.append("V")
  516.     elif x == "blackQueenSide":
  517.         boardType[3][7] = "V"
  518.         boardOwner[3][7] = "M"
  519.         boardType[0][7] = " "
  520.         boardOwner[0][7] = " "
  521.         boardType[2][7] = "K"
  522.         boardOwner[2][7] = "M"
  523.         boardType[4][7] = " "
  524.         boardOwner[4][7] = " "
  525.         kingSideCastle.append("M")
  526.         queenSideCastle.append("M")
  527.  
  528. def pawnUpgrade(endPos):
  529.     while True:
  530.         command = input("Milliseks nupuks te tahate oma etturi uuendada [L/O/V/R]? ")
  531.         if len(command) == 1 and command[0].upper() in "L O V R".split():
  532.             boardType[endPos[0]][endPos[1]] = command[0].upper()
  533.             return
  534.  
  535. def changeBoard(turn, startPos, endPos):
  536.     if boardType[endPos[0]][endPos[1]] == "K":
  537.         if turn == "V":
  538.             print("Valge võit.")
  539.         else:
  540.             print("Musta võit.")
  541.         return False        
  542.     boardType[endPos[0]][endPos[1]] = boardType[startPos[0]][startPos[1]]
  543.     boardOwner[endPos[0]][endPos[1]] = turn
  544.     boardType[startPos[0]][startPos[1]] = " "
  545.     boardOwner[startPos[0]][startPos[1]] = " "
  546.     ################################### Vankri liikumine
  547.     if boardType[endPos[0]][endPos[1]] == "V":
  548.         if startPos == (0,0):
  549.             queenSideCastle.append("V")
  550.         elif startPos == (0,7):
  551.             queenSideCastle.append("M")
  552.         elif startPos == (7,0):
  553.             kingSideCastle.append("V")
  554.         elif startPos == (7,7):
  555.             kingSideCastle.append("M")
  556.     if turn == "V": #Etturite ülendamine mängulaua lõppu jõudes
  557.         if boardType[endPos[0]][endPos[1]] == "E" and endPos[1] == 7:
  558.             pawnUpgrade(endPos)
  559.     else:
  560.         if boardType[endPos[0]][endPos[1]] == "E" and endPos[1] == 0:
  561.             pawnUpgrade(endPos)
  562.            
  563.     if boardType[endPos[0]][endPos[1]] == "K": #Kuninga liikumine
  564.         if turn == "V":
  565.             kingSideCastle.append("V")
  566.             queenSideCastle.append("V")
  567.             whiteKingPos = (endPos[0],endPos[1])
  568.         else:
  569.             kingSideCastle.append("M")
  570.             queenSideCastle.append("M")
  571.             blackKingPos = (endPos[0],endPos[1])
  572.     return True
  573.  
  574. def moveStage(turn):
  575.     while True:
  576.         startPos, endPos, surrender = getMove(turn)
  577.         if surrender == 1:# alla andmine
  578.             if turn == "V":
  579.                 print("Valge andis alla.")
  580.             else:
  581.                 print("Must andis alla.")
  582.             return False
  583.         temp = checkValidity (turn, startPos, endPos)
  584.         if temp != True and temp != False:
  585.             castle(temp)
  586.             return True
  587.         elif temp:
  588.             if changeBoard(turn, startPos, endPos): #returni false kui kuningas võeti ära
  589.                 return True
  590.             else:
  591.                 return False
  592.  
  593. def playAgain():
  594.     while True:
  595.         command = input("Kas soovite uuesti mängida? (jah/ei): ") #Jah
  596.         if len(command)==0:
  597.             nope = ".avi"
  598.         elif command[0].lower() == "j":
  599.             return True
  600.         elif command[0].lower() == "e":
  601.             return False
  602.  
  603. def showTurn(turn):
  604.     if turn == "V":
  605.         print("Valge käik.")
  606.     else:
  607.         print("Musta käik.")
  608.  
  609. def displayTutorial():
  610.     print("-------------------------------------------------------------------------")
  611.     print("SmartChess on malemäng, kus saab iga käik mänguväliseid punkte ülesannete lahendamise eest.")
  612.     print("Pärast igat enda käiku saab osta olemasolevate punktide eest nuppe, mida saab lauale asetada.") #Täiendada juhendit
  613.     print("Mida kauem sa mõtled oma käikude ja nuppude ostmise üle, seda rohkem on su vastasel aega ülesannete lahendamiseks.")
  614.     print("Ülesannete lahendamisel loevad kõik vastused, kuid selle võrra, kui palju te ajast üle lähete,")
  615.     print("saab vastane oma ülesannete lahendamisel aega juurde.")
  616.     print("Pärast mängu alustamist pole pause, et ära hoida sohki tegemine.")
  617.     print("Malelaud on koordinaatteljestik, kus veerud on tähistatud tähtedega a-h")
  618.     print("ja read numbritega 1-8, kusjuures loendamine algab alt vasakust nurgast.")
  619.     print("Käigu sooritamiseks trükkige nelja-sümboliline kombinatsioon, kus esimesed kaks sümbolit on liigutatava nupu koordinaadid")
  620.     print("ja viimased kaks on sihtruudu koordinaadid, nt: Kirjutades E2E4 esimesel käigul, liigub valge ettur ruudul E2 ruutu E4.")
  621.     print("Nuppude tähised on: E - ettur, V - vanker, R - ratsu, O - oda, L - lipp, K - kuningas. Värve tähistatakse tähtedega V ja M.")
  622.     print("Malelaual on nupud tähistatud kahe tähega, kus esimene täht näitab tüüpi, teine värvi.")
  623.     print("Vahel küsitakse teilt küsimus, mille lõpus on sõnad, mille esimesed tähed on nurksulgudes (näiteks [L]ipp).")
  624.     print("Need sõnad on vastusevariandid.")
  625.     print("Sellises olukorras peate teadma, et endale sobiva variandi tegemiseks peate sisestama ühe sulgudes olevatest tähtedest.")
  626.     print("-------------------------------------------------------------------------")
  627.    
  628. def enterToStart():
  629.     input("Vajutage enter, et mäng algaks.")
  630.  
  631. def displayStart():
  632.     print("SmartChess(c) 2014 Beta 1.4")
  633.     print("Copyright: J.Kirme and A.Saidlo")
  634.     print("Kui mängite esimest korda, on soovitatud juhendit lugeda.")
  635.  
  636. def displayOptions(printMode, purchasing, placing, queens, check):
  637.     print("{0:30}".format("Laua kuvamise suurus:") + printMode)
  638.     if check:
  639.         print ("{0:30}".format("Tuli") + "Sees")
  640.     else:
  641.         print ("{0:30}".format("Tuli:") + "Väljas")
  642.     if purchasing:
  643.         print ("{0:30}".format("Uute nuppude ostmine:") + "Sees")
  644.     else:
  645.         print ("{0:30}".format("Uute nuppude ostmine:") + "Väljas")
  646.     if placing == "M":
  647.         print ("{0:30}".format("Nuppude asetamine:") + "Mõlemad")
  648.     elif placing == "T":
  649.         print ("{0:30}".format("Nuppude asetamine:") + "Tagumine rida")
  650.     elif placing == "P":
  651.         print ("{0:30}".format("Nuppude asetamine:") + "Promotion")    
  652.     if queens:
  653.         print ("{0:30}".format("Lippude ostmine:") + "Sees")
  654.     else:
  655.         print ("{0:30}".format("Lippude ostmine:") + "Väljas")        
  656.  
  657. def changeOptions(printMode, purchasing, placing, queens, check):
  658.     while True:
  659.         print("-------------------------------------------------------------------------")
  660.         displayOptions(printMode, purchasing, placing, queens, check)
  661.         print("-------------------------------------------------------------------------")
  662.         print("Millist valikut soovite muuta? ")
  663.         command = input("[S]uurus/T[u]li/[O]stmine/[A]setamine/[L]ipud/[T]agasi ")
  664.         if len(command)==0: #Miks ma neid ei kirjutanud if len!=0? -J
  665.             nope = ".avi"
  666.         elif command[0].upper() == "S":
  667.             printMode = getPrintMode()
  668.         elif command[0].upper() == "O":
  669.             if purchasing == True:
  670.                 purchasing = False
  671.             else:
  672.                 purchasing = True
  673.         elif command[0].upper() == "A":
  674.             while True:
  675.                 placing = input("Milline peaks olema uute nuppude asetamine? ([T]agumine rida/[P]romotion/[M]õlemad) ")
  676.                 if len(placing) != 0:
  677.                     placing = placing[0].upper()
  678.                     if placing in ("T","P","M"):
  679.                         break
  680.                 else:
  681.                     print("Palun jälgige õigeid sisestusreegleid. ")
  682.         elif command[0].upper() == "L":
  683.             if queens == True:
  684.                 queens = False
  685.             else:
  686.                 queens = True
  687.         elif command[0].upper() == "U":
  688.             if check == True:
  689.                 check = False
  690.             else:
  691.                 check = True
  692.         elif command[0].upper() == "T":
  693.             break
  694.     return printMode, purchasing, placing, queens
  695.  
  696. def startMenu(printMode, purchasing, placing, queens, check):
  697.     while True:
  698.         command = input("Kas soovite [A]lustada, [J]uhendit näha või [V]alikuid muuta? ")
  699.         if len(command)==0:
  700.             nope = ".avi"
  701.         elif command[0].upper() == "A":
  702.             return printMode, purchasing, placing, queens
  703.         elif command[0].upper() == "J":
  704.             displayTutorial()
  705.         elif command[0].upper() == "V":
  706.             printMode, purchasing, placing, queens = changeOptions(printMode, purchasing, placing, queens, check)
  707.  
  708. def shop(turn, pointsWhite, pointsBlack, pawnValue, placing):
  709.     R = 0    
  710.     validPlaces = []
  711.     printBoard(printMode)
  712.     if turn == "V":
  713.         points = pointsWhite
  714.     else:
  715.         points = pointsBlack
  716.     while True:
  717.         pood = (input("Kas soovite uue nupu osta?[J/E] "))
  718.         if len(pood) != 0:
  719.             pood = pood[0].upper()
  720.             if pood in ("E","J"):
  721.                 break
  722.         else:
  723.             print("Palun jälgige õigeid sisestusreegleid. ")
  724.     if pood == "J":
  725.         print("-------------------------------------------------------------------------")
  726.         print("{0:10}".format("Pood") + "Punktid:",points,"\n")
  727.         if queens:
  728.             print("{0:10}".format("Lipp:"), 9*pawnValue)
  729.         print("{0:10}".format("Vanker:"), 5*pawnValue)
  730.         print("{0:10}".format("Oda/Ratsu:"), 3*pawnValue)
  731.         print("-------------------------------------------------------------------------")
  732.         while True:
  733.             while True:
  734.                 if queens:
  735.                     newPiece = input("Millist nuppu soovite osta? ([L]ipp, [O]da, [R]atsu, [V]vanker, [T]ühista) ")
  736.                     if len(newPiece) != 0:
  737.                         newPiece = newPiece[0].upper()
  738.                     if newPiece in ("L","O","R","V"):
  739.                         if newPiece == "L" and points >= 9*pawnValue:
  740.                             points -= 9*pawnValue
  741.                             break
  742.                         if newPiece == "V" and points >= 5*pawnValue:
  743.                             points -= 5*pawnValue
  744.                             break
  745.                         if newPiece == "O" and points >= 3*pawnValue:
  746.                             points -= 3*pawnValue
  747.                             break
  748.                         if newPiece == "R" and points >= 3*pawnValue:
  749.                             points -= 3*pawnValue
  750.                             break                    
  751.                 else:
  752.                     newPiece = input("Millist nuppu soovite osta? ([O]da, [R]atsu, [V]vanker, [T]ühista) ")
  753.                     if len(newPiece) != 0:
  754.                         newPiece = newPiece[0].upper()
  755.                     if newPiece in ("O","R","V"):                        
  756.                         if newPiece == "V" and points >= 5*pawnValue:
  757.                             points -= 5*pawnValue
  758.                             break
  759.                         if newPiece == "O" and points >= 3*pawnValue:
  760.                             points -= 3*pawnValue
  761.                             break
  762.                         if newPiece == "R" and points >= 3*pawnValue:
  763.                             points -= 3*pawnValue
  764.                             break
  765.                     elif newPiece == "L":
  766.                         print("Lipud on keelatud.")
  767.                
  768.                 if newPiece == "T":
  769.                     break
  770.                 else:
  771.                     print("Palun sisestage korrektne nuputähis.")
  772.             if newPiece == "T":
  773.                 break
  774.  
  775.             if turn == "V":
  776.                 backup = pointsWhite
  777.                 pointsWhite = points
  778.             else:
  779.                 backup = pointsBlack
  780.                 pointsBlack = points
  781.  
  782.            
  783.             if placing == "M" or placing == "T":
  784.                 if turn == "V":
  785.                     for i in range(8):
  786.                         if boardType[i][0] == " ":
  787.                             validPlaces.append((i,0))
  788.                 else:
  789.                     for i in range(8):
  790.                         if boardType[i][7] == " ":
  791.                             validPlaces.append((i,7))
  792.             if placing == "M" or placing == "P":
  793.                 for i in range(8):
  794.                     for j in range(8):
  795.                         if boardOwner[i][j] == turn and boardType[i][j] != "K":
  796.                             validPlaces.append((i,j))
  797.             while True:
  798.                 place = str(input("Kuhu soovite oma nupu asetada?([T]ühista kui muutsite meelt) "))
  799.                 if len(place) != 0:
  800.                    
  801.                     if len(place) == 2 and place[0].upper() in LETTERSATOH and place[1] in DIGITS1TO8:
  802.                         place = str(place[0].upper()) + str(place[1])
  803.                         place = posConvert(place)
  804.                         if place in validPlaces:
  805.                             boardType[place[0]][place[1]] = newPiece
  806.                             boardOwner[place[0]][place[1]] = turn
  807.                             R = 1
  808.                             break
  809.                         if placing == "T" and boardOwner[place[0]][place[1]] == turn:
  810.                             print("Promotion pole lubatud.")
  811.                         elif placing == "P" and boardOwner[place[0]][place[1]] == " " and place[1] == 0:
  812.                             print("Lubatud on ainult promotion.")
  813.                         elif boardType[place[0]][place[1]] == "K":
  814.                             print("Sa ei saa kuningat promotida :)")
  815.                    
  816.                     elif place[0].upper() == "T":
  817.                         if turn == "V":
  818.                             pointsWhite == backup
  819.                         else:
  820.                             pointsBlack == backup
  821.                         break
  822.                 else:
  823.                     print("Pole korrektne sisend.")
  824.             if R == 1 or place == "T":
  825.                 break        
  826.     return boardType, boardOwner,pointsBlack,pointsWhite    
  827.  
  828. def getNewOperationMath():
  829.     x = random.randint(1000,9999)#Liitmine
  830.     y = random.randint(1000,9999)
  831.     answerRight = str(x+y)
  832.     operationDisplay = str(x) + "+" + str(y)
  833.     pointsWorth = 1
  834.     return answerRight, operationDisplay, pointsWorth
  835.  
  836. def giveOperationMath(turn, pointsWhite, pointsBlack):
  837.     answerRight, operationDisplay, pointsWorth = getNewOperationMath()
  838.     if turn == "V":
  839.         print("Punkte: %s" %pointsWhite)
  840.     else:
  841.         print("Punkte: %s" %pointsBlack)
  842.     answerPlayer = input("%s = " %(operationDisplay))
  843.     return answerRight, answerPlayer, pointsWorth
  844.  
  845. def mathStage(turn, timeLast, pointsWhite, pointsBlack):
  846.     print("Teil on %s sekundit aega." % (round(timeLast, 1)))
  847.     time.sleep(3)
  848.     timeStart = timerStart()
  849.     while True:
  850.         if time.time() - timeStart > timeLast:
  851.             break
  852.         else:
  853.             answerRight, answerPlayer, pointsWorth = giveOperationMath(turn, pointsWhite, pointsBlack)
  854.         if turn == "V":
  855.             if answerRight == answerPlayer:
  856.                 pointsWhite += pointsWorth
  857.             else:
  858.                 pointsWhite -= pointsWorth
  859.         else:
  860.             if answerRight == answerPlayer:
  861.                 pointsBlack += pointsWorth
  862.             else:
  863.                 pointsBlack -= pointsWorth
  864.     timeOverLimit = time.time()-timeStart-timeLast
  865.     print("Vastasele antakse aega %s sekundit ajalimiidi ületamise eest." %(round(timeOverLimit, 1)))
  866.     return timeOverLimit, pointsWhite, pointsBlack
  867.    
  868.  
  869. def questionStage(turn, timeLast, questionLast, questionsBanned, pointsWhite, pointsBlack): #Lisa siit alates
  870.     timeOverLimit = 0
  871.     if timeLast == 0 and turn == "V":
  872.         print("Esimesel käigul ei saa ülesandeid lahendada.")
  873.     elif questionLast == 0: #Must saab juba küsimuse 1. käigul
  874.         while questionLast in questionsBanned:
  875.             questionLast = random.randint(1,100)
  876.     elif turn == "V": #Valge käigul tuleb uus voor
  877.         while questionLast in questionsBanned:
  878.             questionLast = random.randint(1,100)
  879.     if questionLast > 0 and questionLast <= 100: #Pranglimine, vaheta tagasi 1-90 #############################
  880.         print("Pranglimise voor.")
  881.         timeOverLimit, pointsWhite, pointsBlack = mathStage(turn, timeLast, pointsWhite, pointsBlack)
  882.     elif questionLast > 100: #Eesti keel. Vaheta tagasi 91-100 ##########################
  883.         print("Eesti keele voor. See on hetkel olematu. Jätkake.")
  884.     if turn == "V" and questionLast != 0:
  885.         print("Punkte: %s" %(pointsWhite))
  886.     elif questionLast != 0:
  887.         print("Punkte: %s" %(pointsBlack))
  888.     print("Käigu vahetus mõne hetke pärast.")
  889.     time.sleep(5)
  890.     return timeOverLimit, pointsWhite, pointsBlack
  891.  
  892. def timerStart():
  893.     timeStart = time.time()
  894.     return timeStart
  895.  
  896. def timerEnd(timeStart):
  897.     timeCurrent = time.time()-timeStart
  898.     return timeCurrent
  899.    
  900. def showTime(timeCurrent, timeLast):
  901.     print("Teil kulus %s sekundit." %(round(timeCurrent, 1)))
  902.     if timeLast == 0:
  903.         print("Kohe algavad küsimused.")
  904.         time.sleep(2)
  905.  
  906.  
  907.  
  908.  
  909. displayStart()
  910. printMode = "K" #Keskmine suurus
  911. purchasing = True #Nuppude ostmine on sees
  912. queens = True #Lippe saab osta
  913. placing = "M"
  914. check = True
  915. blackKingPos = (4,7)
  916. whiteKingPos = (4,0)
  917. questionLast = 0
  918. timeLast = 0
  919. questionsBanned = [0]
  920. pawnValue = 1
  921. pointsWhite = 0
  922. pointsBlack = 0
  923.  
  924. while True:
  925.     turn = "M"
  926.     kingSideCastle = [] #Vangerduse kontroll
  927.     queenSideCastle = []
  928.     printMode, purchasing, placing, queens = startMenu(printMode, purchasing, placing, queens, check)
  929.     boardType = getNewBoardType()
  930.     boardOwner = getNewBoardOwner()
  931.     enterToStart()
  932.     while True:
  933.         turn = turnChange(turn)
  934.         printBoard(printMode)
  935.         if purchasing:
  936.             timeStart = timerStart()
  937.         showTurn(turn)
  938.         if not moveStage(turn): #returni false kui kuningas võeti ära
  939.             break
  940.         if purchasing:
  941.             boardType, BoardOwner, pointsBlack, pointsWhite = shop(turn, pointsWhite, pointsBlack, pawnValue, placing)
  942.             timeCurrent = timerEnd(timeStart)
  943.             showTime(timeCurrent, timeLast) #Oodata mõni sekund enne küsimuste esitamist
  944.             timeOverLimit, pointsWhite, pointsBlack = questionStage(turn, timeLast, questionLast, questionsBanned, pointsWhite, pointsBlack)
  945.             timeLast = timeCurrent + timeOverLimit
  946.     if not playAgain():
  947.         break
Advertisement
Add Comment
Please, Sign In to add comment