您可以尝试使用Python语言编写一个简单的控制台版象棋小程序,如下所示:
class ChessBoard:
def __init__(self):
self.board = [
["車", "馬", "象", "士", "将", "士", "象", "馬", "車"],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", "砲", " ", " ", " ", " ", " ", "砲", " "],
["卒", " ", "卒", " ", "卒", " ", "卒", " ", "卒"],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
["兵", " ", "兵", " ", "兵", " ", "兵", " ", "兵"],
[" ", "炮", " ", " ", " ", " ", " ", "炮", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
["車", "馬", "象", "士", "帥", "士", "象", "馬", "車"]
]
def display(self):
for row in self.board:
for piece in row:
print(piece, end=" ")
print()
def move(self, start, end):
x1, y1 = start
x2, y2 = end
piece = self.board[x1][y1]
if piece == " ":
print("Invalid move: No piece selected.")
elif (x2 < 0 or x2 > 9) or (y2 < 0 or y2 > 8):
print("Invalid move: Out of board.")
elif (x1, y1) == (x2, y2):
print("Invalid move: Start and end positions are the same.")
elif self.board[x2][y2] != " ":
print("Invalid move: Destination is occupied.")
elif piece == "車":
if x1 == x2:
if y1 < y2:
for i in range(y1+1, y2):
if self.board[x1][i] != " ":
print("Invalid move: Path is blocked.")
return
else:
for i in range(y1-1, y2, -1):
if self.board[x1][i] != " ":
print("Invalid move: Path is blocked.")
return
self.board[x1][y1] = " "
self.board[x2][y2] = piece
elif y1 == y2:
if x1 < x2:
for i in range(x1+1, x2):
if self.board[i][y1] != " ":
print("Invalid move: Path is blocked.")
return
else:
for i in range(x1-1, x2, -1):
if self.board[i][y1] != " ":
print("Invalid move: Path is blocked.")
return
self.board[x1][y1] = " "
self.board[x2][y2] = piece
else:
print("Invalid move: Rook can only move horizontally or vertically.")
elif piece == "馬":
dx = x2 - x1
dy = y2 - y1
if abs(dx) == 2 and abs(dy) == 1:
if dx < 0:
i = x1 - 1
else:
i = x1 + 1
j = y1
elif abs(dx) == 1 and abs(dy) == 2:
i = x1
if dy < 0:
j = y1 - 1
else:
j = y1 + 1
else:
print("Invalid move: Horse can only move in an L-shape.")
return
if self.board[i][j] != " ":
print("Invalid move: Path is blocked.")
return
self.board[x1][y1] = " "
self.board[x2][y2] = piece
elif piece ==
下面是一个简单的PHP下象棋的代码,可以在Web环境下运行:
```php
// 定义棋盘
$chessBoard = [
['車', '馬', '象', '士', '将', '士', '象', '馬', '車'],
['', '', '', '', '', '', '', '', ''],
['', '砲', '', '', '', '', '', '砲', ''],
['卒', '', '卒', '', '卒', '', '卒', '', '卒'],
['', '', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', '', ''],
['兵', '', '兵', '', '兵', '', '兵', '', '兵'],
['', '炮', '', '', '', '', '', '炮', ''],
['', '', '', '', '', '', '', '', ''],
['車', '馬', '象', '士', '帅', '士', '象', '馬', '車']
];
// 定义棋子颜色
$black = '黑';
$red = '红';
// 定义当前轮到的颜色
$player = $black;
// 显示棋盘
function displayBoard($chessBoard) {
for ($i = 0; $i < count($chessBoard); $i++) {
for ($j = 0; $j < count($chessBoard[$i]); $j++) {
echo $chessBoard[$i][$j] . ' ';
}
echo '
';
}
}
// 移动棋子
function movePiece(&$chessBoard, $startX, $startY, $endX, $endY) {
// 判断是否越界
if ($endX < 0 || $endX > 9 || $endY < 0 || $endY > 8) {
echo 'Invalid move: Out of board.';
return false;
}
// 判断起始位置是否有棋子
if ($chessBoard[$startX][$startY] == '') {
echo 'Invalid move: No piece selected.';
return false;
}
// 判断目标位置是否已经有己方棋子
if ($chessBoard[$endX][$endY] != '' && getColor($chessBoard[$startX][$startY]) == getColor($chessBoard[$endX][$endY])) {
echo 'Invalid move: Destination is occupied by your own piece.';
return false;
}
// 判断移动规则
if (!isValidMove($chessBoard, $startX, $startY, $endX, $endY)) {
echo 'Invalid move: Invalid movement for this piece.';
return false;
}
// 移动棋子
$chessBoard[$endX][$endY] = $chessBoard[$startX][$startY];
$chessBoard[$startX][$startY] = '';
return true;
}
// 判断移动规则是否合法
function isValidMove($chessBoard, $startX, $startY, $endX, $endY) {
$piece = $chessBoard[$startX][$startY];
if ($piece == '車') { // 车
if ($startX == $endX) { // 横向移动
$minY = min($startY, $endY);
$maxY = max($startY, $endY);
for ($i = $minY + 1; $i < $maxY; $i++) {
if ($chessBoard[$startX][$i] != '') {
return false;
}
}
return true;
} elseif ($startY == $endY) { // 竖向移动
$minX = min($startX, $endX);
$maxX = max($startX, $endX);
for ($i = $minX + 1; $i < $maxX; $i++) {
if ($chessBoard[$i][$startY] != '') {
return false;
}
}
return true;
}
} elseif ($piece == '马') { // 马
$dx = abs($endX - $startX);
$dy = abs($endY - $startY);
if (($dx == 1 && $dy == 2) || ($dx
`下面是一个简单的PHP下象棋的代码,可以在Web环境下运行:
```php
// 定义棋盘
$chessBoard = [
['車', '馬', '象', '士', '将', '士', '象', '馬', '車'],
['', '', '', '', '', '', '', '', ''],
['', '砲', '', '', '', '', '', '砲', ''],
['卒', '', '卒', '', '卒', '', '卒', '', '卒'],
['', '', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', '', ''],
['兵', '', '兵', '', '兵', '', '兵', '', '兵'],
['', '炮', '', '', '', '', '', '炮', ''],
['', '', '', '', '', '', '', '', ''],
['車', '馬', '象', '士', '帅', '士', '象', '馬', '車']
];
// 定义棋子颜色
$black = '黑';
$red = '红';
// 定义当前轮到的颜色
$player = $black;
// 显示棋盘
function displayBoard($chessBoard) {
for ($i = 0; $i < count($chessBoard); $i++) {
for ($j = 0; $j < count($chessBoard[$i]); $j++) {
echo $chessBoard[$i][$j] . ' ';
}
echo '
';
}
}
// 移动棋子
function movePiece(&$chessBoard, $startX, $startY, $endX, $endY) {
// 判断是否越界
if ($endX < 0 || $endX > 9 || $endY < 0 || $endY > 8) {
echo 'Invalid move: Out of board.';
return false;
}
// 判断起始位置是否有棋子
if ($chessBoard[$startX][$startY] == '') {
echo 'Invalid move: No piece selected.';
return false;
}
// 判断目标位置是否已经有己方棋子
if ($chessBoard[$endX][$endY] != '' && getColor($chessBoard[$startX][$startY]) == getColor($chessBoard[$endX][$endY])) {
echo 'Invalid move: Destination is occupied by your own piece.';
return false;
}
// 判断移动规则
if (!isValidMove($chessBoard, $startX, $startY, $endX, $endY)) {
echo 'Invalid move: Invalid movement for this piece.';
return false;
}
// 移动棋子
$chessBoard[$endX][$endY] = $chessBoard[$startX][$startY];
$chessBoard[$startX][$startY] = '';
return true;
}
// 判断移动规则是否合法
function isValidMove($chessBoard, $startX, $startY, $endX, $endY) {
$piece = $chessBoard[$startX][$startY];
if ($piece == '車') { // 车
if ($startX == $endX) { // 横向移动
$minY = min($startY, $endY);
$maxY = max($startY, $endY);
for ($i = $minY + 1; $i < $maxY; $i++) {
if ($chessBoard[$startX][$i] != '') {
return false;
}
}
return true;
} elseif ($startY == $endY) { // 竖向移动
$minX = min($startX, $endX);
$maxX = max($startX, $endX);
for ($i = $minX + 1; $i < $maxX; $i++) {
if ($chessBoard[$i][$startY] != '') {
return false;
}
}
return true;
}
} elseif ($piece == '马') { // 马
$dx = abs($endX - $startX);
$dy = abs($endY - $startY);
if (($dx == 1 && $dy == 2) || ($dx
`
留言