/// <summary> /// 玩家在x,y放置颜色为chessColor的棋子 /// </summary> /// <param name="chessColor">棋子颜色</param> /// <param name="x">横坐标</param> /// <param name="y">纵坐标</param> private void playerPutChess(int x, int y) { if (playerRound) { if (playerChessColor == black) { chessPicBox[x, y].Image = blackChessImg; } else { chessPicBox[x, y].Image = whiteChessImg; } chessPicBox[x, y].Visible = true; chessPointBoard[x, y] = 1;//玩家放置棋子 chessPointColor[x, y] = playerChessColor; Point p = new Point(x, y); totalCount++; //将历史记录压栈 pointStack.Push(p); colorStack.Push(playerChessColor); playerRound = false; //是否为平局 if (totalCount == 225) { MessageBox.Show("平局,游戏结束!"); PlayerFirstMenuItem.PerformClick(); } else { //是否决出胜负 bool gameOverFlag = isGameOver(); if (gameOverFlag) { MessageBox.Show("游戏结束,玩家胜!"); PlayerFirstMenuItem.PerformClick();//重新开始 } else { computerPutChess();//电脑下棋 } } } else { MessageBox.Show("请等待电脑落子!"); } }
/// <summary> /// 电脑AI下棋 /// </summary> private void computerPutChess() { int compColor = (playerChessColor == black) ? white : black; //AI函数,找到权值最大的棋点 Point p = comp.findBestPoint(chessPointBoard, chessPointColor, compColor, playerChessColor, depth, width); if (compColor == black) { chessPicBox[p.X, p.Y].Image = blackChessImg; } else { chessPicBox[p.X, p.Y].Image = whiteChessImg; } chessPicBox[p.X, p.Y].Visible = true; //电脑放置棋子 chessPointBoard[p.X, p.Y] = 1; chessPointColor[p.X, p.Y] = compColor; totalCount++; //将历史记录压栈 pointStack.Push(p); colorStack.Push(compColor); playerRound = true; //是否为平局 if (totalCount == 225) { MessageBox.Show("平局,游戏结束!"); PlayerFirstMenuItem.PerformClick(); } else { //是否决出胜负 bool gameOverFlag = isGameOver(); if (gameOverFlag) { MessageBox.Show("游戏结束,电脑胜,玩家败!"); //重新开始,默认玩家优先 PlayerFirstMenuItem.PerformClick(); } } }