ToCode
Kanalga Telegramโda oโtish
ืืืคืื ืงืฆืจืื ืืืชืื ืชืื ืืืช ืื ืื ืคืจืง
Ko'proq ko'rsatish1 420
Obunachilar
Ma'lumot yo'q24 soatlar
+27 kunlar
-230 kunlar
Postlar arxiv
1 420
# ืงืื ืืฉืืืืฉ ืืืืจ
ืืืช ืืืขืืืช ืฉืื ื ืืืฆื ืืืชืจ ืืื ืืืขืจืืืช ืืื ืืืชืืจ ืฉื ืฉืืืืฉ ืืืืจ ืืงืื, ืืื ืืืื ืืื - ืืฆื ืืื ืื ืื ื ืจืืฆืื ืืืชืื ืงืื ืฉืืคืฉืจ ืืืื ืืืฉืชืืฉ ืื ืฉืื ืืฉืื, ืืื ืืฆื ืฉื ื ืื ืื ื (ืืจืืข) ืขืกืืงืื ืืื ืืฉืืื ืืขืฉืืช ืืช ืืขืืืื ืืงืฉื ืฉื ืืืชืื ืงืื ืืฉืืืืฉ ืืืืจ. ืืชืืฆืื ืืื ืงืื ืฉื ืจืื ืืื ืงืื ืฉืืชืืื ืืฉืืืืฉ ืืืืจ, ืืื ืืฉืืชื ืื ืืืฉืชืืฉ ืื ืืขืื ืืงืื ืืชื ืืืื ืฉืืฉ ืืืชืจ ืืื ืชืืืืืช. ืืืงืจื ืืืื ืชืคืกืช ืืช ืื ืืืื ืืืชืืช ืืื ืืืืฉ, ืืืงืจื ืืจืข ืชืคืกืช ืืช ืื ืืืืืจ ืืื ืืืชื ืชืงืืข ืืืืฅ ืื ืกื ืืืจืื ืืืืฉ ืืช ืืงืื "ืืฉืืืืฉ ืืืืจ" ืืฆืจืื ืืคืชืืจ ืขืืฉืื ืืืืื ืืฉื ื ืคืืฆ'ืจืื ืืืงืื ืจืง ืืคืืฆ'ืจ ืืืืฉ ืฉืืชื ืืื ื.
ืืืืื? ืืฉืืื. ืงืื ืงืื ืคืืืชืื ืฉืขืืฉื ืืืชืจ ืืื ืขืืืื ืขื ืืฉืคื:
import re
text = "one two three I want to see three four five I am alive"
# delete all spaces in sentences
text = re.sub(r'\s+', '', text)
# replace each letter by its ordinal value
text = [ord(c) for c in text]
# leave only values that are larger than what came before
new_text = [text[0]]
for value in text:
if value > new_text[-1]:
new_text.append(value)
text = new_text
print(sum(text))
ืืงืื ืืฉ ืฉืืืฉื ืืืงืื ืฉืื ืืืื ืืขืืื ืืื ืืืจื ืืฉื ื ืขื ืืขืจื ืฉืืชืืจืช ืื ืขืืฉื ืื ืืืง. ืื ืืืื ืฉืืืื ื ืืื ืื ืืื ืืคืืืช ืืงืื ืกืืืจ ืืืื ืืืืชื ืืงืื, ืืืฃ ืืื ืื ืืืืื ืืืฉืชืืฉ ืื ืืขืื ืืงืื ืืืขืจืืช. ืืืงืจื ืืืจืืข ืืฆืืจืื ืืงืจืื ืืช ืืงืื ืืืืฉ ืืืขืชืืง ืืช ืืฉืืจืืช ืฉืขืืฉืืช ืขืืืื ืืืื.
ืืืจืกื ืืกืืื ืช ืืืชืจ ืฉื ืืงืื ืืื ืขืฉืืื ืืืืจืืืช ืื:
import re
def main():
text = "one two three I want to see three four five I am alive"
text = delete_all_spaces(text)
text = change_to_ord_values(text)
text = change_to_increasing(text)
print_result(text)
def delete_all_spaces(text):
# delete all spaces in sentences
text = re.sub(r'\s+', '', text)
return text
def change_to_ord_values(text):
# replace each letter by its ordinal value
text = [ord(c) for c in text]
return text
def change_to_increasing(text):
# leave only values that are larger than what came before
new_text = [text[0]]
for value in text:
if value > new_text[-1]:
new_text.append(value)
text = new_text
return text
def print_result(text):
print(sum(text))
main()
ืืืื ืืืคื ื ืืื? ืืื ืืกืืืจ ืขืืฉืื ืืคืื ืงืฆืืืช, ืื ืคืื ืงืฆืื ืืงืืืช ืืช ืืงืื ืฉืื ืืืืืืจื ืคืื ืืืชืืื ืืช ืืืืืจื ืืช ืืืชื ืชืืฆืื. ืขืืฉืื ืื ื ืจืฆื ืืืฉืชืืฉ ืจืง ืืืืง ืืืงืื ืืืงืื ืืืจ ื ืืื - ื ืืื?
ืจืื ืืกืืืืืื ืฉืื.
ืืคืื ืงืฆืืืช ืื ืืกืคืืง ืื ืจืืืช ืืฉืืื ืฉืืืื ืืขืืืืช ืืืงืื ืืืจ, ืืชืืชื ืืืชื ืคืฉืื ืืืื ืฉืื ืื ืฉืืื ืื ืืื ืืขืื ืืื ืืืืืืงื ืฉืจืืจืืชืืช ืืืืจื. ืืจืื ืืืชืจ ืกืืืจ ืฉืืืฉืื ืื ืกื ืืืฉืชืืฉ ืืืืช ืืื, ืืืื ืฉืืื ืื ืืืืืง ืงืื ืืืกืื ืฉืืคืื ืงืฆืื ืฆืจืืื ืืื ืืืชืงืข ืืืจืืื ืืืืฉ ืฉื ืืงืื.
ืื ืื ืื ืืฉ ืืื ืืื ืืืชืื ืงืื ืื ืจื, ืืืจืืืืงืืืจื ื ืืื ื ืฉืงื ืืืฉืชืืฉ ืื ืืขืื ืืงืืืืช ืืืชื ืืืืขืื ืืชืืจ ืืงืืืืช ื ืืกืคืื ืืืขืจืืช ืฉืืฆืืจืื ืืืชื - ืืื ืขื ืื ืืืชืื ืืช ืืงืื ืืื ืจื. ืืื ืฉืืื ืืืืืืจ ืืงืื ืฉืจืง ื ืจืื ืื ืจื, ืืฉืืืฉืืจ ืืฉื ืื ืฉื ื ืกื ืืฉืื ืืืชื ืืขืื ืืงืื.1 420
# ืืืฉ ืืืชืจ: ืชืขืืืืช ืกืืื ืงืืจืก
ืฉืชื ืืืืขืืช ืืฉืืืืช ืืืืื - ืงืืื ืื ืืืืชื ืืื ืขืืืืื ืขื PDF-ืื ื Ruby, ืืืืจ ืฉื ื ืืืกืคืชื ืืืชืจ ืื ืื ืื ืฉืืืคืฉืจ ืืื ืืืืจืื ืชืขืืื ืืืจื ืฉืกืืืืชื ืงืืจืก. ืืขืืฉืื ืืคืจืืื.
## ืืื ืขืืืืื ืขื PDF ื Ruby
ืืฉื ืื ืฉืืืชืจ ืืื ืืืืืืจ ืืจืื ืื ืฉืื ืืื ืืื ื ืกืืืืช ืืืงืฉื ืืงืื ืืืืฉืื ืืืฉืืจ ืขื ืงืืจืกืื ืฉืกืืืื ืคื. ืชืงืืคื ืืืื ืืจืืื ืื ืืืืชื ืืช ืืจืขืืื. ืชืขืืืืช ื ืจืื ืื (ืืขืืืื ื ืจืืืช) ืืื ืฉืจืื ืืืืฉื ืืืขืืจ, ืืื ืืฉืื ืฉืฆืจืื ืืื ืืขืืืจ ืืืื ืืืขืืื. ืืืืื ืืื ืืืืื ืฉื ืขืฆืื ืืื ืืืฉืื ืจืืฆื ืืืืืื ืืืืื ืืช ืื ืชืืง ืขืืืืืช ืืืืืชื ืฉืืื ืืืชืจ ืืื ืชืขืืื.
ืืขื ืื ืื ืืกืชืืจ ืฉืขืืืื ืืืฉืืจ ืืกืืืจ ืฉืกืืืืชื ืงืืจืก ืขืืืจ ืืืืง ืืื ืืืชืืื ืืงืืจืก ืืืจืืืช ืืช ืื ืืฉืืขืืจืื, ืืขืืืจ ืืืืง ืืืืขืกืืงืื ืฉืืื ืืืกืืช ืืฉืืื ืืช ืขืืืช ืืงืืจืก. ืืฉืืื ืฉืชื ืืงืืืฆืืช ืืืื ืืืกืคืชื ืืืื ืืืชืจ ืงืฆืช ืงืื ืฉืขืืงืจื ืืงืืข ืืื:
class CertificatesController < ApplicationController
def show
@bundle = Bundle.find_by(slug: params[:b])
authorize! :graduate, @bundle
if current_user.name_on_certificate.blank? || current_user.certificate_gender.blank?
return redirect_to(edit_user_registration, flash: t('notifications.missing_certificate_info'))
end
pdf = Prawn::Document.new
pdf.font Rails.root.join('certificates', './Alef-Regular.ttf')
pdf.bounding_box([140, 554], width: 300, height: 100) do
pdf.text current_user.name_on_certificate, direction: :rtl, size: 24
end
pdf.bounding_box([80, 370], width: 500, height: 100) do
pdf.text @bundle.name, size: 24
end
pdf.bounding_box([374, 242], width: 50, height: 100) do
pdf.text @bundle.recommended_duration_in_hours, size: 16
end
# get the existing pdf
template = CombinePDF.load Rails.root.join('certificates', "certificate_template_#{current_user.certificate_gender}.pdf")
user = CombinePDF.parse pdf.render
template.pages[0] << user.pages[0]
send_data template.to_pdf, filename: "tocode_#{@bundle.slug}.pdf", type: "application/pdf"
end
end
ืืืื ืฉืื ื ืืืื ืืืื ื ืขืฉื ืงืฆืช Code Review ืืื ืืืืื ืงืฆืช ืขื ืจืืื ืืขื ื Trade Offs ืืคืืชืื ืคืืฆ'ืจืื:
1. ืืืจ ืจืืฉืื ืฉืื ื ืื ืืืื ืืงืื ืื ืฉืื ืืงืื ืืชืื ืืคืื ืงืฆืื ืืืช ืืจืืื. ืืขืืื ืืืฉืื ืืืืชื ืืืงื ืืช ืื ืืืืง ืืืจืืื ืืืืฆืื ืืืชื ืืคืื ืงืฆืื ืืืจืช ืืงืืืก ืืืจ. ืืื ืืื ืื ืื ืจืื ืืงืจื ืืกืื Refactoring ืืืฉืื, ืืื ืื ื ืืืฉื ืฉืืงืื ืฉืืื ื ืชืฉืชืืชื ืืืขืจืืช ืืื ืืขืื ืืืชืืื ืขื ืืฉืื ืฉืขืืื ืืืืชืงืื ืืฉื.
2. ืืืืง ืืจืืฉืื ืฉื ืืคืื ืงืฆืื ืืืจืื ื-5 ืืฉืืจืืช ืืจืืฉืื ืืช. ืืื ืืืืง ืฉืืชื ืืืืืื ืืืืฆืจ ืชืขืืื, ืืืืืจ ืฉืกืืืืชื ืืช ืืงืืจืก ืืฉืื ื ืืืืข ืืื ืงืืจืืื ืืื. ืืคืื ืงืฆืื t ืฉื ืจืืืืก ืืื ืืืจื ืืืขืื ืืืืขื ืืงืืืฅ ืชืจืืื, ืืืืืคื ืืืื ืื ืืืงืกืืื ืืืชืจ ืฉืืืจืื ืืงืืืฅ ืชืจืืื ืืฆื ืืคืืจืื yml.
3. ืืืืง ืืฉื ื ืฉื ืืคืื ืงืฆืื ืืืฆืจ ืืช ืืชืขืืื ืืคืืจืื PDF ืืขืืจืช ืฉืชื ืกืคืจืืืช: prawn ื combine_pdf. ืฉืชืืื ืื ืืื ืืชืืืืงืืช ืืื ืขืืืืืช. ืืฉืืื ืฉืืชืขืืื ืชืฆื ืืคื ืืฆืจืชื ืฉื ื ืงืืฆื PDF ืืชืืจ ืชืื ืืช (ืืื ืืื ืื ืขื ืืืืื "ืืฉืชืชืฃ" ืืืฉื ืื ืืื ืืช ืขื ืืืืื "ืืฉืชืชืคื") ืืฉืืจืชื ืืืชื ืืชืืงืืืช ืืคืจืืืงื. ืืงืื ืื ื ื ืืชื ื prawn ืืืืฆืจ PDF ืืืฉ ืจืง ืขื ืืืืงืื ืืืื ืืืื ืืชืขืืื, ืืืืืจ ืฉื ืืืฉืชืืฉ, ืฉื ืืงืืจืก ืืืื ืฉืขืืช ืืงืืจืก. ืืืจื ืื ืื ื ืืฉืชืืฉ ื combine_pdf ืืื ืืฉืื ืืช ืืืืงืื ืืืื ืืืื ืขื ืืชืื ืืช ืืืงืื ืงืืืฅ ืชืขืืื ืขื ืื ืชืื ืื. ืฉืืื ืืฉืื ืื ืฉืื ืืขืืืื ืืืืฆืขืช ืืืืืจืื ืืื ืงืืฆืื ืืื ืืื, ืืื ืื ืฆืจืื ืืืืฆืื ืฉืืืช ืืืืชืืืื ืขื ืืชื ืืฉืืืืช.
4. ืืฉืืจื ืืืืจืื ื ืฉืืืืช ืืช ืืชืขืืื ืืืจื ืืืคืืคื.
## ืืื ืืืืจืื ืชืขืืื ืืงืืจืก ืฉืกืืืืชื
ืื ืืชื ืื ืืืื ืืืชืจ ืืจืืฆืื ืืจืืืช ืืื ื ืจืืืช ืชืขืืื, ืื ืื ืฉืืชื ืฆืจืืืื ืืขืฉืืช ืืื ืืกืืื ืืฆืคืืช ืืืืืฉืื ืงืืจืก (ืืงืืื ืฉืืืจ ืขืฉืืชื ืืช ืื), ืืื ืืืกื ืชืืื ืืขื ืืื ืื ืฉื ืืงืืจืก ืชืืคืืข ืืื ืชืืื ืืืฉื ืืฉืืื ืืจืฉืืืช ืืฉืืขืืจืื ืขื ืืคืฉืจืืช ืืืืจืื ืืช ืืชืขืืื.
## ืชืงืืืช? ืฉืืืืช? ืืืจื ืืืชื
ืืืื ืืฉืื ืื ืืืจืืชื ืืช ืืชืขืืื ืื ืชืขืืืืช ืฉืืื ืืขืืืื ืืฉ ืืื ืฉืืืืช, ืืขืจืืช, ืชืงืืืช ืื ืืืฉืืืช ืขื ืืคืืฆ'ืจ ืฉืืื ืืืืืื ืื ืืฉืืืจื ืืืืขื ืืืชืจ. ืืฉืื ืืฉืืืข ืืืืืฉืื ืืฉืคืจ ืืช ืืชืฉืชืืช.1 420
# ืืืงืจ ืืคืืชืื
ืืื ืืืืจืืช ืืืืืื ืืงืจืื ืืืืืงืช ืชืืื ื R&D, ืงืืฆืืจ ืฉื ืืืงืจ ืืคืืชืื. ืื ืจืง ืฉืืื ืืืืื ืื ื ืฉืฉื ื ืืืืจืื ืืืื ืฉืื ืื ืืืืจืฉืื ืืชื ืืืืช ืฉืื ื:
ืคืืชืื ืื ืืืืง ืฉืื ืื ื ืืืชืืื ืคืืฆ'ืจืื ืืืชืงื ืื ืืืืื. ืืฆืืื ืืคืืชืื ืืืืขื ืืฉืืืชืืื ืงืื ืืคื, ืืฆืื ืืืชืืขื. ืืฉืืืืืื ืืืืช ืืกืชืืจืื ืืืฉืืืขืจืืช ืืกืืฃ ืืื ืขืืืื ื ืจืืืช ืืืชืจ ืืื ืืื ืฉืืืชื ืืฉืืชืืืช.
ืืืงืจ ืื ืืืืง ืฉืื ืื ื ืืืืืื ืืืืืื ืฉืืืืช ืขืืืื ืืืฉืืช. ืืืงืจ ืืืื ืืืืืฉื. ืืืงืจ ืืื ืื ืืื ืืจื ืืื ืืืื. ืืืงืจ ืื ืื ืฉืงืืจื ืืฉืืืฉืืื ืฉืืืฉื ืืืื ืืืจืื POC ืจืง ืืฉืืื ืืืืืช ืฉืืฉืื ืืืื ืืกืืกื ืืืชืืื ืืื ืฉืืื ืืฆืจืื ืืืืืง ืืช ืืื. ืืกืืฃ ืืืืงืจ ืืืขืจืืช ืชืืื ืืืืชื ืืฆื ืื ืืคืืื ืืจืืข ืืืชืจ. ืื ืฉืืฉืชืคืจ ืื ืืืืข ืฉืื.
ืคืืชืื ื ืืชื ืืจืืฉื ืืืื ืืืชืจ. ืืืงืจ ืืื ืืืจื ืืืืืื ืงืืืื. ืื ืชืืืชืจื ืขื ืืฃ ืืื ืืื.
1 420
# ืื ืืฉืื ื
ืืืืืจืช ืืื ืืช ืืืจืงืืช ืืืจืฆืืื ื ืืกืชืืื ืขืืื ืืืืื ื ืืชืชื ืืืืื ืืืื ืืืจ - Cucmber? Que es eso? ืืื ืฉืืื, ืืืื ืืจืืจ ืืื ืืืขืชื. ืืืจืืช ืฉืื ืฉืื, ืื ืืืืขื, ืื ืืืื ืช ืืจืืืกืื ืืื ืืืื ืืืจ ืฉื ืืืืื ืืขืืจ ืืชื ืื ื-3 ืฉืคืืช (ืื ืืืืช, ืงืืืื ืืช ืืกืคืจืืืช), ืืืืืื ืืืืฉื ืืื ืืื ืฉืื ืฉืืื ืืื ืืงืื ืืืืืื ืืช ืืืืจ ืื ืืืืช ืฉืืื.
ืืืื ืฉืืชืืืฆื? ืืจื ืื ืคื ืืืจืฅ ืื ืื ื ืคืืืฉืื ืืช ืืขืจืืืช ืืื ืฉืื ืจืืื, ืขื ืื ืืืืืืืก ืืืื ืืืืขื ืจืฉืืืช, ืืืื ืืืช ืื ืฉืื ืืืืจ ืืืืืช ืื ืืืจื ืืืืื.
ืืื ืืคืืกื ืืื ืืื ื ืขื ืฉืคืืช ืื ืขื ืืจืงืืช, ืืื ืขื ืืืืื ืืืืช.
ืขื ืืืืืื ืืช ืืืืื ืื ืืืืช, ืฉื ืืฆืืช ืืื ืืงืื ืกืืืื ืืื ื ืจืืืช ืืืชื ื ืืชื ืช ืืืฉืื. ืขื ืืืืืื ืืช ืืืืื ืฉืคืช ืชืื ืืช ืืืฉื ืื ืืื ืืงื ืืืฉื ืืคืืื ืืคืจืืืงื ืื ืืืื ืฉืื, ืืืฉื ืขื ืืื ืฉืืืื git ืื ืืื ืืืืืืืช ืขื ื, ืฉืคืฉืื ืื ื ืจืืืช ืคืจืงืืืช ืืืืืืื ืกืืืืช. ืขื ืฉืืื ืืื ืืืืืืื ืื ืฆื ืืืชื, ืืืืืื ืฉืืืชื ืืืื ืืงืืฃ ืืืชื ื ืื ืืืื.
1 420
# ืื ืืืงื ืจืง ืืงื
ืื ืืืงื ืจืง ืืงื? ืืืืช?
ืืืื ืืชืืืช ืืืืืงื?
ืืืื ืืชืืืช ืืชืืขืื ืืคืืฆ'ืจ?
ืืืื ืืจืฆืช ืืืืืงืืช ืืืฉื ืืช ืืชืืงืื ืื ืืืืงื ืฉื ืืืืงืืช ืื ืจืืืื ืืืืช?
ืืืื ื ืืกืื ืืืืขืช ืงืืืื ืืืื?
ืืืื ืืืืคื ืืฉืจืช ืืืชืืืืืืช ืขื ืงืื ืคืืืงืืื?
ืชืืงืื ืื ืืืืจืื ืฉืฉืืืจืื 50 ืืืจืื ืืืจืื ืื ืืืกืืคืื ื ืงืืืืช ืืืฃ ืืื. ืขืืืฃ ืืงืืช ืืืืืจ ืืืืชืื ืืช ืืชืืงืื ืืฆืืจื ื ืืื ื. ืืืฆื ืฉืขื ืฉืืืื ืืกืืช ืขืืฉืื ืชืขืื ืืจืื ืืืชืจ ืืขืื ืืืืฉืืื.
1 420
# ืขืืืคื ืืจืืืืงืืืจื ืืืื ืขื ืืขืงืฃ ืืื
ืื ืืจืืืงื ืขืืืืื ืขื ืชืืขืื ืืืฉ ืืืืช ืืืืืืืืช ืฉื ืชืคืกื ืืช ืืขืื ืฉื ืกืืกืืืื ืงืจืืืก ืืืืชื ืืืื ืื "ืืชืื ืืช ืืืืืขืจืช ืืืืชืจ ืืจืืืงื". ืื ืืงืื:
function CountLabel({ count }) {
const [prevCount, setPrevCount] = useState(count);
const [trend, setTrend] = useState(null);
if (prevCount !== count) {
setPrevCount(count);
setTrend(count > prevCount ? 'increasing' : 'decreasing');
}
return (
<>
<h1>{count}</h1>
{trend && <p>The count is {trend}</p>}
</>
);
}
ืืจืขืืื ืืื ืฉืืงืืืคืื ื ืื CountLabel ืืงืืืช ืืช ืืขืจื ืฉื count ืืืืืฅ ืืฆืจืืื ืืืฆืื ืืืื - ืืื ืืขืจื ืื ืืืื ืืืื ืื ืงืื ืืืขืจื ืืงืืื. ืืฉืืื ืื ืืื ืฉืืืจืช ืืช ืืขืจื ืืงืืื ื State ืืื ืื ืื ื ืชืงืืขืื ืืฆืจืืืื ืืขืืื ืืช ืืกืืืื ืืชืื ืืงืื ืืจืืฉื ืฉื ืคืื ืงืฆืืืช CountLabel. ืื ืืชืืขืื ืฉื ืจืืืงื ืืืืื ืฉืื ืื ืืืคืฆืื ืืืื, ืืื ืฉืืืฆืืื ืืกืืืืื ืืื ืืืจื ืืืืืื ืืคืชืืจ ืืขืื.
ืืงืื ืืงืกื ืืกืืคืืจ ืืื ืืื ืฉืขื ืขืืฉืื ืืืืืฆื ืืืชื ืืืฉืชืืฉ ื useEffect ืืื ืืขืืื ืืฉืชื ื ืกืืืื ืืขืงืืืช ืฉืื ืื ืฉื prop, ืืขืืฉืื ืื ืืืืืฆืื ืืืืชืจ ืขื ื useEffect ืืืขืืื ืืช ืืขืจื ืืฉืืจืืช ืืคืื ืงืฆืื ืืจืืฉืืช. ืืืืช ืืื ืฉืฉืชื ืืืืคืฆืืืช ืื ืืืืืช, ืืฉืชืืื ืืืืืช ืืกื ืืื ืืขืงืฃ ืขืืืจ ืืจืืืืงืืืจื ืืจืืขื.
ืคืืชืจืื ื ืืื ืืฉืืืจืช ืืืื ืืืื ืืฉืืืจ ืืช ืืืืื ืืื ืขื ืืกืืืื ืืืงืืจื - ืืืืืจ ืื ืฉืืืืืง ืืช count ืืืืจืื ืขื ืขืืืื ืฉืื ืืื ืื ืื ืฉืฆืจืื ืืฉืืืจ ืืช ืืืืื. ืฉืืืจืช ืืืืจืื ืฉื ืืขืจื ืืงืืื ืืื ืชืืื ื ืฉื ืืกืืืื, ืืืืืง ืืื ืฉืืืจื ืฉื ืืฉืชื ื ืืกืืืื ื Local Storage ืื ืื ืื ืืคืืืฆืื ืืืจืช ืขื ืืขืจื. ืืื ืคืืชืจืื ืขื Custom Hook ืืืื ืืืืืช ืืจืื ืืืชืจ ืคืฉืื ืืืืชืจ ื ืืื ืืืืฆืขื ืฉื ืกืืืื ื ืืกืฃ ืืงืืืคืื ื ืืช ืืื. ืื ื ืจืื ืืื:
import { useState } from "react";
function useTrendedState(initialValue) {
const [value, setValue] = useState(initialValue);
const [prevValue, setPrevValue] = useState(initialValue);
function setter(...args) {
setPrevValue(value);
return setValue(...args);
}
return [value, setter, prevValue];
}
function CountLabel({ count, prevCount }) {
let trend;
if (count < prevCount) {
trend = "Decreasing";
} else if (count > prevCount) {
trend = "Increasing";
} else {
trend = "Stable";
}
return (
<>
<h1>{count}</h1>
{trend && <p>The count is {trend}</p>}
</>
);
}
export default function App() {
const [count, setCount, prevCount] = useTrendedState(0);
return (
<>
<button onClick={() => setCount(count + 1)}>Increment</button>
<button onClick={() => setCount(count - 1)}>Decrement</button>
<CountLabel count={count} prevCount={prevCount} />
</>
);
}
ืขื ืืืจื ืืจืืืื ื ืงืื ืืคืืจืฉ ืฉืืืคื ืืืฆื ืฉ count ืื ืืฉืชื ื. ื ืืื ืฉืืืื ืฉืื ืืกืคืจืื ืืืืื ืฉ useState ืืืืื ืื ืขืืฉื ืืืื ืืฉืืื ื ืงืจื ืขื ืืขืจื ืฉืืืจ ืงืืื ืฉื ืืืฆื ืืื ืื ืืคืฉืจื ืืงืื ืื ืืืื, ืืื ืืืฆืืื ืืืชืจ ืืืจืืืื ืืืืื ืืคืฉืจ ืืืืืื ืืฉืืขืืช ืฉืื ื ืืืืืช ืฉืื ืืืชื ืืืืฆืจืช ืืื ืืืฆืขื ืฉืืชืืขืื (ืืืฉื ืืขืืืื ืขื ืืขืจืืื ืื ืืืืืงืืื).
ืื ืคืชืื ืืืืื ืื ืืฉืื, ืืื ืืื ืฉืืฉ ืืืชื ืืคืจืืืืืืจืง, ืืื ืื ืืืจ ืืืืจืื ืืชืขื ืคืชื ืืืืื ืืื ืขืืืฃ ืืืืืจ ืืืืื ืฉืืืืช ืืืจืฉืช ืืืชื. ืืื, ืื ื ืื ืืืื ืืืฉืื ืขื ืืืืช ืืืืื.1 420
# ืคืจืกืื
ืืืืืื ื ืืฉื ืืืฉ, ืคืจืกืื ืืืืืื ืืืืืช ืืจื ืงืื ืืืืืืจ ืืืืืืฆืื ืื ืงืืืืช ืืคืชื. ืืืืืื-
1. ืืืจื ืฉืืฆืืื ืืืชืืื ืฉืืืฉื ืืืืฉืื ืืกืคืืจื ืื ื ืงืื ื ื ืขืื ืจืืฆื ืืืฉืืช.
2. ืืฉืืกืืื ืืืชืื ืืช ืืืคืืืงืฆืื ืืจืืฉืื ื ืฉืื, ืื ื ืงืื ื ืืขืฆืื ืืืืคืื ืืืฉ.
3. ืืฉืืกืืื ืืืงืื ืืขืจืืช ืืืืงืืช ืืืืืืืืืช ืืืชืจ, ืื ื ืงืื ื ืืขืฆืื ืืจืืืกืื ืืื ืก ืืงืฆืืขื ืื ืืฉื ืืืืงืืช.
ืืืืจ ืืืฉืื ืืฉืืืืฉ ืืคืจืกืื ืืื ืืืืืจ ืคืจืกืื ืฉืืืืืจื ืืฆืืื ืืช ืืืืืืืฆืื ืืืืื ืืืืื ืงืฉืืจืื ืืืืืื ืืช ืฉืืชื ืื ืกืื ืืฉืคืจ. ืื ืชืืื ืืืืืื ืฉืืืจื ืืคืจืก ืชืืืื ืืืืฉืื ืืชืืืื ืืืฃ ืืืืืืจ ืืช ืืงืฆื.
1 420
sys.argv = sys.argv[:1]
unittest.main()
sys.exit(0)
print("Hello world")
# part 1
print(f"2 * 2 = {two_by_two()}")
<<<<<<< HEAD
=======
# part 2
print(f"3 * 3 = {three_by_three()}")
>>>>>>> ae4ea11 (add tests)
ืืืช ืืืืคืกื ืืฉืืจื ืืืืจืื ื - ืื ื ืืืืง ืืืชื ืืืืฉืื ืืช ืืจืืืืืก:
$ git add .
$ git rebase --continue
[detached HEAD 82f6008] add tests
1 file changed, 13 insertions(+)
Successfully rebased and updated detached HEAD.
ื ืขืืืจ ืืช main ืืืงืื ืืืืฉ ืฉืื ืื ืงืื:
$ git branch -f main HEAD
$ git switch main
$ git log --oneline --graph
* 82f6008 (HEAD -> main) add tests
* 3e0d862 add generic method
* 05df720 add script docs
* 5161eec fixed print statement
* c299ef3 add print statement
* 6adbc5b initial commit
ืขืืฉืื ืืืืกืืืจืื ื ืจืืืช ืืจืื ืืืชืจ ืืื, ืืื ืฉืื ืืืจ ืืขื ืฃ ืฉืืืื. ืื ืืขืชืื ื ืจืฆื ืืืืืจ ืืืื, ืืืืกืืฃ ืขืื ืงืืืืืื ืืืืื ืื ืชืืื ืขื ืื ืฉืื ืืขืื. ืืขื ืืื ืืืืื ืืื ืฉืื ืขืฉืืชื push ืืืืืจ ืืคื ื ืืจืืืืืก, ืชืฆืืจืื ืืืืืฃ ืืืจืกื ืืขืืืื ืช ืืืืจืืก ืืช ืื ืฉืฉืืชืคืชื, ืืื ืืืื ืืกืื ืื ืฉืื ืืืจืื ืฉืขืืืืื ืืชืื ืขื ืืคืจืืืงื ืืืืื ืืืจ ืืฉืชืืฉื ืืืืจืกื ืฉืฉืืชืคืชื.1 420
print(f"2 * 2 = {two_by_two()}")
<<<<<<< HEAD
# part 2
print(f"3 * 3 = {three_by_three()}")
=======
>>>>>>> parent of 91cdcd3 (fixed conflicts)
ืื ื ืจืืื ืืงืืืฅ ืฉืืงืืขืื ืืืกืืื ืื ืื ืืืืช ืืื ืฉืืชืืืืื ืืงืื ืฉื ืื ืก ืืืืจืื ืฅ ืฉืื ื ืจืืฆื ืืืืืง, ืืฉืืืจ ืจืง ืืช ื HEAD ืืืกืืื ืขื ืืกืงืจืืคื ืืื:
import unittest, sys
class TestMultMethod(unittest.TestCase):
def test_mul_positives(self):
self.assertEqual(6, mul(2, 3))
##
# This script does all kinds of nice things
# to help children learn math
#
def two_by_two():
return mul(2, 2)
def mul(x, y):
return x * y
# run self test with --test
if len(sys.argv) > 1 and sys.argv[1] == "--test":
sys.argv = sys.argv[:1]
unittest.main()
sys.exit(0)
print("Hello world")
# part 1
print(f"2 * 2 = {two_by_two()}")
ืืืกืืื ืืช ื revert ืขื:
$ git add demo.py
$ git revert --continue
$ git log --oneline --graph
* 492cb53 (HEAD -> main) Revert printing 3 by 3
* ae4ea11 add tests
* 5ad40d1 add generic method
* 91cdcd3 fixed conflicts
|\
| * 6a8ccb0 add comments
| * 71b3207 fixed syntax
| * b2cc15b first try
* | 05df720 add script docs
|/
* 5161eec fixed print statement
* c299ef3 add print statement
* 6adbc5b initial commit
ืืงืื ืืืจ ืืืฆืื ืืคื ื ืืืืืื, ืืืืืื ืืืคืืข ืืืืกืืืจืื ืืื ืืืืืื ืฉืื ืืจืง ืฆืจืื ืืืืืจ ืืขืืื ืื ืืืืกืืฃ ืงืืืื ืขื ืชืืงืื ืืืจื ืงืืืื 6a8ccb0, ืืืืืจ ืืขื ืฃ ืืฉืืืจ, ืืืื ืฉืื ืืคืฉืจ ืืืื ืืืืฉ ืืฉืื ืฉืืืจ ืืืืื ื. ืื ืืคืฉืจ ืืืื ืืขืฉืืช rebase ืืฉืืืฉืช ืืงืืืืืื ืืืขืืืชืืื ืืื ืืืื ืืช ืืืืจืกื ืืืืฉื ืืืจืืืกืช ืฉืืื.
## ืืื ืืืื ืืช ืืืืืื ืขื rebase
ืืจื ืืืจืช ืืืฉืช ืืืืชื ืืขืื, ืื ืื ืืืคืชื ืขืืืื ืืช ืืืืืจ, ืืื ืืขืฉืืช rebase ืืงืืืืืื ae4ea11 ื 5ad40d1 ืื ืฉืืืชื ืืงืื ืฉืืฉ ืืื "ืืืืืฉ" ืขื ืงืืืื 05df720 ืฉืืื ืืคื ื ืืืืืื. ืื ื ืจืื ืืื:
$ git rebase --onto 05df720 91cdcd3 ae4ea11
ืืฉ ืคื 3 ืืกืคืจื ืงืืืืืื:
1. ืืจืืฉืื ืืื ืืงืืืื ืฉืืื ื ืื ืื ื ืจืืฆืื ืืืืฉืื (ืืืืืง ืืคื ื ืืืืืื ืืืขืืืชื)
2. ืืฉื ื ืืื ืงืืืื ืืืืืื (ืืงืืืื ืืืืจืื ืฉืื ืื ื ืจืืฆืื ืืืืืง)
3. ืืฉืืืฉื ืืื ืืงืืืื ืืืืจืื ืืคืจืืืงื (ืืงืืืื ืืืื ืื ืื ื ืจืืฆืื ืืืืืข). ืืคืฉืจ ืืืฉืชืืฉ ืืฉื main ืืืงืื ืืกืคืจ ืงืืืื ืืื.
ืืชืืฆืื ืืื ืืืืื ืงืื ืคืืืงื ืืื ืื ืฆืคืื:
Auto-merging demo.py
CONFLICT (content): Merge conflict in demo.py
error: could not apply 5ad40d1... add generic method
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 5ad40d1... add generic method
ืืงืื ืขื ืืงืื ืคืืืงื ืืกืืื ืืื:
##
# This script does all kinds of nice things
# to help children learn math
#
<<<<<<< HEAD
=======
def three_by_three():
return mul(3, 3)
>>>>>>> 5ad40d1 (add generic method)
def two_by_two():
return mul(2, 2)
def mul(x, y):
return x * y
print("Hello world")
# part 1
print(f"2 * 2 = {two_by_two()}")
ืื ื ืืืืง ืืช ืืคืื ืงืฆืื ืืืืืชืจืช ืืื ืืชืงื ืืช ืืงืื ืคืืืงื ืืืืฉืื ืืจืืืืืก, ืจืง ืืฉืืื ืืงืื ืงืื ืคืืืงื ื ืืกืฃ:
$ git add .
$ git rebase --continue
Auto-merging demo.py
CONFLICT (content): Merge conflict in demo.py
error: could not apply ae4ea11... add tests
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply ae4ea11... add tests
ืืงืื ืขืืฉืื ืขื ืืงืื ืคืืืงื ืืกืืื ืืื:
import unittest, sys
class TestMultMethod(unittest.TestCase):
def test_mul_positives(self):
self.assertEqual(6, mul(2, 3))
##
# This script does all kinds of nice things
# to help children learn math
#
def two_by_two():
return mul(2, 2)
def mul(x, y):
return x * y
# run self test with --test
if len(sys.argv) > 1 and sys.argv[1] == "--test":1 420
# ืืื ืืืื ืืืืื ืฉื ืขื ืฃ ืืฉื ื git
ืืขืืืื ืขื ืืื ืื ืื ื ืืืืืื ืืื ืืช ืื ืคืืฆ'ืจ ืืขื ืฃ ืืฉืื ืืจืง ืืืจื ืฉืืืงื ื ืืืื ืขืืื ืืืื ืืชืื main (ืืืช ืื ืืืจื ืืืืืื ืืขืืื, ืืื ืืื ืืกืคืืง ืคืืคืืืจืืช ืืื ืืืืืฉื ืืืืื ืกืืจืื). ืืขืื ืฉืืืืจืช ืื ืคืขื ืืฉืืืช ืขืืืื ืืื ืืื ืืฆืืจื ืืืื ืืืืื ืฉื ืขื ืฃ, ืืืฉื ืื ืืืจื ืฉืืืืื ื ืืืขืืื ื ืืคืจืืืงืฉื ืคืชืืื ืืืืื ื ืืช ืืืื ืืืื ืฉืคืกืคืกื ื ืืืืืงืืช.
## ืืืืจ ืืืืืื
ืืฉืืื ืืืืืื ืื ืืชื ืืืืจ ืืกืงืจืืคื ืคืืืชืื ืฉืืืื ืฉืื ื ืจืื ืืื:
* ae4ea11 (HEAD -> main) add tests
* 5ad40d1 add generic method
* 91cdcd3 fixed conflicts
|\
| * 6a8ccb0 add comments
| * 71b3207 fixed syntax
| * b2cc15b first try
* | 05df720 add script docs
|/
* 5161eec fixed print statement
* c299ef3 add print statement
* 6adbc5b initial commit
ืืคืฉืจ ืืจืืืช ืืช ืืขื ืฃ ืืืืืื ืืื ืืงืืืื add comments ืืงืืืื first try. ืืืืช ืืื ืฉืืฉ ืขืื ืืื ืขื ืคืื ืฉืืืืื ืคื ืืื, ืืื ืจืง ืืขื ืฃ ืืื ืืื ืงืื ืคืืืงืืื ืืืื ื ืืฆืจ ืฉื Merge Commit (ืื ืงืืืื ืืกืคืจ 91cdcd3). ืฉืืจ ืืืืืืืื ืขืืจื ืืืง ืขื Fast Forward. ืื ืืกืฃ ืืืจื ืื ืืืืื ืื ืืืงืชื ืืช ืฉื ืืขื ืฃ.
ืืืืืจ ืืฉ ืจืง ืกืงืจืืคื ืคืืืชืื ืืืื ืืงืืืฅ demo.py ืืื ืืชืืื ืฉืื:
import unittest, sys
class TestMultMethod(unittest.TestCase):
def test_mul_positives(self):
self.assertEqual(6, mul(2, 3))
##
# This script does all kinds of nice things
# to help children learn math
#
def three_by_three():
return mul(3, 3)
def two_by_two():
return mul(2, 2)
def mul(x, y):
return x * y
# run self test with --test
if len(sys.argv) > 1 and sys.argv[1] == "--test":
sys.argv = sys.argv[:1]
unittest.main()
sys.exit(0)
print("Hello world")
# part 1
print(f"2 * 2 = {two_by_two()}")
# part 2
print(f"3 * 3 = {three_by_three()}")
ืืืงืืืืืื ืฉืื ื ืจืืฆื ืืืื ืื ืืื ืฉืืืกืืคื ืืช ืืคืืฆ'ืจ ืฉื "ืืืคืกืช 3 ืืคืื 3", ืืคืืฆ'ืจ ืืื ืืืจืื ืืืคืื ืงืฆืื three_by_three ืืืืืืขืช ืืืืคืกื ืืฉืืจื ืืืืจืื ื ืฉื ืืกืงืจืืคื. ืืื ืฉืื ืื ืื ืืื ืขืื ืฉืื ืืืื ืฉื ืืขื ืืื ืืืงืืืืช ืืกืงืจืืคื ืืืื ืฆืคืืืื ืงืื ืคืืืงืืื.
## ืืื ืืืื ืืช ืืืืืื ืขื revert
ืืจื ืืืช ืืืื ืืช ืืืืืื ืืื ืคืงืืืช revert. ืืื ืืฉ ืขื ืื ืฉืชื ืืขืืืช:
1. ืจืืืืจื ืืื ืคืงืืื ืืืื ืจืืืฉื ืืฉืื ืืืื ืฉืงืจื ืืืจื ืืืืืื, ืืืื ืืื ืฉืขืืจื ืืืืืจ ืืืชืจ ืฉืื ืืืื ืืืจื ืืงืืืื ืฉืืชื ืจืืฆืื ืืืื, ืื ื revert ืืืื ืคืืืช ืืืื. ืจืืืืจื ืืขืฆื ืืืืฆืจ ืงืืืื ืืืฉ ืฉืืืืง ืืช ืืงืื ืฉืืงืืืื ืืืขืืืชื ืืื ืืก.
2. ืื ืื ืคืขื ืืขืชืื ืชืจืฆื ืืืื ืืืืฉ ืืช ืืขื ืฃ (ืืืฉื ืื ืชืืฆืื ืืช ืืืขืื ืืชืชืงื ื ืืืชื), ืืื ืื ืืืื ืืืืฉ ืืช ืืงืืืืืื ืฉืืืจ "ืืืืื" ื"ืจืืืจืื" (reverted), ืื ื revert ืื ืืฉื ื ืืืจืื ืืืืกืืืจืื ืืื ืืื ืืกื ืืื ืงืืืื ืืืฉ.
ืื ืืืืจ ืฉืื ื ืืงื ืืช ืืืจื ืืืืช ืืืขืชืื ื ืจืฆื ืืชืงื ืืฉืื ืืขื ืฃ ืฉืืืจ ืืืื ื ืฆืืจื ืืขืฉืืช rebase ืืื ืืงืืืืืื ืืืืชื ืขื ืฃ - ืืืืืจ ืืงืืืืืื 6a8ccb0, 71b3207 ื b2cc15b - ืื ืืืจืช ืฉืืืฉืชื ืื ืืืืืื ืืืืฉ ืืขืืื.
ืืื ืืงืจื ืืื ืืชื ืืืื ืื ืืืืชืจ ืขื ืืืืื ืขืชืืื ืืืื ื ืชืงืื. ืื ื ืืืืืจ ืฉืืจืืคื ืืื:
$ git log
* ae4ea11 (HEAD -> main) add tests
* 5ad40d1 add generic method
* 91cdcd3 fixed conflicts
|\
| * 6a8ccb0 add comments
| * 71b3207 fixed syntax
| * b2cc15b first try
* | 05df720 add script docs
|/
* 5161eec fixed print statement
* c299ef3 add print statement
* 6adbc5b initial commit
ืืืื ืฉืืืืืื ืืืฆืข ื Merge Commit ืื ื ืืืื ืืืืฆืจ (ื ื, ืื ืกืืช ืืืืฆืจ) ืืช ืงืื ืืืืคืื ืืฉืืจืืช ืืืืชื merge commit. ืืืื ืฉืื merge commit ืื ื ืฆืจืื ืืฆืืื ืืืื ืจืื ืฉืื ืืืืฆืืช ืืช ืืขื ืฃ ืืจืืฉื, ืืืืืจ ืื ืฉืืงืื ืืืื ืืืื. ืืืืกืคืืจ ืืชืืื ื-1 ืืืื ืืฆืื ืืืกืคืจ ืืื 1:
$ git revert 91cdcd3 -m 1
ืืชืืฆืื ืืื ืืืืื ืงืื ืคืืืงื ืืื ืืคืฉืจ ืืืืืช ืขื ืื. ืืื ื ืจืื ืืงืืืฅ ืขื ืืงืื ืคืืืงื:
import unittest, sys
class TestMultMethod(unittest.TestCase):
def test_mul_positives(self):
self.assertEqual(6, mul(2, 3))
##
# This script does all kinds of nice things
# to help children learn math
#
<<<<<<< HEAD
def three_by_three():
return mul(3, 3)
=======
>>>>>>> parent of 91cdcd3 (fixed conflicts)
def two_by_two():
return mul(2, 2)
def mul(x, y):
return x * y
# run self test with --test
if len(sys.argv) > 1 and sys.argv[1] == "--test":
sys.argv = sys.argv[:1]
unittest.main()
sys.exit(0)
print("Hello world")
# part 11 420
Direction::Left => "<",
Direction::Right => ">",
});
for snake_pos in self.board.snake.iter().skip(1) {
printer.print(snake_pos, "X");
}
printer.print(self.board.apple, "O");
}
fn on_event(&mut self, event: Event) -> EventResult {
match event {
Event::Key(key) => {
self.direction = match key {
Key::Down => Direction::Down,
Key::Up => Direction::Up,
Key::Left => Direction::Left,
Key::Right => Direction::Right,
_ => self.direction,
};
self.step();
}
_ => (),
}
EventResult::Ignored
}
fn required_size(&mut self, _: Vec2) -> Vec2 {
self.board.size.map_x(|x| 2 * x)
}
}
fn main() {
// Creates the cursive root - required for every application.
let mut siv = cursive::default();
let game = GameView::new();
let tx = siv.cb_sink().clone();
thread::spawn(|| {
loop {
thread::sleep(Duration::from_millis(500));
game.step();
}
});
thread::spawn(move || {
// game.board.step(Direction::Up);
loop {
thread::sleep(Duration::from_millis(500));
tx.send(Box::new(|si| {
si.call_on_name("game", |view: &mut GameView| {
let still_alive = view.step();
if !still_alive {
panic!("You lose");
}
}).unwrap();
})).unwrap();
}
});
siv.add_layer(game.with_name("game"));
siv.set_fps(24);
// Starts the event loop.
siv.run();
}
ืืืชื ืชืืืื ืืืฆืื ืื ืืืืืจ ืืืืืืื ืฉื ืืกืืืจื ืืงืืฉืืจ:
https://github.com/ynonp/rust-8-days/tree/main/day7%20-%20snake%20gui/snake
## ืชืจืืืืื ืืืจืืื
1. ืืจืืข ืืืฉืืง ืงืฆืช ืืฉืขืื ืื ืื ืืฉื ื ืืื ืชืคืืืื ื ืืื ืืืืืจืืช ืชืืฉืืจ ืืื (step ื ืงืจืืช ืคืขื ืืืฆื ืฉื ืื). ืชืืืื ืขืืื ื ืืช ืืงืื ืื ืฉืืกืคืืจ ืืืฆืื ื ืงืืืืช.
2. ืืืจื ืฉืืฉ ืืื ื ืงืืืืช ืขืืื ื ืืช ืืงืื ืืื ืฉืืื ืฉืืฉ ืืืชืจ ื ืงืืืืช ืืืืจืืช ืืืฉืืง ืชืืื.
3. ืืืกืืคื ืืคืฉืจืืช ืืฆืืื ืืืืฆืข - ืืืืฆื ืขื ืืคืชืืจ q ืืกืืืืช ืืช ืืืฉืืง.
4. ืืืกืืคื ืืกื Game Over ืืืจื ืฉืื ืืฉ ืืชื ืืฉ ืืงืืจ ืื ืืขืฆืื.
ืืชื ืืจืืื ืืืืื ืื ืืคืจืกื ืืช ืืคืืชืจืื ืืช ืฉืืื ืืื (ืื ืืื ืงืื ืืคืืชืจืื ืืช ืื ืชืืืจื ืืืขืืืช ืืืชื ืืืืืืื) ืืื ืื ื ื ืืคืจื ืืื ืืฉ ืืืืจ ื ืืฉืื ืืืืจ ืขื ืจืืกื ืืืืื ืืจื ื.1 420
ืืืจื ืฉื ืฆืื ืืช ื View ืื ืืฉืชืืฉ ืืืืฅ ืขื ืืคืชืืจ ืืืืืืืืช cursive ืืืื ืก ืืคืื ืงืฆืื
on_event ืืืืชื ืื ื ืืืืื ืืช ืืืคื ืืืืืฆื. ืืื ืืืฉืืง ืกื ืืืง ืืฉ ืขืื ืืืจ ืฉืฆืจืื ืืขืฉืืช - ืืืืื ืืช ืื ืืฉ. ืืืช ืื ืื ืื ื ืฆืจืืืื ืืขืฉืืช ืืคื ืฉืขืื, ืืื ืงืฉืจ ืืืืจืืขืื ืฉืงืจื ืื ืื. ืืคืจืืืืืืจืงืื ืจืืื ืืืจืื ืืฉ ืืจื ืืืืืืจ ืืืจืืข timeout ืขื View, ืืื ืืืจืืฅ ืงืื ืื X ืฉื ืืืช. ื cursive ืื ืืฆืืชื ืืจื ืืืคืขืื ืฉืขืื ืืจืงืข ืฉืืคืขืื ืืืจืืข, ืืืื ืืจืขืืื ืืฉื ื ืฉืื ืืื ืืืจืืฅ Thread ืืจืงืข, ืฉืื ืืฆื ืฉื ืื ืืคืขืื ืืช ืืคืื ืงืฆืื step ืฉื Game. ืื ืืชืืจืจ ืืืจืื ืืืชืจ ืืกืืื ืืื ืฉืงืืืืืชื ืืืื ืืืื ืืืขืืืช ืฉื Rust. ืืืืืง ืืจืืฉืื ืฉืื ื ืจืืฆื ืืืืืืง ืืื ืืืืืืฉ ืฉื GameView, ืฉืืืื ืืช ืืคืื ืงืฆืื step:
impl GameView {
pub fn new() -> Self {
return GameView {
board: game::Game::new(),
direction: Direction::Down,
};
}
pub fn step(&mut self) -> bool {
return self.board.step(self.direction);
}
}
ืืขืฆื ืื ืืกืฃ ืืื ืื ืฉืืืจ ืกืืคืจืชื ืขื ื View ืฉืื, ืืืกืคืชื ืื ืขืืฉืื ืขืื ืฉืชื ืคืื ืงืฆืืืช - ืืคืื ืงืฆืื new ืฉืืืฆืจืช GameView ืืืฉ, ืืืคืื ืงืฆืื step ืฉืืคืขืืื ืืช ืืคืื ืงืฆืื step ืฉื ืืืฉืืง ืฉืืืฆืื ื View.
ืืงืื ื main ืื ื ืืืชื:
let mut siv = cursive::default();
let game = GameView::new();
let tx = siv.cb_sink().clone();
thread::spawn(move || {
// game.board.step(Direction::Up);
loop {
thread::sleep(Duration::from_millis(500));
tx.send(Box::new(|si| {
si.call_on_name("game", |view: &mut GameView| {
let still_alive = view.step();
if !still_alive {
panic!("You lose");
}
}).unwrap();
})).unwrap();
}
});
ืืจืืจ ืืื ืื ื ืืคืขืื Thread ืืืฉ ืืื ืฉืืจืืฅ ืืจืงืข (ืขืฉืื ื ืืฉืื ืืืื ืืืืืืช ืืชืงืฉืืจืช), ืืื ืฉืืื ืื ืฉืื ื ืื ืืืื ืืชืื ื Closure ืฉื ื Thread ืืงืจืื ื game.step ืกืชื ืื. ืืืืืจ ืงืื ืืื ืื ืืื ืืชืงืืคื:
thread::spawn(|| {
loop {
thread::sleep(Duration::from_millis(500));
game.step();
}
});
ืืืืื ืืืื ืฉืืฃ ืืื ืื ืืืื ืืืืืื ื Rust ืฉ game ืืืฉืืจ ืื ืื ืืื ืฉื Thread ื ืฉืืจ ืื (ืืืื ืื ืืฉืชื ื ืคื ืืื ืฉื ืืคืื ืงืฆืื), ืื ืจืืกื ืฆืจืื ืืืขืืืจ ืืช ืืืขืืืช ืขื game ืืชืื ื Closure, ืืื ืื ืืื ืืขืฉื ืืช ืื ืื ื ืืื ืืืฉืชืืฉ ื game ืืืืฅ ื Closure. ืืืืื ืืืจืืช ืืืจืืช ืฉืื ืื ื ืืืืขืื ืฉื Thread ืืืฉื ื ืืืจืืฉื ืืืฉืืจื ืืืื ืืืืชื ืืฉื ืืื, ืจืืกื ืื ืืืืข ืืช ืื ืืืื ืืืจืฉ ืฉื ืืืจ ืื ืืฉื ืืื ืื ืื ืืช Game. ืืืืช ืืืืจื ืฉืืื ืื ืืื ืืขืฉืืช.
ืืื ืืืื ืืืชื ืืืืจืกื ืืงืฆืช ืืืชืจ ืืกืืจืืืช ืฉื ืืงืื ืฉืืืจืชื ืืืฉืืืจ ืืช game ืืืขืืืช ืืชืืืืืื ืืจืืฉื, ืืืืคืขืื ืืช ืืคืื ืงืฆืื step ืืืืฆืขืืช ืฉืืืืช ืืืืขื ืื Thread ืืืฉื ื. ืืคืื ืงืฆืื siv.cb_sink ืฉื ืกืคืจืืืช cursive ืืืืฆืจืช ืฆืื ืืจ ืชืงืฉืืจืช ืืืชื ืื ื ืืืื ืืฉืืื ืืืืขืืช ืืชืืืืืื ื ืจืงืข ืืืืฉืื ืืจืืฉื, ืืคืงืืื send ืฉื ืฆืื ืืจ ืืชืงืฉืืจืช ืฉืืืืช ืืช ืืืืืขื ืืชืืื ืืืืืขื ืืื Closure ืืขืฆืื ืฉืืงืื Reference ืฉื ืืืืืงื ื siv ืืจืืฉื. ืืชืื ื Reference ืื ื ืืคืขืื ืืช call_on_name ืืื ืืงืื Reference ื Game View, ืืืจืื ืื ื ืืคืขืื ืืช step, ืื ืื ืืื ืฉืืฃ ืืื ืืืงื ืืขืืืช ืขื ืฉืื ืืืจ. ืื ื ืงืืจื ืืงืื ืืื "ืืก ืจืืกื", ืื ืื ื ืขืืื ืืืชืจ ืงืฉื ืจืง ืืฉืืื ืืืชืืื ืืฉืืืช ืืขืืืื ืฉื ืจืืกื. ืืืืจื ืืื ื ืฆืืจื ืืืืืื ืื ืืขืจื ืฉื ืจืืกื - ืืืืืจ ืชืืื ืืืช ืืืืืืช ืืืชืจ - ืฉืืื ืืช ืืืืืจ.
ืื ื ืกืื ืืช ืื ืงืืขื ืืงืื ืืืคืืกื ืืขืื ืงืฆืช ืงืืขืื ืฉื ืืืืืจืื ื ืืืข ืืชืืื ืืืื ืฉื main.rs:
use cursive::{
event::{Event, EventResult, Key},
view::{Nameable},
Printer, Vec2,
};
use game::{Direction};
use std::{thread};
use std::time::Duration;
mod game;
struct GameView {
// Actual board, unknown to the player.
board: game::Game,
direction: Direction,
}
impl GameView {
pub fn new() -> Self {
return GameView {
board: game::Game::new(),
direction: Direction::Down,
};
}
pub fn step(&mut self) -> bool {
return self.board.step(self.direction);
}
}
impl cursive::view::View for GameView {
fn draw(&self, printer: &Printer) {
printer.print(self.board.snake.front().unwrap(), match self.direction {
Direction::Up => "^",
Direction::Down => "V",
Endi mavjud! Telegram Tadqiqoti 2025 โ yilning asosiy insaytlari 
