2017-02-25 90 views
1

我正在建立一個腳本來檢查玩家賓果卡上隨機選擇的數字。我認爲我有足夠的邏輯,直到我測試。 我如何檢查無論訂單是否有效?下面的是我的大型程序中的邏輯塊。python中的賓果遊戲邏輯

 chosennumbers = ['B8', 'O69', 'I27', 'G56'] 

    jordonsboard = ['B8', 'I18', 'N38', 'G47', 'O66', 
        'B10', 'I27', 'N44', 'G53', 'O71', 
        'B3', 'I19', 'fre', 'G48', 'O67', 
        'B4', 'I25', 'N39', 'G56', 'O65', 
        'B9', 'I30', 'N34', 'G58', 'O69'] 
    hitnodes = [12] # this list houses hits on your board added 12 because its a free space 
    win = False 
    iterator = 0 
    for i in chosennumbers: 
     if i in jordonsboard: 
      hitnodes.append(iterator) 
     iterator += 1 

if 0 in hitnodes: 
    if 6 in hitnodes: 
     if 12 in hitnodes: 
      if 18 in hitnodes: 
       if 24 in hitnodes: 
        win = True 
        print('Bingo!') 
    elif 2 in hitnodes: 
     if 3 in hitnodes: 
      if 4 in hitnodes: 
       win = True 
       print('Bingo!') 
    elif 5 in hitnodes: 
     if 10 in hitnodes: 
      if 15 in hitnodes: 
       if 20 in hitnodes: 
        win = True 
        print('Bingo!') 
if 1 in hitnodes: 
    if 6 in hitnodes: 
     if 11 in hitnodes: 
      if 16 in hitnodes: 
       if 21 in hitnodes: 
        win = True 
        print('Bingo!') 
if 2 in hitnodes: 
    if 7 in hitnodes: 
     if 12 in hitnodes: 
      if 17 in hitnodes: 
       if 22 in hitnodes: 
        win = True 
        print('Bingo!') 
if 3 in hitnodes: 
    if 8 in hitnodes: 
     if 13 in hitnodes: 
      if 18 in hitnodes: 
       if 23 in hitnodes: 
        win = True 
        print('Bingo!') 
if 4 in hitnodes: 
    if 8 in hitnodes: 
     if 16 in hitnodes: 
      if 20 in hitnodes: 
       win = True 
       print('Bingo!') 
    elif 9 in hitnodes: 
     if 14 in hitnodes: 
      if 19 in hitnodes: 
       if 24 in hitnodes: 
        win = True 
        print('Bingo!') 

回答

1

您需要set.intersection()。您可以將所有數字放入一個集合中,並檢查它與列表的交集是否與集合相同。

my_set = {0, 6, 12, 18, 24} 

if my_set.intersection(hitnodes) == my_set: 
    # do stuff 
+0

哦哇我不知道十字路口存在。謝謝 – M4dW0r1d

0

這就是我最終這樣做的結果。由於我需要知道每個解決方案中的每個項目是否與我的匹配項匹配,因此我是如何構建它的。

> import urllib.request import urllib.parse import re import collections 
> 
> url = 
> 'http://www.executiveadministrator.com/cgi-local/inoutPROhosted4/inoutPRO.pl?refresh=1&ID=AFTCO' 
> #testlist = ['B9,','B8,','B3,','B4,'] counter = 0 #inicialize counter hitlist = [] #inicialize hitlist 
> 
> board = ['B8,', 'I18,', 'N38,', 'G47,', 'O66,', 
>   'B10,','I27,', 'N44,', 'G53,', 'O71,', 
>   'B3,', 'I19,', 'fre,', 'G48,', 'O67,', 
>   'B4,', 'I25,', 'N39,', 'G56,', 'O65,', 
>   'B9,', 'I30,', 'N34,', 'G58,', 'O69,'] 
> 
> resp = urllib.request.urlopen(url) #request html data from website 
> respdata = resp.read() #store request data in respdata 
> 
> string = '<TEXTAREA ROWS="2" NAME="1 ANNOUNCEMENTS-return" COLS="50" 
> WRAP=VIRTUAL>(.*?)</TEXTAREA>'#search within text area for bingo 
> numbers search = re.findall(string, str(respdata)) #find all between 
> <b> and </b> searchstring = str(search) #convert search results to 
> string and assign cleanlist = searchstring.split() #split seachstring 
> to a cleaned up list cleanlist.append('fre,') #add free space 
> solutions = 
> [(0,6,12,18,24),(4,8,12,16,20),(0,5,10,15,20),(1,6,11,16,21), 
>    (2,7,12,17,22),(3,8,13,18,23),(4,9,14,19,24),(0,1,2,3,4), 
>     (5,6,7,8,9),(10,11,12,13,14),(15,16,17,18,19),(20,21,22,23,24)] 
> 
> for i in board: #for each item in board 
>  for j in cleanlist: #and for each item in cleanlist 
>   if i == j: #check to see if they match append match 
>    hitlist.append(counter)#add hit number to hitlist 
>    print('hit: {} at space: {}'.format(i, counter)) 
>  counter += 1 
> #check each possible solution against current hit list for each in solutions: #for each item in solutions 
>  if collections.Counter(hitlist) == collections.Counter(each): #check to see if all items match 
>   print('BINGO')