हाल ही में मैं एक हेक्स बोर्ड गेम https://en.wikipedia.org/ पर काम कर रहा हूं। wiki/Hex_(board_game) प्रोजेक्ट जो मुझे पायथन में मिला। हालाँकि, मेरे जंग खाए हुए कोडिंग कौशल के कारण मुझे एक ऐसे फ़ंक्शन का पता लगाने में वास्तविक परेशानी हो रही है जो बोर्ड सामग्री (जो एक एन * एन सरणी के अंदर संग्रहीत हैं) में लेता है और उन्हें एक उचित प्रारूप में प्रिंट करता है। आउटपुट इस तरह दिखना चाहिए:
आकार 4 से 21 तक भिन्न होता है, लेकिन मैं इसे ठीक से संभाल सकता हूं। हालांकि, यह देखते हुए कि अंततः हेक्सागोन्स के अंदर बोर्ड सामग्री होनी चाहिए (एक सफेद टुकड़े के लिए डब्ल्यू और काले रंग के लिए बी), मुझे बोर्ड सामग्री को गतिशील रूप से प्रिंट करने का एक तरीका चाहिए। इसके लिए, मैंने खेल की शुरुआत में एक सूची (बोर्ड_प्रिंट) का उपयोग किया, जिसमें प्रत्येक पंक्ति के लिए एक स्ट्रिंग होती है और गतिशील रूप से खेले गए चाल (ए 3, बी 2 इत्यादि) के आधार पर इसकी सामग्री को बदल देती है:
# Function to change and print board after a move is played
def play(move, whose_turn):
global turn
color_string = 'W' if whose_turn == 0 else 'B'
column = " ".join(re.findall("[a-zA-Z]+", move))
row = int(re.findall(r'\d+', move)[0])
# Check if there is a piece already placed there and move is in bounds
if 0 <= row <= board_size and 0 <= col_index[column] <= board_size:
if board[row - 1][col_index[column]] != 0:
print('Error! There is a piece already placed there')
return False
else:
board[row - 1][col_index[column]] = 1 if color_string == 'W' else 2
moves.append((row, col_index[column]))
# Modify board_print contents
# 4-3 4-7 4-11 4-15...
# 6-5 6-9 6-13 ...
# A is 0 etc, board index to print index mapping is 1->4 2->6 3->8..,,
for index, row_string in enumerate(board_print):
if index == 2 * row + 2:
# Handle A differently because index starts from 0
if column == 'A':
new_string = row_string[:index] + color_string + row_string[index + 1:]
else:
# Because col_index starts from 0 , add 1 . then multiply the result by col index to match the
# print format
new_string = row_string[
:index + (col_index[column] + 1) * col_index[
column] + 2] + color_string + row_string[
index + (
col_index[
column] + 1) *
col_index[
column] + 3:]
board_print[index] = new_string
# Print board
for row in board_print:
print(row)
print('Move played: ', move)
moves_print.append(board_print)
return True
else:
print('Error!Move is out of bounds')
return False # Don't change turn if move was illegal
जो ठीक काम किया:
लेकिन तब मुझे एहसास हुआ कि मुझे एक पूर्ववत () और लोड () फ़ंक्शन को लागू करने की आवश्यकता है (चाल और चाल_प्रिंट सूचियों में उनके संबंधित बोर्ड_प्रिंट स्ट्रिंग्स के साथ किए गए सभी कदम शामिल हैं), जिसका अर्थ है कि यह मूर्खतापूर्ण दृष्टिकोण अब काम नहीं कर सकता है। मुझे कंसोल पर मुद्रित हेक्स ग्रिड में बोर्ड सामग्री को ठीक से मैप करने का एक तरीका चाहिए। संदर्भ के लिए, बोर्ड को सूचियों की लंबाई N सूची के रूप में दर्शाया गया है, जिसमें प्रत्येक उपन्यास एक पंक्ति (बोर्ड [i] [j] = 0 (कोई टुकड़ा नहीं) या 1 (सफेद) या 2 (काला)) का प्रतिनिधित्व करता है। वर्तमान में मैं आउटपुट और बोर्ड_प्रिंट सूची को इस तरह प्रारंभ करता हूं:
column_names = ' A B C D E F G H I J K L M N O P Q R S T'
def start_board(n, load_game):
# Append each row to the board print array
rows_index = 0 # to index rows
number_index = 0 # For printing out the row numbers in the board
if not load_game: print(' ' * (n + 3) + 'W H I T E')
board_print.append(' ' * (n + 3) + 'W H I T E')
# Format the top rows properly with spaces
if not load_game: print(column_names[:(4 + (3 * n + 4))])
board_print.append(column_names[:(4 + (3 * n + 4))])
if not load_game: print(' ' + '- ' * n)
board_print.append(' ' + '- ' * n)
if not load_game: print(' ' + '/ \\_' * (n - 1) + '/ \\')
board_print.append(' ' + '/ \\_' * (n - 1) + '/ \\')
# Loop enough times to print entire board. That is, 2 * n times
for i in range(2 * n):
if i == 0 or i % 2 == 0:
# Even pattern
if not load_game: print(
' ' * number_index + (str(rows_index + 1) + ' ' + '| ' * n + '| ' + str(rows_index + 1)))
board_print.append(
' ' * number_index + (str(rows_index + 1) + ' ' + '| ' * n + '| ' + str(rows_index + 1)))
rows_index += 1
else:
# Odd pattern
# Check if it's final row for proper formatting
if i == (2 * n) - 1:
if not load_game: print(' ' * (number_index + 2) + '\\_/ ' * n)
board_print.append(
' ' * (number_index + 2) + '\\_/ ' * n)
else:
if not load_game: print(' ' * (number_index + 2) + '\\_/ ' * n + '\\')
board_print.append(
' ' * (number_index + 2) + '\\_/ ' * n + '\\')
number_index += 1
# Print final row (column names and BLACK)
if not load_game: print(' ' * 2 * (n - 1) + column_names[:(4 + (3 * n + 4))])
board_print.append(
' ' * 2 * (n - 1) + column_names[:(4 + (3 * n + 4))])
moves_print.append(board_print)
खराब स्वरूपण/इंडेंटेशन के लिए क्षमा करें, यह एक मोटा मसौदा है और मुझे PyCharm से कोड कॉपी करने में समस्या हो रही है।
1 उत्तर
एक मजेदार छोटे व्यायाम की तरह लगता है। यहाँ मैं क्या लेकर आया हूँ
column_names = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def print_board(board):
rows = len(board)
cols = len(board[0])
indent = 0
headings = " "*5+(" "*3).join(column_names[:cols])
print(headings)
tops = " "*5+(" "*3).join("-"*cols)
print(tops)
roof = " "*4+"/ \\"+"_/ \\"*(cols-1)
print(roof)
color_mapping = lambda i : " WB"[i]
for r in range(rows):
row_mid = " "*indent
row_mid += " {} | ".format(r+1)
row_mid += " | ".join(map(color_mapping,board[r]))
row_mid += " | {} ".format(r+1)
print(row_mid)
row_bottom = " "*indent
row_bottom += " "*3+" \\_/"*cols
if r<rows-1:
row_bottom += " \\"
print(row_bottom)
indent += 2
headings = " "*(indent-2)+headings
print(headings)
यह सिर्फ आपके विनिर्देशों के अनुसार बोर्ड को प्रिंट करने के लिए है (खाली कोशिकाओं के लिए 0, सफेद के लिए 1, काले रंग के लिए 2)।
board=[[0,0,0,0],[0,0,0,1],[0,0,0,2],[1,2,0,0],[0,2,1,0]]
print_board(board)
निम्न आउटपुट देता है
A B C D
- - - -
/ \_/ \_/ \_/ \
1 | | | | | 1
\_/ \_/ \_/ \_/ \
2 | | | | W | 2
\_/ \_/ \_/ \_/ \
3 | | | | B | 3
\_/ \_/ \_/ \_/ \
4 | W | B | | | 4
\_/ \_/ \_/ \_/ \
5 | | B | W | | 5
\_/ \_/ \_/ \_/
A B C D
मुझे बताएं कि क्या कुछ अस्पष्ट है
संबंधित सवाल
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।