رمز السطر الجديد (\n) وتنسيق اللوحة
نريد الآن حل مشكلة عرض لوحة Tic-Tac-Toe. هذه إحدى الطرق الممكنة لطباعة اللوحة:
def print_board(board):
for row in board:
print(''.join(row))
print_board([['X', 'O', 'X'], [' ', 'O', 'O'], [' ', 'X', ' ']])
إذا لم تكن تعرف ما تفعله "".join، يمكنك البحث عنها وفهم المثال بالتجربة.
هذه بداية جيدة، لكن الأفضل أن تكون لدينا دالة تعيد نصًا بدل أن تطبعه مباشرة. عندها يستطيع أي كود آخر استخدام النص بطرق مختلفة: يمكن تعديله، أو وضع إطار حوله، أو أخذ بضعة أسطر منه، أو حفظه في ملف بدل الشاشة. وفي حالتنا نريد أيضًا اختباره باستخدام check_result.
هذا لا يعمل كما نريد:
check_result(print_board([...]), "...")
لأن print_board لا تستخدم return، ولذلك تعيد None تلقائيًا.
نريد بدل ذلك شيئًا من الشكل:
def format_board(board): ... return ...
check_result(format_board([...]), "...")
ثم يمكن لـprint(format_board(board)) أن يطبع لوحة مثل التي رأيناها في البداية.
لكن كيف نكتب ونختبر نصًا يحتوي على عدة أسطر؟ قد نرغب في كتابة شيء كهذا:
check_result(
format_board([
['X', 'O', 'X'],
[' ', 'O', 'O'],
[' ', 'X', ' ']
]),
"XOX
OO
X "
)
جرّب وسترى أن كتابة النص بهذه الطريقة لا تعمل.
عادة يجب أن تكون السلسلة النصية المكتوبة بعلامة اقتباس واحدة أو مزدوجة في سطر واحد، لذلك هذا غير صالح:
string = "First line Second line" print(string) لكن بايثون يوفر طريقة لكتابة نص يمتد على أكثر من سطر: علامات الاقتباس الثلاثية، أي ثلاث علامات اقتباس متتالية، سواء ثلاث علامات اقتباس مفردة أو ثلاث علامات اقتباس مزدوجة، حول محتوى النص.
شغّل المثال التالي:
string = 'First line\n Second line'
print(string)
رائع! يمكن للنص ذي علامات الاقتباس الثلاثية أن يمتد على عدة أسطر، وتظهر هذه الأسطر فعلًا في الناتج.
وكما في علامات الاقتباس المفردة والمزدوجة، فهذه مجرد صيغة لكتابة النص وليست نوعًا جديدًا من البيانات. """abc""" هي القيمة نفسها التي تكتبها هكذا: "abc".
لكن المتغير string يحتوي على شيء جديد. اكتب string في الطرفية لترى التمثيل الداخلي.
هذا هو السر: \n يمثل محرف سطر جديد (newline).
هو محرف واحد، مثل الحرف العادي أو المسافة ' '. إنه المحرف الموجود بين سطرين منفصلين عندما تضغط Enter.
داخل النص الحرفي في بايثون، الصيغة \n تمثل محرف السطر الجديد. القيمة النصية نفسها لا تحتوي على حرفي \ وn منفصلين، وإنما تحتوي على محرف واحد. يمكنك التحقق من ذلك في الطرفية بقياس طول نص يحتوي على سطر جديد واحد.
استخدم الآن محرف السطر الجديد لكتابة الدالة format_board. يجب أن يعمل الحل مع لوحة مربعة board بأي حجم:
def format_board(board):
...
check_result(
format_board([
['X', 'O', 'X'],
['O', ' ', ' '],
[' ', 'X', 'O']
]),
'XOX\nO \n XO'
)
ابنِ نصًا للوحة كلها. أضف أحرف كل صف، ثم أضف \n بين الصفوف. انتبه إلى أن الصف الأخير لا يحتاج إلى سطر جديد بعده.
Look carefully at the test case we provided. It shows you all you need!
You need to build up a string for the whole board. Start with an empty string.
For each row, add the characters from that row to the string.
You'll need a nested loop.
When you reach the end of a row, you need to add a newline before the next row.
'\n'is just like any other character! You can add it as usual with+.Notice that the end of the last row is different than the others.
Before you add a newline, you'll need to check if it's the last row or not.
Your outer loop should loop over the length of the board.
Then check if you are at the last index or not.
ممتاز! حل نموذجي يمكن أن يكون:
def format_board(board):
result = ''
for i in range(len(board)):
for char in board[i]:
result += char
if i != len(board) - 1:
result += '\n'
return result
وإذا بحثت عن join واستخدمتها، فهذا ممتاز أيضًا. قد يكون الحل مثل:
def format_board(board):
joined_rows = []
for row in board:
joined_rows.append("".join(row))
return "\n".join(joined_rows)
يمكنك الانتقال إلى الصفحة التالية، أو حل تحدٍّ إضافي: اكتب نسخة محسنة من format_board تعرض فواصل الصفوف والأعمدة. إذا كانت:
board = [
['X', 'O', 'X'],
[' ', 'O', 'O'],
[' ', 'X', ' ']
]
فيجب أن يطبع print(format_board(board)):
X|O|X -+-+- |O|O -+-+- |X| ويجب أن يعمل مع لوحة مربعة بأي حجم.
استخدام join مناسب جدًا هنا. الاختبار:
def format_board(board):
...
check_result(
format_board([
['X', 'O', 'X'],
['O', ' ', ' '],
[' ', 'X', 'O']
]),
'X|O|X\n-+-+-\nO| | \n-+-+-\n |X|O'
)
هناك نوعان من الأسطر: أسطر القطع وبينها |، وأسطر الفصل المكونة من - وبينها +. ابنِ كل نوع باستخدام join، ثم اجمع الأسطر مع محارف السطر الجديد.
There are two types of lines to be displayed: one type has the pieces joined by
|s in between them, the other type has-s joined by+s in between them.Both of these types of lines can be built up by using
joinappropriately.For example, how can you convert a row
['X', 'O', 'X']into'X|O|X'usingjoin?Similarly, how can you obtain
'-+-+-'usingjoin? To what list should you applyjoin?Once you figured out how to build up both types of lines, how can you combine them to obtain the final result?
Notice that the lines with the
+-signs are always the same.And there is one line with
+-separating every consecutive pair of lines with pieces.You can use
joinon the lines themselves!The lines with the pieces can be joined together with the
+-line in between them (with newlines added in appropriate places).To do that, first you need to keep the lines with the pieces stored in a list as you are building them.
Then apply
jointo that list, with the+-line as separator.To add the newlines to the
+-line correctly, take a look at the test case we provided.
عمل ممتاز! كان هذا تحديًا حقيقيًا.
أصبحت تعرف كيف تبني نصًا مكوّنًا من عدة أسطر، وحللت مشكلة عرض لوحة اللعبة للاعبين.
بعد ذلك ستتعلم المزيد عن الأنواع في بايثون وكيفية التحويل بينها، ثم كيفية استقبال إدخال اللاعبين. أنت الآن تقريبًا في منتصف مشروع اللعبة، فواصل.