public void newChessBroard(int playerNewNum) { playerNum = playerNewNum; label1.Text = "当前棋手:玩家" + playerNewNum.ToString(); 导出ToolStripMenuItem.Enabled = false; label2.BackColor = Color.FromName(playerColor[playerNewNum]); rowNum = (groupBox1.Height - 12) / 36; columnNum = (groupBox1.Width - 12) / 36; for (int i = 1; i <= lastRowNum; i++) { for (int j = 1; j <= lastColumnNum; j++) { groupBox1.Controls.Remove(Btn[i, j]); } } lastRowNum = rowNum; lastColumnNum = columnNum; for (int i = 1; i <= rowNum; i++) { for (int j = 1; j <= columnNum; j++) { Btn[i, j] = new chessBtn { btnPosX = i, btnPosY = j, Location = new Point(12 + 36 * (j - 1), 12 + 36 * (i - 1)), Size = new Size(30, 30), Text = "", TabStop = false, }; groupBox1.Controls.Add(Btn[i, j]); Btn[i, j].Click += new EventHandler(Btn_Click); playerNameTmp[i, j] = 0; } } }
private void Btn_Click(object sender, EventArgs e) { chessBtn chessBt = (chessBtn)sender; playedChessCnt++; chessBt.Enabled = false; 导出ToolStripMenuItem.Enabled = true; chessBt.BackColor = Color.FromName(playerColor[playerNum]); playerNameTmp[chessBt.btnPosX, chessBt.btnPosY] = playerNum; int cou = 0; int i, j; //行 for (i = chessBt.btnPosY + 1; playerNameTmp[chessBt.btnPosX, i] == playerNum; i++) { cou++; } for (i = chessBt.btnPosY - 1; playerNameTmp[chessBt.btnPosX, i] == playerNum; i--) { cou++; } if (cou == winChessNum - 1) { ifPlayerWin(); } //列 cou = 0; for (i = chessBt.btnPosX + 1; playerNameTmp[i, chessBt.btnPosY] == playerNum; i++) { cou++; } for (i = chessBt.btnPosX - 1; playerNameTmp[i, chessBt.btnPosY] == playerNum; i--) { cou++; } if (cou == winChessNum - 1) { ifPlayerWin(); } //右对角线 cou = 0; i = chessBt.btnPosX + 1; j = chessBt.btnPosY + 1; while (playerNameTmp[i, j] == playerNum) { cou++; i++; j++; } i = chessBt.btnPosX - 1; j = chessBt.btnPosY - 1; while (playerNameTmp[i, j] == playerNum) { cou++; i--; j--; } if (cou == winChessNum - 1) { ifPlayerWin(); } //左对角线 cou = 0; i = chessBt.btnPosX - 1; j = chessBt.btnPosY + 1; while (playerNameTmp[i, j] == playerNum) { cou++; i--; j++; } i = chessBt.btnPosX + 1; j = chessBt.btnPosY - 1; while (playerNameTmp[i, j] == playerNum) { cou++; i++; j--; } if (cou == winChessNum - 1) { ifPlayerWin(); } if (ifWin) { label2.BackColor = Color.FromName(playerColor[playerNum]); label1.Text = "玩家" + playerNum.ToString() + "胜利!!"; } else { if (playerNum == playerCnt) { playerNum = 1; } else { playerNum++; } label1.Text = "当前棋手:玩家" + playerNum.ToString(); label2.BackColor = Color.FromName(playerColor[playerNum]); } }