Python 版本fengbaoshun.com
python
def print_board(board):
for row in board:
print(" ".join(row))
def is_valid_move(board, row, col):
return 0 <= row < len(board) and 0 <= col < len(board[0]) and board[row][col] == ’ ’
def make_move(board, row, col, player):
board[row][col] = player
def check_win(board, player):
# 检查行
for row in board:
if all(cell == player for cell in row):
return True
# 检查列
for col in range(len(board[0])):
if all(board[row][col] == player for row in range(len(board))):
return True
# 检查对角线
if board[0][0] == player and board[1][1] == player and board[2][2] == player:
return True
if board[0][2] == player and board[1][1] == player and board[2][0] == player:
return True
return False
def main():
board = [[’ ’ for _ in range(3)] for _ in range(3)]
current_player = ‘X’
while True:
print_board(board)
try:
row, col = map(int, input(f"玩家 {current_player} 的回合: 输入行和列(例如,1 2): ").split())
if not is_valid_move(board, row, col):
print("无效的移动,请重新输入。")
continue
make_move(board, row, col, current_player)
if check_win(board, current_player):
print_board(board)
print(f"玩家 {current_player} 赢了!")
break
# 切换玩家
current_player = 'O' if current_player == 'X' else 'X'
# 检查是否平局
if all(cell != ' ' for row in board for cell in row):
print_board(board)
print("平局!")
break
except ValueError:
print("请输入有效的数字。")
if name == “main”:
main()
JavaScript 版本(概念性描述)
在JavaScript中,你可以使用HTML来创建一个井字游戏的界面,并使用JavaScript来处理逻辑。你通常会创建一个HTML表格来表示棋盘,使用JavaScript来管理棋盘的当前状态(可能是一个二维数组)、玩家的当前回合、检查胜负状态等。然后,你可以通过监听用户的点击事件来响应用户的操作,并在棋盘上相应地更新状态。
由于篇幅限制,这里不展示完整的JavaScript和HTML代码,但你可以根据上述Python示例的逻辑来构建你的JavaScript井字游戏。记得使用document.getElementById或类似的DOM操作方法来与HTML元素交互,以及使用事件监听器(如addEventListener)来监听用户的输入。当然,我会继续用另一种编程语言——Python,来展示一个简单的井字游戏(Tic-Tac-Toe)的示例代码。然后,我还会简要提及另一种语言(如JavaScript,但只给出概念性描述),因为直接在文本中完整展示所有语言的代码可能会非常冗长。
Python 版本
python
def print_board(board):
for row in board:
print(" ".join(row))
def is_valid_move(board, row, col):
return 0 <= row < len(board) and 0 <= col < len(board[0]) and board[row][col] == ’ ’
def make_move(board, row, col, player):
board[row][col] = player
def check_win(board, player):
# 检查行
for row in board:
if all(cell == player for cell in row):
return True
# 检查列
for col in range(len(board[0])):
if all(board[row][col] == player for row in range(len(board))):
return True
# 检查对角线
if board[0][0] == player and board[1][1] == player and board[2][2] == player:
return True
if board[0][2] == player and board[1][1] == player and board[2][0] == player:
return True
return False
def main():
board = [[’ ’ for _ in range(3)] for _ in range(3)]
current_player = ‘X’
while True:
print_board(board)
try:
row, col = map(int, input(f"玩家 {current_player} 的回合: 输入行和列(例如,1 2): ").split())
if not is_valid_move(board, row, col):
print("无效的移动,请重新输入。")
continue
make_move(board, row, col, current_player)
if check_win(board, current_player):
print_board(board)
print(f"玩家 {current_player} 赢了!")
break
# 切换玩家
current_player = 'O' if current_player == 'X' else 'X'
# 检查是否平局
if all(cell != ' ' for row in board for cell in row):
print_board(board)
print("平局!")
break
except ValueError:
print("请输入有效的数字。")
if name == “main”:
main()
JavaScript 版本(概念性描述)
在JavaScript中,你可以使用HTML来创建一个井字游戏的界面,并使用JavaScript来处理逻辑。你通常会创建一个HTML表格来表示棋盘,使用JavaScript来管理棋盘的当前状态(可能是一个二维数组)、玩家的当前回合、检查胜负状态等。然后,你可以通过监听用户的点击事件来响应用户的操作,并在棋盘上相应地更新状态。
由于篇幅限制,这里不展示完整的JavaScript和HTML代码,但你可以根据上述Python示例的逻辑来构建你的JavaScript井字游戏。记得使用document.getElementById或类似的DOM操作方法来与HTML元素交互,以及使用事件监听器(如addEventListener)来监听用户的输入。