public Chess PlaceAchess(int x, int y, chessType type) { Point nodeID = FindthecloestNode(x, y); //找出最近的節點 if (nodeID == NOmatchNode) //如果沒有的話,回傳null { return(null); } //如果有的話,檢查棋子是不是已經存在了 if (chess[nodeID.X, nodeID.Y] != null) { return(null); } //根據 Type 產生對應的棋子 Point formpos = adjustNodePosition(nodeID);//為了讓棋子剛好在座標點上 if (type == chessType.BLACK) { chess[nodeID.X, nodeID.Y] = new Black(formpos.X, formpos.Y); } else if (type == chessType.WHITE) { chess[nodeID.X, nodeID.Y] = new White(formpos.X, formpos.Y); } lastchess = nodeID;//紀錄最後下棋子的位置 return(chess[nodeID.X, nodeID.Y]); }
public void Whowin() { int centerX = borad.Lastchess.X; int centerY = borad.Lastchess.Y; // |(x-1,y-1)|(x,y-1)|(x+1,y-1)| // |---------|-------|---------| //檢查八個不同方向 | (x-1,y) | (x,y) | (x+1,y) | // |---------|-------|---------| // |(x-1,y+1)|(x,y+1)|(x+1,y+1)| for (int xdir = -1; xdir <= 1; xdir++) { for (int ydir = -1; ydir <= 1; ydir++) { if (xdir == 0 && ydir == 0) { //如果有符合條件的話,會跳過下面所有的條件,直接開始下一輪迴圈 continue; } int count = 1;//紀錄現在有幾顆棋子 while (count < 5) { int targetX = centerX + count * xdir; int targetY = centerY + count * ydir; //檢查是不是有超出邊界和檢查顏色是不是一樣 if (targetX < 0 || targetX >= Borad.Nodecount || targetY < 0 || targetY >= Borad.Nodecount || borad.GetchessType(targetX, targetY) != currentplayer) { break; } count++; } if (count == 5)//檢查是不是有五顆棋子 { winner = currentplayer; } } } }
public Chess PlaceAchess(int x, int y) { Chess chess = borad.PlaceAchess(x, y, currentplayer); if (chess != null) { Whowin();//檢查現在下棋的人是不是獲勝 //交換下棋部分 if (currentplayer == chessType.BLACK)//如果棋子是黑色的話,下一次按就變成白色的 { currentplayer = chessType.WHITE; } else if (currentplayer == chessType.WHITE)//如果棋子是白色的話,下一次按就變成黑色的 { currentplayer = chessType.BLACK; } return(chess);//傳回棋子 } return(null); }