Python 有没有一种方法可以让我在屏幕上刷新一块板,而不是让它在控制台上堆积?
我刚刚完成了一个Connect-Four游戏,现在我想知道如何在不让控制台反复刷新的情况下将棋盘保持在一个位置。我在网上到处搜索任何解决方案,但什么也找不到 下面是我的完整代码。您可以在IDE上随意运行它。任何帮助都将不胜感激Python 有没有一种方法可以让我在屏幕上刷新一块板,而不是让它在控制台上堆积?,python,python-3.x,Python,Python 3.x,我刚刚完成了一个Connect-Four游戏,现在我想知道如何在不让控制台反复刷新的情况下将棋盘保持在一个位置。我在网上到处搜索任何解决方案,但什么也找不到 下面是我的完整代码。您可以在IDE上随意运行它。任何帮助都将不胜感激 import random import ast def winner(board): """This function accepts the Connect Four board as a parameter. If
import random
import ast
def winner(board):
"""This function accepts the Connect Four board as a parameter.
If there is no winner, the function will return the empty string "".
If the user has won, it will return 'X', and if the computer has
won it will return 'O'."""
row = 0
while row <= len(board) - 1:
count = 0
last = ''
col = 0
while col <= len(board[0]) - 1:
row_win = board[row][col]
if row_win == " ":
count = 0
col += 1
continue
if row_win == last:
count += 1
else:
count = 1
if count >= 4:
return row_win
last = row_win
col += 1
row += 1
col = 0
while col <= len(board[0]) - 1:
count = 0
last = ''
row = 0
while row <= len(board) - 1:
col_win = board[row][col]
if col_win == " ":
count = 0
row += 1
continue
if col_win == last:
count += 1
else:
count = 1
if count >= 4:
return col_win
last = col_win
row += 1
col += 1
row = 0
while row <= len(board) - 1:
col = 0
while col <= len(board[0]) - 1:
try:
if (board[row][col] == board[row + 1][col + 1] and board[row + 1][col + 1] == board[row + 2][
col + 2] and
board[row + 2][col + 2] == board[row + 3][col + 3]) and (
board[row][col] != " " or board[row + 1][col + 1] != " " or board[row + 2][col + 2] != " " or
board[row + 3][col + 3] != " "):
return board[row][col]
except:
IndexError
col += 1
row += 1
row = 0
while row <= len(board) - 1:
col = 0
while col <= len(board[0]) - 1:
try:
if (board[row][col] == board[row + 1][col - 1] and board[row + 1][col - 1] == board[row + 2][
col - 2] and
board[row + 2][col - 2] == board[row + 3][col - 3]) and (
board[row][col] != " " or board[row + 1][col - 1] != " " or board[row + 2][col - 2] != " " or
board[row + 3][col - 3] != " "):
return board[row][col]
except:
IndexError
col += 1
row += 1
# No winner: return the empty string
return ""
def display_board(board):
"""This function accepts the Connect Four board as a parameter.
It will print the Connect Four board grid (using ASCII characters)
and show the positions of any X's and O's. It also displays
the column numbers on top of the board to help
the user figure out the coordinates of their next move.
This function does not return anything."""
header = " "
i = 1
while i < len(board[0]):
header = header + str(i) + " "
i += 1
header = header + str(i)
print(header)
separator = "+---+"
i = 1
while i < len(board[0]):
separator = separator + "---+"
i += 1
print(separator)
for row in board:
print(" ", " | ".join(row))
separator = "+---+"
i = 1
while i < len(board[0]):
separator = separator + "---+"
i += 1
print(separator)
print()
def make_user_move(board):
"""This function accepts the Connect Four board as a parameter.
It will ask the user for a row and column. If the row and
column are each within the range of 1 and 7, and that square
is not already occupied, then it will place an 'X' in that square."""
valid_move = False
while not valid_move:
try:
col_num = "What col would you like to move to (1 - " + str(len(board[0])) + ")? "
col = int(input(col_num))
if board[0][col - 1] != ' ':
print("Sorry, that column is full. Please try again!\n")
else:
col = col - 1
for row in range(len(board) - 1, -1, -1):
if board[row][col] == ' ' and not valid_move:
board[row][col] = 'X'
valid_move = True
except:
ValueError
print("Please enter a valid option! ")
return board
def make_computer_move(board):
"""This function accepts the Connect Four board as a parameter.
It will randomly pick row and column values between 0 and 6.
If that square is not already occupied it will place an 'O'
in that square. Otherwise, another random row and column
will be generated."""
computer_valid_move = False
while not computer_valid_move:
col = random.randint(0, len(board) - 1)
if board[0][col] != ' ':
print("Sorry, that column is full. Please try again!\n")
else:
for row in range(len(board) - 1, -1, -1):
if board[row][col] == ' ' and not computer_valid_move:
board[row][col] = 'O'
computer_valid_move = True
return board
def main():
"""The Main Game Loop:"""
cf_board = []
row = int(input("How many rows do you want your game to have? "))
col = int(input("How many columns do you want your game to have? "))
move_order = input("Would you like to move first? (y/n) ")
while move_order.lower() != "y" and move_order.lower() != "n":
print("Invalid input! Please choose y (yes) or n (no) - case insensitive. ")
move_order = input("Would you like to move first? (y/n) ")
if move_order.lower() == "y":
users_turn = True
elif move_order.lower() == "n":
users_turn = False
free_cells = col * row
row_str = "\" \""
board_str = "\" \""
i = 0
j = 0
while i < col - 1:
row_str = row_str + "," + "\" \""
i += 1
board_list = [row_str]
while j < row - 1:
board_list.append(row_str)
j += 1
cf_board = [list(ast.literal_eval(x)) for x in board_list]
# for i in range(row-1):
# cf_board.append(row_str)
while not winner(cf_board) and (free_cells > 0):
display_board(cf_board)
if users_turn:
cf_board = make_user_move(cf_board)
users_turn = not users_turn
else:
cf_board = make_computer_move(cf_board)
users_turn = not users_turn
free_cells -= 1
display_board(cf_board)
if (winner(cf_board) == 'X'):
print("Y O U W O N !")
elif (winner(cf_board) == 'O'):
print("T H E C O M P U T E R W O N !")
elif free_cells == 0:
print("S T A L E M A T E !")
print("\n*** GAME OVER ***\n")
# Start the game!
main()
随机导入
导入ast
def优胜者(董事会):
“”“此函数接受Connect Four board作为参数。
如果没有赢家,函数将返回空字符串“”。
如果用户赢了,它将返回“X”,如果计算机赢了
赢得它将返回“O”
行=0
而row我猜您想要实现的想法类似于make a
例如,在unix/linux系统上,您可以在一个位置用“\r”字符而不是默认情况下在print()函数上使用的“\n”来更新进度条或简单句子:
from sys import stdout
from time import sleep
def realtime_sentence():
name = 0
while name <= 100:
stdout.write("\r My name is {}".format(name))
name += 1
sleep(0.2)
def realtime_progress_bar():
progress = 0
while progress <= 100:
stdout.write("\r Progress: {} {}%".format(progress*"#", progress))
progress += 1
sleep(0.2)
realtime_sentence()
realtime_progress_bar()
从系统导入标准输出
从时间上导入睡眠
def realtime_句子():
name=0
我不明白你的问题。您使用什么创建此板?始终将代码作为文本,而不是链接到外部门户。@furas刚刚更改了它!一些控制台可能有特殊的代码来移动光标、绘制颜色等,然后您可以只更改控制台中的一些位置,并保留板的其余部分,而无需重新绘制。但并不是所有的控制台都有这些特殊代码。您可以尝试在internet中查找这些代码,并在print()
中使用它,或者尝试使用使用这些代码的模块。另请参阅