Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- namespace WindowsFormsApplication1
- {
- public partial class ClientForm : Form
- {
- TcpClient client;
- bool x;
- Point[] PointsArr;
- Game g;
- public ClientForm()
- {
- InitializeComponent();
- }
- private void Client_Paint(object sender, PaintEventArgs e)
- {
- }
- private void ClientForm_Load(object sender, EventArgs e)
- {
- g = new Game();
- }
- private void Get()
- {
- char[] buffer = new char[10];
- StreamReader re = new StreamReader(client.GetStream());
- re.Read(buffer, 0, 5);
- string str = new string(buffer);
- x = (str[0] != 'x');
- while(true)
- {
- re = new StreamReader(client.GetStream());
- re.Read(buffer, 0, 5);
- str = new string(buffer);
- got(Convert.ToInt32(str));
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- client = new TcpClient(textBox1.Text, Convert.ToInt32(textBox2.Text));
- Thread t = new Thread(Get);
- t.Start();
- PointsArr = g.drawXO(pictureBox1.CreateGraphics());
- }
- private void pictureBox1_Click(object sender, EventArgs e)
- {
- }
- private void set(int num)
- {
- if (g.Set(x, num))
- {
- int width = (PointsArr[2].Y - PointsArr[0].Y);
- if (x)
- g.drawX(new Point(PointsArr[2].X + 20 + width * ((num - 1) % 3), PointsArr[0].Y + 20 + width * ((num - 1) / 3)), pictureBox1.CreateGraphics());
- else
- g.drawO(new Point(PointsArr[2].X + 20 + width * ((num - 1) % 3), PointsArr[0].Y + 20 + width * ((num - 1) / 3)), pictureBox1.CreateGraphics());
- StreamWriter wr = new StreamWriter(client.GetStream());
- wr.Write(num);
- wr.Flush();
- }
- }
- private void got(int num)
- {
- if (g.Set(!x, num))
- {
- int width = (PointsArr[2].Y - PointsArr[0].Y);
- //server:
- if (!x)
- g.drawX(new Point(PointsArr[2].X + 20 + width * ((num - 1) % 3), PointsArr[0].Y + 20 + width * ((num - 1) / 3)), pictureBox1.CreateGraphics());
- else
- g.drawO(new Point(PointsArr[2].X + 20 + width * ((num - 1) % 3), PointsArr[0].Y + 20 + width * ((num - 1) / 3)), pictureBox1.CreateGraphics());
- }
- }
- private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
- {
- if (PointsArr != null)
- {
- if (g.myTurn(false))//i started!
- {
- int x = e.X;
- int y = e.Y;
- int width = (PointsArr[2].Y - PointsArr[0].Y);
- if (y < PointsArr[2].Y && x > PointsArr[2].X)
- {
- if (x < PointsArr[0].X)
- set(1);
- else if (x > PointsArr[0].X && x < PointsArr[0].X + width)
- set(2);
- else if (x < PointsArr[0].X + 2 * width)
- set(3);
- }
- else if (y < PointsArr[2].Y + width && x > PointsArr[2].X)
- {
- if (x < PointsArr[0].X)
- set(4);
- else if (x > PointsArr[0].X && x < PointsArr[0].X + width)
- set(5);
- else if (x < PointsArr[0].X + 2 * width)
- set(6);
- }
- else if (y < PointsArr[2].Y + 2 * width && x > PointsArr[2].X)
- {
- if (x < PointsArr[0].X)
- set(7);
- else if (x > PointsArr[0].X && x < PointsArr[0].X + width)
- set(8);
- else if (x < PointsArr[0].X + 2 * width)
- set(9);
- }
- }
- else
- {
- MessageBox.Show("Error: It's not your turn!");
- }
- }
- else
- {
- //MessageBox.Show("Error: Board has not been set yet!");
- }
- }
- private void pictureBox1_Paint(object sender, PaintEventArgs e)
- {
- g.drawXO(e.Graphics);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement