public qizi placeqizi(int x, int y, qztype type) { //找出最近的节点 Point nodeid = FindTheCloseNode(x, y); //如果没有的话,false if (nodeid == NO_MATCH) { return(null); } //如果有的话,检查是否已经有棋子存在 if (qizis[nodeid.X, nodeid.Y] != null) { return(null); } //根据type产生对应的棋子 Point formPos = conertToFormPosition(nodeid); if (type == qztype.BLACK) { qizis[nodeid.X, nodeid.Y] = new black(formPos.X, formPos.Y); } else if (type == qztype.WHITE) { qizis[nodeid.X, nodeid.Y] = new white(formPos.X, formPos.Y); } //记录最后下棋子的位置 lastPlaceNode = nodeid; return(qizis[nodeid.X, nodeid.Y]); }
public qizi placeqizi(int x, int y) { qizi qz = qp.placeqizi(x, y, currentPlayer); if (qz != null) { //检查是否现在下棋的人获胜 checkWinner(); //交换棋手 if (currentPlayer == qztype.BLACK) { currentPlayer = qztype.WHITE; } else if (currentPlayer == qztype.WHITE) { currentPlayer = qztype.BLACK; } return(qz); } return(null); }
//游戏规则 private void checkWinner() { int centerX = qp.LastPlaceNode.X; int centerY = qp.LastPlaceNode.Y; //检查8个方向的棋子 for (int xDir = -1; xDir <= 1; xDir++) { for (int yDir = -1; yDir <= 1; yDir++) { //排除中间的情况 if (xDir == 0 && yDir == 0) { //跳过后面的代码再次进入循环 continue; } //记录现在看到几颗相同的棋子 int count = -2; while (count < 2) { int targetX = centerX + count * xDir; int targetY = centerY + count * yDir; //检查颜色是否相同 if (targetX < 0 || targetX >= qipan.nodecount || targetY < 0 || targetY >= qipan.nodecount || qp.GetQztype(targetX, targetY) != currentPlayer) { break; } count++; } //检查是否看到五颗棋子 if (count == 2) { Winner = currentPlayer; } int count1 = 0; while (count1 < 5) { int targetX = centerX + count1 * xDir; int targetY = centerY + count1 * yDir; //检查颜色是否相同 if (targetX < 0 || targetX >= qipan.nodecount || targetY < 0 || targetY >= qipan.nodecount || qp.GetQztype(targetX, targetY) != currentPlayer) { break; } count1++; } //检查是否看到五颗棋子 if (count1 == 5) { Winner = currentPlayer; } } } }