Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. using System.IO;
  11. using System.Net;
  12. using System.Net.Sockets;
  13.  
  14. namespace WindowsFormsApplication1
  15. {
  16. public partial class ClientForm : Form
  17. {
  18. TcpClient client;
  19. bool x;
  20. Point[] PointsArr;
  21. Game g;
  22. public ClientForm()
  23. {
  24. InitializeComponent();
  25. }
  26.  
  27. private void Client_Paint(object sender, PaintEventArgs e)
  28. {
  29. }
  30.  
  31. private void ClientForm_Load(object sender, EventArgs e)
  32. {
  33. g = new Game();
  34. }
  35. private void Get()
  36. {
  37. char[] buffer = new char[10];
  38. StreamReader re = new StreamReader(client.GetStream());
  39. re.Read(buffer, 0, 5);
  40. string str = new string(buffer);
  41. x = (str[0] != 'x');
  42. while(true)
  43. {
  44. re = new StreamReader(client.GetStream());
  45. re.Read(buffer, 0, 5);
  46. str = new string(buffer);
  47. got(Convert.ToInt32(str));
  48. }
  49. }
  50. private void button1_Click(object sender, EventArgs e)
  51. {
  52. client = new TcpClient(textBox1.Text, Convert.ToInt32(textBox2.Text));
  53. Thread t = new Thread(Get);
  54. t.Start();
  55. PointsArr = g.drawXO(pictureBox1.CreateGraphics());
  56. }
  57.  
  58. private void pictureBox1_Click(object sender, EventArgs e)
  59. {
  60.  
  61. }
  62. private void set(int num)
  63. {
  64. if (g.Set(x, num))
  65. {
  66. int width = (PointsArr[2].Y - PointsArr[0].Y);
  67. if (x)
  68. g.drawX(new Point(PointsArr[2].X + 20 + width * ((num - 1) % 3), PointsArr[0].Y + 20 + width * ((num - 1) / 3)), pictureBox1.CreateGraphics());
  69. else
  70. g.drawO(new Point(PointsArr[2].X + 20 + width * ((num - 1) % 3), PointsArr[0].Y + 20 + width * ((num - 1) / 3)), pictureBox1.CreateGraphics());
  71. StreamWriter wr = new StreamWriter(client.GetStream());
  72. wr.Write(num);
  73. wr.Flush();
  74. }
  75.  
  76. }
  77. private void got(int num)
  78. {
  79. if (g.Set(!x, num))
  80. {
  81. int width = (PointsArr[2].Y - PointsArr[0].Y);
  82. //server:
  83. if (!x)
  84. g.drawX(new Point(PointsArr[2].X + 20 + width * ((num - 1) % 3), PointsArr[0].Y + 20 + width * ((num - 1) / 3)), pictureBox1.CreateGraphics());
  85. else
  86. g.drawO(new Point(PointsArr[2].X + 20 + width * ((num - 1) % 3), PointsArr[0].Y + 20 + width * ((num - 1) / 3)), pictureBox1.CreateGraphics());
  87. }
  88. }
  89. private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
  90. {
  91. if (PointsArr != null)
  92. {
  93. if (g.myTurn(false))//i started!
  94. {
  95. int x = e.X;
  96. int y = e.Y;
  97. int width = (PointsArr[2].Y - PointsArr[0].Y);
  98. if (y < PointsArr[2].Y && x > PointsArr[2].X)
  99. {
  100. if (x < PointsArr[0].X)
  101. set(1);
  102. else if (x > PointsArr[0].X && x < PointsArr[0].X + width)
  103. set(2);
  104. else if (x < PointsArr[0].X + 2 * width)
  105. set(3);
  106. }
  107. else if (y < PointsArr[2].Y + width && x > PointsArr[2].X)
  108. {
  109. if (x < PointsArr[0].X)
  110. set(4);
  111. else if (x > PointsArr[0].X && x < PointsArr[0].X + width)
  112. set(5);
  113. else if (x < PointsArr[0].X + 2 * width)
  114. set(6);
  115. }
  116. else if (y < PointsArr[2].Y + 2 * width && x > PointsArr[2].X)
  117. {
  118. if (x < PointsArr[0].X)
  119. set(7);
  120. else if (x > PointsArr[0].X && x < PointsArr[0].X + width)
  121. set(8);
  122. else if (x < PointsArr[0].X + 2 * width)
  123. set(9);
  124. }
  125. }
  126. else
  127. {
  128. MessageBox.Show("Error: It's not your turn!");
  129. }
  130. }
  131. else
  132. {
  133. //MessageBox.Show("Error: Board has not been set yet!");
  134. }
  135. }
  136.  
  137. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  138. {
  139. g.drawXO(e.Graphics);
  140. }
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement