{"id":359,"date":"2025-03-13T13:25:02","date_gmt":"2025-03-13T13:25:02","guid":{"rendered":"https:\/\/news.techjunior.vn\/?p=359"},"modified":"2025-03-13T13:25:02","modified_gmt":"2025-03-13T13:25:02","slug":"%f0%9f%a7%a9-cung-con-tao-tro-choi-ghep-hinh-puzzle-hoc-lap-trinh-that-de-dang","status":"publish","type":"post","link":"https:\/\/news.techjunior.vn\/?p=359","title":{"rendered":"\ud83e\udde9 C\u00f9ng Con T\u1ea1o Tr\u00f2 Ch\u01a1i &#8220;Gh\u00e9p H\u00ecnh (Puzzle)&#8221; \u2013 H\u1ecdc L\u1eadp Tr\u00ecnh Th\u1eadt D\u1ec5 D\u00e0ng!"},"content":{"rendered":"\n<p>Trong th\u1eddi \u0111\u1ea1i c\u00f4ng ngh\u1ec7 ph\u00e1t tri\u1ec3n, vi\u1ec7c cho tr\u1ebb <strong>l\u00e0m quen v\u1edbi l\u1eadp tr\u00ecnh t\u1eeb s\u1edbm<\/strong> gi\u00fap c\u00e1c em ph\u00e1t tri\u1ec3n t\u01b0 duy logic v\u00e0 kh\u1ea3 n\u0103ng gi\u1ea3i quy\u1ebft v\u1ea5n \u0111\u1ec1. M\u1ed9t c\u00e1ch ti\u1ebfp c\u1eadn \u0111\u01a1n gi\u1ea3n, th\u00fa v\u1ecb m\u00e0 cha m\u1eb9 c\u00f3 th\u1ec3 c\u00f9ng con kh\u00e1m ph\u00e1 ch\u00ednh l\u00e0 <strong>t\u1ea1o tr\u00f2 ch\u01a1i &#8220;Gh\u00e9p H\u00ecnh (Puzzle)&#8221;<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc <strong>Tr\u00f2 ch\u01a1i &#8220;Gh\u00e9p H\u00ecnh&#8221; l\u00e0 g\u00ec?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"512\" height=\"512\" src=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/image-23.png\" alt=\"\" class=\"wp-image-361\" style=\"width:436px;height:auto\" srcset=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/image-23.png 512w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/image-23-300x300.png 300w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/image-23-150x150.png 150w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/figure>\n\n\n\n<p>Trong tr\u00f2 ch\u01a1i n\u00e0y, m\u1ed9t h\u00ecnh \u1ea3nh s\u1ebd \u0111\u01b0\u1ee3c chia th\u00e0nh nhi\u1ec1u m\u1ea3nh nh\u1ecf v\u00e0 x\u00e1o tr\u1ed9n v\u1ecb tr\u00ed. <strong>Nhi\u1ec7m v\u1ee5 c\u1ee7a ng\u01b0\u1eddi ch\u01a1i<\/strong> l\u00e0 s\u1eafp x\u1ebfp l\u1ea1i c\u00e1c m\u1ea3nh \u0111\u1ec3 t\u1ea1o th\u00e0nh b\u1ee9c h\u00ecnh ho\u00e0n ch\u1ec9nh.<\/p>\n\n\n\n<p><strong>V\u00ed d\u1ee5 d\u1ec5 hi\u1ec3u:<\/strong> H\u00e3y t\u01b0\u1edfng t\u01b0\u1ee3ng con b\u1ea1n c\u00f3 m\u1ed9t b\u1ee9c tranh y\u00eau th\u00edch, nh\u01b0ng b\u1ee9c tranh \u0111\u00f3 b\u1ecb c\u1eaft th\u00e0nh nhi\u1ec1u ph\u1ea7n. Nhi\u1ec7m v\u1ee5 c\u1ee7a con l\u00e0 gh\u00e9p l\u1ea1i \u0111\u00fang th\u1ee9 t\u1ef1 \u0111\u1ec3 kh\u00f4i ph\u1ee5c b\u1ee9c tranh.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>T\u1ea1i sao tr\u00f2 ch\u01a1i n\u00e0y l\u1ea1i c\u00f3 \u00edch cho tr\u1ebb?<\/strong><\/h3>\n\n\n\n<p>\u2705 <strong>Ph\u00e1t tri\u1ec3n t\u01b0 duy logic:<\/strong><br>Tr\u1ebb h\u1ecdc c\u00e1ch ph\u00e2n t\u00edch, so s\u00e1nh v\u00e0 t\u00ecm ra m\u1ea3nh gh\u00e9p ph\u00f9 h\u1ee3p d\u1ef1a tr\u00ean h\u00ecnh d\u1ea1ng v\u00e0 m\u00e0u s\u1eafc.<\/p>\n\n\n\n<p>\u2705 <strong>R\u00e8n luy\u1ec7n k\u1ef9 n\u0103ng gi\u1ea3i quy\u1ebft v\u1ea5n \u0111\u1ec1:<\/strong><br>Khi th\u1eed gh\u00e9p sai, tr\u1ebb s\u1ebd h\u1ecdc c\u00e1ch th\u1eed l\u1ea1i v\u00e0 t\u00ecm h\u01b0\u1edbng gi\u1ea3i quy\u1ebft t\u1ed1t h\u01a1n.<\/p>\n\n\n\n<p>\u2705 <strong>L\u00e0m quen v\u1edbi l\u1eadp tr\u00ecnh qua c\u00e1c kh\u00e1i ni\u1ec7m c\u01a1 b\u1ea3n:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Bi\u1ebfn s\u1ed1:<\/strong> L\u01b0u tr\u1eef v\u1ecb tr\u00ed c\u00e1c m\u1ea3nh gh\u00e9p.<\/li>\n\n\n\n<li><strong>M\u1ea3ng (Array):<\/strong> Qu\u1ea3n l\u00fd c\u00e1c m\u1ea3nh gh\u00e9p theo th\u1ee9 t\u1ef1.<\/li>\n\n\n\n<li><strong>\u0110i\u1ec1u ki\u1ec7n (if-else):<\/strong> Ki\u1ec3m tra \u0111\u00fang ho\u1eb7c sai khi gh\u00e9p.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc67 <strong>Cha m\u1eb9 c\u00f3 th\u1ec3 h\u1ed7 tr\u1ee3 con nh\u01b0 th\u1ebf n\u00e0o?<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>B\u1eaft \u0111\u1ea7u t\u1eeb tr\u00f2 ch\u01a1i th\u1ee7 c\u00f4ng:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In m\u1ed9t b\u1ee9c \u1ea3nh y\u00eau th\u00edch<\/strong>, c\u1eaft th\u00e0nh 4-6 m\u1ea3nh v\u00e0 c\u00f9ng con ch\u01a1i gh\u00e9p h\u00ecnh tr\u01b0\u1edbc khi l\u1eadp tr\u00ecnh.<\/li>\n\n\n\n<li>\u0110i\u1ec1u n\u00e0y gi\u00fap con hi\u1ec3u r\u00f5 quy lu\u1eadt v\u00e0 c\u1ea5u tr\u00fac c\u1ee7a tr\u00f2 ch\u01a1i.<\/li>\n<\/ul>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>C\u00f9ng con l\u1eadp tr\u00ecnh tr\u00f2 ch\u01a1i \u0111\u01a1n gi\u1ea3n:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>N\u1ebfu con ch\u01b0a quen v\u1edbi l\u1eadp tr\u00ecnh, cha m\u1eb9 c\u00f3 th\u1ec3 h\u01b0\u1edbng d\u1eabn t\u1eebng b\u01b0\u1edbc ho\u1eb7c s\u1eed d\u1ee5ng c\u00e1c c\u00f4ng c\u1ee5 tr\u1ef1c quan nh\u01b0 <strong>Scratch<\/strong> (ph\u00f9 h\u1ee3p cho tr\u1ebb t\u1eeb 7-12 tu\u1ed5i).<\/li>\n\n\n\n<li>V\u1edbi tr\u1ebb \u0111\u00e3 quen l\u1eadp tr\u00ecnh c\u01a1 b\u1ea3n, h\u00e3y c\u00f9ng con th\u1eed t\u1ea1o tr\u00f2 ch\u01a1i n\u00e0y b\u1eb1ng <strong>Python ho\u1eb7c JavaScript<\/strong>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udcc4 <strong>V\u00ed d\u1ee5 m\u00e3 ngu\u1ed3n \u0111\u01a1n gi\u1ea3n (b\u1eb1ng Python)<\/strong><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0e <strong>G\u1ee3i \u00fd m\u1edf r\u1ed9ng tr\u00f2 ch\u01a1i:<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>D\u1ec5 h\u01a1n cho tr\u1ebb nh\u1ecf:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>D\u01b0\u1edbi \u0111\u00e2y l\u00e0 v\u00ed d\u1ee5 \u0111\u01a1n gi\u1ea3n m\u00f4 ph\u1ecfng tr\u00f2 ch\u01a1i x\u00e1o tr\u1ed9n m\u1ea3nh gh\u00e9p s\u1ed1:\n\n<code>import random\n\n# T\u1ea1o c\u00e1c m\u1ea3nh gh\u00e9p (m\u1ea3ng s\u1ed1 t\u1eeb 1 \u0111\u1ebfn 9)\npuzzle = &#91;i for i in range(1, 10)]\nrandom.shuffle(puzzle)\n<\/code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-cyan-bluish-gray-color\">\n<\/mark><code># Hi\u1ec3n th\u1ecb tr\u1ea1ng th\u00e1i ban \u0111\u1ea7u\n<\/code>print(\"\ud83e\udde9 Tr\u00f2 ch\u01a1i gh\u00e9p h\u00ecnh - X\u1ebfp l\u1ea1i c\u00e1c m\u1ea3nh s\u1ed1 t\u1eeb 1 \u0111\u1ebfn 9 theo \u0111\u00fang th\u1ee9 t\u1ef1!\")\n<code>print(puzzle)\n\n# Cho ph\u00e9p ng\u01b0\u1eddi ch\u01a1i \u0111\u1ed5i ch\u1ed7 c\u00e1c m\u1ea3nh gh\u00e9p\nwhile puzzle != &#91;1, 2, 3, 4, 5, 6, 7, 8, 9]:\n    try:\n        a = int(input(\"Nh\u1eadp v\u1ecb tr\u00ed th\u1ee9 nh\u1ea5t (1-9): \")) - 1\n        b = int(input(\"Nh\u1eadp v\u1ecb tr\u00ed th\u1ee9 hai (1-9): \")) - 1\n        puzzle&#91;a], puzzle&#91;b] = puzzle&#91;b], puzzle&#91;a]\n        print(puzzle)\n    except:\n        print(\"\u26a0\ufe0f Vui l\u00f2ng nh\u1eadp s\u1ed1 h\u1ee3p l\u1ec7!\")\n\nprint(\"\ud83c\udf89 Ch\u00fac m\u1eebng! B\u1ea1n \u0111\u00e3 ho\u00e0n th\u00e0nh tr\u00f2 ch\u01a1i!\")<\/code><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Gi\u1ea3m s\u1ed1 l\u01b0\u1ee3ng m\u1ea3nh gh\u00e9p (v\u00ed d\u1ee5: t\u1eeb 1 \u0111\u1ebfn 4).<\/li>\n\n\n\n<li>Th\u00eam h\u01b0\u1edbng d\u1eabn chi ti\u1ebft h\u01a1n khi tr\u1ebb nh\u1eadp sai.<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udccc <strong>Kh\u00f3 h\u01a1n \u0111\u1ec3 th\u1eed th\u00e1ch:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>T\u0103ng s\u1ed1 l\u01b0\u1ee3ng m\u1ea3nh gh\u00e9p (v\u00ed d\u1ee5: t\u1eeb 1 \u0111\u1ebfn 16).<\/li>\n\n\n\n<li>Th\u00eam b\u1ed9 \u0111\u1ebfm th\u1eddi gian \u0111\u1ec3 t\u1ea1o \u00e1p l\u1ef1c nh\u1eb9.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udcca <strong>Tr\u1ebb h\u1ecdc \u0111\u01b0\u1ee3c g\u00ec t\u1eeb tr\u00f2 ch\u01a1i n\u00e0y?<\/strong><\/h3>\n\n\n\n<p>\u2705 <strong>T\u01b0 duy gi\u1ea3i quy\u1ebft v\u1ea5n \u0111\u1ec1:<\/strong> \u0110\u01b0a ra chi\u1ebfn l\u01b0\u1ee3c th\u1eed v\u00e0 s\u1eeda sai \u0111\u1ec3 ho\u00e0n th\u00e0nh m\u1ee5c ti\u00eau.<\/p>\n\n\n\n<p>\u2705 <strong>T\u00ednh ki\u00ean nh\u1eabn:<\/strong> Kh\u00f4ng n\u1ea3n l\u00f2ng khi g\u1eb7p kh\u00f3 kh\u0103n, t\u1eadp trung v\u00e0o m\u1ee5c ti\u00eau cu\u1ed1i c\u00f9ng.<\/p>\n\n\n\n<p>\u2705 <strong>K\u1ef9 n\u0103ng l\u1eadp tr\u00ecnh:<\/strong> L\u00e0m quen v\u1edbi kh\u00e1i ni\u1ec7m <strong>x\u00e1o tr\u1ed9n (shuffle)<\/strong>, <strong>v\u00f2ng l\u1eb7p (loop)<\/strong> v\u00e0 <strong>\u0111i\u1ec1u ki\u1ec7n ki\u1ec3m tra (if-else)<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Trong th\u1eddi \u0111\u1ea1i c\u00f4ng ngh\u1ec7 ph\u00e1t tri\u1ec3n, vi\u1ec7c cho tr\u1ebb l\u00e0m quen v\u1edbi l\u1eadp tr\u00ecnh t\u1eeb s\u1edbm gi\u00fap c\u00e1c em ph\u00e1t tri\u1ec3n t\u01b0 duy logic v\u00e0 kh\u1ea3 n\u0103ng gi\u1ea3i quy\u1ebft v\u1ea5n \u0111\u1ec1&#8230;.<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-359","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=359"}],"version-history":[{"count":1,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/359\/revisions"}],"predecessor-version":[{"id":362,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/359\/revisions\/362"}],"wp:attachment":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}