public void DetectServer() { string ip = "127.0.0.1"; tcpForChoosingServer = new TCPModel(ip, port); tcpForChoosingServer.ConnectToServer(); string str = tcpForChoosingServer.ReadData(); port = int.Parse(str); Console.WriteLine("Port la: " + port); }
private void btnLogin_Click(object sender, EventArgs e) { Connect(); tcpForPlayer.SendData("dangnhap"); string t = tcpForPlayer.ReadData(); MessageBox.Show(t); Client c = new Client(tcpForPlayer, tcpForOpponent); this.Hide(); c.ShowDialog(); this.Show(); }
private void btnSearchRoom_Click(object sender, EventArgs e) { tcpForPlayer.SendData("timphong"); //string t=f.tcpForPlayer.ReadData(); string t = tcpForPlayer.ReadData(); MessageBox.Show(t); DanhBai d = new DanhBai(tcpForPlayer, tcpForOpponent); this.Hide(); d.ShowDialog(); //tcpForPlayer.SendData(textBox1.Text); //textBox2.Text=tcpForOpponent.ReadData(); }
private void btnReady_Click(object sender, EventArgs e) { tcpForPlayer.SendData("chiabai"); string result = tcpForPlayer.ReadData(); //Nếu người chơi nhận được lượt đánh thì enable btbDanh int players = Convert.ToInt32(result[result.Length - 2].ToString()); op = new int[players - 1]; int Myturn = Convert.ToInt16(result.Last().ToString()); op[0] = (Myturn + 1) % players; for (int i = 1; i < players - 1; i++) { op[i] = (op[i - 1] + 1) % players; } if (result.Contains("turn")) { result = result.Remove(result.Length - 6);//Xóa 6 kí tự cuối chuối(là string "turni") btnDanh.Enabled = true; } else { result = result.Remove(result.Length - 2); } ConvertToListDouble(BaiHienTai, result);//Convert từ string sang List<double> //Kiểm tra xem bài của người cho có tới trắng hay không if (XuLyBai.KiemTraToiTrang(BaiHienTai) == true) { tcpForPlayer.SendData(ConvertToString(BaiHienTai) + "wintrang"); } txbBai.Text = ConvertToString(BaiHienTai);//gọi hàm load hình LoadHinh(BaiHienTai) for (int i = 0; i < BaiHienTai.Count(); i++) { pictureBoxList1[i].Visible = true; pictureBoxList1[i].Image = Image.FromFile(path + BaiHienTai[i].ToString() + ".png"); pictureBoxList1[i].BackColor = Color.White; pictureBoxList1[i].BorderStyle = BorderStyle.FixedSingle; pictureBoxList1[i].Tag = BaiHienTai[i]; } btnReady.Enabled = false; }
public DanhBai(TCPModel player, TCPModel Opponent) { InitializeComponent(); tcpForPlayer = player; tcpForOpponent = Opponent; btnDanh.Enabled = false; btnBoLuot.Enabled = false; if (tcpForPlayer.ReadData() == "isplaying") { btnReady.Enabled = false; } Y = pictureBox1.Location.Y; pictureBoxList1.Add(pictureBox1); pictureBoxList1.Add(pictureBox2); pictureBoxList1.Add(pictureBox3); pictureBoxList1.Add(pictureBox4); pictureBoxList1.Add(pictureBox5); pictureBoxList1.Add(pictureBox6); pictureBoxList1.Add(pictureBox7); pictureBoxList1.Add(pictureBox8); pictureBoxList1.Add(pictureBox9); pictureBoxList1.Add(pictureBox10); pictureBoxList1.Add(pictureBox11); pictureBoxList1.Add(pictureBox12); pictureBoxList1.Add(pictureBox13); pictureBoxList2.Add(pictureBox14); pictureBoxList2.Add(pictureBox15); pictureBoxList2.Add(pictureBox16); pictureBoxList2.Add(pictureBox17); pictureBoxList2.Add(pictureBox18); pictureBoxList2.Add(pictureBox19); pictureBoxList2.Add(pictureBox20); pictureBoxList2.Add(pictureBox21); pictureBoxList2.Add(pictureBox22); pictureBoxList2.Add(pictureBox23); pictureBoxList2.Add(pictureBox24); pictureBoxList2.Add(pictureBox25); pictureBoxList2.Add(pictureBox26); Thread t = new Thread(NhanBai); t.Start(); }
void NhanBai()//Dùng để nhận bài của đối thủ { while (true) { try { string result = tcpForOpponent.ReadData(); //Bài của đối thủ vừa đánh //Nếu nhận được "newturn" thì người chơi có quyền đánh những lá tùy ý. if (result == "newturn") { BaiCuaDoiThu.Clear(); btnDanh.Enabled = true; } //Trường người chơi trước bỏ lượt, và lượt hiện tại của người chơi này else if (result == "turn") { btnDanh.Enabled = true; btnBoLuot.Enabled = true; } //Trường hợp tới lượt đánh của người chơi else if (result.Contains("turn")) { MessageBox.Show(result.Last().ToString()); result = result.Remove(result.Length - 5);//Xóa 4 kí tự cuối chuối(là string "turn") btnDanh.Enabled = true; btnBoLuot.Enabled = true; ConvertToListDouble(BaiCuaDoiThu, result); string temp = ConvertToString(BaiCuaDoiThu); txtBaiCuaDoiThu.Text = temp; // Gọi hàm load hình: LoadHinh(BaiCuaDoiThu); //for (int i = 0; i < 13; i++) //{ // pictureBoxList3[i].Image.Dispose(); // pictureBoxList3[i].BackColor = DefaultBackColor; // pictureBoxList3[i].BorderStyle = BorderStyle.None; // pictureBoxList3[i].Tag = null; //} //List<double> y = new List<double> { 3.1 }; //for (int i = 0; i < y.Count(); i++) //{ // pictureBoxList3[i].Image = Image.FromFile(path + y[i].ToString() + ".png"); // pictureBoxList3[i].BackColor = Color.White; // pictureBoxList3[i].BorderStyle = BorderStyle.FixedSingle; // pictureBoxList3[i].Tag = y[i]; // // pictureBoxList3[i].Visible = true; //} HienThiBai(temp); } //Đây là trường hợp đã có người chơi trong bàn win. Tiến hành Load bài đó và set lại game else if (result.Contains("win")) { MessageBox.Show("Player " + result.Last() + " win"); result = result.Remove(result.Length - 4); ConvertToListDouble(BaiCuaDoiThu, result); txtBaiCuaDoiThu.Text = ConvertToString(BaiCuaDoiThu);//Load bài của đối thủ //for (int i = 0; i < 13; i++) //{ // pictureBoxList2[i].Image = null; // pictureBoxList2[i].BackColor = DefaultBackColor; // pictureBoxList2[i].BorderStyle = BorderStyle.None; // pictureBoxList2[i].Tag = null; //} //for (int i = 0; i < BaiCuaDoiThu.Count(); i++) //{ // pictureBoxList2[i].Image = Image.FromFile(path + BaiCuaDoiThu[i].ToString() + ".png"); // pictureBoxList2[i].BackColor = Color.White; // pictureBoxList2[i].BorderStyle = BorderStyle.FixedSingle; // pictureBoxList2[i].Tag = BaiCuaDoiThu[i]; //} BaiHienTai.Clear(); BaiCuaDoiThu.Clear(); btnDanh.Enabled = false; btnBoLuot.Enabled = false; btnReady.Enabled = true; } //Đây là trường hợp người chơi không có lượt đánh. Chỉ nhận được bài của đối thủ else if (char.IsDigit(result[0])) { MessageBox.Show(result.Last().ToString()); result = result.Remove(result.Length - 1); ConvertToListDouble(BaiCuaDoiThu, result); txtBaiCuaDoiThu.Text = ConvertToString(BaiCuaDoiThu);//Load bài của đối thủ //ResetPictureBoxList3(BaiCuaDoiThu.Count()); //for (int i = 0; i < BaiCuaDoiThu.Count(); i++) //{ // pictureBoxList3[i].Image = Image.FromFile(path + BaiCuaDoiThu[i].ToString() + ".png"); // pictureBoxList3[i].BackColor = Color.White; // pictureBoxList3[i].BorderStyle = BorderStyle.FixedSingle; // pictureBoxList3[i].Tag = BaiCuaDoiThu[i]; //} string temp = ConvertToString(BaiCuaDoiThu); HienThiBai(temp); } else if (result.Contains("boluot")) { MessageBox.Show("Player " + result.Last() + " pass"); } result = ""; } catch (Exception e) { MessageBox.Show(e.StackTrace); } } }
public void Receive() { while (true) { bool anycards = true; string s = tcpForPlayer.ReadData(); if (string.Equals(s, "new")) { New = true; } // else { New = false; } string a = s; if (string.Equals(s, "OK")) //check "ok" signal from room { button2.Enabled = true; button3.Enabled = true; button2.BackColor = Color.PeachPuff; button3.BackColor = Color.PeachPuff; MessageBox.Show("It's " + this.Text + " turn"); } if (!Loaded) { arrPBleft[0] = pictureBox40; arrPBmid[0] = pictureBox27; arrPBright[0] = pictureBox53; arrPBleft[1] = pictureBox41; arrPBmid[1] = pictureBox28; arrPBright[1] = pictureBox54; arrPBleft[2] = pictureBox42; arrPBmid[2] = pictureBox29; arrPBright[2] = pictureBox55; arrPBleft[3] = pictureBox43; arrPBmid[3] = pictureBox30; arrPBright[3] = pictureBox56; arrPBleft[4] = pictureBox44; arrPBmid[4] = pictureBox31; arrPBright[4] = pictureBox57; arrPBleft[5] = pictureBox45; arrPBmid[5] = pictureBox32; arrPBright[5] = pictureBox58; arrPBleft[6] = pictureBox46; arrPBmid[6] = pictureBox33; arrPBright[6] = pictureBox59; arrPBleft[7] = pictureBox47; arrPBmid[7] = pictureBox34; arrPBright[7] = pictureBox60; arrPBleft[8] = pictureBox48; arrPBmid[8] = pictureBox35; arrPBright[8] = pictureBox61; arrPBleft[9] = pictureBox49; arrPBmid[9] = pictureBox36; arrPBright[9] = pictureBox62; arrPBleft[10] = pictureBox50; arrPBmid[10] = pictureBox37; arrPBright[10] = pictureBox63; arrPBleft[11] = pictureBox51; arrPBmid[11] = pictureBox38; arrPBright[11] = pictureBox64; arrPBleft[12] = pictureBox52; arrPBmid[12] = pictureBox39; arrPBright[12] = pictureBox65; for (int i = 0; i < 13; i++) { string temp = @"PNG\" + "blue_back.png";//img link arrPBleft[i].Image = Image.FromFile(temp); arrPBleft[i].SizeMode = PictureBoxSizeMode.StretchImage; arrPBmid[i].Image = Image.FromFile(temp); arrPBmid[i].SizeMode = PictureBoxSizeMode.StretchImage; arrPBright[i].Image = Image.FromFile(temp); arrPBright[i].SizeMode = PictureBoxSizeMode.StretchImage; } //load opp cards deck = s.Split(';');//get cards from server and split for (int i = 0; i < 13; i++) { Sort(deck, 13); if (string.Equals(deck[i], "15.1") || string.Equals(deck[i], "15.2") || string.Equals(deck[i], "15.3") || string.Equals(deck[i], "15.4")) { dem_heo++; } string temp = @"PNG\" + deck[i].Replace('.', '_') + ".png";//img link arrPB[i].Image = Image.FromFile(temp); arrPB[i].SizeMode = PictureBoxSizeMode.StretchImage; } Loaded = true; } if (anycards)//opp cards { try { if (!string.Equals(a, "OK")) { if (a.Contains("l>")) { string[] tam = a.Split('>'); //MessageBox.Show(tam[1]); string[] demso = tam[1].Split('/'); for (int i = 0; i < demso.Count() - 1; i++) { arrPBleft[l].Visible = false; l++; } a = a.Remove(0, 2); //MessageBox.Show(a); } if (a.Contains("m>")) { string[] tam = a.Split('>'); //MessageBox.Show(tam[1]); string[] demso = tam[1].Split('/'); for (int i = 0; i < demso.Count() - 1; i++) { arrPBmid[m].Visible = false; m++; } a = a.Remove(0, 2); //MessageBox.Show(a); } if (a.Contains("r>")) { string[] tam = a.Split('>'); //MessageBox.Show(tam[1]); string[] demso = tam[1].Split('/'); for (int i = 0; i < demso.Count() - 1; i++) { arrPBright[r].Visible = false; r++; } a = a.Remove(0, 2); //MessageBox.Show(a); } handDeck = a.Split('/'); bool havecard = false; for (int i = 0; i < 13; i++) { if (arrPBhands[i].Image != null) { havecard = true; } } if (havecard && button2.Enabled == false) { for (int i = 0; i < 13; i++) { arrPBhands[i].Image = null; } } for (int i = 0; i < handDeck.Count() - 1; i++) { string temp = @"PNG\" + handDeck[i].Replace('.', '_') + ".png"; arrPBhands[i].Image = Image.FromFile(temp); arrPBhands[i].SizeMode = PictureBoxSizeMode.StretchImage; } } }catch { anycards = false; } } if (Loaded) { int cwin = 0, cwin1 = 0; for (int i = 0; i < 13; i++) { if (string.Equals(deck[i], "3.1") || string.Equals(deck[i], "3.2") || string.Equals(deck[i], "3.3") || string.Equals(deck[i], "3.4")) { cwin++; } if (string.Equals(deck[i], "15.1") || string.Equals(deck[i], "15.2") || string.Equals(deck[i], "15.3") || string.Equals(deck[i], "15.4")) { cwin1++; } } if (cwin == 4 || cwin1 == 4) { MessageBox.Show(this.Text + " win!"); tcpForPlayer.SendData("~" + this.Text + " win!"); button1.Enabled = true; done = true; } if (s.Contains('~')) { button1.Enabled = true; button2.Enabled = button3.Enabled = false; MessageBox.Show(s); //string[] win = s.Split('~'); //string temp = ""; //for (int i = 0; i < (win.Count() - 1); i++) //{ // temp = temp + win[i]; // button1.Enabled = true; //} //MessageBox.Show(temp); done = true; return; } // MessageBox.Show("win count: " + win_count.ToString()); } } }
public void Commmunication() { int rem = 0; string[] s; string[] OrderAndCard; while (true) { string str = tcpForPlayer.ReadData(); s = str.Split('#'); //MessageBox.Show(str); //Console.WriteLine(s); for (int z = 1; z < s.Length; z++) { OrderAndCard = s[z].Split('|'); //for(int i=0;i<OrderAndCard.Length;i++) //{ // MessageBox.Show(OrderAndCard[i]); //} switch (OrderAndCard[0]) { case "Init": for (int i = 0; i < 4; i++) { if (OrderAndCard[i + 1] != tcpForPlayer.UpdateInformation()) { for (int y = 0; y < 13; y++) { Enemy[rem, y].Visible = true; } EnemyName[rem] = OrderAndCard[i + 1]; EnemyCardLeft[rem] = 12; rem++; } } label3.Text = EnemyName[0]; label4.Text = EnemyName[1]; label5.Text = EnemyName[2]; break; case "NewTurn": label7.Text = ""; label8.Text = ""; label9.Text = ""; label10.Text = ""; HideButton(CardHavePlay); break; case "Yourturn": label6.Text = OrderAndCard[1] + "'s Turn"; if (OrderAndCard[1] == tcpForPlayer.UpdateInformation()) { OPlayerCard.Clear(); for (int i = 2; i < OrderAndCard.Length; i++) { OPlayerCard.Add(new Card(OrderAndCard[i])); } yourturn = true; label2.Text = "yourturn"; PBCountDown.Visible = true; PBCountDown.Value = 0; BeginInvoke((Action)(() => CountDownTimer.Start())); } break; case "Skip": //MessageBox.Show("here"); //MessageBox.Show(s[z]); if (OrderAndCard[1] == tcpForPlayer.UpdateInformation()) { label10.Text = "Bo qua"; break; } for (int i = 0; i < 3; i++) { if (EnemyName[i] == OrderAndCard[1]) { switch (i) { case 0: label7.Text = "Bo qua"; break; case 1: label8.Text = "Bo qua"; break; case 2: label9.Text = "Bo qua"; break; } break; } } break; case "YourCard": GetCard(OrderAndCard); MyCard.Sort(compare); SeperateData(MyCard, CardButton); MatchForm(); break; case "PlayerTurn": SeperateData(OrderAndCard, CardHavePlay); for (int i = 0; i < 3; i++) { if (EnemyName[i] == OrderAndCard[1]) { //MessageBox.Show(s[z]); //MessageBox.Show(EnemyCardLeft[i].ToString()); //MessageBox.Show((EnemyCardLeft[i] - OrderAndCard.Length ).ToString()); for (int y = EnemyCardLeft[i]; y > EnemyCardLeft[i] - OrderAndCard.Length + 2; y--) { Enemy[i, y].Visible = false; } EnemyCardLeft[i] = EnemyCardLeft[i] - OrderAndCard.Count() + 2; break; } } break; case "CreateRoomSuccessful": MessageBox.Show("CreateRoomSuccessful"); ReadyForm(); label1.Visible = true; label1.Text = "Room: " + OrderAndCard[1]; break; case "RoomFound": MessageBox.Show("RoomFound"); ReadyForm(); label1.Visible = true; label1.Text = "Room: " + OrderAndCard[1]; break; case "Win": Thread.Sleep(1000); for (int i = 0; i < 3; i++) { for (int y = 0; y < 13; y++) { Enemy[i, y].Visible = false; } } WiningForm(tcpForPlayer.UpdateInformation() == OrderAndCard[1]); label3.Text = ""; label4.Text = ""; label5.Text = ""; label6.Text = ""; label7.Text = ""; label8.Text = ""; label9.Text = ""; label10.Text = ""; HideButton(CardButton); HideButton(CardHavePlay); MyCard.Clear(); break; //case "FlipDown": // FlipDown(Int32.Parse(OrderAndCard[1]), CardHavePlay); // break; case "Exit": HideButton(CardButton); HideButton(CardHavePlay); ChooseRoomForm(); CountDownTimer.Stop(); PBCountDown.Visible = false; label2.Visible = false; label1.Visible = false; MyCard.Clear(); break; } } } //while (true) //{ // Console.WriteLine("chokhang"); //} }