PHP 로또 비지니스로직 : lotto 1 to 45
728x90
반응형
로또 함수
<?php /** * registered date 20100502 * programmed by Seok Kyun. Choi. 최석균 * http://syaku.tistory.com */ // 로또 번호의 배열로 받기 function _lotto_num_array($num) { $num_ar = split(',',$num); $cnt = 0; for ($i = 0; $i < sizeof($num_ar); $i++ ) { if ($num_ar[$i] != "") { $array[$cnt] = $num_ar[$i]; $cnt++; } } return $array; } // 로또 정렬 function _lotto_sort($array) { sort($array); $ret = ""; foreach ($array as $key => $val) { $ret .= "," . $val; } return $ret; } // 로또 자동추첨 function _auto_num() { $array = array('01','02','03','04','05','06','07','08','09',10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45); $rand = rand(0,44); $num1 = $array[$rand]; array_splice($array,$rand,1); $rand = rand(0,43); $num2 = $array[$rand]; array_splice($array,$rand,1); $rand = rand(0,42); $num3 = $array[$rand]; array_splice($array,$rand,1); $rand = rand(0,41); $num4 = $array[$rand]; array_splice($array,$rand,1); $rand = rand(0,40); $num5 = $array[$rand]; array_splice($array,$rand,1); $rand = rand(0,39); $num6 = $array[$rand]; array_splice($array,$rand,1); $num = "," . $num1 . "," . $num2 . "," . $num3 . "," . $num4 . "," . $num5 . "," . $num6; return $num; } // 로또번호 자동추첨 : 중복 번호 방지함. function _auto_num_proc($lotto) { if ($lotto == "" || $lotto == 0 || $lotto == null) return false; $conn = new connection(); $commit = true; while($commit) { $num = _auto_num(); $lotto_num_array = _lotto_num_array($num); $lotto_num_array_sort = _lotto_sort($lotto_num_array); // 중복된 로또번호 체크 참여자 상관없이 5개까지 유효함 $conn->query = "select count(rdate) from xJackpot where number = " . $lotto . " and lotto_num = '" . $lotto_num_array_sort . "'"; $rs = $conn->object('array'); $lotto_num_count = $rs[0]; $conn->destroy(); if ($lotto_num_count == 0) { $commit = false; $ret = $num; } } $conn->close(); return $ret; } // 로또 담첨자 선정을 위한 동일 번호 검색기 function _lotto_seach($user_num_array,$sys_num_array) { $cnt = 0; for ($i = 0; $i < sizeof($user_num_array); $i++ ) { // 동일번호 5개 $user = $user_num_array[$i]; for ($ii = 0; $ii < sizeof($sys_num_array); $ii++ ) { // 동일번호 5개 $sys = $sys_num_array[$ii]; if ($user == $sys) { $cnt++; } } } return $cnt; } // 로또 당첨자 선정 function _lotto_vip($sys_num,$bonus,$user_num) { // 참여자 $user_num_array = _lotto_num_array($user_num); $user_num_array_sort = _lotto_sort($user_num_array); // 당첨번호 $sys_num_array = _lotto_num_array($sys_num); $sys_num_array_sort = _lotto_sort($sys_num_array); $success = false; // 1등 if($user_num_array_sort == $sys_num_array_sort) { $success = true; return 1; } $cnt = _lotto_seach($user_num_array,$sys_num_array); // 2등 if (strstr($user_num,$bonus) != "") { // 보너스 번호 검색 if ($cnt == 5) { $success = true; return 2; } } // 3등 if ($cnt == 4) { $success = true; return 3; } // 4등 if ($cnt == 3) { $success = true; return 4; } // 5등 if ($cnt == 2) { $success = true; return 5; } return 0; } ?> // 예제 <?php $user_num = ",01,03,04,43,02,05"; $lotto_num = ",01,04,22,12,33,03"; $lotto_num_7 = "07"; $vip = _lotto_vip($lotto_num,$lotto_num_7,$user_num); echo $vip . " 등"; ?>
728x90
반응형
'개발노트 > PHP' 카테고리의 다른 글
PHP 문법 정리 : 자주 사용하는 PHP (0) | 2014.05.23 |
---|---|
PHP 배열 조합 함수 : jQuery extend for PHP (0) | 2014.04.11 |
PHP json2xml 사용법 및 XE 로그인연동 : json2xml , XpressEngine (0) | 2010.10.12 |
PHP 디비 커넥션 : Database Connection (4) | 2010.06.09 |