⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0223.htm

📁 JspServlet教程专栏 对javaservlet讲述的非常详细
💻 HTM
字号:
<html>

<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1  {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>一个jsp编写的小游戏程序</strong></big></p>
<div align="right">---摘自互联网</div>

<br>&lt;%@&nbsp; page&nbsp; language=javascript&nbsp; %>&lt;%!<br>
/**<br>
*&nbsp; Simple&nbsp; implementation&nbsp; of&nbsp; tic-tac-toe&nbsp; illustrating<br>
*&nbsp; dividing&nbsp; implementation&nbsp; into&nbsp; files.<br>
*<br>
*&nbsp; The&nbsp; session&nbsp; variable&nbsp; stores:<br>
*&nbsp; user_score&nbsp; --&nbsp; the&nbsp; user's&nbsp; score<br>
*&nbsp; comp_score&nbsp; --&nbsp; the&nbsp; computer's&nbsp; score<br>
*&nbsp; state&nbsp; --&nbsp; the&nbsp; state&nbsp; of&nbsp; the&nbsp; game&nbsp; stored&nbsp; as&nbsp; an&nbsp; array.<br>
*&nbsp; --&nbsp; 0&nbsp; =&nbsp; no&nbsp; move,&nbsp; 'x'&nbsp; user,&nbsp; 'o'&nbsp; computer<br>
*<br>
*&nbsp; User&nbsp; input&nbsp; comes&nbsp; through&nbsp; the&nbsp; query&nbsp; string:<br>
*&nbsp; ?move=n<br>
*/<br>
<br>
/*<br>
*&nbsp; Exception&nbsp; handling&nbsp; is&nbsp; done&nbsp; by&nbsp; error.jsp.<br>
*<br>
*&nbsp; All&nbsp; calculations&nbsp; are&nbsp; done&nbsp; before&nbsp; creating&nbsp; any&nbsp; of&nbsp; the&nbsp; output&nbsp; so&nbsp; the<br>
*&nbsp; error&nbsp; handling&nbsp; doesn't&nbsp; mix&nbsp; up&nbsp; generated&nbsp; text&nbsp; and&nbsp; error&nbsp; text.<br>
*/<br>
<br>
import&nbsp; score;&nbsp; //&nbsp; import&nbsp; the&nbsp; functions&nbsp; in&nbsp; score.es<br>
<br>
var&nbsp; state&nbsp; =&nbsp; session.value.state;<br>
/*<br>
*&nbsp; If&nbsp; the&nbsp; player&nbsp; hasn't&nbsp; played&nbsp; before,&nbsp; initialize&nbsp; the&nbsp; state.<br>
*/<br>
if&nbsp; (!&nbsp; state)&nbsp; {<br>
session.value.user_score&nbsp; =&nbsp; 0;<br>
session.value.comp_score&nbsp; =&nbsp; 0;<br>
session.value.state&nbsp; =&nbsp; [0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0];<br>
state&nbsp; =&nbsp; session.value.state;<br>
}<br>
/*<br>
*&nbsp; If&nbsp; no&nbsp; moves&nbsp; are&nbsp; available,&nbsp; reinitialize&nbsp; the&nbsp; state.<br>
*/<br>
for&nbsp; (i&nbsp; =&nbsp; 0;&nbsp; i&nbsp; &lt;&nbsp; state.length;&nbsp; i++)&nbsp; {<br>
if&nbsp; (state&nbsp; ==&nbsp; 0)<br>
break;<br>
}<br>
if&nbsp; (i&nbsp; ==&nbsp; state.length)&nbsp; {<br>
state&nbsp; =&nbsp; session.value.state&nbsp; =&nbsp; [0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0];<br>
}<br>
<br>
var&nbsp; move&nbsp; =&nbsp; request.form.move;<br>
/*<br>
*&nbsp; Response&nbsp; stores&nbsp; the&nbsp; message&nbsp; to&nbsp; tell&nbsp; the&nbsp; user&nbsp; based&nbsp; on&nbsp; the&nbsp; state<br>
*&nbsp; change,&nbsp; e.g.&nbsp; win,&nbsp; lose,&nbsp; bad&nbsp; move.<br>
*/<br>
var&nbsp; response&nbsp; =&nbsp; null;<br>
<br>
/*<br>
*&nbsp; Interpret&nbsp; the&nbsp; user's&nbsp; move&nbsp; and&nbsp; calculate&nbsp; the&nbsp; computer's&nbsp; move.<br>
*<br>
*&nbsp; The&nbsp; AI&nbsp; behind&nbsp; the&nbsp; computer's&nbsp; move&nbsp; is&nbsp; in&nbsp; score.es.<br>
*/<br>
if&nbsp; (!&nbsp; move)&nbsp; {<br>
}<br>
else&nbsp; if&nbsp; (move&nbsp; >=&nbsp; 0&nbsp; &&&nbsp; move&nbsp; &lt;=&nbsp; 8)&nbsp; {<br>
if&nbsp; (state[move]&nbsp; ==&nbsp; 0)&nbsp; {<br>
state[move]&nbsp; =&nbsp; 'x';<br>
<br>
if&nbsp; (isUserWin(score(state)))&nbsp; {<br>
response&nbsp; =&nbsp; "user_win";<br>
session.value.user_score++;<br>
session.value.state&nbsp; =&nbsp; [0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0];<br>
}<br>
else&nbsp; if&nbsp; (isComputerWin(final&nbsp; =&nbsp; computer_move(state)))&nbsp; {<br>
response&nbsp; =&nbsp; "computer_win";<br>
session.value.comp_score++;<br>
session.value.state&nbsp; =&nbsp; [0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0];<br>
}&nbsp; else&nbsp; if&nbsp; (isTie(final))&nbsp; {<br>
response&nbsp; =&nbsp; "tie";<br>
session.value.state&nbsp; =&nbsp; [0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0,&nbsp; 0];<br>
}<br>
}<br>
else&nbsp; {<br>
response&nbsp; =&nbsp; "bad_move";<br>
}<br>
}&nbsp; else&nbsp; {<br>
writeln("move:&nbsp; "&nbsp; +&nbsp; move);<br>
response&nbsp; =&nbsp; "bad_move";<br>
}<br>
<br>
/**<br>
*&nbsp; HTML&nbsp; for&nbsp; a&nbsp; single&nbsp; square.<br>
*<br>
*&nbsp; Legal&nbsp; moves&nbsp; are&nbsp; actually&nbsp; links&nbsp; with&nbsp; the&nbsp; proper&nbsp; move,&nbsp; e.g.&nbsp; the&nbsp; center<br>
*&nbsp; square&nbsp; would&nbsp; be:<br>
*<br>
*&nbsp; &lt;td>&lt;a&nbsp; href='tictactoe.jsp?move=5'>&nbsp; &lt;/a><br>
*/<br>
function&nbsp; square(index)<br>
{<br>
var&nbsp; value&nbsp; =&nbsp; state[index];<br>
<br>
if&nbsp; (value&nbsp; ==&nbsp; 'x')&nbsp; {<br>
out.write("&lt;td&nbsp; style='color:blue'>X&lt;/td>");<br>
}<br>
else&nbsp; if&nbsp; (value&nbsp; ==&nbsp; 'o')&nbsp; {<br>
out.write("&lt;td&nbsp; style='color:red'>O&lt;/td>");<br>
}<br>
else&nbsp; {<br>
out.write('&lt;td>&lt;a&nbsp; href="tictactoe.jsp?move=',&nbsp; index,&nbsp; '">&nbsp; &lt;/a>&lt;/td>');<br>
}<br>
}<br>
<br>
/**<br>
*&nbsp; HTML&nbsp; for&nbsp; the&nbsp; entire&nbsp; board.<br>
*/<br>
function&nbsp; board()<br>
{<br>
out.writeln("&lt;table&nbsp; border&nbsp; cellpadding=3>");<br>
out.write("&lt;tr>");&nbsp; square(0);&nbsp; square(1);&nbsp; square(2);&nbsp; out.writeln();<br>
out.write("&lt;tr>");&nbsp; square(3);&nbsp; square(4);&nbsp; square(5);&nbsp; out.writeln();<br>
out.write("&lt;tr>");&nbsp; square(6);&nbsp; square(7);&nbsp; square(8);&nbsp; out.writeln();<br>
out.writeln("&lt;/table>");<br>
}<br>
<br>
/*<br>
*&nbsp; Now&nbsp; that&nbsp; all&nbsp; calculations&nbsp; are&nbsp; done,&nbsp; we&nbsp; can&nbsp; safely&nbsp; create&nbsp; the&nbsp; page.<br>
*/<br>
%><br>
&lt;html>&lt;head>&lt;title>TicTacToe&lt;/title>&lt;/head><br>
&lt;body&nbsp; bgcolor=white><br>
<br>
&lt;!--&nbsp; Easy&nbsp; way&nbsp; to&nbsp; get&nbsp; back&nbsp; without&nbsp; unwinding&nbsp; the&nbsp; state.&nbsp; --><br>
&lt;a&nbsp; href='../index.xtp'>[back]&lt;/a><br>
<br>
&lt;!--&nbsp; Tell&nbsp; the&nbsp; player&nbsp; how&nbsp; well&nbsp; she's&nbsp; doing.&nbsp; --><br>
<br>
&lt;table><br>
&lt;tr>&lt;th>You:&lt;td>&lt;%=&nbsp; session.value.user_score&nbsp; %><br>
&lt;tr>&lt;th>Computer:&lt;td>&nbsp; &lt;%=&nbsp; session.value.comp_score&nbsp; %><br>
&lt;/table><br>
<br>
&lt;%<br>
<br>
/*<br>
*&nbsp; Prompt&nbsp; based&nbsp; on&nbsp; the&nbsp; state&nbsp; of&nbsp; the&nbsp; board.<br>
*/<br>
switch&nbsp; (response)&nbsp; {<br>
case&nbsp; "user_win":<br>
%>&lt;h1>You&nbsp; Win!&lt;/h1>&lt;%<br>
board();<br>
%>&lt;h1>New&nbsp; Game:&lt;/h1>&lt;%<br>
break;<br>
<br>
case&nbsp; "computer_win":<br>
%>&lt;h1>You&nbsp; Lose!&lt;/h1>&lt;%<br>
board();<br>
%>&lt;h1>New&nbsp; Game:&lt;/h1>&lt;%<br>
break;<br>
<br>
case&nbsp; "tie":<br>
%>&lt;h1>Tie&nbsp; game!&lt;/h1>&lt;%<br>
board();<br>
%>&lt;h1>New&nbsp; Game:&lt;/h1>&lt;%<br>
break;<br>
<br>
case&nbsp; "bad_move":<br>
%>&lt;h1>Bad&nbsp; Move.&lt;/h1>&lt;%<br>
break;<br>
<br>
default:<br>
%>&lt;h1>Your&nbsp; Move.&lt;/h1>&lt;%<br>
break;<br>
}<br>
<br>
state&nbsp; =&nbsp; session.value.state;<br>
<br>
/*<br>
*&nbsp; draw&nbsp; the&nbsp; board<br>
*/<br>
board();<br>
%><br>
<br>
&lt;/body>&lt;/html>

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -