iT邦幫忙

0

求救 賓果

python 寫賓果
要如何檢查是否有連線
我下面有我大概的想法
但想請問有沒有另種方式
import random
qq=[]
for i in range (1,26):
qq.append(i)
random.shuffle(qq)
def print_broad():
for j in range(25):
print(qq[j],end="\t")
if (j%5==4):
print("\n")
print_broad()
b=0
a=5
while(b<=4):
ans=input("輸入1~25一個數字:\n")
if(int(ans)>=26 or int(ans)<=0):
print("輸入錯誤,請輸入數字\n")
continue
else:
qq[qq.index(int(ans))] = "X"
print_broad()

    for j in range(5):
        
        if(qq[j]=="X"):
            b=b+1
        else:
            b=0
        
   

print("+++")

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-11-18 12:36:32
最佳解答

原本的寫法只會判斷「第一列」
底下補成會判斷「五列」
剩下的你自己補

import random
qq=[]
for i in range (1,26):
	qq.append(i)
	random.shuffle(qq)
def print_broad():
	for j in range(25):
		print(qq[j],end="\t")
		if (j%5==4):
			print("\n")

print_broad()
b=0
a=5
while(b<=4):
	ans=input("Input 1~25:\n")
	if(int(ans)>=26 or int(ans)<=0):
		print("Wrong, number only\n")
		continue
	else:
		qq[qq.index(int(ans))] = "X"
		if(b<=4):
			b=0
			for j in range(5):        
				if(qq[j]=="X"):
					b=b+1
		if(b<=4):
			b=0
			for j in range(5,10):        
				if(qq[j]=="X"):
					b=b+1
		if(b<=4):
			b=0
			for j in range(10,15):        
				if(qq[j]=="X"):
					b=b+1
		if(b<=4):
			b=0
			for j in range(15,20):        
				if(qq[j]=="X"):
					b=b+1
		if(b<=4):
			b=0
			for j in range(20,25):        
				if(qq[j]=="X"):
					b=b+1
	print_broad()
print("+++")
0
japhenchen
iT邦大師 1 級 ‧ 2020-11-18 13:53:18

文長,請慢用

from random import shuffle
import os
def clear(): return os.system('cls' if os.name == 'nt' else 'clear')  # 清畫面用的函數

def checkbingoLines(xl):
    #檢查目前陣列符合賓果的條件組數並傳回
    result = 0 
    if isinstance(xl,list) and len(xl)==25:
        #縱向
        for x in [0,1,2,3,4]:
            curr = 0
            for y in range(0,5):
                if xl[x+y*5] < 0 :
                    curr += 1
            if curr==5 :
                result +=1 
        #橫向
        for x in [0,5,10,15,20]:
            curr = 0
            for y in range(0,5):
                if xl[x+y] < 0 :
                    curr += 1
            if curr==5 :
                result +=1 
        #斜向2條
        curr = 0
        for xcross in [0,6,12,18,24]:
            if xl[xcross]<0:
                curr +=1 
        if curr == 5 :
            result +=1 
        curr = 0
        for xcross in [4,8,12,16,20]:
            if xl[xcross]<0:
                curr +=1 
        if curr == 5 :
            result +=1 
    return result

Alist = list() #產生一個list(陣列)
for i in range(1, 26):
    Alist.append(i)
shuffle(Alist) #隨機陣列

bglines = 0
err = False
while bglines < 5:
    clear()
    sNumber = input("請輸入號碼(1~25)")
    try:
        iNumber = int(sNumber)
        if iNumber>=1 and iNumber<=25:
            found = False            
            for idx,numb in enumerate(Alist): 
                #找到對應的數字並變成負數
                if numb==iNumber :
                    Alist[idx]*=-1
                    found=True #如果有找到
            if found==False:#如果沒有找到,則警告
                print("{0}你已填寫過,請重新輸入".format(sNumber))
                input("")
            else:
                #用checkbingoLines函數(寫在上面)檢查有幾條組合
                bglines = checkbingoLines(Alist) 
        else :
            print("錯誤,請輸入1~25之間的數字")
            input("")

        # 以下測試用,印出目前所有數字的狀況,負號為填寫過的
        for i in range(0,5):
            print("\t".join("{0}".format(n).zfill(3) for n in Alist[i*5:i*5+5]))
        print("目前已有{0}組連線".format(bglines))
        #以上測試用,正式玩要註解掉

        input()
    except:
        print("請輸入正確數字")
        input("")

print("賓果!!!你贏了")

本例子設定5組連線就BINGO

lusaka216 iT邦新手 5 級 ‧ 2020-11-19 17:09:33 檢舉

感謝您的分享

0
ccutmis
iT邦高手 4 級 ‧ 2020-11-18 16:31:05

分享一下土法煉鋼的寫法,可以自訂棋盤格的格數。
判斷連線的函式是 count_connect()

import random,os
from math import sqrt

def fix_0(arg,padding=' '):
    if arg!='X':
        return padding+str(arg).strip().rjust(2," ")+padding
    else:
        return padding+arg+padding

def arr_to_2d_arr(tmp_arr):
    col_num=int(sqrt(len(tmp_arr)))
    output_arr=[]
    sub_arr=[]
    for i in range(0,len(tmp_arr)):
        if i!=1 and (i+1)%col_num ==0:
            sub_arr.append(tmp_arr[i])
            output_arr.append(sub_arr)
            sub_arr=[]
        else:
            sub_arr.append(tmp_arr[i])
    return output_arr

def draw_board(tmp_arr):
    len_num=len(tmp_arr)
    for i in range(0,len_num):
        for j in range(0,len_num):
            if j!=len_num-1:
                print(fix_0(tmp_arr[i][j]),end='')
            else:
                print(fix_0(tmp_arr[i][j]),end='\n')

def pos_in_2d_arr(tmp_arr,seek_val):
    len_num=len(tmp_arr)
    for i in range(0,len_num):
        for j in range(0,len_num):
            if str(tmp_arr[i][j])==seek_val.strip():
                return i,j
    return False

def count_connect(tmp_arr):
    count=0
    len_num=len(tmp_arr)
    for i in range(0,len_num): #水平
        is_conn=1
        for j in range(0,len_num):
            if tmp_arr[i][j]!='X': is_conn=0
        if is_conn==1: count+=1
    for i in range(0,len_num): #垂直
        is_conn=1
        for j in range(0,len_num):
            if tmp_arr[j][i]!='X': is_conn=0
        if is_conn==1: count+=1
    is_conn=1
    for i in range(0,len_num): # ↘倒斜線
        if tmp_arr[i][i]!='X': is_conn=0
    if is_conn==1: count+=1
    is_conn=1
    for i in range(0,len_num): # ↗正斜線
        if tmp_arr[i][len_num-1-i]!='X': is_conn=0
    if is_conn==1: count+=1
    return count

if __name__=='__main__':
    row_num=5 #棋盤寬度 可設3以上之非偶數正整數,例:3,5,7,9...
    win_num=3 #結束遊戲需要幾條線
    qq=[i for i in range(1,(row_num**2)+1)]
    random.shuffle(qq)
    aa=arr_to_2d_arr(qq)
    while True:
        os.system('cls')
        connected=count_connect(aa)
        print('Connected: ',connected)
        print("-------"*win_num)
        draw_board(aa)
        print("-------"*win_num)
        if connected<win_num:
            choice=input("input num:(1-"+str(row_num**2)+")")
            if pos_in_2d_arr(aa,choice)!=False:
                x,y=pos_in_2d_arr(aa,choice)
                aa[x][y]='X'
        else:
            print("+"*win_num)
            exit()

我要發表回答

立即登入回答