2016-08-01 25 views
-3

首先,我意識到下面的代碼可能不是很好,所以對任何讓你感到畏懼的道歉,我只是想盡可能多地編碼,希望得到更好。一個新手的hang子手項目:處理重複的字母

這是一個小hang子手遊戲項目的一部分,我試圖找出處理字符串中重複字母的最佳方法。

這就是我現在:

def checkDupes(word): 

    global dupeList 
    global repeatTimesDupes 

    if repeatTimesDupes != 0: 
     dupeCount = 0 
     for i in range(len(word)): 
      temp = word[i] 
      print("temp letter is: ", temp) 

      for j in range(i+1,len(word)): 
       if word[j] == temp: 
        if temp not in dupeList: 
         dupeList.append(word[j]) 
         print("the dupeList contains: ", dupeList)#debug 


    repeatTimesDupes -= 1 

def getLetter(position,buttons,word): 
    i = 96 
    index = 0 
    letter = chr(i) 

    for button in buttons: 
     if button != None: 
      i+=1 
      if button.collidepoint(position): 
       print("the position is: ", position) 
       print(i) 
       for j in range(len(word)): 
        print(word[j] , chr(i)) 
        if word[j] == chr(i): 
         index = j 
         return chr(i), index 

     else: 
      return '?', -1 



def checkForLetter(word,letter): 

    inWord = " " 
    for i in range(len(word)): 
     if word[i] == letter: 
      inWord = True 
      break 
     else: 
      print(len(word)) 
      print (word[i]) 
      inWord = False 

    return inWord 

#========================== Start Loop =========================================== 

while done == False: 
    events = pygame.event.get() 
    screen.fill(BGCOLOR) 
    timedelta = clock.tick_busy_loop(60) 
    timedelta /= 1000 # Convert milliseconds to seconds 
    for event in events: 
     if event.type == pygame.QUIT: 
      done = True 

     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_ESCAPE:     
       done = True 

     if event.type == pygame.MOUSEBUTTONUP: 
      if event.button == MOUSEBUTTONLEFT: 
       pos = pygame.mouse.get_pos() 
       for button in buttonsList: 
        if button.collidepoint(pos): 
         if button != None: 
          checkDupes(gameWord) 
          letter, atIndex = getLetter(pos,buttonsList,gameWord) 
          letterSelected = True 
          moveCounter+=1 


    screen.blit(blackBG,(0,0)) 
    showButtons(letterList) 
    showLetterSlots(gameWord,screenRect) 
    setCounters(moveMade,mistakeMade) 

    if letterSelected: 
     inGameWord = checkForLetter(gameWord, letter) 
     if inGameWord: 
      print(atIndex) 
      print(letter) 
      letterRen = wordFonts.render(letter,1,(0,255,0)) 
      renderList[atIndex] = letterRen 
      print("The render list is: ", renderList) 

      renCount = 0 
      for r in lineRectList: 
       if renderList[renCount] != '?' : 
        screen.blit(renderList[renCount],((r.centerx-10),430)) 
       if renCount <= len(gameWord): 
        renCount+=1 



#update game screen 
    clock.tick(60) 
    pygame.display.update() 



#========================== End Loop ============================================= 

pygame.quit() 

我正在尋找快速的方法來處理重複,使他們與他們的比賽以及位圖混合。我已經放慢了所有循環的字母組合,所以我不確定我目前的getDupes是否可行。

如果有人願意看這個,並提供一些意見,我非常感激。

謝謝你的時間。

+5

請將您的代碼發佈範圍縮小到與您所問的問題相關的內容。 –

+0

完成並完成,雖然所有downvotes雖然?這個地方在精英主義中是如此根深蒂固,以至於人們看不起那些試圖改進的新手? 對不起,我不想成爲一個屁股,但我對我發佈的每個問題的否定回答有點令人沮喪。我的意思是,我應該尋求其他方面的幫助嗎? – Wretch11

+1

@ Wretch11歡迎來到堆棧溢出!降伏投票並不是對你的個人反思。你在這裏非常受歡迎。 Downvotes是表示該帖子需要改進的信號,而不是完全拒絕您提交的內容。現在,讓我們看看我們是否能夠弄清楚這裏發生了什麼。這裏有什麼期望的行爲?如果有重複的字符串,應該發生什麼? – Ares

回答

1

根據您在評論中描述的內容,在這種情況下使用dictionary對象似乎是合理的。你不只是想存儲這封信,你要存儲這裏的那些字母出現在

字典有一個鍵和一個值。例如:

{'jack': 4095, 'jill': 12} 關鍵是jack和值是4095

在這種情況下,我們不會使用int作爲值。我們實際上會使用一個int數組。

所以,你的字典可能是這樣的:

{'o':[1, 5, 6, 3, 2, 1]}其中那些數字是指數,這封信是在遇到。這將適用於任意數量的重複字母。然後,在你的按鈕中,你知道哪些「blit」,因爲它們與字符串的順序相同。

Python字典文件: https://docs.python.org/3/tutorial/datastructures.html

FWIW,一些重構會在這裏做你的服務也是如此。

+0

我現在就試試看,看着字典列表的理解,我可以做得更快。謝謝你,當我嘗試這個時,我會再檢查一次。 – Wretch11