Advertisement
Guest User

SmartChess beta 1.5.2

a guest
Nov 4th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 40.07 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]] == turn:
  154.             break
  155.         elif boardOwner[Y][X[1]] != " ":
  156.             if boardType[Y][X[1]] == "V" or boardType[Y][X[1]] == "L": #paremal
  157.                 return True
  158.             else:
  159.                 break
  160.         Y += 1
  161.        
  162.     Y = X[0] - 1
  163.     while Y > -1:
  164.         if boardOwner[Y][X[1]] == turn:
  165.             break
  166.         elif boardOwner[Y][X[1]] != " ":
  167.             if boardType[Y][X[1]] == "V" or boardType[Y][X[1]] == "L": #vasakul
  168.                 return True
  169.             else:
  170.                 break
  171.         Y -= 1
  172.     Y = X[1] + 1
  173.     while Y < 8:
  174.         if boardOwner[X[0]][Y] == turn:
  175.             break
  176.         elif boardOwner[X[0]][Y] != " ":
  177.             if boardType[X[0]][Y] == "V" or boardType[X[0]][Y] == "L": #üles
  178.                 return True
  179.             else:
  180.                 break
  181.         Y += 1
  182.     Y = X[1] - 1
  183.     while Y > -1:
  184.         if boardOwner[X[0]][Y] == turn:
  185.             break
  186.         elif boardOwner[X[0]][Y] != " ":
  187.             if boardType[X[0]][Y] == "V" or boardType[X[0]][Y] == "L": # alla
  188.                 return True
  189.             else:
  190.                 break
  191.         Y -= 1
  192.        
  193.     Y = X[0] + 1
  194.     Z = X[1] + 1
  195.     while Y < 8 and Z < 8:
  196.         if boardOwner[Y][Z] == turn:
  197.             break
  198.         elif boardOwner[Y][Z] != " ":
  199.             if boardType[Y][Z] == "O" or boardType[Y][Z] == "L": #Asimuut 45
  200.                 return True
  201.             else:
  202.                 break
  203.         Y += 1
  204.         Z += 1
  205.        
  206.     Y = X[0] - 1
  207.     Z = X[1] - 1
  208.     while Y > -1 and Z > -1:
  209.         if boardOwner[Y][Z] == turn:
  210.             break
  211.         elif boardOwner[Y][Z] != " ":
  212.             if boardType[Y][Z] == "O" or boardType[Y][Z] == "L": #asimuut 225
  213.                 return True
  214.             else:
  215.                 break
  216.         Y -= 1
  217.         Z -= 1
  218.        
  219.     Y = X[0] + 1
  220.     Z = X[1] - 1
  221.     while Y < 8 and Z > -1:
  222.         if boardOwner[Y][Z] == turn:
  223.             break
  224.         elif boardOwner[Y][Z] != " ":            
  225.             if boardType[Y][Z] == "O" or boardType[Y][Z] == "L": #asimuut 135
  226.                 return True
  227.             else:
  228.                 break
  229.            
  230.         Y += 1
  231.         Z -= 1
  232.     Y = X[0] - 1
  233.     Z = X[1] + 1
  234.     while Z < 8 and Y > -1:
  235.         if boardOwner[Y][Z] == turn:
  236.             break
  237.         elif boardOwner[Y][Z] != " ":
  238.             if boardType[Y][Z] == "O" or boardType[Y][Z] == "L": #asimuut 315
  239.                 return True
  240.             else:
  241.                 break
  242.         Y -= 1
  243.         Z += 1
  244.                                     #Ratsud
  245.     if X[0]+2 < 8:                  #Paremal
  246.         if X[1]+1 < 8:
  247.             if boardType[X[0]+2][X[1]+1] == "R":
  248.                 if boardOwner[X[0]+2][X[1]+1] != turn:
  249.                     return True
  250.         if X[1]-1 > -1:
  251.             if boardType[X[0]+2][X[1]-1] == "R":
  252.                 if boardOwner[X[0]+2][X[1]-1] != turn:
  253.                     return True
  254.     if X[0]-2 > -1:                 #Vasakul
  255.         if X[1]+1 < 8:
  256.             if boardType[X[0]-2][X[1]+1] == "R":
  257.                 if boardOwner[X[0]-2][X[1]+1] != turn:
  258.                     return True
  259.         if X[1]-1 > -1:
  260.             if boardType[X[0]-2][X[1]-1] == "R":
  261.                 if boardOwner[X[0]-2][X[1]-1] != turn:
  262.                     return True
  263.     if X[1]+2 < 8:                  #Üleval
  264.         if X[0]+1 < 8:
  265.             if boardType[X[0]+1][X[1]+2] == "R":
  266.                 if boardOwner[X[0]+1][X[1]+2] != turn:
  267.                     return True
  268.         if X[0]-1 > -1:
  269.             if boardType[X[0]-1][X[1]+2] == "R":
  270.                 if boardOwner[X[0]-1][X[1]+2] != turn:
  271.                     return True
  272.     if X[1]-2 > -1:                  #All
  273.         if X[0]+1 < 8:
  274.             if boardType[X[0]+1][X[1]-2] == "R":
  275.                 if boardOwner[X[0]+1][X[1]-2] != turn:
  276.                     return True
  277.         if X[0]-1 > -1:
  278.             if boardType[X[0]-1][X[1]-2] == "R":
  279.                 if boardOwner[X[0]-1][X[1]-2] != turn:
  280.                     return True
  281.     if turn == "V":
  282.         if X[1]+1 < 8:
  283.             if X[0]+1 < 8:
  284.                 if boardType[X[0]+1][X[1]+1] == "E" and boardOwner[X[0]+1][X[1]+1] != turn:
  285.                     return True
  286.             if X[0]-1 > -1:
  287.                 if boardType[X[0]-1][X[1]+1] == "E" and boardOwner[X[0]-1][X[1]+1] != turn:
  288.                     return True
  289.     else:
  290.         if X[1]-1 > -1:
  291.             if X[0]+1 < 8:
  292.                 if boardType[X[0]+1][X[1]-1] == "E" and boardOwner[X[0]+1][X[1]-1] != turn:
  293.                     return True
  294.             if X[0]-1 > -1:
  295.                 if boardType[X[0]-1][X[1]-1] == "E" and boardOwner[X[0]-1][X[1]-1] != turn:
  296.                     return True
  297.  
  298.    
  299. def checkValidity(turn, startPos, endPos):
  300.     validMoves = []
  301.     startx = startPos[0]
  302.     starty = startPos[1]
  303.     endx = endPos[0]
  304.     endy = endPos[1]
  305.     if boardType[startx][starty] == "E": #Lisab validMoves listi kõik võimalikud käigud etturi poolt.
  306.         if turn == "V":
  307.             if boardType[startx][starty+1] == " ":
  308.                 validMoves.append((startx,starty+1))
  309.             if startx+1 < 8 and boardOwner[startx+1][starty+1] == "M":
  310.                 validMoves.append((startx+1,startPos[1]+1))
  311.             if startx-1 > -1 and boardOwner[startx-1][starty+1] == "M":
  312.                 validMoves.append((startx-1,starty+1))
  313.             if starty == 1 and boardType[startx][starty+2] == " ":
  314.                 validMoves.append((startx,3))
  315.         if turn == "M":
  316.             if boardType[startx][starty-1] == " ":
  317.                 validMoves.append((startx,starty-1))
  318.             if startx+1 < 8 and boardOwner[startx+1][starty-1] == "V":
  319.                 validMoves.append((startx+1,starty-1))
  320.             if startx-1 > -1 and boardOwner[startx-1][starty-1] == "V":
  321.                 validMoves.append((startx-1,starty-1))
  322.             if starty == 6 and boardType[startx][starty-2] == " ":
  323.                 validMoves.append((startx,4))
  324.                
  325.     if boardType[startx][starty] == "K":
  326.         if startx-1 > -1 and (boardOwner[startx-1][starty] != turn):
  327.             validMoves.append((startx-1,starty))
  328.         if startx+1 < 8 and (boardOwner[startx-1][starty] != turn):
  329.             validMoves.append((startx+1,starty))
  330.         if starty+1 < 8:
  331.             for i in range(3):
  332.                 if startx-1+i < 8 and startx-1+i > - 1:
  333.                     if boardOwner[startx-1+i][starty+1] != turn:
  334.                         validMoves.append((startx-1+i,starty+1))
  335.         if starty-1 > -1:
  336.             for i in range(3):
  337.                 if startx-1+i < 8 and startx-1+i > - 1:
  338.                     if boardOwner[startx-1+i][starty-1] != turn:
  339.                         validMoves.append((startx-1+i,starty-1))
  340.  
  341.     if boardType[startx][starty] == "R":
  342.         if starty+2 < 8:
  343.             if startx-1 > -1:
  344.                 if boardOwner[startx-1][starty+2] != turn:
  345.                     validMoves.append((startx-1,starty+2))
  346.             if startx+1 < 8:
  347.                 if boardOwner[startx+1][starty+2] != turn:
  348.                     validMoves.append((startx+1,starty+2))
  349.         if starty-2 > -1:
  350.             if startx-1 > -1:
  351.                 if boardOwner[startx-1][starty-2] != turn:
  352.                     validMoves.append((startx-1,starty-2))
  353.             if startx+1 < 8:
  354.                 if boardOwner[startx+1][starty-2] != turn:
  355.                     validMoves.append((startx+1,starty-2))
  356.         if startx+2 < 8:
  357.             if starty-1 > -1:
  358.                 if boardOwner[startx+2][starty-1] != turn:
  359.                     validMoves.append((startx+2,starty-1))
  360.             if starty+1 < 8:
  361.                 if boardOwner[startx+2][starty+1] != turn:
  362.                     validMoves.append((startx+2,starty+1))
  363.         if startx-2 > -1 :
  364.             if starty-1 > -1:
  365.                 if boardOwner[startx-2][starty-1] != turn:
  366.                     validMoves.append((startx-2,starty-1))
  367.             if starty+1 < 8:
  368.                 if boardOwner[startx-2][starty+1] != turn:
  369.                     validMoves.append((startx-2,starty+1))
  370.  
  371.     if boardType[startx][starty] == "V" or boardType[startx][starty] == "L":
  372.         for moveLength in range(1,8):
  373.             if starty+moveLength < 8: #1. Üles
  374.                 nextTileNumber = (startx, (starty+moveLength))
  375.                 nextTileOwner = boardOwner[startx][starty+moveLength]
  376.                 if nextTileOwner != turn:
  377.                     if nextTileOwner == " ":
  378.                         validMoves.append(nextTileNumber)
  379.                     else:
  380.                         validMoves.append(nextTileNumber)
  381.                         break
  382.                 else:
  383.                     break
  384.         for moveLength in range(1,8):
  385.             if startx+moveLength < 8: #2. Paremale
  386.                 nextTileNumber = (startx+moveLength, starty)
  387.                 nextTileOwner = boardOwner[startx+moveLength][starty]
  388.                 if nextTileOwner != turn:
  389.                     if nextTileOwner == " ":
  390.                         validMoves.append(nextTileNumber)
  391.                     else:
  392.                         validMoves.append(nextTileNumber)
  393.                         break
  394.                 else:
  395.                     break
  396.         for moveLength in range(1,8):
  397.             if starty-moveLength > (-1): #3. Alla
  398.                 nextTileNumber = (startx, starty-moveLength)
  399.                 nextTileOwner = boardOwner[startx][starty-moveLength]
  400.                 if nextTileOwner != turn:
  401.                     if nextTileOwner == " ":
  402.                         validMoves.append(nextTileNumber)
  403.                     else:
  404.                         validMoves.append(nextTileNumber)
  405.                         break
  406.                 else:
  407.                     break
  408.         for moveLength in range(1,8):
  409.             if startx-moveLength > (-1): #4. Vasakule
  410.                 nextTileNumber = (startx-moveLength, starty)
  411.                 nextTileOwner = boardOwner[startx-moveLength][starty]
  412.                 if nextTileOwner != turn:
  413.                     if nextTileOwner == " ":
  414.                         validMoves.append(nextTileNumber)
  415.                     else:
  416.                         validMoves.append(nextTileNumber)
  417.                         break
  418.                 else:
  419.                     break
  420.     if boardType[startx][starty] == "O" or boardType[startx][starty] == "L":
  421.         for moveLength in range(1,8): #1. asimuut 45
  422.             if startx+moveLength < 8 and starty+moveLength < 8:
  423.                 nextTileNumber = (startx+moveLength, starty+moveLength)
  424.                 nextTileOwner = boardOwner[startx+moveLength][starty+moveLength]
  425.                 if nextTileOwner != turn:
  426.                     if nextTileOwner == " ":
  427.                         validMoves.append(nextTileNumber)
  428.                     else:
  429.                         validMoves.append(nextTileNumber)
  430.                         break
  431.                 else:
  432.                     break
  433.         for moveLength in range(1,8): #2. asimuut 135
  434.             if startx+moveLength < 8 and starty-moveLength > (-1):
  435.                 nextTileNumber = (startx+moveLength, starty-moveLength)
  436.                 nextTileOwner = boardOwner[startx+moveLength][starty-moveLength]
  437.                 if nextTileOwner != turn:
  438.                     if nextTileOwner == " ":
  439.                         validMoves.append(nextTileNumber)
  440.                     else:
  441.                         validMoves.append(nextTileNumber)
  442.                         break
  443.                 else:
  444.                     break
  445.         for moveLength in range(1,8): #1. asimuut 225
  446.             if startx-moveLength > (-1) and starty-moveLength > (-1):
  447.                 nextTileNumber = (startx-moveLength, starty-moveLength)
  448.                 nextTileOwner = boardOwner[startx-moveLength][starty-moveLength]
  449.                 if nextTileOwner != turn:
  450.                     if nextTileOwner == " ":
  451.                         validMoves.append(nextTileNumber)
  452.                     else:
  453.                         validMoves.append(nextTileNumber)
  454.                         break
  455.                 else:
  456.                     break
  457.         for moveLength in range(1,8): #1. asimuut 315
  458.             if startx+moveLength > (-1) and starty+moveLength < 8:
  459.                 nextTileNumber = (startx-moveLength, starty+moveLength)
  460.                 nextTileOwner = boardOwner[startx-moveLength][starty+moveLength]
  461.                 if nextTileOwner != turn:
  462.                     if nextTileOwner == " ":
  463.                         validMoves.append(nextTileNumber)
  464.                     else:
  465.                         validMoves.append(nextTileNumber)
  466.                         break
  467.                 else:
  468.                     break
  469.                
  470.     if check == True:
  471.         boardTypeTemp = [[" "] * 8 for i in range(8)]
  472.         boardOwnerTemp = [[" "] * 8 for i in range(8)]
  473.         for i in range(8):
  474.             for y in range(8):
  475.                 boardTypeTemp[i][y]=(boardType[i][y])
  476.                 boardOwnerTemp[i][y]=(boardOwner[i][y])
  477.         boardTypeTemp[endPos[0]][endPos[1]] = boardType[startPos[0]][startPos[1]]
  478.         boardOwnerTemp[endPos[0]][endPos[1]] = turn
  479.         boardTypeTemp[startPos[0]][startPos[1]] = " "
  480.         boardOwnerTemp[startPos[0]][startPos[1]] = " "
  481.         if boardType[startx][starty] == "K":
  482.                 if checkCheck((endPos[0],endPos[1]),boardTypeTemp,boardOwnerTemp):
  483.                     print("Tuli! Te ei saa sinna käia.")
  484.                     return False
  485.         elif turn == "V":
  486.             if checkCheck(whiteKingPos,boardTypeTemp,boardOwnerTemp):
  487.                 print("Tuli! Te ei saa sinna käia.")
  488.                 return False
  489.         else:
  490.             if checkCheck(blackKingPos,boardTypeTemp,boardOwnerTemp):
  491.                 print("Tuli! Te ei saa sinna käia.")
  492.                 return False    
  493.     if boardType[startx][starty] == "K": ################ Vangerdus
  494.         if turn == "V":
  495.             if (endx, endy) == (6, 0) and "V" not in kingSideCastle:
  496.                 if boardType[5][0] == " " and not checkCheck((5,0),boardType,boardOwner) and boardType[6][0] == " " and not checkCheck((6,0),boardType,boardOwner):
  497.                     return "whiteKingSide"
  498.             elif (endx, endy) == (2, 0) and "V" not in queenSideCastle:
  499.                 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] == " ":
  500.                     return "whiteQueenSide"
  501.         else:
  502.             if (endx, endy) == (6, 7) and "M" not in kingSideCastle:
  503.                 if boardType[5][7] == " " and not checkCheck((5,7),boardType,boardOwner) and boardType[6][7] == " " and not checkCheck((6,7),boardType,boardOwner):
  504.                     return "blackKingSide"
  505.             elif (endx, endy) == (2, 7) and "M" not in queenSideCastle:
  506.                 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] == " ":
  507.                     return "blackQueenSide"
  508.                        
  509.     if (endx, endy) in validMoves:
  510.         return True
  511.     else:
  512.         print("Te ei saa sinna käia.")
  513.         return False
  514.  
  515. def castle(x): ########## Vangerdamise laua muutmine
  516.     if x == "whiteKingSide":
  517.         boardType[5][0] = "V"
  518.         boardOwner[5][0] = "V"
  519.         boardType[7][0] = " "
  520.         boardOwner[7][0] = " "
  521.         boardType[6][0] = "K"
  522.         boardOwner[6][0] = "V"
  523.         boardType[4][0] = " "
  524.         boardOwner[4][0] = " "
  525.         kingSideCastle.append("V")
  526.         queenSideCastle.append("V")
  527.     elif x == "blackKingSide":
  528.         boardType[5][7] = "V"
  529.         boardOwner[5][7] = "M"
  530.         boardType[7][7] = " "
  531.         boardOwner[7][7] = " "
  532.         boardType[6][7] = "K"
  533.         boardOwner[6][7] = "M"
  534.         boardType[4][7] = " "
  535.         boardOwner[4][7] = " "
  536.         kingSideCastle.append("M")
  537.         queenSideCastle.append("M")
  538.     elif x == "whiteQueenSide":
  539.         print("x")
  540.         boardType[3][0] = "V"
  541.         boardOwner[3][0] = "V"
  542.         boardType[0][0] = " "
  543.         boardOwner[0][0] = " "
  544.         boardType[2][0] = "K"
  545.         boardOwner[2][0] = "V"
  546.         boardType[4][0] = " "
  547.         boardOwner[4][0] = " "
  548.         kingSideCastle.append("V")
  549.         queenSideCastle.append("V")
  550.     elif x == "blackQueenSide":
  551.         boardType[3][7] = "V"
  552.         boardOwner[3][7] = "M"
  553.         boardType[0][7] = " "
  554.         boardOwner[0][7] = " "
  555.         boardType[2][7] = "K"
  556.         boardOwner[2][7] = "M"
  557.         boardType[4][7] = " "
  558.         boardOwner[4][7] = " "
  559.         kingSideCastle.append("M")
  560.         queenSideCastle.append("M")
  561.  
  562. def pawnUpgrade(endPos):
  563.     while True:
  564.         command = input("Milliseks nupuks te tahate oma etturi uuendada [L/O/V/R]? ")
  565.         if len(command) == 1 and command[0].upper() in "L O V R".split():
  566.             boardType[endPos[0]][endPos[1]] = command[0].upper()
  567.             return
  568.  
  569. def changeBoard(turn, startPos, endPos):
  570.     if boardType[endPos[0]][endPos[1]] == "K":
  571.         if turn == "V":
  572.             print("Valge võit.")
  573.         else:
  574.             print("Musta võit.")
  575.         return False        
  576.     boardType[endPos[0]][endPos[1]] = boardType[startPos[0]][startPos[1]]
  577.     boardOwner[endPos[0]][endPos[1]] = turn
  578.     boardType[startPos[0]][startPos[1]] = " "
  579.     boardOwner[startPos[0]][startPos[1]] = " "
  580.     # Vankri liikumine
  581.     if boardType[endPos[0]][endPos[1]] == "V":
  582.         if startPos == (0,0):
  583.             queenSideCastle.append("V")
  584.         elif startPos == (0,7):
  585.             queenSideCastle.append("M")
  586.         elif startPos == (7,0):
  587.             kingSideCastle.append("V")
  588.         elif startPos == (7,7):
  589.             kingSideCastle.append("M")
  590.     if turn == "V": #Etturite ülendamine mängulaua lõppu jõudes
  591.         if boardType[endPos[0]][endPos[1]] == "E" and endPos[1] == 7:
  592.             pawnUpgrade(endPos)
  593.     else:
  594.         if boardType[endPos[0]][endPos[1]] == "E" and endPos[1] == 0:
  595.             pawnUpgrade(endPos)
  596.            
  597.     if boardType[endPos[0]][endPos[1]] == "K": #Kuninga liikumine
  598.         if turn == "V":
  599.             kingSideCastle.append("V")
  600.             queenSideCastle.append("V")
  601.             whiteKingPos = (endPos[0],endPos[1])
  602.         else:
  603.             kingSideCastle.append("M")
  604.             queenSideCastle.append("M")
  605.             blackKingPos = (endPos[0],endPos[1])
  606.     return True
  607.  
  608. def moveStage(turn):
  609.     while True:
  610.         startPos, endPos, surrender = getMove(turn)
  611.         if surrender == 1:# alla andmine
  612.             if turn == "V":
  613.                 print("Valge andis alla.")
  614.             else:
  615.                 print("Must andis alla.")
  616.             return False
  617.         temp = checkValidity (turn, startPos, endPos)
  618.         if temp != True and temp != False:
  619.             castle(temp)
  620.             return True
  621.         elif temp:
  622.             if changeBoard(turn, startPos, endPos): #returni false kui kuningas võeti ära
  623.                 return True
  624.             else:
  625.                 return False
  626.  
  627. def playAgain():
  628.     while True:
  629.         command = input("Kas soovite uuesti mängida? (jah/ei): ") #Jah
  630.         if len(command)==0:
  631.             nope = ".avi"
  632.         elif command[0].lower() == "j":
  633.             return True
  634.         elif command[0].lower() == "e":
  635.             return False
  636.  
  637. def showTurn(turn, pointsWhite, pointsBlack):
  638.     if turn == "V":
  639.         print("Valge käik. Punkte: %s" %(pointsWhite))
  640.     else:
  641.         print("Musta käik. Punkte: %s" %(pointsBlack))
  642.     print("")
  643.  
  644. def displayTutorial(): #TutorialTexti pikkus ei tohi jaguda selle jagatava arvuga (praegu 70)
  645.     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."
  646.     print("-------------------------------------------------------------------------")
  647.     for x in range(len(textwrap.wrap(tutorialText))):
  648.         print(textwrap.wrap(tutorialText)[x])
  649.     print("-------------------------------------------------------------------------")
  650.    
  651. def enterToStart():
  652.     input("Vajutage enter, et mäng algaks.")
  653.  
  654. def displayStart():
  655.     print("SmartChess(c) 2014 Beta 1.5.1")
  656.     print("Copyright: J.Kirme and A.Saidlo")
  657.     print("Kui mängite esimest korda, on soovitatud juhendit lugeda.")
  658.  
  659. def displayOptions(printMode, purchasing, placing, queens, check):
  660.     print("{0:30}".format("Laua kuvamise suurus:") + printMode)
  661.     if check:
  662.         print ("{0:30}".format("Tuli:") + "Sees")
  663.     else:
  664.         print ("{0:30}".format("Tuli:") + "Väljas")
  665.     if purchasing:
  666.         print ("{0:30}".format("Uute nuppude ostmine:") + "Sees")
  667.     else:
  668.         print ("{0:30}".format("Uute nuppude ostmine:") + "Väljas")
  669.     if placing == "M":
  670.         print ("{0:30}".format("Nuppude asetamine:") + "Mõlemad")
  671.     elif placing == "T":
  672.         print ("{0:30}".format("Nuppude asetamine:") + "Tagumine rida")
  673.     elif placing == "P":
  674.         print ("{0:30}".format("Nuppude asetamine:") + "Promotion")    
  675.     if queens:
  676.         print ("{0:30}".format("Lippude ostmine:") + "Sees")
  677.     else:
  678.         print ("{0:30}".format("Lippude ostmine:") + "Väljas")        
  679.  
  680. def changeOptions(printMode, purchasing, placing, queens, check):
  681.     while True:
  682.         print("-------------------------------------------------------------------------")
  683.         displayOptions(printMode, purchasing, placing, queens, check)
  684.         print("-------------------------------------------------------------------------")
  685.         print("Millist valikut soovite muuta? ")
  686.         command = input("[S]uurus/T[u]li/[O]stmine/[A]setamine/[L]ipud/[T]agasi ")
  687.         if len(command)==0: #Miks ma neid ei kirjutanud if len!=0? -J
  688.             nope = ".avi"
  689.         elif command[0].upper() == "S":
  690.             printMode = getPrintMode()
  691.         elif command[0].upper() == "O":
  692.             if purchasing == True:
  693.                 purchasing = False
  694.             else:
  695.                 purchasing = True
  696.         elif command[0].upper() == "A":
  697.             while True:
  698.                 placing = input("Milline peaks olema uute nuppude asetamine? ([T]agumine rida/[P]romotion/[M]õlemad) ")
  699.                 if len(placing) != 0:
  700.                     placing = placing[0].upper()
  701.                     if placing in ("T","P","M"):
  702.                         break
  703.                 else:
  704.                     print("Palun jälgige õigeid sisestusreegleid. ")
  705.         elif command[0].upper() == "L":
  706.             if queens == True:
  707.                 queens = False
  708.             else:
  709.                 queens = True
  710.         elif command[0].upper() == "U":
  711.             if check == True:
  712.                 check = False
  713.             else:
  714.                 check = True
  715.         elif command[0].upper() == "T":
  716.             break
  717.     return printMode, purchasing, placing, queens
  718.  
  719. def startMenu(printMode, purchasing, placing, queens, check):
  720.     while True:
  721.         command = input("Kas soovite [A]lustada, [J]uhendit näha või [V]alikuid muuta? ")
  722.         if len(command)==0:
  723.             nope = ".avi"
  724.         elif command[0].upper() == "A":
  725.             return printMode, purchasing, placing, queens
  726.         elif command[0].upper() == "J":
  727.             displayTutorial()
  728.         elif command[0].upper() == "V":
  729.             printMode, purchasing, placing, queens = changeOptions(printMode, purchasing, placing, queens, check)
  730.  
  731. def shop(turn, pointsWhite, pointsBlack, pawnValue, placing):
  732.     R = 0    
  733.     validPlaces = []
  734.     printBoard(printMode)
  735.     if turn == "V":
  736.         points = pointsWhite
  737.     else:
  738.         points = pointsBlack
  739.     while True:
  740.         pood = (input("Kas soovite uue nupu osta?[J/E] "))
  741.         if len(pood) != 0:
  742.             pood = pood[0].upper()
  743.             if pood in ("E","J"):
  744.                 break
  745.         else:
  746.             print("Palun jälgige õigeid sisestusreegleid. ")
  747.     if pood == "J":
  748.         print("-------------------------------------------------------------------------")
  749.         print("{0:10}".format("Pood") + "Punktid:",points,"\n")
  750.         if queens:
  751.             print("{0:10}".format("Lipp:"), 9*pawnValue)
  752.         print("{0:10}".format("Vanker:"), 5*pawnValue)
  753.         print("{0:10}".format("Oda/Ratsu:"), 3*pawnValue)
  754.         print("-------------------------------------------------------------------------")
  755.         while True:
  756.             while True:
  757.                 if turn == "V": ############# Siia lisasin
  758.                     points = pointsWhite
  759.                 else:
  760.                     points = pointsBlack
  761.                 if queens:
  762.                     newPiece = input("Millist nuppu soovite osta? ([L]ipp, [O]da, [R]atsu, [V]anker, [T]ühista) ")
  763.                     if len(newPiece) != 0:
  764.                         newPiece = newPiece[0].upper()
  765.                     if newPiece in ("L","O","R","V"):
  766.                         if newPiece == "L" and points >= 9*pawnValue:
  767.                             points -= 9*pawnValue
  768.                             break
  769.                         if newPiece == "V" and points >= 5*pawnValue:
  770.                             points -= 5*pawnValue
  771.                             break
  772.                         if newPiece == "O" and points >= 3*pawnValue:
  773.                             points -= 3*pawnValue
  774.                             break
  775.                         if newPiece == "R" and points >= 3*pawnValue:
  776.                             points -= 3*pawnValue
  777.                             break                    
  778.                 else:
  779.                     newPiece = input("Millist nuppu soovite osta? ([O]da, [R]atsu, [V]vanker, [T]ühista) ")
  780.                     if len(newPiece) != 0:
  781.                         newPiece = newPiece[0].upper()
  782.                     if newPiece in ("O","R","V"):                        
  783.                         if newPiece == "V" and points >= 5*pawnValue:
  784.                             points -= 5*pawnValue
  785.                             break
  786.                         if newPiece == "O" and points >= 3*pawnValue:
  787.                             points -= 3*pawnValue
  788.                             break
  789.                         if newPiece == "R" and points >= 3*pawnValue:
  790.                             points -= 3*pawnValue
  791.                             break
  792.                     elif newPiece == "L":
  793.                         print("Lipud on keelatud.")
  794.                
  795.                 if newPiece == "T":
  796.                     break
  797.                 else:
  798.                     print("Palun sisestage korrektne nuputähis.")
  799.             if newPiece == "T":
  800.                 break
  801.  
  802.             if turn == "V":
  803.                 backup = pointsWhite
  804.                 pointsWhite = points
  805.             else:
  806.                 backup = pointsBlack
  807.                 pointsBlack = points
  808.  
  809.            
  810.             if placing == "M" or placing == "T":
  811.                 if turn == "V":
  812.                     for i in range(8):
  813.                         if boardType[i][0] == " ":
  814.                             validPlaces.append((i,0))
  815.                 else:
  816.                     for i in range(8):
  817.                         if boardType[i][7] == " ":
  818.                             validPlaces.append((i,7))
  819.             if placing == "M" or placing == "P":
  820.                 for i in range(8):
  821.                     for j in range(8):
  822.                         if boardOwner[i][j] == turn and boardType[i][j] != "K":
  823.                             validPlaces.append((i,j))
  824.             while True:
  825.                 place = str(input("Kuhu soovite oma nupu asetada?([T]ühista kui muutsite meelt) "))
  826.                 if len(place) != 0:
  827.                    
  828.                     if len(place) == 2 and place[0].upper() in LETTERSATOH and place[1] in DIGITS1TO8:
  829.                         place = str(place[0].upper()) + str(place[1])
  830.                         place = posConvert(place)
  831.                         if place in validPlaces:
  832.                             boardType[place[0]][place[1]] = newPiece
  833.                             boardOwner[place[0]][place[1]] = turn
  834.                             R = 1
  835.                             break
  836.                         if placing == "T" and boardOwner[place[0]][place[1]] == turn:
  837.                             print("Promotion pole lubatud.")
  838.                         elif placing == "P" and boardOwner[place[0]][place[1]] == " " and place[1] == 0:
  839.                             print("Lubatud on ainult promotion.")
  840.                         elif boardType[place[0]][place[1]] == "K":
  841.                             print("Sa ei saa kuningat promotida :)")
  842.                    
  843.                     elif place[0].upper() == "T":
  844.                         if turn == "V":
  845.                             pointsWhite = backup
  846.                         else:
  847.                             pointsBlack = backup
  848.                         break
  849.                 else:
  850.                     print("Pole korrektne sisend.")
  851.             if R == 1 or place == "T":
  852.                 break        
  853.     return boardType, boardOwner,pointsBlack,pointsWhite    
  854.  
  855. def getNewOperationMath():
  856.     x = random.randint(100,999)#Liitmine
  857.     y = random.randint(100,999)
  858.     answerRight = str(x+y)
  859.     operationDisplay = str(x) + "+" + str(y)
  860.     pointsWorth = 1
  861.     return answerRight, operationDisplay, pointsWorth
  862.  
  863. def giveOperationMath(turn, pointsWhite, pointsBlack):
  864.     answerRight, operationDisplay, pointsWorth = getNewOperationMath()
  865.     if turn == "V":
  866.         print("Punkte: %s" %pointsWhite)
  867.     else:
  868.         print("Punkte: %s" %pointsBlack)
  869.     answerPlayer = input("%s = " %(operationDisplay))
  870.     return answerRight, answerPlayer, pointsWorth
  871.  
  872. def mathStage(turn, timeLast, pointsWhite, pointsBlack): #Veel whitespace'i
  873.     print("")
  874.     print("Teil on %s sekundit aega ülesannete lahendamiseks." % (round(timeLast, 1)))
  875.     print("")
  876.     time.sleep(3)
  877.     timeStart = timerStart()
  878.     while True:
  879.         if time.time() - timeStart > timeLast:
  880.             break
  881.         else:
  882.             answerRight, answerPlayer, pointsWorth = giveOperationMath(turn, pointsWhite, pointsBlack)
  883.             print("")
  884.         if turn == "V":
  885.             if answerRight == answerPlayer:
  886.                 pointsWhite += pointsWorth
  887.             else:
  888.                 pointsWhite -= pointsWorth
  889.         else:
  890.             if answerRight == answerPlayer:
  891.                 pointsBlack += pointsWorth
  892.             else:
  893.                 pointsBlack -= pointsWorth
  894.     timeOverLimit = time.time()-timeStart-timeLast
  895.     print("Vastasele antakse aega %s sekundit ajalimiidi ületamise eest." %(round(timeOverLimit, 1)))
  896.     return timeOverLimit, pointsWhite, pointsBlack
  897.    
  898.  
  899. def questionStage(turn, timeLast, questionLast, questionsBanned, pointsWhite, pointsBlack): #Lisa siit alates
  900.     timeOverLimit = 0
  901.     if timeLast == 0 and turn == "V":
  902.         print("")
  903.         print("Esimesel käigul ei saa ülesandeid lahendada.")
  904.         print("")
  905.     elif questionLast == 0: #Must saab juba küsimuse 1. käigul
  906.         while questionLast in questionsBanned:
  907.             questionLast = random.randint(1,100)
  908.     elif turn == "V": #Valge käigul tuleb uus voor
  909.         while questionLast in questionsBanned:
  910.             questionLast = random.randint(1,100)
  911.     if questionLast > 0 and questionLast <= 100: #Pranglimine, vaheta tagasi 1-90 (kui EK töötab) #############################
  912.         #print("Pranglimise voor.") ################################### Vaheta tagasi kui teisi voore tuleb
  913.         timeOverLimit, pointsWhite, pointsBlack = mathStage(turn, timeLast, pointsWhite, pointsBlack)
  914.     elif questionLast > 100: #Eesti keel. Vaheta tagasi 91-100 (kui see töötab) ##########################
  915.         print("Eesti keele voor.")
  916.     if turn == "V" and questionLast != 0:
  917.         print("Punkte: %s" %(pointsWhite))
  918.     elif questionLast != 0:
  919.         print("Punkte: %s" %(pointsBlack))
  920.     print("Käigu vahetus mõne hetke pärast.")
  921.     time.sleep(5)
  922.     return timeOverLimit, pointsWhite, pointsBlack
  923.  
  924. def timerStart():
  925.     timeStart = time.time()
  926.     return timeStart
  927.  
  928. def timerEnd(timeStart):
  929.     timeCurrent = time.time()-timeStart
  930.     return timeCurrent
  931.    
  932. def showTime(timeCurrent, timeLast):
  933.     print("")
  934.     print("Teil kulus %s sekundit." %(round(timeCurrent, 1)))
  935.     #if timeLast == 0: ######### Tundub, et see on mõttetu rida
  936.     print("Kohe algavad küsimused.")
  937.     time.sleep(2)
  938.  
  939.  
  940.  
  941.  
  942. displayStart()
  943. printMode = "K" #Keskmine suurus
  944. purchasing = True #Nuppude ostmine on sees
  945. queens = True #Lippe saab osta
  946. placing = "M"
  947. check = True
  948. questionsBanned = [0]
  949. pawnValue = 1
  950.  
  951.  
  952. while True:
  953.     turn = "M"
  954.     kingSideCastle = [] #Vangerduse kontroll
  955.     queenSideCastle = []
  956.     printMode, purchasing, placing, queens = startMenu(printMode, purchasing, placing, queens, check)
  957.     boardType = getNewBoardType()
  958.     boardOwner = getNewBoardOwner()
  959.     enterToStart()
  960.     pointsWhite = 0
  961.     pointsBlack = 0
  962.     timeLast = 0
  963.     questionLast = 0
  964.     blackKingPos = (4,7)
  965.     whiteKingPos = (4,0)
  966.     while True:
  967.         turn = turnChange(turn)
  968.         printBoard(printMode)
  969.         if purchasing:
  970.             timeStart = timerStart()
  971.         showTurn(turn, pointsWhite, pointsBlack)
  972.         if not moveStage(turn): #returni false kui kuningas võeti ära
  973.             break
  974.         if purchasing:
  975.             boardType, BoardOwner, pointsBlack, pointsWhite = shop(turn, pointsWhite, pointsBlack, pawnValue, placing)
  976.             timeCurrent = timerEnd(timeStart)
  977.             showTime(timeCurrent, timeLast) #Oodata mõni sekund enne küsimuste esitamist
  978.             timeOverLimit, pointsWhite, pointsBlack = questionStage(turn, timeLast, questionLast, questionsBanned, pointsWhite, pointsBlack)
  979.             timeLast = timeCurrent + timeOverLimit
  980.     if not playAgain():
  981.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement