Advertisement
Guest User

SmartChess 1.5.3

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