private void connectionEstablished(Connection con, ConnectionType conType) { int turn = r.Next(2); richTextBox1.Text += $"Connected{con.IPRemoteEndPoint}" + Environment.NewLine; if (p1 == null) { p1 = new Player(con); p1.ready = false; } else { p2 = new Player(con); p2.ready = false; } con.RegisterRawDataHandler("Image", (RawData, connection) => { if (connection == p1.con) { p1.pic = ImageConvert.bytes2Image(RawData.Data); } else { p2.pic = ImageConvert.bytes2Image(RawData.Data); p1.con.SendRawData("image", ImageConvert.image2Bytes(p2.pic)); p2.con.SendRawData("image", ImageConvert.image2Bytes(p1.pic)); if (turn == 1) { p1.FirstTurn = true; p1.con.SendRawData("firstTurn", RawDataConverter.GetBytes(true)); p1.turn = true; } else { p2.FirstTurn = true; p2.con.SendRawData("firstTurn", RawDataConverter.GetBytes(true)); p2.turn = false; } } }); con.RegisterRawDataHandler("Nick", (RawData, connection) => { if (connection == p1.con) { p1.nick = RawData.ToUTF8String(); } else { p2.nick = RawData.ToUTF8String(); p1.con.SendRawData("nick", Encoding.UTF8.GetBytes(p2.nick)); p2.con.SendRawData("nick", Encoding.UTF8.GetBytes(p1.nick)); } }); con.RegisterRawDataHandler("Ready", (RawData, connection) => { if (connection == p1.con) { p1.ready = RawData.ToBoolean(); if (p2 != null) { p1.con.SendRawData("ready", RawDataConverter.GetBytes(p2.ready)); } p2.con.SendRawData("ready", RawDataConverter.GetBytes(p1.ready)); } else { p2.ready = RawData.ToBoolean(); p1.con.SendRawData("ready", RawDataConverter.GetBytes(p2.ready)); p2.con.SendRawData("ready", RawDataConverter.GetBytes(p1.ready)); } }); con.RegisterRawDataHandler("mapUpdate", (RawData, connection) => { char[] newmap = RawData.ToUTF8String().ToArray(); for (int i = 0; i < 9; i++) { map[i] = int.Parse(newmap[i].ToString()); } string mapString = ""; for (int i = 0; i < 9; i++) { mapString += map[i].ToString(); } if (connection == p1.con) { p1.turn = false; p2.turn = true; p1.con.SendRawData("Turn", RawDataConverter.GetBytes(p1.turn)); p2.con.SendRawData("Turn", RawDataConverter.GetBytes(p2.turn)); p2.con.SendRawData("MapUpdate", Encoding.UTF8.GetBytes(mapString)); } else { p2.turn = false; p1.turn = true; p1.con.SendRawData("Turn", RawDataConverter.GetBytes(p1.turn)); p2.con.SendRawData("Turn", RawDataConverter.GetBytes(p2.turn)); p1.con.SendRawData("MapUpdate", Encoding.UTF8.GetBytes(mapString)); } if (checkGameWin() == 1 && p1.FirstTurn) { p1.con.SendRawData("win", RawDataConverter.GetBytes(true)); p2.con.SendRawData("lose", RawDataConverter.GetBytes(true)); map = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; } else if (checkGameWin() == 2 && p2.FirstTurn) { p1.con.SendRawData("win", RawDataConverter.GetBytes(true)); p2.con.SendRawData("lose", RawDataConverter.GetBytes(true)); map = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; } else if (checkGameWin() == 1 && p2.FirstTurn) { p2.con.SendRawData("win", RawDataConverter.GetBytes(true)); p1.con.SendRawData("lose", RawDataConverter.GetBytes(true)); map = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; } else if (checkGameWin() == 2 && p1.FirstTurn) { p2.con.SendRawData("win", RawDataConverter.GetBytes(true)); p1.con.SendRawData("lose", RawDataConverter.GetBytes(true)); map = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; } } ); }
public Form3(int port, string ip, Image localpic, string localnick, int rounds) { win = false; lose = false; Turn = false; firstTurn = false; score = 0; enemyScore = 0; game = true; this.rounds = rounds; localNickName = localnick; localImage = localpic; ConnectionResult connectionResult = ConnectionResult.TCPConnectionNotAlive; connection = ConnectionFactory.CreateTcpConnection(ip, port, out connectionResult); if (connectionResult == ConnectionResult.Connected) { MessageBox.Show("Connected", "connected"); } InitializeComponent(); pictureBox2.Image = localImage; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; label1.Text = localnick; connection.SendRawData("Image", ImageConvert.image2Bytes(localImage)); connection.SendRawData("Nick", Encoding.UTF8.GetBytes(localNickName)); connection.RegisterRawDataHandler("image", (RawData, con) => { enemyImage = ImageConvert.bytes2Image(RawData.Data); pictureBox1.Image = enemyImage; }); connection.RegisterRawDataHandler("nick", (RawData, con) => { EnemyNickName = Encoding.UTF8.GetString(RawData.Data); label2.Text = EnemyNickName; }); connection.RegisterRawDataHandler("ready", (RawData, con) => { enemyReady = RawData.ToBoolean(); label2.ForeColor = Color.Green; }); connection.RegisterRawDataHandler("Turn", (RawData, con) => { Turn = RawData.ToBoolean(); }); connection.RegisterRawDataHandler("firstTurn", (Rawdata, con) => { firstTurn = Rawdata.ToBoolean(); Turn = true; }); connection.RegisterRawDataHandler("MapUpdate", (RawData, con) => { char[] newmap = RawData.ToUTF8String().ToArray(); for (int i = 0; i < 9; i++) { map[i] = int.Parse(newmap[i].ToString()); } for (int i = 0; i < 9; i++) { if (map[i] == 1) { panel1.Controls[i].BackgroundImage = Properties.Resources.Cross; } else if (map[i] == 2) { panel1.Controls[i].BackgroundImage = Properties.Resources.Circle; } } }); connection.RegisterRawDataHandler("win", (RawData, Con) => { score += 1; label3.Text = score.ToString(); ClearMap(); }); connection.RegisterRawDataHandler("lose", (RawData, Con) => { enemyScore += 1; label4.Text = enemyScore.ToString(); ClearMap(); }); label1.ForeColor = Color.Red; label2.ForeColor = Color.Red; }