Advertisement
Guest User

SmartChess beta 1.5

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