用9个数字分别代表九宫格棋盘的9个位置,小明和小黑下棋,用一串数字代表落子位置,小明先走,编写程序实现输入一串数字判断输赢
时间: 2024-04-08 10:29:57 浏览: 67
可以使用以下的 Python 代码实现输入一串数字判断输赢:
```python
def check_winner(move_sequence):
winning_positions = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]
player_moves = [[], []]
for i, move in enumerate(move_sequence):
player_moves[i % 2].append(int(move))
for positions in winning_positions:
if all(pos in player_moves[i % 2] for pos in positions):
return f"Player {i % 2 + 1} wins!"
if len(move_sequence) == 9:
return "It's a draw!"
return "Game in progress."
# 示例输入:123456789
move_sequence = input("请输入一串数字代表落子位置:")
result = check_winner(move_sequence)
print(result)
```
你可以输入一串数字来测试这个程序,例如输入 `123456789`,程序将根据输入的数字判断输赢并输出结果。
阅读全文