熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用ASP實現網上“五子棋”大賽

2022-06-13   來源: .NET編程 

  “五子棋”是大家都非常熟悉的一種益智類游戲相關的游戲軟件也非常多在此我向大家隆重推出一款新的純“網絡版”的五子棋它利用ASP做為開發環境因此在Internet上對戰的時候無需下載客戶端軟件只要你有一個浏覽器就可以了你也可以在局域網上進行這個游戲

  對於ASP我不想做過多的說明在以前的文章中我已經說過很多

  游戲的過程是這樣的我們首先登錄然後可以選擇戰場有“金星”“木星”“水星”“火星”“土星”等五個戰場如果你自認為是一個高手可以選擇金星否則可以依次選擇其它幾個星球做為戰場如果這個星球上沒有擂主你就應該以擂主的身份進入然後別人就可以和你進行對戰了當然如果所有的星球上都已經開始了戰爭那麼你就只能等一會兒了

  如果你是以擂主身份進入的你可以等待別人來和你對戰如果你是以攻擂手的身份登錄你可以很快的和擂主展開戰斗開始戰斗後由擂主執黑先行然後由攻擂手出招如此這般當最後決出勝負的時候會有相應的提示

  該站點有幾個主要的文件是

  Indexasp 登錄文件

  Indexasp中間文件

  Matchasp 比賽文件

  Backjpg whitejpg blackjpg 中間用到的圖形文件分別指的棋盤白子和黑子如下圖

  在程序的初始化的時候將棋盤上分成若干的表格將背景圖片放入其中形成一個棋盤然後在放上一個棋子後再將圖片換上相應的白子或黑子就可以了

  先看看登錄文件

  <%@ Language=VBScript %>

  <HTML>

  <HEAD>

  <META NAME="GENERATOR" Content="Microsoft Visual Studio ">

  <title>星球大戰之“五子連珠”大賽!</title>

  </HEAD>

  <BODY>

  <p align=center><font size=>星球大戰</font></P>

  <table border= width=% align=center>

  <tr><td>星球</td><td>擂主</td><td>大戰</td><td>攻擂</td></tr>

  <tr>

  <td>金星</td>

  <td>

  <%

  if application("first")="" then

  %>

  <form action=indexasp?flag=First method=post>

  <INPUT id=text name=txtFirst size=><INPUT id=submit name=submit

  type=submit value=進入>

  </form>

  <%

  else

  responsewrite application("first")

  end if

  %>

  </td>

  <td>大戰</td>

  <td>

  <%

  if application("second")="" then

  %>

  <form action=indexasp?flag=second method=post>

  <INPUT id=text name=txtSecond size=><INPUT id=submit

  name=submit type=submit value=進入>

  </form>

  <%

  else

  responsewrite application("Second")

  end if

  %>

  </td>

  </tr>

  <tr>

  <td>木星</td>

  <td>

  <%

  if application("first")="" then

  %>

  <form action=indexasp?flag=First method=post>

  <INPUT id=text name=txtFirst size=><INPUT id=submit name=submit

  type=submit value=進入>

  </form>

  <%

  else

  responsewrite application("first")

  end if

  %>

  </td>

  <td>大戰</td>

  <td>

  <%

  if application("second")="" then

  %>

  <form action=indexasp?flag=second method=post>

  <INPUT id=text name=txtSecond size=><INPUT id=submit

  name=submit type=submit value=進入>

  </form>

  <%

  else

  responsewrite application("Second")

  end if

  %>

  </td>

  </tr>

  <tr>

  <td>金星</td>

  <td>

  <%

  if application("first")="" then

  %>

  <form action=indexasp?flag=First method=post>

  <INPUT id=text name=txtFirst size=><INPUT id=submit name=submit

  type=submit value=進入>

  </form>

  <%

  else

  responsewrite application("first")

  end if

  %>

  </td>

  <td>大戰</td>

  <td>

  <%

  if application("second")="" then

  %>

  <form action=indexasp?flag=second method=post>

  <INPUT id=text name=txtSecond size=><INPUT id=submit

  name=submit type=submit value=進入>

  </form>

  <%

  else

  responsewrite application("Second")

  end if

  %>

  </td>

  </tr>

  <tr>

  <td>金星</td>

  <td>

  <%

  if application("first")="" then

  %>

  <form action=indexasp?flag=First method=post>

  <INPUT id=text name=txtFirst size=><INPUT id=submit name=submit

  type=submit value=進入>

  </form>

  <%

  else

  responsewrite application("first")

  end if

  %>

  </td>

  <td>大戰</td>

  <td>

  <%

  if application("second")="" then

  %>

  <form action=indexasp?flag=second method=post>

  <INPUT id=text name=txtSecond size=><INPUT id=submit

  name=submit type=submit value=進入>

  </form>

  <%

  else

  responsewrite application("Second")

  end if

  %>

  </td>

  </tr>

  <tr>

  <td>金星</td>

  <td>

  <%

  if application("first")="" then

  %>

  <form action=indexasp?flag=First method=post>

  <INPUT id=text name=txtFirst size=><INPUT id=submit name=submit

  type=submit value=進入>

  </form>

  <%

  else

  responsewrite application("first")

  end if

  %>

  </td>

  <td>大戰</td>

  <td>

  <%

  if application("second")="" then

  %>

  <form action=indexasp?flag=second method=post>

  <INPUT id=text name=txtSecond size=><INPUT id=submit

  name=submit type=submit value=進入>

  </form>

  <%

  else

  responsewrite application("Second")

  end if

  %>

  </td>

  </tr> 

  </table>

  </BODY>

  </HTML>

  這個文件不用做過多的說明就是一個大的表格大家可以從那裡進入如果某個位置已經被使用則它會變成該大 俠的名字不允許重復登錄在登錄後進入

  indexasp這是一個中間交換文件在裡面進行一些變量的處理及賦值然後再進入matchasp進行正式的比賽我們來看一下indexasp的內容

  <%@ Language=VBScript %>

  <%

  if RequestForm("txt" & requestquerystring("flag"))<>"" then

  responsewrite "in!" & ""

  session("nice")=RequestForm("txt" & requestquerystring("flag"))

  responsewrite session("nice") & ""

  responsewrite application(requestquerystring("flag"))

  if application(requestquerystring("flag"))="" then

  application(requestquerystring("flag"))=session("nice")

  session("class")=right(requestquerystring("flag"))

  if left(requestquerystring("flag"))="f" then

  application(session("nice"))=false

  elseif left(requestquerystring("flag"))="s" then

  application(session("nice"))=false

  application(application("first" & session("class")))=true

  end if

  ResponseRedirect "matchasp"

  end if

  

  end if

  %>

  <HTML>

  </HTML>

  該程序中用session(“nice”)來記錄您登錄的大名以後的處理中很多都要用到它用它來區分很多的用戶如果您是從擂主的位置進入則您的權利是

  first否則是second用session("class")來記錄位於哪個戰場場“金”“木”

  “水”“火”“土”分別表示為因為有多個戰場可能同時開戰如果不能正確地區分開則可能導致一片混亂application(session("nice"))是一個邏輯型變量用真假來表示您是否可以出棋如果是假您要等待一個如果是真您就可以下棋了只有一個戰場上的兩個人都進入了以後擂主方可以下棋而且只有當擂主出子以後攻擂手才可以下棋准備好以後下面就進入賽場請看文件matchasp

  <html>

  <head>

  <meta httpequiv="ContentType" content="text/html; charset=gb">

  <meta name="GENERATOR" content="Microsoft FrontPage ">

  <meta name="ProgId" content="FrontPageEditorDocument">

  <META httpequiv=refresh content=

  <title>連珠大賽</title>

  </head>

  <%

  ResponseWrite "擂主:" & application("first" & session("class"))

  & "    攻擂" & application("second" & session

  ("class"))

  if RequestQueryString("pos")<>"" then

  application("pos" & session("class") & RequestQueryString("pos"))

  =true

  end if

  %>

  <body>

  <%

  ResponseWrite application("aaa")

  if application("first" & session("class"))=session("nice") then

  color="black"

  if RequestQueryString("pos")<>"" then

  if session("last")="" then session("last")="abc"

  if session("last")<> RequestQueryString("pos") then

  application(application("first" & session("class")))=false

  if application("second" & session("class"))<>"" then application

  (application("second" & session("class")))=true

  session("last")=RequestQueryString("pos")

  END IF

  end if

  else

  if application("second" & session("class"))=session("nice") then

  color="white"

  if RequestQueryString("pos")<>"" then

  if session("last")="" then session("last")="abc"

  if session("last")<> RequestQueryString("pos") then

  application(application("first" & session("class")))=true

  application(application("second" & session("class")))=false

  session("last")=RequestQueryString("pos")

  END IF

  end if

  else

  if application("second" & session("class"))="" then

  application("second" & session("class"))=session("nice")

  color="white"

  if RequestQueryString("pos")<>"" then

  if session("last")="" then session("last")="abc"

  if session("last")<> RequestQueryString("pos") then

  application(application("first" & session("class")))=true

  application(application("second" & session("class")))=false

  session("last")=RequestQueryString("pos")

  END IF

  end if

  else

  color=""

  end if

  end if

  end if

  ResponseWrite "<table width= height= border= cellspacing=

  cellpadding=>"

  for i= to

  ResponseWrite "<tr>"

  for j= to

  if application("pos" & session("class") & cstr(i) & "_" & cstr(j))=""

  then

  application("color" & session("class") & "_" & RequestQueryString

  ("pos"))=color

  ResponseWrite "<td width= height=>"

  responsewrite application(application("first" & session("class")))

  if application(session("nice"))=true then

  responsewrite "<a href=matchasp?pos=" & cstr(i) & "_" & cstr(j)

  & "><img border= src=backjpg width= height=></a>"

  else

  responsewrite "<img border= src=backjpg width= height=>"

  end if

  responsewrite "</td>"

  else

  ResponseWrite "<td width= height=><img border= src=" &

  application("color" & session("class") & "_" & cstr(i) & "_" & cstr

  (j)) & "jpg width= height=></td>" & chr()

  end if

  next

  ResponseWrite "</tr>"

  next

  ResponseWrite "</table>"

  判斷輸贏

  if application("first" & session("class"))=session("nice") then

  m="black"

  h="white"

  else

  h="black"

  m="white"

  end if

  橫向判斷""

  for i= to

  win=false

  loss=false

  five_me=

  five_he=

  for j= to

  if application("color" & session("class") & "_" & cstr(i) & "_" & cstr

  (j))=m then

  five_me=five_me+

  five_he=

  elseif application("color" & session("class") & "_" & cstr(i) & "_" &

  cstr(j))=h then

  five_me=

  five_he=five_he+

  else

  five_me=

  five_he=

  end if

  if five_me= then

  win=true

  loss=false

  exit for

  elseif five_he= then

  win=false

  loss=true

  exit for

  end if

  next

  if win or loss then exit for

  next

  if win then

  responsewrite "你贏了!"

  application(application("first" & session("class")))=false

  application(application("second" & session("class")))=false

  end if

  if loss then

  responsewrite "對不起你輸了!"

  application(application("first" & session("class")))=false

  application(application("second" & session("class")))=false

  end if

  列向判斷"|"

  for j= to

  win=false

  loss=false

  five_me=

  five_he=

  for i= to

  if application("color" & session("class") & "_" & cstr(i) & "_" & cstr

  (j))=m then

  five_me=five_me+

  five_he=

  elseif application("color" & session("class") & "_" & cstr(i) & "_" &

  cstr(j))=h then

  five_me=

  five_he=five_he+

  else

  five_me=

  five_he=

  end if

  if five_me= then

  win=true

  loss=false

  exit for

  elseif five_he= then

  win=false

  loss=true

  exit for

  end if

  next

  if win or loss then exit for

  next

  if win then

  responsewrite "你贏了!"

  application(application("first" & session("class")))=false

  application(application("second" & session("class")))=false

  end if

  if loss then

  responsewrite "對不起你輸了!"

  application(application("first" & session("class")))=false

  application(application("second" & session("class")))=false

  end if

  斜向判斷"/"

  for i= to

  win=false

  loss=false

  five_me=

  five_he=

  for j=i+ to step

  if application("color" & session("class") & "_" & cstr(j) & "_" & cstr

  (i+j))=m then

  five_me=five_me+

  five_he=

  elseif application("color" & session("class") & "_" & cstr(j) & "_" &

  cstr(i+j))=h then

  five_me=

  five_he=five_he+

  else

  five_me=

  five_he=

  end if

  if five_me= then

  win=true

  loss=false

  exit for

  elseif five_he= then

  win=false

  loss=true

  exit for

  end if

  next

  if win or loss then exit for

  next

  if win then

  responsewrite "你贏了!"

  application(application("first" & session("class")))=false

  application(application("second" & session("class")))=false

  end if

  if loss then

  responsewrite "對不起你輸了!"

  application(application("first" & session("class")))=false

  application(application("second" & session("class")))=false

  end if

  斜向判斷""

  for i= to

  win=false

  loss=false

  five_me=

  five_he=

  for j=i to

  if application("color" & session("class") & "_" & cstr(ij) & "_"

  & cstr(j))=m then

  five_me=five_me+

  five_he=

  elseif application("color" & session("class") & "_" & cstr(ij)

  & "_" & cstr(j))=h then

  five_me=

  five_he=five_he+

  else

  five_me=

  five_he=

  end if

  if five_me= then

  win=true

  loss=false

  exit for

  elseif five_he= then

  win=false

  loss=true

  exit for

  end if

  next

  if win or loss then exit for

  next

  if win then

  responsewrite "你贏了!"

  application(application("first" & session("class")))=false

  application(application("second" & session("class")))=false

  end if

  if loss then

  responsewrite "對不起你輸了!"

  application(application("first" & session("class")))=false

  application(application("second" & session("class")))=false

  end if

  %>

  </body>

  </html>

  如果某點沒有棋子則加上一個超連接根據擂主與攻擂手的區別放上一個不同顏色的棋子最後的一段是用來判斷輸與贏分為橫列和兩個斜線方向具體內容看一下就可以明白

  最後要說的一點就是globalasa 這是站點上的一個核心文件為了在退出棋局後將位置留給別人需要在sesison_onend中加入代碼進行處理如下

  <script language=vbscript runat = server>

  sub application_onstart

  sessiontimeout=

  end sub

  sub session_onstart

  if application("num")="" then

  application("num")=

  end if

  application("num")=application("num")+

  end sub

  </script>

  <script language=vbscript runat = server>

  sub session_onend

  if application("num")="" then

  application("num")=

  end if

  application("num")=application("num")

  if application("first")=session("nice") then

  application("first")=""

  elseif application("second")=session("nice") then

  application("second")=""

  elseif application("first")=session("nice") then

  application("first")=""

  elseif application("second")=session("nice") then

  application("second")=""

  elseif application("first")=session("nice") then

  application("first")=""

  elseif application("second")=session("nice") then

  application("second")=""

  elseif application("first")=session("nice") then

  application("first")=""

  elseif application("second")=session("nice") then

  application("second")=""

  elseif application("first")=session("nice") then

  application("first")=""

  elseif application("second")=session("nice") then

  application("second")=""

  end if

  for i= to

  for j= to

  application("color" & session("class") & "_" & cstr(i) & "_" & cstr

  (J))=""

  application("pos" & session("class") & cstr(i) & "_" & cstr(j))=""

  next

  next

  application(session("nice"))=""

  end sub

  </script>

  以上就是幾個主要的文件大家可以一試什麼想做圍棋?把棋盤改一下就可以了

  以上是網為您介紹的用ASP實現網上“五子棋”大賽希望對您有所幫助


From:http://tw.wingwit.com/Article/program/net/201311/14337.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.