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