public CPlayer(CPlayer _player) { Row = _player.Row; Column = _player.Column; State = _player.State; ColorPlayer = _player.ColorPlayer; PlayerFlag = _player.PlayerFlag; }
/// <summary> /// Hàm dựng mặc định /// </summary> public MainWindow() { InitializeComponent(); //Thiet lap ban co ban dau cvs_gomoku.Width = cell_quantity * cell_width; cvs_gomoku.Height = cell_quantity * cell_height; border.Width = cvs_gomoku.Width + 4; border.Height = cvs_gomoku.Height + 4; wdw_gomoku.MinHeight = wdw_gomoku.Height = border.Height + 60; wdw_gomoku.MinWidth = wdw_gomoku.Width = border.Width + lvw_chat.Width + 60; user = tbx_name.Text; createMatrix(cell_quantity); color1 = Brushes.Red; color2 = Brushes.Blue; //chế độ chơi mặc đinh player_user = new CPlayer(color1, (int)EPlayerFlag.Player1); player_user.State = true; player_pc = new CPlayer(color2, (int)EPlayerFlag.Player2); player_server = new CPlayer(color2, (int)EPlayerFlag.Server); newGame(); }
/// <summary> /// Xóa tag <br /> và thay bằng \n /// </summary> /// <param name="s">Chuỗi có tag <br /></param> /// <returns></returns> private String strimMes(string s) { for (int i = 25; i < s.Length - 6; i++) { if (s[i] == '<' && s[i + 1] == 'b' && s[i + 2] == 'r' && s[i + 3] == ' ' && s[i + 4] == '/' && s[i + 5] == '>') { string s1 = s.Substring(i+6); if (s1.Equals("You are the first player!")) { testshot_online = 3; if (player_pc.State == true) { Random rd = new Random(); player_pc = new CPlayer(rd.Next(0, 11), rd.Next(0, 11), true, color1, (int)EPlayerFlag.COM); drawGomoku(player_pc); socket.Emit("MyStepIs", JObject.FromObject(new { row = player_pc.Row, col = player_pc.Column })); testshot_online = 2; } } else { testshot_online = 1; } return s = (s.Remove(i, 6)).Insert(i, "\n"); } } return s; }
/// <summary> /// Kết nối /// </summary> private void connectServer() { socket = IO.Socket(System.Configuration.ConfigurationSettings.AppSettings["IPConnectGomoku"]); socket.On(Socket.EVENT_CONNECT, () => { this.Dispatcher.Invoke((Action)(() => { lvw_chat.Items.Add("Server: Connected to Server" + getTime()); })); }); socket.On(Socket.EVENT_MESSAGE, (data) => { this.Dispatcher.Invoke((Action)(() => { lvw_chat.Items.Add(data + ""); })); }); socket.On(Socket.EVENT_CONNECT_ERROR, (data) => { this.Dispatcher.Invoke((Action)(() => { lvw_chat.Items.Add("connect Error"); })); }); socket.On("ChatMessage", (data) => { if (((Newtonsoft.Json.Linq.JObject)data)["message"].ToString() == "Welcome!") { this.Dispatcher.Invoke((Action)(() => { socket.Emit("MyNameIs", user); // socket.Emit("ConnectToOtherPlayer"); })); } else { this.Dispatcher.Invoke((Action)(() => { String name = ""; String s = ((Newtonsoft.Json.Linq.JObject)data)["message"].ToString(); s = strimMes(s); if (name == "") name = "Server: "; else name += ": "; lvw_chat.Items.Add(name + s + getTime()); })); } }); socket.On(Socket.EVENT_ERROR, (data) => { this.Dispatcher.Invoke((Action)(() => { lvw_chat.Items.Add(data + " event error"); })); }); socket.On("NextStepIs", (data) => { this.Dispatcher.Invoke((Action)(() => { lock (synch) { int row = (int)((JObject)data).GetValue("row"); int col = (int)((JObject)data).GetValue("col"); if (checkPoint(col, row)) { testshot_online = 3; player_server = new CPlayer(row, col, true, color2, (int)EPlayerFlag.Server); drawGomoku(player_server); if (checkWinner(player_server)) { showWinner((int)EPlayerFlag.Server); newGame(); } else { //String s = "Your turn!"; //lvw_chat.Items.Add(s); } //Tính nước chơi cho COM if (player_pc.State == true) { lock (synch_pc_ol) { player_pc = findWayforPC(3, 1); player_pc.ColorPlayer = color1; player_pc.State = true; drawGomoku(player_pc); socket.Emit("MyStepIs", JObject.FromObject(new { row = player_pc.Row, col = player_pc.Column })); if (checkWinner(player_pc)) { showWinner((int)EPlayerFlag.Player1); newGame(); } } } } } })); }); }
/// <summary> /// Vẽ cờ lên bàn cờ /// </summary> /// <param name="pObject">Đối tượng được vẽ</param> /// <returns></returns> private bool drawGomoku(CPlayer pObject) { //Xét ô đã được đánh checkPoint(pObject.Column, pObject.Row); Ellipse cell_elip = createElip(cell_width - 4, cell_height - 4, pObject.Column, pObject.Row, pObject.ColorPlayer); matrix[pObject.Column, pObject.Row] = pObject.PlayerFlag; cvs_gomoku.Children.Add(cell_elip); return true; }
/// <summary> /// Tìm kiếm nước đi cho COM /// </summary> /// <param name="p1"></param> /// <param name="p2"></param> /// <returns>Nước đi mới được lưu trong đối tượng CPlayer mới</returns> private CPlayer findWayforPC(int p1, int p2) { int col, row; do { Point p = TimKiemNuocDi(p1, p2); col = (int)p.X; row = (int)p.Y; } while (matrix[col, row] != 0); CPlayer pc_new = new CPlayer(row, col, true, color2, (int)EPlayerFlag.COM); return pc_new; }
/// <summary> /// Kiểm tra người thắng sau khi đi 1 nước /// </summary> /// <param name="pOject">Đối tượng được kiểm tra</param> /// <returns>Cờ cho biết là đã thắng chưa</returns> private bool checkWinner(CPlayer pOject) { int kt = pOject.PlayerFlag; int count = 1; int _row = pOject.Row, _col = pOject.Column; int x = pOject.Column, y = pOject.Row; //kiểm tra theo hàng while (y - 1 >= 0 && matrix[x, y - 1] == kt) { count++; y -= 1; } y = _row; while (y + 1 < 12 && matrix[x, y + 1] == kt) { count++; y += 1; } if (count == 5) return true; //kiểm tra theo cột: x = _col; y = _row; count = 1; while (x - 1 >= 0 && matrix[x - 1, y] == kt) { count++; x -= 1; } x = _col; while (x + 1 < 12 && matrix[x + 1, y] == kt) { count++; x += 1; } if (count == 5) return true; //theo chiều chéo trên xuống x = _col; y = _row; count = 1; while (x - 1 >= 0 && y - 1 >= 0 && matrix[x - 1, y - 1] == kt) { count++; x -= 1; y -= 1; } x = _col; y = _row; while (x + 1 < 12 && y + 1 < 12 && matrix[x + 1, y + 1] == kt) { count++; x += 1; y += 1; } if (count == 5) return true; //theo chiều chéo dưới lên x = _col; y = _row; count = 1; while (x - 1 >= 0 && y + 1 < 12 && matrix[x - 1, y + 1] == kt) { count++; x -= 1; y += 1; } x = _col; y = _row; while (x + 1 < 12 && y - 1 > 0 && matrix[x + 1, y - 1] == kt) { count++; x += 1; y -= 1; } if (count == 5) return true; return false; }
/// <summary> /// Xử lý sự kiện mousedown trên bàn cờ /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cvs_gomoku_MouseDown(object sender, MouseButtonEventArgs e) { lock (synch) { int col = -1, row = -1; Point p = new Point(); p = e.GetPosition(cvs_gomoku); col = (int)(p.X / cell_width); row = (int)(p.Y / cell_height); //xác định vị trí người chơi nhấp vào: if (checkPoint(col, row)) //Kiểm tra ví trí chọn có hợp lệ { //xét trường hợp: người chơi nhấp vào bàn cờ /*TH1: Người chơi có chọn chế độ chơi * TH1.1 Người chơi chơi offline */ if (player_user.State == true) { if (player_server.State == false) { //chế độ người chơi offine 1vs2 player_user = new CPlayer(row, col, true); //kiểm tra chơi người vs người if (player_pc.State == false) { testshot = 3 - testshot; player_user.PlayerFlag = testshot; player_user.ColorPlayer = testshot == (int)EPlayerFlag.Player1 ? color1 : color2; drawGomoku(player_user); if (checkWinner(player_user)) showWinner(testshot); } else //1vsCOM { player_user = new CPlayer(row, col, true, color1, (int)EPlayerFlag.Player1); drawGomoku(player_user); if (checkWinner(player_user)) showWinner((int)EPlayerFlag.Player1); else { player_pc = findWayforPC(1, 2); drawGomoku(player_pc); if (checkWinner(player_pc)) showWinner((int)EPlayerFlag.COM); } testshot = 2; } } else //chế độ online { if (player_pc.State == false) { if (testshot_online == 3) { testshot_online = 4 - testshot_online; player_user = new CPlayer(row, col, true, color1, (int)EPlayerFlag.Player1); drawGomoku(player_user); socket.Emit("MyStepIs", JObject.FromObject(new { row = row, col = col })); if (checkWinner(player_user)) showWinner((int)EPlayerFlag.Player1); } else { lvw_chat.Items.Add("Server: This is not your turn" + getTime()); } } } } } } }