在無 WebSocket、無 SignalR、無 Node.js 的經典 ASP 環境下,利用 Application 物件 跨 Session 共享棋盤狀態,配合 頁面自動重新整理 (<meta http-equiv="refresh" content="3">) 實現雙人輪流落子的五子棋對戰。雖非即時推送架構,但邏輯完整、程式碼精簡,極適合理解 Web 狀態管理本質與演算法實作。
| 鍵值模式 | 用途 | 範例 |
|---|---|---|
first{class} | 擂主 (黑棋) Session("nice") | firstA1B2C3 |
second{class} | 挑戰者 (白棋) Session("nice") | secondA1B2C3 |
pos{class}{i}_{j} | 棋盤格狀態 (True=已落子) | posA1B2C38_8 |
color{class}{i}_{j} | 棋子顏色 ("black"/"white") | colorA1B2C38_8 |
{playerName} | 輪到該玩家落子旗標 (True/False) | JohnDoe = True |
{class} 為 Session("class"),作為房間識別碼;Session("nice") 為玩家暱稱。
<%
' 房間識別
room = Session("class")
me = Session("nice")
' 顯示雙方
Response.Write "擂主: " & Application("first" & room) & " 攻擂: " & Application("second" & room)
' 處理落子請求 ?pos=8_8
If Request.QueryString("pos") <> "" Then
Application("pos" & room & Request.QueryString("pos")) = True
End If
' 角色判定與顏色指派
If Application("first" & room) = me Then
color = "black"
' 輪流邏輯:記錄上一手 last,避免重複落子
If Request.QueryString("pos") <> "" Then
If Session("last") = "" Then Session("last") = "abc"
If Session("last") <> Request.QueryString("pos") Then
Application(me) = False ' 自己結束回合
If Application("second" & room) <> "" Then
Application(Application("second" & room)) = True ' 對方開始回合
End If
Session("last") = Request.QueryString("pos")
End If
End If
ElseIf Application("second" & room) = me Then
color = "white"
' 同理白棋輪流邏輯
' ...
ElseIf Application("second" & room) = "" Then
' 第二位玩家加入成為挑戰者
Application("second" & room) = me
color = "white"
' ... 初始化輪流旗標
Else
color = "" ' 觀戰者
End If
%>
Response.Write "
' 判定變數初始化
If Application("first" & room) = me Then
m = "black" : h = "white"
Else
m = "white" : h = "black"
End If
' 通用掃描函數 (簡化示意)
Function CheckFive(dirX, dirY)
For startI = 1 To 15
For startJ = 1 To 15
fiveMe = 0 : fiveHe = 0
For step = 0 To 4
x = startI + step * dirX
y = startJ + step * dirY
If x < 1 Or x > 15 Or y < 1 Or y > 15 Then Exit For
c = Application("color" & room & x & "_" & y)
If c = m Then fiveMe = fiveMe + 1 : fiveHe = 0
ElseIf c = h Then fiveHe = fiveHe + 1 : fiveMe = 0
Else fiveMe = 0 : fiveHe = 0
End If
If fiveMe = 5 Then CheckFive = "win" : Exit Function
If fiveHe = 5 Then CheckFive = "loss" : Exit Function
Next
Next
Next
CheckFive = "none"
End Function
' 四方向檢查
result = CheckFive(0, 1) ' 橫向
If result = "none" Then result = CheckFive(1, 0) ' 縱向
If result = "none" Then result = CheckFive(1, 1) ' 主對角線 \
If result = "none" Then result = CheckFive(1, -1) ' 副對角線 /
If result = "win" Then
Response.Write "你贏了!"
Application("first" & room) = "" : Application("second" & room) = "" ' 重置房間
ElseIf result = "loss" Then
Response.Write "對不起,你輸了!"
Application("first" & room) = "" : Application("second" & room) = ""
End If
<meta http-equiv="refresh" content="3"> 每 3 秒重載頁面,模擬「推送」效果。現代替代方案:AJAX 長輪詢 / WebSocket / SignalR / Server-Sent Events。Session("last") 記錄上一手座標,防止重複提交同一座標導致邏輯錯亂。i_j 字串鍵值模擬二維陣列,ASP Application 物件不支援真正多維陣列。game.asp # 主頁面:角色分配、棋盤渲染、勝負判定、Meta Refresh
include/conn.asp # (可選) 資料庫連線,若需持久化對戰記錄
style.css # 棋盤、棋子、訊息樣式
這個 ASP 五子棋範例雖簡陋,卻完整展示了無狀態 HTTP 協議下如何用伺服器端共享記憶體 (Application) 實現多人即時互動,以及棋類遊戲勝負判定的經典四向掃描演算法。理解其原理與侷限,是邁向現代即時 Web 應用架構的優秀踏腳石。