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

📄 index.aspx.cs

📁 C#编写的网络小游戏(井字过三关).
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Noughts_And_Crosses
{
	/// <summary>
	/// Summary description for index.
	/// </summary>
	public class index : System.Web.UI.Page
	{

		private int m_intMouseX;
		private int m_intMouseY;
		protected ImageButton imgPlayingGrid;
		private int m_intWinNumber;
		private int m_intCurrentShape;
		private int[] m_intBoard;

		private const int SQUARE_ONE	= 1;
		private const int SQUARE_TWO	= 2;
		private const int SQUARE_THREE	= 3;
		private const int SQUARE_FOUR	= 4;
		private const int SQUARE_FIVE	= 5;
		private const int SQUARE_SIX	= 6;
		private const int SQUARE_SEVEN	= 7;
		private const int SQUARE_EIGHT	= 8;
		private const int SQUARE_NINE	= 9;

		private const int SHAPE_NOUGHT	= 1;
		private const int SHAPE_CROSS	= 10;
		private const int SHAPE_BLANK	= 0;

		private const int WIN_ONE		= 0;
		private const int WIN_TWO		= 1;
		private const int WIN_THREE		= 2;
		private const int WIN_FOUR		= 3;
		private const int WIN_FIVE		= 4;
		private const int WIN_SIX		= 5;
		private const int WIN_SEVEN		= 6;
		protected Button Button1;
		protected Label Label1;
		private const int WIN_EIGHT		= 7;

		private void Page_Load(object sender, System.EventArgs e)
		{
			//if the session variable which indicates if the player has won is not empty
			if(Session["s_intWinNumber"] != null)
			{
				try
				{
					//try casting teh session value to store in page variable
					m_intWinNumber = (int)Session["s_intWinNumber"];
				}
				catch(Exception)
				{
					m_intWinNumber = -1;
				}

			}
			else
			{
				m_intWinNumber = -1;
			}

			//reset the variable which tracks whether to draw an X or O
			m_intCurrentShape = 0;

			if(!(IsPostBack))
			{
				//reset the game the first time the page loads
				ResetGame();
			}
			else
			{
				//if the session variable which stores which shapes occupy which squares on the grid is not empty
				if(Session["s_intBoard"] != null)
				{
					//store the board session data in a page scope array
					m_intBoard = (int []) Session["s_intBoard"];

					//try storing the querystring mouse x and y values in a page scope int
					try
					{
						m_intMouseX = Int32.Parse(Request.QueryString["imgPlayingGrid.x"]);
						m_intMouseY = Int32.Parse(Request.QueryString["imgPlayingGrid.y"]);
					}
					catch(Exception)
					{
						m_intMouseX = -1;
						m_intMouseY = -1;

					}

					//get the value of the square the user clicked on
					int intSquare = GetSquarePicked();

					// if a square has been picked and there is currently no winner
					if(intSquare != -1 && m_intWinNumber	== -1)
					{
						//if the square picked does not already have a shape in it
						if(m_intBoard[intSquare-1] == SHAPE_BLANK)
						{
							//determin which character to draw based on how many move have been taken
							switch(((int)Session["s_intTotalMoves"]))
							{
								case 1:
								case 3:
								case 5:
								case 7:
								case 9:
									m_intCurrentShape = SHAPE_NOUGHT;
									break;
								case 0:
								case 2:
								case 4:
								case 6:
								case 8:
									m_intCurrentShape = SHAPE_CROSS;
									break;
							}

							//assign the appropriate shape to the board arrray
							m_intBoard[intSquare-1] = m_intCurrentShape;

							//increment the total moves session variable by one
                            Session["s_intTotalMoves"] = ((int)Session["s_intTotalMoves"]) + 1;
						}
					}

					//place the page scope board array into the session scope board array
					Session["s_intBoard"] = m_intBoard;

				}else{

					//reset the game
					ResetGame();
					
				}

			}
		}

		/// <summary>
		/// Returns the square the player chose based on mouse x and y coordinates when the
		/// board was clicked
		/// </summary>
		/// <returns>int representing the square chosen</returns>
		private int GetSquarePicked()
		{
			if(m_intMouseX > 0 && m_intMouseX < 100 && m_intMouseY > 0 && m_intMouseY < 100)
			{
				return SQUARE_ONE;

			}
			else if(m_intMouseX > 100 && m_intMouseX < 200 && m_intMouseY > 0 && m_intMouseY < 100)
			{
				return SQUARE_TWO;
			
			}
			else if(m_intMouseX > 200 && m_intMouseX < 300 && m_intMouseY > 0 && m_intMouseY < 100)
			{
				return SQUARE_THREE;
			}
			else if(m_intMouseX > 0 && m_intMouseX < 100 && m_intMouseY > 100 && m_intMouseY < 200)
			{
				return SQUARE_FOUR;
			
			}
			else if(m_intMouseX > 100 && m_intMouseX < 200 && m_intMouseY > 100 && m_intMouseY < 200)
			{
				return SQUARE_FIVE;
			
			}
			else if(m_intMouseX > 200 && m_intMouseX < 300 && m_intMouseY > 100 && m_intMouseY < 200)
			{
				return SQUARE_SIX;
			
			}
			else if(m_intMouseX > 0 && m_intMouseX < 100 && m_intMouseY > 200 && m_intMouseY < 300)
			{
				return SQUARE_SEVEN;
			
			}
			else if(m_intMouseX > 100 && m_intMouseX < 200 && m_intMouseY > 200 && m_intMouseY < 300)
			{
				return SQUARE_EIGHT;
			
			}
			else if(m_intMouseX > 200 && m_intMouseX < 300 && m_intMouseY > 200 && m_intMouseY < 300)
			{
				return SQUARE_NINE;
			
			}
			else
			{
				return -1;

			}
		}

		/// <summary>
		/// Resets the session variables which effectively resets the game
		/// </summary>
		private void ResetGame()
		{

			//create a new session array representing the board with a blank shape in each square
			Session["s_intBoard"] = new int[] {SHAPE_BLANK, SHAPE_BLANK, SHAPE_BLANK, SHAPE_BLANK,
															SHAPE_BLANK, SHAPE_BLANK, SHAPE_BLANK, SHAPE_BLANK, 
															SHAPE_BLANK};

			// set the total number of moves to 0
			Session["s_intTotalMoves"] = 0;

			//set the winstatus to -1 (no win)
			Session["s_intWinNumber"] = -1;

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		/// <summary>
		/// Reset button onclick
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Button1_Click(object sender, System.EventArgs e)
		{
			//reset the game
			ResetGame();
		
		}
	}
}

⌨️ 快捷键说明

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