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

📄 ncboard.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;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace Noughts_And_Crosses
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		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;
		private const int WIN_EIGHT		= 7;
		private int[] m_intBoard;
		private int m_intWinNumber;
		private int m_intWinShape;

		private void Page_Load(object sender, System.EventArgs e)
		{
			//if the session variable containing the board is not empty
			if(Session["s_intBoard"] != null)
			{
				//store the board session variable in a page scope variable
				m_intBoard = (int []) Session["s_intBoard"];
			}
			else
			{
				//create the board session variable 
				Session["s_intBoard"] = new int[] {SHAPE_BLANK, SHAPE_BLANK, SHAPE_BLANK, SHAPE_BLANK,
													  SHAPE_BLANK, SHAPE_BLANK, SHAPE_BLANK, SHAPE_BLANK, 
													  SHAPE_BLANK};
			}

			//Create the bitmap object that will contian the game board image
			Bitmap objDynamicImage = new Bitmap(300, 300, PixelFormat.Format24bppRgb);
			
			//Create a graphics object from the bitmap object we just created
			Graphics objGraphics = Graphics.FromImage(objDynamicImage);

			//clear the grpahics object
			objGraphics.Clear(Color.White);

			//Draw the playing grid onto the Graphics object
			DrawGrid(objGraphics);

			//Draw all the moves onto the graphics object
			DrawMoves(objGraphics);

			//if there is a winner
			if(CheckForWinner())
			{
				//create winner string with default value of Naughts
				string strWinnerIs = " Naughts";

				//if Crosses won
				if(m_intWinShape == SHAPE_CROSS)
				{
					//set winner string value to crosses
					strWinnerIs = " Crosses";
				}
			
				//draw the line through the winning shapes
				DrawWinningLine(objGraphics);
				
				//draw the winning message box onto the grpahics object
				objGraphics.FillRectangle(new SolidBrush(Color.FromArgb(210,Color.White)),50,130,220,35);
				objGraphics.DrawRectangle(new Pen(Color.Black, 0.5F),50, 130, 220, 35);
				objGraphics.DrawString(strWinnerIs + " Won", new Font("verdana", 20, FontStyle.Bold), new SolidBrush(Color.Black), new Point(50,130));
			}


			//clear the response stream
			Response.ClearContent();

			//set the content type of the response to JPEG
			Response.ContentType = "image/pjpeg";

			//Send our dynamic image bitmap object to the client
			objDynamicImage.Save(Response.OutputStream, ImageFormat.Jpeg);
		}

		/// <summary>
		/// Draws the winning line through the winning shapes
		/// </summary>
		/// <param name="g">Graphics object to draw on</param>
		private void DrawWinningLine(Graphics g)
		{
			//crete a transparant red pen (the value 175 indicates the transparancy level used)
			Pen ObjLinePen = new Pen(Color.FromArgb(175, Color.Red), 12);

			//if a player has won
			if(m_intWinNumber != -1)
			{
				//draw the line depending on which squares the winning shapes are located in
				switch(m_intWinNumber)
				{
					case WIN_ONE:
						g.DrawLine(ObjLinePen,new Point(0,50),new Point(300,50));
						break;
					case WIN_TWO:
						g.DrawLine(ObjLinePen,new Point(0,150),new Point(300,150));
						break;
					case WIN_THREE:
						g.DrawLine(ObjLinePen,new Point(0,250),new Point(300,250));
						break;
					case WIN_FOUR:
						g.DrawLine(ObjLinePen,new Point(50,0),new Point(50,300));
						break;
					case WIN_FIVE:
						g.DrawLine(ObjLinePen,new Point(150,0),new Point(150,300));
						break;
					case WIN_SIX:
						g.DrawLine(ObjLinePen,new Point(250,0),new Point(250,300));
						break;
					case WIN_SEVEN:
						g.DrawLine(ObjLinePen,new Point(0,0),new Point(300,300));
						break;
					case WIN_EIGHT:
						g.DrawLine(ObjLinePen,new Point(300,0),new Point(0,300));
						break;
				}
			}
		}

		/// <summary>
		/// checks if a player won, if so stores the winning shape and the winning
		/// squares on the grid
		/// </summary>
		/// <returns>returns true or false depending if a player won</returns>
		private bool CheckForWinner()
		{
			int[] intSums = new int[8];

			intSums[WIN_ONE]	= m_intBoard[0] + m_intBoard[1] + m_intBoard[2];
			intSums[WIN_TWO]	= m_intBoard[3] + m_intBoard[4] + m_intBoard[5];
			intSums[WIN_THREE]	= m_intBoard[6] + m_intBoard[7] + m_intBoard[8];

			intSums[WIN_FOUR]	= m_intBoard[0] + m_intBoard[3] + m_intBoard[6];
			intSums[WIN_FIVE]	= m_intBoard[1] + m_intBoard[4] + m_intBoard[7];
			intSums[WIN_SIX]	= m_intBoard[2] + m_intBoard[5] + m_intBoard[8];

			intSums[WIN_SEVEN]	= m_intBoard[0] + m_intBoard[4] + m_intBoard[8];
			intSums[WIN_EIGHT]	= m_intBoard[2] + m_intBoard[4] + m_intBoard[6];

			//for each of the eight posible winning lines on the grid
			for(int i = 0; i < 8; i++)
			{
				//if the total of the shapes in this winning line is 3 then noughts won
				if(intSums[i] == 3)
				{
					//set the page scope winning number variable to the current winning line
					m_intWinNumber = i;

					//set the session scope winning number variable to the current winning line
					Session["s_intWinNumber"] = i;

					//set the winning shape to noughts
					m_intWinShape = SHAPE_NOUGHT;

					//return that there is a winner
					return true;
				}
				else if(intSums[i] == 30)
				{
					//set the page scope winning number variable to the current winning line
					m_intWinNumber = i;

					//set the session scope winning number variable to the current winning line
					Session["s_intWinNumber"] = i;

					//set the winning shape to crosses
					m_intWinShape = SHAPE_CROSS;

					//return that there is a winner
					return true;
				}
			}
			//return that there is no winner
			return false;
		}

		/// <summary>
		/// Draw all of the moves onto the board
		/// </summary>
		/// <param name="g">graphics object to draw with</param>
		private void DrawMoves(Graphics g)
		{
			//for each square on the board
			for(int i = 0; i < 9; i++)
			{
				switch(m_intBoard[i])
				{
					//if a nought is to be drawn
					case SHAPE_NOUGHT:
						//draw a nought on current square
						DrawMove(g, i + 1, SHAPE_NOUGHT);
						break;
					//if a nought is to be drawn
					case SHAPE_CROSS:
						//draw a cross on current square
						DrawMove(g, i + 1, SHAPE_CROSS);
						break;
				}
			}
		}

		/// <summary>
		/// Draws a single move onto the game board
		/// </summary>
		/// <param name="g"> graphics object to draw with</param>
		/// <param name="intSquare">square to draw on</param>
		/// <param name="intShape">shape to draw</param>
		private void DrawMove(Graphics g, int intSquare, int intShape)
		{
			int intX = 0; int intY = 0;

			//set the x anf y position for the shape based on which square was requested
			switch(intSquare)
			{
				case SQUARE_ONE:
					intX = 10;
					intY = 10;
					break;
				case SQUARE_TWO	:
					intX = 110;
					intY = 10;
					break;
				case SQUARE_THREE:
					intX = 210;
					intY = 10;
					break;
				case SQUARE_FOUR:
					intX = 10;
					intY = 110;
					break;
				case SQUARE_FIVE:
					intX = 110;
					intY = 110;
					break;
				case SQUARE_SIX:
					intX = 210;
					intY = 110;
					break;
				case SQUARE_SEVEN:
					intX = 10;
					intY = 210;
					break;
				case SQUARE_EIGHT:
					intX = 110;
					intY = 210;
					break;
				case SQUARE_NINE:
					intX = 210;
					intY = 210;
					break;
			}
			switch(intShape)
			{
					//if the shape is nought
				case SHAPE_NOUGHT:
					
					//draw a nought in the chosen square
					DrawNought(g, intX, intY);
					break;
				//if the shape is cross
				case SHAPE_CROSS:

					//draw a cross in the chosen square
					DrawCross(g, intX, intY);
					break;
			}
		}

		/// <summary>
		/// Draws a green cross
		/// </summary>
		/// <param name="g">graphics object to draw cross with</param>
		/// <param name="intX">x position to draw to</param>
		/// <param name="intY">y position to draw to</param>
		private void DrawCross(Graphics g, int intX, int intY)
		{
			//create a green pen 4 pixels wide
			Pen objGreenPen = new Pen(Color.Green, 4);

			//draw the two lines of the cross
			g.DrawLine(objGreenPen, new Point(intX, intY), new Point(intX + 80, intY + 80));
			g.DrawLine(objGreenPen, new Point(intX + 80, intY), new Point(intX, intY + 80));
		}

		/// <summary>
		/// draws a blue circle
		/// </summary>
		/// <param name="g">graphics object to draw nought with</param>
		/// <param name="intX">x position to draw to</param>
		/// <param name="intY">y position to draw to</param>
		private void DrawNought(Graphics g, int intX, int intY)
		{
			//create a blue pen 3 pixels wide
			Pen objBluePen = new Pen(Color.Blue, 3);

			//Draw the nought
			g.DrawEllipse(objBluePen, intX, intY, 80, 80);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="g">graphics object to draw grid with</param>
		private void DrawGrid(Graphics g)
		{
			//create a black pen 4 pixels wide
			Pen objBlackPen = new Pen(Color.Black, 4);

			//Draw the four lines of the grid
			g.DrawLine(objBlackPen, new Point(0, 100), new Point(300, 100));
			g.DrawLine(objBlackPen, new Point(0, 200), new Point(300, 200));
			g.DrawLine(objBlackPen, new Point(100, 0), new Point(100, 300));
			g.DrawLine(objBlackPen, new Point(200, 0), new Point(200, 300));
		}
		#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.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}

⌨️ 快捷键说明

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