Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Text.RegularExpressions;
  8. using System.Web;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11.  
  12. namespace WebApplication3
  13. {
  14. public partial class WebForm1 : System.Web.UI.Page
  15. {
  16. public static bool isX = true;
  17. public static string[] spaces = new string[]{"a","b","c","d","e","f","g","h","i"};
  18.  
  19.  
  20. protected void Page_Load(object sender, EventArgs e)
  21. {
  22. }
  23.  
  24. protected void TextboxChange(object sender, EventArgs e)
  25. {
  26. string chatString = ChatBox.Text;
  27. DateTime today = DateTime.Today;
  28. // int dt = Convert.ToInt32((today.Ticks % 4000000));
  29. int dt = 4;
  30. Label1.Text = "it work " + dt;
  31. SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database1ConStr"].ToString());
  32. SqlCommand cmd = new SqlCommand();
  33.  
  34. cmd.CommandText = "INSERT INTO tblChat (message) VALUES (@msgText)";
  35. cmd.Parameters.Add("@msgText", SqlDbType.NVarChar).Value = chatString;
  36. // cmd.Parameters.Add("@dt", SqlDbType.Int).Value = dt;
  37.  
  38. cmd.Connection = conn;
  39.  
  40. conn.Open();
  41.  
  42. cmd.ExecuteNonQuery();
  43.  
  44. conn.Close();
  45.  
  46. }
  47.  
  48. protected void space(object sender, EventArgs e)
  49. {
  50. // object ref
  51. Button btn = sender as Button;
  52.  
  53. // store the space for checking wins
  54. string buttonString, intString;
  55. int spaceNum;
  56. buttonString = btn.ID;
  57. intString = Regex.Match(buttonString, @"\d+").Value;
  58. spaceNum = Int32.Parse(intString);
  59.  
  60. if (btn.Text != "X" && btn.Text != "O")
  61. {
  62. if (isX)
  63. {
  64. // change button & array
  65. btn.Text = "X";
  66. btn.Enabled = false;
  67. isX = false;
  68. spaces[spaceNum] = "X";
  69. }
  70. else if (isX == false)
  71. {
  72. btn.Text = "O";
  73. isX = true;
  74. btn.Enabled = false;
  75. spaces[spaceNum] = "O";
  76. }
  77. string winS = CheckWin();
  78. Label1.Text = winS;
  79. }
  80. if(Label1.Text.Contains("wins"))
  81. {
  82. EndGame();
  83. }
  84. }
  85.  
  86. public string CheckWin()
  87. {
  88. // check all win conditions
  89.  
  90. // horizontal win conditions
  91. if (spaces[0] == spaces[1] && spaces[1] == spaces[2])
  92. return (spaces[0] + " wins!");
  93. else if (spaces[3] == spaces[4] && spaces[4] == spaces[5])
  94. return (spaces[3] + " wins!");
  95. else if (spaces[6] == spaces[7] && spaces[7] == spaces[8])
  96. return (spaces[0] + " wins!");
  97.  
  98. // vertical win conditions
  99. else if (spaces[0] == spaces[3] && spaces[3] == spaces[6])
  100. return (spaces[0] + " wins!");
  101. else if (spaces[1] == spaces[4] && spaces[4] == spaces[7])
  102. return (spaces[1] + " wins!");
  103. else if (spaces[2] == spaces[5] && spaces[5] == spaces[8])
  104. return (spaces[2] + " wins!");
  105.  
  106. // diagonal
  107. else if (spaces[0] == spaces[4] && spaces[4] == spaces[8])
  108. return (spaces[0] + " wins!");
  109. else if (spaces[2] == spaces[4] && spaces[4] == spaces[6])
  110. return (spaces[2] + " wins!");
  111.  
  112. // no winner
  113. else
  114. return "No Winner Yet!";
  115. }
  116.  
  117. public void EndGame()
  118. {
  119. Table1.Enabled = false;
  120. }
  121. /*
  122. protected void methodName(object sender, EventArgs e)
  123. {
  124.  
  125. }
  126. */
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement