-- ÄÄÇ»ÅÍ°¡ ¼±ÅÃÇÑ °øÀ» ¾Ë·Á ÁØ´Ù. function func_selected_ball( t ) for i, v in pairs( t ) do print( i, v ) end end -- ÀÚ¸´¼ö¿Í ¸Â´Â°ÍÀ» ÀúÀåÇÑ´Ù. function func_ball_posmatch( t1, t2 ) local match_count = 0 for i = 1, 4, 1 do if t1[i] == t2[i] then match_count = match_count + 1 end end return match_count end -- °°Àº °ªÀÌ ÀÖ´Ù¸é, 0ÀÌ ¾Æ´Ñ À§Ä¡¸¦ ¸®ÅÏÇØ ÁØ´Ù. function func_ball_elementmatch_impl( str, t2 ) local courser = 0 for i = 1, 4, 1 do if str == t2[i] then courser = i break end end return courser end -- ¸î°³°¡ °°ÀºÁö ¸®ÅÏÇØ ÁØ´Ù. function func_ball_elementmatch( t1, t2 ) local ta = { t1[1], t1[2], t1[3], t1[4] } local tb = { t2[1], t2[2], t2[3], t2[4] } local element_match = 0 for i = 1, 4, 1 do pos = func_ball_elementmatch_impl( ta[i - element_match ], tb ) if 0 ~= pos then table.remove( ta, i - element_match ) table.remove( tb, pos ) element_match = element_match + 1 end end return element_match end -- ¹®ÀÚ¿­À» ´ë¹®ÀÚ·Î function computer_setting_table() math.randomseed( os.time() ) print( math.random( 4 ) .. "¸¶½ºÅÍ ¸¶ÀÎµå °ÔÀÓÀ» ½ÃÀÛÇÕ´Ï´Ù." ) local ball_color = { "R", "G", "B", "Y" } local com_select_ball = { ball_color[math.random( 4 )], ball_color[math.random( 4 )], ball_color[math.random( 4 )], ball_color[math.random( 4 )], } return com_select_ball end function user_setting_table() str = io.read() -- ¹®ÀÚ¿­À» ÀÔ·Â ¹Þ´Â´Ù. str = string.upper( str ) -- strÀ» ´ë¹®ÀÚ·Î ¹Ù²Û´Ù. if 4 ~= string.len( str ) then return nil end user_select_ball = { string.sub(str, 1, 1), string.sub(str, 2, 2), string.sub(str, 3, 3), string.sub(str, 4, 4) } return user_select_ball end function gaming( count ) local com_table = computer_setting_table() print( "ÃÑ 100¹øÀÇ ½Ãµµ ¾È¿¡ ¸¶½ºÅÍ ¸¶ÀÎµå °ÔÀÓÀ» Ŭ¸®¾î ÇÏÁö ¸øÇϽøé Áø °Í ÀÔ´Ï´Ù." ) print( "°ø »öÀº 'R','G','B','Y' ÀÔ´Ï´Ù. ") for i = 1, count, 1 do local user_table = user_setting_table() if nil == user_table then print("Ç×»ó °ªÀº 4°³¸¸ ¹ÞÀ» ¼ö ÀÖ½À´Ï´Ù.") else match_ball = func_ball_posmatch( com_table, user_table ) element_match_ball = func_ball_elementmatch( com_table, user_table ) print( "ÀÚ¸´¼ö±îÁö ¸Â´Â ¿ø¼Ò °¹¼ö : ", match_ball ) print( "¸ÂÃá ¿ø¼Ò °¹¼ö : ", element_match_ball ) if 8 == match_ball + element_match_ball then print("ÃàÇÏÇÕ´Ï´Ù. ´ä¿¡ µµ´ÞÇÏ¿´½À´Ï´Ù.") return true, com_table else print("¾ÈŸ°©½À´Ï´Ù. ´Ù½Ã ½Ãµµ Çϼ¼¿ä.") end end end return false, com_table end local iswin, com_table = gaming( 30 ) if iswin == false then print( "ÄÄÇ»ÅÍ¿¡°Ô Áö¼Ì½À´Ï´Ù. ¤»¤»¤»" ) end print( "ÄÄÇ»ÅÍ°¡ ¼±ÅÃÇÑ °ø" ) func_selected_ball( com_table )