純 ASP + HTML/JavaScript 實作的網頁版五子棋,無需安裝外掛,支援多房間 (五個星球戰場)、擂主/挑戰者雙角色、回合制落子、勝負判定。核心技術:Application 物件共享遊戲狀態、Session 識別玩家身分、表格佈局棋盤、圖片切換顯示棋子。
| 變數名 | 用途 |
| first1 ~ first5 | 五個戰場的擂主暱稱 (空字串=無人) |
| second1 ~ second5 | 五個戰場的挑戰者暱稱 |
| board1 ~ board5 | 各戰場棋盤狀態 (字串編碼或陣列) |
| turn1 ~ turn5 | 當前回合 (1=黑/擂主, 2=白/挑戰者) |
關鍵程式碼片段
index.asp - 大廳顯示邏輯
<%@ Language=VBScript %>
<table border=1 width=80% align=center>
<tr><th>星球</th><th>擂主</th><th>狀態</th><th>挑戰者</th></tr>
<%
Dim planets, i
planets = Array("金星","木星","水星","火星","土星")
For i = 1 To 5
Response.Write "| " & planets(i-1) & " | "
' 擂主欄
If Application("first" & i) = "" Then
Response.Write " | "
Response.Write "待機中 | "
Else
Response.Write "" & Application("first" & i) & " | "
Response.Write "對戰中 | "
End If
' 挑戰者欄
If Application("second" & i) = "" Then
Response.Write " | "
Else
Response.Write "" & Application("second" & i) & " | "
End If
Response.Write "
"
Next
%>
</table>
index0.asp - 身分驗證與狀態寫入
<%@ Language=VBScript %>
<%
Dim flag, nickname
flag = Request.QueryString("flag") ' First1, Second2, ...
nickname = Trim(Request.Form("txt" & flag))
If nickname = "" Then Response.Redirect "index.asp"
' 檢查該位置是否仍空閒
If Application(flag) = "" Then
Application.Lock
Application(flag) = nickname
Application.Unlock
Session("playerName") = nickname
Session("playerRole") = IIf(InStr(flag,"First")>0, "host", "guest")
Session("battleID") = Right(flag, 1) ' 1~5
Response.Redirect "match.asp"
Else
Response.Write "該位置已被佔用,返回大廳"
End If
%>
match.asp - 棋盤渲染與落子處理 (核心邏輯簡述)
<%@ Language=VBScript %>
<%
Dim bid, role, myName, opponent
bid = Session("battleID") ' 1~5
role = Session("playerRole") ' host/guest
myName = Session("playerName")
' 簡易棋盤狀態:用字串 "0,0,0,1,2,..." 表示 15x15=225 格
' 0=空, 1=黑(擂主), 2=白(挑戰者)
Dim boardKey, boardStr, cells
boardKey = "board" & bid
boardStr = Application(boardKey)
If boardStr = "" Then boardStr = String(225*2-1, "0") ' "0,0,0..." 以逗號分隔
cells = Split(boardStr, ",")
' 處理落子請求 (假設用 QueryString x,y)
Dim x, y, idx, currentPlayer
x = CInt(Request("x")) : y = CInt(Request("y"))
If x >= 0 And x < 15 And y >= 0 And y < 15 Then
idx = y * 15 + x
currentPlayer = IIf(role="host", 1, 2)
' 簡易驗證:輪到自己、該格為空
If CInt(Application("turn" & bid)) = currentPlayer And cells(idx) = "0" Then
cells(idx) = CStr(currentPlayer)
Application.Lock
Application(boardKey) = Join(cells, ",")
Application("turn" & bid) = IIf(currentPlayer=1, 2, 1)
Application.Unlock
End If
End If
' --- 以下輸出 HTML 棋盤 ---
%>
<table border=0 cellspacing=0 cellpadding=0>
<%
Dim r, c, cellVal, imgSrc
For r = 0 To 14
Response.Write ""
For c = 0 To 14
cellVal = cells(r*15+c)
Select Case cellVal
Case "1": imgSrc = "black0.jpg"
Case "2": imgSrc = "white0.jpg"
Case Else: imgSrc = "back0.jpg"
End Select
Response.Write "" & _
" | "
Next
Response.Write "
"
Next
%>
</table>
<p>當前回合:<%=IIf(Application("turn"&bid)=1, "黑方(擂主)", "白方(挑戰者)")%>
你是:<%=IIf(role="host","黑方(擂主)","白方(挑戰者)")%></p>
勝負判定演算法 (簡述)
落子後以該點為中心,向四個方向 (水平、垂直、主對角、副對角) 連續計算同色棋子數,任一方向達 5 顆即獲勝。建議用 VBScript 函式封裝,在 match.asp 落子成功後呼叫。
擴充建議
- 加入 悔棋 功能:記錄每步棋盤快照至 Application 陣列
- 加入 觀戰模式:第三人進入時 Session 設為 "viewer",不參與落子
- 使用 WebSocket (SignalR/Node.js) 替代輪詢,實現即時推播
- 將棋盤狀態改存 資料庫,支援斷線重連與歷史回放
完整原始碼下載
本專案完整程式碼 (含圖片素材) 可參考經典 ASP 教學網站的「五子棋大賽」範例專案。
copyright ©
萬盛學電腦網 all rights reserved