//Contructors public BanCo(MainWindow frm, Grid grd) { Option = new Option(); OWin = new _5O_Win(); row = column = 12; currPlayer = Player.None; end = Player.None; frmParent = frm; grdBanCo = grd; board = new Player[row, column]; ResetBoard(); eBoard = new LuongGiaBanCo(this); hv = new HinhVuong(); CreateHV(); grdBanCo.Children.Add(hv); grdBanCo.MouseDown += new System.Windows.Input.MouseButtonEventHandler(grdBanCo_MouseDown); }
//ván mới public void newGame() { isWin = false; isAIOnlineWin = false; turn = 1; _onlineturn = -1; player1 = new Player(); player2 = new Player(); BoardMerge = new int[12, 12]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Button a = (Button)grid.Children[12 * i + j]; if (i % 2 == 0 && j % 2 == 0 || i % 2 != 0 && j % 2 != 0) { setBackground(a, name0); } else if (i % 2 == 0 && j % 2 != 0 || i % 2 != 0 && j % 2 == 0) { setBackground(a, name1); } } } }
//kiểm tra ô trống trên bàn cờ bool isblank(Player player, int x, int y) { if (player.play[x, y] == 0) return true; return false; }
//highlight dòng win offline private void highlight(Player player, int result, String path) { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (player.play[i, j] == result) { Button k = (Button)grid.Children[12 * i + j]; setBackground(k, path); } } } }
public bool RuleFiveBreakable(Player playerplaying, Position pos) { System.Console.WriteLine("Rule 5 breakable..."); Position firstOfLine; int lenght; for (int xAxis = -1; xAxis <= 1; xAxis++) { for (int yAxis = -1; yAxis <= 1; yAxis++) { if (xAxis != 0 || yAxis != 0) { firstOfLine = GetFirstOfLine(pos, xAxis, yAxis, playerplaying._numPlayer); lenght = GetAlignLenght(firstOfLine, xAxis, yAxis, playerplaying._numPlayer); for (int z = 0; z <= (lenght - 5); z++) { if (CheckUnbreakableAlign(firstOfLine, xAxis, yAxis, 5, playerplaying._numPlayer, false)) { _referee._winner = playerplaying._numPlayer; return true; } firstOfLine.x -= xAxis; firstOfLine.y -= yAxis; } } } } return false; }
public bool RuleDoubleThree(Player playerplaying, Position pos) { System.Console.WriteLine("Rule double 3..."); int checkPos = -3; // Attention !!! _interBoard[pos.x, pos.y]._value = playerplaying._numPlayer; while (checkPos < 0) { int[] cpt = { 0, 0, 0, 0 }; for (int i = 0; i < 5; i++) { cpt[0] += checkDoubleThree(Orientation.Horizontal, checkPos, i, pos, playerplaying._numPlayer, 4); cpt[1] += checkDoubleThree(Orientation.Vertical, checkPos, i, pos, playerplaying._numPlayer, 4); cpt[2] += checkDoubleThree(Orientation.DownDiagonal, checkPos, i, pos, playerplaying._numPlayer, 4); cpt[3] += checkDoubleThree(Orientation.UpDiagonal, checkPos, i, pos, playerplaying._numPlayer, 4); } if (cpt[0] == 3 || cpt[1] == 3 || cpt[2] == 3 || cpt[3] == 3) { Console.WriteLine("\nTrois libre alligné !\n"); } checkPos++; } checkPos = -4; while (checkPos < 0) { int[] cpt = { 0, 0, 0, 0 }; for (int i = 0; i < 6; i++) { cpt[0] += checkDoubleThree(Orientation.Horizontal, checkPos, i, pos, playerplaying._numPlayer, 5); cpt[1] += checkDoubleThree(Orientation.Vertical, checkPos, i, pos, playerplaying._numPlayer, 5); cpt[2] += checkDoubleThree(Orientation.DownDiagonal, checkPos, i, pos, playerplaying._numPlayer, 5); cpt[3] += checkDoubleThree(Orientation.UpDiagonal, checkPos, i, pos, playerplaying._numPlayer, 5); } if (cpt[0] == 3 || cpt[1] == 3 || cpt[2] == 3 || cpt[3] == 3) { Console.WriteLine("\nTrois libre espacé !\n"); } checkPos++; } // Attention !!! _interBoard[pos.x, pos.y]._value = 0; return true; }
public bool RuleCaptureTenStones(Player playerplaying, Position pos) { System.Console.WriteLine("Rule 10 stones..."); for (int xAxis = -1; xAxis <= 1; xAxis++) { for (int yAxis = -1; yAxis <= 1; yAxis++) { if (xAxis != 0 || yAxis != 0) { if (CheckCapture(pos, xAxis, yAxis, 3, playerplaying._numPlayer)) { Position rmvPos = new Position(pos.x + xAxis, pos.y + yAxis); SetInterValue(rmvPos, 0); SetByteValue(rmvPos, 0); rmvPos.x += xAxis; rmvPos.y += yAxis; SetInterValue(rmvPos, 0); SetByteValue(rmvPos, 0); playerplaying._nbToken += 2; } } } } if (playerplaying._nbToken >= 10) { _referee._winner = playerplaying._numPlayer; return true; } return false; }
public bool RuleAlignFiveStones(Player playerplaying, Position pos) { System.Console.WriteLine("Rule 5 align..."); Position firstOfLine; for (int xAxis = -1; xAxis <= 1; xAxis++) { for (int yAxis = -1; yAxis <= 1; yAxis++) { if (xAxis != 0 || yAxis != 0) { firstOfLine = GetFirstOfLine(pos, xAxis, yAxis, playerplaying._numPlayer); if (CheckAlign(firstOfLine, xAxis, yAxis, 5, playerplaying._numPlayer)) { _referee._winner = playerplaying._numPlayer; return true; } } } } return false; }
public bool putTocken(Player playerplaying, Position pos) { System.Console.WriteLine("Puting token..."); bool isTokenValid = false; if (possiblePos(playerplaying, pos)) { if (_isRuleDoubleThree) { if (RuleDoubleThree(playerplaying, pos)) { SetInterValue(pos, playerplaying._numPlayer); SetByteValue(pos, playerplaying._numPlayer); isTokenValid = true; } } else { SetInterValue(pos, playerplaying._numPlayer); SetByteValue(pos, playerplaying._numPlayer); isTokenValid = true; } if (isTokenValid) { if (!RuleCaptureTenStones(playerplaying, pos)) { if (_isRuleBreakable) { RuleFiveBreakable(playerplaying, pos); RefreshBreakable(); } else { RuleAlignFiveStones(playerplaying, pos); } } } } return isTokenValid; }
public bool possiblePos(Player playerplaying, Position pos) { if (pos.x >= 0 && pos.x < 19 && pos.y >= 0 && pos.y < 19) { if (_interBoard[pos.x, pos.y]._value == 0) { return true; } } return false; }
//Hàm lấy đối thủ của người chơi hiện tại private Player DoiNgich(Player cur) { if (cur == Player.Com) return Player.Human; if (cur == Player.Human) return Player.Com; return Player.None; }
//Hàm lượng giá thế cờ private void LuongGia(Player player) { int cntHuman = 0, cntCom = 0;//Biến đếm Human,Com #region Luong gia cho hang for (int i = 0; i < row; i++) { for (int j = 0; j < column - 4; j++) { //Khởi tạo biến đếm cntHuman = cntCom = 0; //Đếm số lượng con cờ trên 5 ô kế tiếp của 1 hàng for (int k = 0; k < 5; k++) { if (board[i, j + k] == Player.Human) cntHuman++; if (board[i, j + k] == Player.Com) cntCom++; } //Lượng giá //Nếu 5 ô kế tiếp chỉ có 1 loại cờ (hoặc là Human,hoặc la Com) if (cntHuman * cntCom == 0 && cntHuman != cntCom) { //Gán giá trị cho 5 ô kế tiếp của 1 hàng for (int k = 0; k < 5; k++) { //Nếu ô đó chưa có quân đi if (board[i, j + k] == Player.None) { //Nếu trong 5 ô đó chỉ tồn tại cờ của Human if (cntCom == 0) { //Nếu đối tượng lượng giá là Com if (player == Player.Com) { //Vì đối tượng người chơi là Com mà trong 5 ô này chỉ có Human //nên ta sẽ cộng thêm điểm phòng thủ cho Com eBoard.GiaTri[i, j + k] += PhongThu[cntHuman]; } //Ngược lại cộng điểm phòng thủ cho Human else { eBoard.GiaTri[i, j + k] += TanCong[cntHuman]; } //Nếu chơi theo luật Việt Nam if (this.Option.GamePlay == LuatChoi.Vietnamese) //Xét trường hợp chặn 2 đầu //Nếu chận 2 đầu thì gán giá trị cho các ô đó bằng 0 if (j - 1 >= 0 && j + 5 <= column - 1 && board[i, j - 1] == Player.Com && board[i, j + 5] == Player.Com) { eBoard.GiaTri[i, j + k] = 0; } } //Tương tự như trên if (cntHuman == 0) //Nếu chỉ tồn tại Com { if (player == Player.Human) //Nếu người chơi là Người { eBoard.GiaTri[i, j + k] += PhongThu[cntCom]; } else { eBoard.GiaTri[i, j + k] += TanCong[cntCom]; } //Trường hợp chặn 2 đầu if (this.Option.GamePlay == LuatChoi.Vietnamese) if (j - 1 >= 0 && j + 5 <= column - 1 && board[i, j - 1] == Player.Human && board[i, j + 5] == Player.Human) { eBoard.GiaTri[i, j + k] = 0; } } if ((j + k - 1 > 0) && (j + k + 1 <= column - 1) && (cntHuman == 4 || cntCom == 4) && (board[i, j + k - 1] == Player.None || board[i, j + k + 1] == Player.None)) { eBoard.GiaTri[i, j + k] *= 3; } } } } } } #endregion //Tương tự như lượng giá cho hàng #region Luong gia cho cot for (int i = 0; i < row - 4; i++) { for (int j = 0; j < column; j++) { cntHuman = cntCom = 0; for (int k = 0; k < 5; k++) { if (board[i + k, j] == Player.Human) cntHuman++; if (board[i + k, j] == Player.Com) cntCom++; } if (cntHuman * cntCom == 0 && cntCom != cntHuman) { for (int k = 0; k < 5; k++) { if (board[i + k, j] == Player.None) { if (cntCom == 0) { if (player == Player.Com) eBoard.GiaTri[i + k, j] += PhongThu[cntHuman]; else eBoard.GiaTri[i + k, j] += TanCong[cntHuman]; // Truong hop bi chan 2 dau. if ((i - 1) >= 0 && (i + 5) <= row - 1 && board[i - 1, j] == Player.Com && board[i + 5, j] == Player.Com) { eBoard.GiaTri[i + k, j] = 0; } } if (cntHuman == 0) { if (player == Player.Human) eBoard.GiaTri[i + k, j] += PhongThu[cntCom]; else eBoard.GiaTri[i + k, j] += TanCong[cntCom]; // Truong hop bi chan 2 dau. if (this.Option.GamePlay == LuatChoi.Vietnamese) if (i - 1 >= 0 && i + 5 <= row - 1 && board[i - 1, j] == Player.Human && board[i + 5, j] == Player.Human) eBoard.GiaTri[i + k, j] = 0; } if ((i + k - 1) >= 0 && (i + k + 1) <= row - 1 && (cntHuman == 4 || cntCom == 4) && (board[i + k - 1, j] == Player.None || board[i + k + 1, j] == Player.None)) { eBoard.GiaTri[i + k, j] *= 3; } } } } } } #endregion //Tương tự như lượng giá cho hàng #region Luong gia tren duong cheo chinh (\) for (int i = 0; i < row - 4; i++) { for (int j = 0; j < column - 4; j++) { cntHuman = cntCom = 0; for (int k = 0; k < 5; k++) { if (board[i + k, j + k] == Player.Human) cntHuman++; if (board[i + k, j + k] == Player.Com) cntCom++; } if (cntHuman * cntCom == 0 && cntCom != cntHuman) { for (int k = 0; k < 5; k++) { if (board[i + k, j + k] == Player.None) { if (cntCom == 0) { if (player == Player.Com) eBoard.GiaTri[i + k, j + k] += PhongThu[cntHuman]; else eBoard.GiaTri[i + k, j + k] += TanCong[cntHuman]; // Truong hop bi chan 2 dau. if (this.Option.GamePlay == LuatChoi.Vietnamese) if (i - 1 >= 0 && j - 1 >= 0 && i + 5 <= row - 1 && j + 5 <= column - 1 && board[i - 1, j - 1] == Player.Com && board[i + 5, j + 5] == Player.Com) eBoard.GiaTri[i + k, j + k] = 0; } if (cntHuman == 0) { if (player == Player.Human) eBoard.GiaTri[i + k, j + k] += PhongThu[cntCom]; else eBoard.GiaTri[i + k, j + k] += TanCong[cntCom]; // Truong hop bi chan 2 dau. if (this.Option.GamePlay == LuatChoi.Vietnamese) if ((i - 1) >= 0 && j - 1 >= 0 && i + 5 <= row - 1 && j + 5 <= column - 1 && board[i - 1, j - 1] == Player.Human && board[i + 5, j + 5] == Player.Human) { eBoard.GiaTri[i + k, j + k] = 0; } } if ((i + k - 1) >= 0 && (j + k - 1) >= 0 && (i + k + 1) <= row - 1 && (j + k + 1) <= column - 1 && (cntHuman == 4 || cntCom == 4) && (board[i + k - 1, j + k - 1] == Player.None || board[i + k + 1, j + k + 1] == Player.None)) { eBoard.GiaTri[i + k, j + k] *= 3; } } } } } } #endregion //Tương tự như lượng giá cho hàng #region Luong gia tren duong cheo phu (/) for (int i = 4; i < row - 4; i++) { for (int j = 0; j < column - 4; j++) { cntCom = 0; cntHuman = 0; for (int k = 0; k < 5; k++) { if (board[i - k, j + k] == Player.Human) cntHuman++; if (board[i - k, j + k] == Player.Com) cntCom++; } if (cntHuman * cntCom == 0 && cntHuman != cntCom) { for (int k = 0; k < 5; k++) { if (board[i - k, j + k] == Player.None) { if (cntCom == 0) { if (player == Player.Com) eBoard.GiaTri[i - k, j + k] += PhongThu[cntHuman]; else eBoard.GiaTri[i - k, j + k] += TanCong[cntHuman]; // Truong hop bi chan 2 dau. if (i + 1 <= row - 1 && j - 1 >= 0 && i - 5 >= 0 && j + 5 <= column - 1 && board[i + 1, j - 1] == Player.Com && board[i - 5, j + 5] == Player.Com) { eBoard.GiaTri[i - k, j + k] = 0; } } if (cntHuman == 0) { if (player == Player.Human) eBoard.GiaTri[i - k, j + k] += PhongThu[cntCom]; else eBoard.GiaTri[i - k, j + k] += TanCong[cntCom]; // Truong hop bi chan 2 dau. if (this.Option.GamePlay == LuatChoi.Vietnamese) if (i + 1 <= row - 1 && j - 1 >= 0 && i - 5 >= 0 && j + 5 <= column - 1 && board[i + 1, j - 1] == Player.Human && board[i - 5, j + 5] == Player.Human) { eBoard.GiaTri[i - k, j + k] = 0; } } if ((i - k + 1) <= row - 1 && (j + k - 1) >= 0 && (i - k - 1) >= 0 && (j + k + 1) <= column - 1 && (cntHuman == 4 || cntCom == 4) && (board[i - k + 1, j + k - 1] == Player.None || board[i - k - 1, j + k + 1] == Player.None)) { eBoard.GiaTri[i - k, j + k] *= 3; } } } } } } #endregion }
//Hàm đánh cờ public void grdBanCo_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { System.GC.Collect();//Thu gôm rác if (this.Option.WhoPlayWith == Player.Com)//Nếu chọn kiểu chơi đánh với máy { Point toado = e.GetPosition(grdBanCo); //Lấy tọa độ chuột //Xử lý tọa độ int cl = ((int)toado.X / length); int rw = ((int)toado.Y / length); Node node = new Node(); if (board[rw, cl] == Player.None) //Nếu ô bấm chưa có cờ { if (currPlayer == Player.Human && end == Player.None)//Nếu lượt đi là người và trận đấu chưa kết thúc { board[rw, cl] = currPlayer;//Lưu loại cờ vừa đánh vào mảng DrawDataBoard(rw, cl, true, true);//Vẽ con cờ theo lượt chơi end = CheckEnd(rw, cl);//Kiểm tra xem trận đấu kết thúc chưa if (end == Player.Human)//Nếu người thắng cuộc là người { OnWin();//Khai báo sự kiện Win OnWinOrLose();//Hiển thị 5 ô Win. } else if (end == Player.None) //Nếu trận đấu chưa kết thúc { currPlayer = Player.Com;//Thiết lập lại lượt chơi OnHumanDanhXong(); // Khai báo sự kiện người đánh xong } } if (currPlayer == Player.Com && end == Player.None)//Nếu lượt đi là máy và trận đấu chưa kết thúc { //Tìm đường đi cho máy eBoard.ResetBoard(); LuongGia(Player.Com);//Lượng giá bàn cờ cho máy node = eBoard.GetMaxNode();//lưu vị trí máy sẽ đánh int r, c; r = node.Row; c = node.Column; board[r, c] = currPlayer; //Lưu loại cờ vừa đánh vào mảng DrawDataBoard(r, c, true, true); //Vẽ con cờ theo lượt chơi end = CheckEnd(r, c);//Kiểm tra xem trận đấu kết thúc chưa if (end == Player.Com)//Nếu máy thắng { OnLose();//Khai báo sư kiện Lose OnWinOrLose();//Hiển thị 5 ô Lose. } else if (end == Player.None) { currPlayer = Player.Human;//Thiết lập lại lượt chơi OnComDanhXong();// Khai báo sự kiện người đánh xong } } } } else if (this.Option.WhoPlayWith == Player.Human) //Nếu chọn kiểu chơi 2 người đánh với nhau { //Player.Com sẽ đóng vai trò người chơi thứ 2 Point toado = e.GetPosition(grdBanCo);//Lấy thông tin tọa độ chuột //Xử lý tọa độ int cl = ((int)toado.X / length); int rw = ((int)toado.Y / length); if (board[rw, cl] == Player.None)//Nếu ô bấm chưa có cờ { if (currPlayer == Player.Human && end == Player.None)//Nếu lượt đi là người và trận đấu chưa kết thúc { board[rw, cl] = currPlayer;//Lưu loại cờ vừa đánh vào mảng DrawDataBoard(rw, cl, true, true);//Vẽ con cờ theo lượt chơi end = CheckEnd(rw, cl);//Kiểm tra xem trận đấu kết thúc chưa if (end == Player.Human)//Nếu người chơi 1 thắng { currPlayer = Player.Human; //Thiết lập lại lượt chơi OnWin();//Khai báo sư kiện Win OnWinOrLose();//Hiển thị 5 ô Win. } else { currPlayer = Player.Com;//Thiết lập lại lượt chơi OnHumanDanhXong();// Khai báo sự kiện người chơi 1 đánh xong } } else if (currPlayer == Player.Com && end == Player.None) { board[rw, cl] = currPlayer;//Lưu loại cờ vừa đánh vào mảng DrawDataBoard(rw, cl, true, true);//Vẽ con cờ theo lượt chơi end = CheckEnd(rw, cl);//Kiểm tra xem trận đấu kết thúc chưa if (end == Player.Com)//Nếu người chơi 2 thắng { OnWin();//Khai báo sư kiện Win OnWinOrLose();//Hiển thị 5 ô Win. // MessageBox.Show("Player B chien thang"); } else { currPlayer = Player.Human;//Thiết lập lại lượt chơi OnComDanhXong();// Khai báo sự kiện người chơi 2 đánh xong } } } } else if (this.Option.WhoPlayWith == Player.Online) // chọn người chơi online { Point toado = e.GetPosition(grdBanCo); //Lấy tọa độ chuột //Xử lý tọa độ int cl = ((int)toado.X / length); int rw = ((int)toado.Y / length); Node node = new Node(); if (board[rw, cl] == Player.None) //Nếu ô bấm chưa có cờ { if (currPlayer == Player.Human && end == Player.None)// /Nếu lượt đi là mình và trận đấu chưa kết thúc { connect.guitoado(MainWindow.socket, rw, cl); board[rw, cl] = currPlayer;//Lưu loại cờ vừa đánh vào mảng // Thread.Sleep(5000); DrawDataBoard(rw, cl, true, true);//Vẽ con cờ theo lượt chơi end = CheckEnd(rw, cl);//Kiểm tra xem trận đấu kết thúc chưa if (end == Player.Human)//Nếu người thắng cuộc là mình { OnWin();//Khai báo sự kiện Win OnWinOrLose();//Hiển thị 5 ô Win. } else if (end == Player.None) //Nếu trận đấu chưa kết thúc { currPlayer = Player.Online;//Thiết lập lại lượt chơi OnHumanDanhXong(); // Khai báo sự kiện người đánh xong } } else if (currPlayer == Player.Online && end == Player.None)//Nếu lượt đi là người chơi online và trận đấu chưa kết thúc { board[rows, columns] = currPlayer;//Lưu loại cờ vừa đánh vào mảng DrawDataBoard(rows, columns, true, true);//Vẽ con cờ theo lượt chơi end = CheckEnd(rows, columns);//Kiểm tra xem trận đấu kết thúc chưa if (end == Player.Online)//Nếu người chơi 2 thắng { OnWin();//Khai báo sư kiện Win OnWinOrLose();//Hiển thị 5 ô Win. } else { currPlayer = Player.Human;//Thiết lập lại lượt chơi OnComDanhXong();// Khai báo sự kiện người chơi 2 đánh xong } } } } }
public void DiNgauNhien() { if (currPlayer == Player.Com) { board[row / 2, column / 2] = currPlayer; DrawDataBoard(row / 2, column / 2, true, true); currPlayer = Player.Human; OnComDanhXong();//Khai báo sự kiện khi máy đánh xong } }
//Bắt đầu lại trò chơi mới public void PlayAgain() { OWin = new _5O_Win(); grdBanCo.Children.Clear(); grdBanCo.Children.Add(hv); ResetBoard(); this.DrawGomokuBoard(); if (this.Option.WhoPlayWith == Player.Com) { if (end == Player.None) { currPlayer = Player.Com; } if (currPlayer == Player.Com && this.Option.WhoPlayWith == Player.Com) { DiNgauNhien(); } } else { if (end == Player.None) { if (currPlayer == Player.Human) { currPlayer = Player.Com; } else if (currPlayer == Player.Com) { currPlayer = Player.Human; } } } end = Player.None; }
//Bắt đầu trò chơi mới public void NewGame() { currPlayer = this.Option.LuotChoi;//Lấy thông tin lượt chơi if (this.Option.WhoPlayWith == Player.Com)//Nếu chọn kiểu chơi với máy { if (currPlayer == Player.Com)//Nếu lược đi là máy { DiNgauNhien(); } } }