一個簡單的猜單詞的小遊戲

忘了從哪看到的這遊戲,睡覺前想起來就立馬寫了個,
沒什麼好的思路,樣式什麼的也就沒追求了,
用的SESSION配合AJAX弄的,
小碼農,求建議指導,
PS:沒怎麼優化,勿噴哈,
直接複製本地就能運行了,



001. <?php
002. session_start();
003. header("Content-type:text/html;charset=utf-8");
004. $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
005. function get_word(){
006. $wordtext="Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of Yii Software LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.";
007. $words=preg_split("/[\s,]+/",$wordtext);
008. do{
009. $i=rand(0,count($words)-1);
010. $word=strtoupper($words[$i]);
011. }while(strlen($word)<5 || !ctype_alpha($word));
012. return $word;
013. }
014. function guess($word){
015. return str_repeat('_',strlen($word));
016. }
017. function output($word){
018. $str='';
019. for($i=0;$i<strlen($word);$i++){
020. $str.=$word[$i]."&nbsp;";
021. }
022. return rtrim($str);
023. }
024. if(isset($_GET['op']) && $_GET['op']=='start'){
025. $k=$_GET['k'];
026. $_SESSION['num']=$k;
027. exit;
028. }
029. if(isset($_GET['restart']) && $_GET['restart']==1){
030. session_unset();
031. header("location:$url");
032. exit;
033. }
034. if(!isset($_SESSION['word'])){
035. $word=get_word();
036. $_SESSION['word']=$word;
037. }else{
038. $word=$_SESSION['word'];
039. }
040. $guessguess_word=guess($word);
041. if(isset($_GET['op']) && $_GET['op']=='ajax'){
042. $k=$_GET['k'];
043. $re='';
044. if(!isset($_SESSION['already'])){
045. $_SESSION['already']=guess($_SESSION['word']);
046. }
047. if(!isset($_SESSION['count'])){
048. $_SESSION['count']=0;
049. }
050. if(strpos($_SESSION['word'],$k)!==false){
051. for($i=0;$i<strlen($_SESSION['word']);$i++){
052. if($_SESSION['word'][$i]!=$k){
053. $re.='_';
054. }else{
055. $re.=$_SESSION['word'][$i];
056. }
057. }
058. for($i=0;$i<strlen($_SESSION['word']);$i++){
059. if($_SESSION['already'][$i]=='_'){
060. $_SESSION['already'][$i]=$re[$i];
061. }
062. }
063. }else{
064. $_SESSION['count']+=1;
065. }
066. $return=output($_SESSION['already']);
067. if($_SESSION['count'] <= $_SESSION['num']){
068. if($_SESSION['already']==$_SESSION['word']){
069. $return.="|1";
070. }else{
071. $return.="|0";
072. }
073. }else{
074. $return.="|2";
075. }
076. echo $return;
077. exit;
078. }
079. ?>
080. <script>
081. function Ajax(recvType){
082. var aj=new Object();
083. aj.recvType=recvType ? recvType.toUpperCase() : 'HTML';
084. aj.targetUrl='';
085. aj.sendString='';
086. aj.resultHandle=null;
087. aj.createXMLHttpRequest=function(){
088. var xmlHttp = false;
089. if(window.XMLHttpRequest){
090. xmlHttp = new XMLHttpRequest();
091. }else if(window.ActiveXObject){
092. try{
093. xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
094. }catch(error1){
095. try{
096. xmlHttp = new ActiveXobject("Microsoft.XMLHttp");
097. }catch(error2){
098. xmlHttp = false;
099. }
100. }
101. }
102. return xmlHttp;
103. }
104. aj.XMLHttpRequest=aj.createXMLHttpRequest();
105. aj.processHandle=function(){
106. if(aj.XMLHttpRequest.readyState == 4){
107. if(aj.XMLHttpRequest.status == 200){
108. if(aj.recvType=="HTML"){
109. aj.resultHandle(aj.XMLHttpRequest.responseText);
110. }else if(aj.recvType=="XML"){
111. aj.resultHandle(aj.XMLHttpRequest.responseXML);
112. }
113. }
114. }
115. }
116. aj.get=function(targetUrl, resultHandle){
117. aj.targetUrl=targetUrl;
118. if(resultHandle!=null){
119. aj.XMLHttpRequest.onreadystatechange=aj.processHandle;
120. aj.resultHandle=resultHandle;
121. }
122. if(window.XMLHttpRequest){
123. aj.XMLHttpRequest.open("get", aj.targetUrl);
124. aj.XMLHttpRequest.send(null);
125. }else{
126. aj.XMLHttpRequest.open("get", aj.targetUrl, true);
127. aj.XMLHttpRequest.send();
128. }
129. }
130. return aj;
131. }
132. </script>
133. <script>
134. var ajax=Ajax();
135. function select(v){
136. document.getElementById(v).style.display='none';
137. ajax.get("guess.php?op=ajax&k="+v, function(r){
138. var t=r.split("|");
139. document.getElementById('word').innerHTML=t[0];
140. if(t[1] == 1){
141. document.getElementById('select').style.display='none';
142. document.getElementById('result').innerHTML='成功';
143. }else if(t[1] == 2){
144. document.getElementById('select').style.display='none';
145. document.getElementById('result').innerHTML='失敗';
146. }
147. });
148. }
149. function check(v){
150. ajax.get("guess.php?op=start&k="+v, function(r){
151. window.location.href="<?phpecho $url;?>";
152. });
153. }
154. </script>
155. <?php
156. if(!isset($_SESSION['num'])){
157. echo '<inputtype="radio"onclick="check(10);"/> easy: wrong 10<br/>';
158. echo '<inputtype="radio"onclick="check(5);"/> normal: wrong 5<br/>';
159. echo '<inputtype="radio"onclick="check(3);"/> hard: wrong 3<br/>';
160. }else{
161. //echo $word;
162. echo "最多能夠猜錯 ".$_SESSION['num']." 次";
163. echo "<br/>";
164. echo "<divid='word'>";
165. echo output($guess_word);
166. echo "</div>";
167. echo '<br/>';
168. echo '<divid="select">';
169. for($i=ord('A');$i<=ord('Z');++$i){
170. echo "\n";
171. $letter=chr($i);
172. echo '<spanid="'.$letter.'"style="display:"><ahref="javascript:void(0);"onclick="select(\''.$letter.'\');">'.$letter.'</a></span>';
173. }
174. echo '</div>';
175. echo '<br/>';
176. echo '<br/>';
177. echo '<divid="result"></div>';
178. echo '<br/>';
179. echo '<br/>';
180. echo '<ahref="'.$url.'?restart=1">從新開始</a>';
181. }
182. ?>
相關文章
相關標籤/搜索