public Form1() { InitializeComponent(); // khởi tạo bàn cờ caro 9 ô (chiều rộng x chiều dài) caro = new Caro(soDong, soCot); // vẽ bàn cờ grs = panelBanCo.CreateGraphics(); }
private static void Receiver() { IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(receivingIP), port); while (!receiverStop) { byte[] data = receivingClient.Receive(ref endPoint); string message = Encoding.ASCII.GetString(data); // get host player name: "get:hostname" string[] mess_code = message.Split(':'); // giao tiếp giữa 2 client switch (mess_code[0]) { case "hello_form_host": // init sender InitSender(receivingIP); // get hostname SendData("get:hostname"); SendData("set:joinname:" + FormLogin.join_name); break; case "set": switch (mess_code[1]) { case "hostname": FormLogin.host_name = mess_code[2]; break; case "joinname": FormLogin.join_name = mess_code[2]; break; case "play": int player = Convert.ToInt32(mess_code[2]); int x = Convert.ToInt32(mess_code[3]); int y = Convert.ToInt32(mess_code[4]); Caro.DanhCo(x, y, player, Form1.grs); Form1.turn++; //MessageBox.Show(Convert.ToString(Form1.turn)); break; case "win": MessageBox.Show("Player " + mess_code[2] + " won"); Caro caro = new Caro(Form1.soDong, Form1.soCot); caro.NewGame(Form1.grs); caro.vebanco(Form1.grs); caro.check(Form1.soDong, Form1.soCot); break; } break; case "get": switch (mess_code[1]) { case "hostname": SendData("set:hostname:" + FormLogin.host_name); break; } break; } } }
private void panelBanCo_MouseClick(object sender, MouseEventArgs e) { if (turn % 2 == 0 && FormLogin.player == 1) //if turn is even { // hiển thị nước đánh Point point = e.Location; int vi_tri = Caro.DanhCo(point.X, point.Y, FormLogin.player, grs); // kiểm tra win if (vi_tri != 0) { // gửi thông tin cho người chơi còn lại biết mày vừa đánh ở đâu LAN.SendData("set:play:" + FormLogin.player + ":" + point.X + ":" + point.Y); bool win = caro.CheckWin(FormLogin.player, vi_tri); KeHuyDiet.Add(vi_tri); turn++; if (win) { // gửi cho thằng chơi cùng biết mày là người chiến thắng LAN.SendData("set:win:" + FormLogin.player); // hiển thị nếu mày là người chiến thắng MessageBox.Show("Player " + FormLogin.player + " won"); // tạo game mới caro.NewGame(grs); caro.vebanco(grs); caro.check(soDong, soCot); } } } else if (turn % 2 != 0 && FormLogin.player == 2) { Point point = e.Location; int vi_tri = Caro.DanhCo(point.X, point.Y, FormLogin.player, grs); if (vi_tri != 0) { LAN.SendData("set:play:" + FormLogin.player + ":" + point.X + ":" + point.Y); bool win = caro.CheckWin(FormLogin.player, vi_tri); KeHuyDiet.Add(vi_tri); turn++; if (win) { // gửi cho thằng chơi cùng biết mày là người chiến thắng LAN.SendData("set:win:" + FormLogin.player); // hiển thị nếu mày là người chiến thắng MessageBox.Show("Player " + FormLogin.player + " won"); caro.NewGame(grs); caro.vebanco(grs); caro.check(soDong, soCot); } } } }