Python入門習題----五子棋判勝負以及最長線
【問題描述】
已知兩人分別執白棋和黑棋在一個圍棋棋盤上下五子棋,若同一顏色的棋子在同一條橫行、縱行或斜線上連成5個棋子,則執該顏色棋子的人獲勝。編寫程式讀入某一時刻下棋的狀態,並判斷是否有人獲勝。
輸入的棋盤大小是19×19,用數字0表示空位置(即沒有棋子),用數字1表示該位置下了一白色棋子,用數字2表示該位置下了一黑色棋子。假設同一顏色的棋子在同一條橫行、縱行或斜線上連成的棋子個數不會超過5個,並且最多有一人連成線的棋子個數為5。
【輸入形式】
從控制檯輸入用來表示棋盤狀態的數字0、1或2;每行輸入19個數字,各數字之間以一個空格分隔,每行最後一個數字後沒有空格;共輸入19行表示棋盤狀態的數字。
【輸出形式】
若有人獲勝,則先輸出獲勝人的棋子顏色(1表示白色棋子,2表示黑色棋子),然後輸出英文冒號:,最後輸出連成5個棋子連線的起始位置(棋盤橫行自上往下、縱行自左往右從1開始計數,橫行最小的棋子在棋盤上的橫行數和縱行數作為連線的起始位置,兩數字之間以一個英文逗號,作為分隔符)。
若沒有人獲勝,則輸出當前同一顏色的棋子在同一條橫行、縱行或斜線上連成最長線的棋子個數。
【輸入樣例1】
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 2 0 1 1 2 0 0 0 0 0 0 0
0 0 0 0 0 2 1 1 1 1 2 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 2 1 2 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 2 2 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
【輸出樣例2】
2
【樣例說明】
在輸入的樣例1中,執白棋(數字1表示)的人已經獲勝,連成5個棋子的起始位置在第9行第8列,所以輸出1:9,8。
在輸入的樣例2中,還沒有同一顏色的棋子連成5個,所以無人獲勝;連成最長線的棋子個數為2,所以輸出2。
參考程式碼:
"""
Created on Sat Nov 10 19:07:27 2018
@author: Dina
"""
print_falg,max_count = 0,0
checkerboard=[]
for i in range(19):
checkerboard.append(input().split())
for i in range(19):
for j in range(19):
if checkerboard[i][j] != '0' :
#檢查 每行 是否有連續五個同一顏色的棋子
if i+1<19 and i+2<19 and i+3<19 and i+4<19:
if checkerboard[i][j] == checkerboard[i+1][j] and checkerboard[i][j] == checkerboard[i+2][j] and checkerboard[i][j] == checkerboard[i+3][j] and checkerboard[i][j] == checkerboard[i+4][j]:
print(checkerboard[i][j]+':'+str(i+1)+','+str(j+1))
print_falg = 1
break
else:
count1 = 0
if checkerboard[i][j] == checkerboard[i+1][j] :
count1 +=1
if checkerboard[i][j] == checkerboard[i+2][j] :
count1 +=1
if checkerboard[i][j] == checkerboard[i+3][j] :
count1 +=1
if checkerboard[i][j] == checkerboard[i+4][j] :
count1 +=1
# 若 count1 大於 max_count 就賦值給 max_count
if count1 > max_count:
max_count = count1
#檢查 每列 是否有連續五個同一顏色的棋子
if j+1<19 and j+2<19 and j+3<19 and j+4<19:
if checkerboard[i][j] == checkerboard[i][j+1] and checkerboard[i][j] == checkerboard[i][j+2] and checkerboard[i][j] == checkerboard[i][j+3] and checkerboard[i][j] == checkerboard[i][j+4]:
print(checkerboard[i][j]+':'+str(i+1)+','+str(j+1))
print_falg = 1
break
else:
count2 = 0
if checkerboard[i][j] == checkerboard[i][j+1] :
count2 +=1
if checkerboard[i][j] == checkerboard[i][j+2] :
count2 +=1
if checkerboard[i][j] == checkerboard[i][j+3] :
count2 +=1
if checkerboard[i][j] == checkerboard[i][j+4] :
count2 +=1
# 若 count2 大於 max_count 就賦值給 max_count
if count2 > max_count:
max_count = count2
#檢查 斜線上 是否有連續五個同一顏色的棋子
if i+1<19 and i+2<19 and i+3<19 and i+4<19 and j+1<19 and j+2<19 and j+3<19 and j+4<19:
if checkerboard[i][j] == checkerboard[i+1][j+1] and checkerboard[i][j] == checkerboard[i+2][j+2] and checkerboard[i][j] == checkerboard[i+3][j+3] and checkerboard[i][j] == checkerboard[i+3][j+4]:
print(checkerboard[i][j]+':'+str(i+1)+','+str(j+1))
print_falg = 1
break
else:
count3 = 0
if checkerboard[i][j] == checkerboard[i+1][j+1] :
count3 +=1
if checkerboard[i][j] == checkerboard[i+2][j+2] :
count3 +=1
if checkerboard[i][j] == checkerboard[i+3][j+3] :
count3 +=1
if checkerboard[i][j] == checkerboard[i+4][j+4] :
count3 +=1
# 若 count3 大於 max_count 就賦值給 max_count
if count3 > max_count:
max_count = count3
#若沒有人獲勝,則輸出當前同一顏色的棋子在同一條橫行、縱行或斜線上連成最長線的棋子個數。
if print_falg == 0:
print(max_count+1)
提交可通過: