Python Tic Tac Toe:显示分数,防止重复输入,打印绘图
我是Python和编程新手,我正在尝试根据特定的指导方针制作一个tic-tac-toe游戏 这是我的代码:Python Tic Tac Toe:显示分数,防止重复输入,打印绘图,python,python-3.x,Python,Python 3.x,我是Python和编程新手,我正在尝试根据特定的指导方针制作一个tic-tac-toe游戏 这是我的代码: import random import time marker = {'Player 1 ': 'X', 'Player 2 ': 'O', } board = [' ',' ',' ',' ',' ',' ',' ',' ',' '] turn = '0' game_round ='0' def display_board(board): print('7 |8 |9'
import random
import time
marker = {'Player 1 ': 'X', 'Player 2 ': 'O', }
board = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
turn = '0'
game_round ='0'
def display_board(board):
print('7 |8 |9')
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
print(' | |')
print('-----------')
print('4 |5 |6')
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
print(' | |')
print('-----------')
print('1 |2 |3')
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
print(' | |')
def choose_first():
if random.randint(0, 1) == 0:
return 'Player 1 '
elif random.randint(0, 1) == 1:
return 'Player 2 '
def display_score(score):
score = 0
if score(board,mark):
score += 1
#Prints final score after each round
def place_marker(board, marker, position):
board[position] = marker
#places marker (X or O) in position
def win_check(board,mark):
win_combs = (
[1, 2, 3], [4, 5, 6], [7, 8, 9], [3, 6, 9], [1, 4, 7], [2, 5, 8],
[1, 5, 9], [3, 5, 7],)
for comb in win_combs:
if (board[comb[0]] == board[comb[1]] == board[comb[2]] == mark):
return True
return False
#returns True if 3 same marks (X or O) are in line(win)
def board_check(board):
if board != [' '] :
return False
else:
return True
#returns False if there are still empty blocks in the board
#or True otherwise.
def player_choice(board, turn):
first = turn
while turn not in '1 2 3 4 5 6 7 8 9'.split():
turn = input(turn +'Insert number 1-9 :')
return int(turn)
#Player (turn) selects a block
#An int is returned [1,9]
#Checks if value is already submitted in the same block
def replay():
print('Do you want to play again? (yes or no)')
return input().lower().startswith('y')
#Asks players to play again
def next_player(turn):
first=turn
if first == 'Player 1 ':
return 'Player 2 '
else:
return 'Player 1 '
def main():
score = {} # dictionary containing players scores
print('Getting ready to start!\nCoin Toss ', end = '')
for t in range(10):
print(".", flush='True', end=' ')
time.sleep(0.2)
print()
# turn variable reffers to the player whos currently playing
turn = choose_first()
print("\n " + turn + ' goes first!!')
# first variable reffers to the player who played first
first = turn
game_round = 1
while True:
theBoard = [' '] * 10
game_on = True
while game_on:
display_board(theBoard)
position = player_choice(theBoard, turn)
place_marker(theBoard, marker[turn], position)
if win_check(theBoard, marker[turn]):
display_board(theBoard)
print(turn+ "Won")
score[turn] = score.get(turn, 0) + 1
game_on = False
elif board_check(theBoard):
display_board(theBoard)
print('Draw')
game_on = False
else:
turn = next_player(turn)
if not replay():
ending = ''
if game_round>1 : ending = 's'
print("After {} round{}".format(game_round, ending))
display_score(score)
break
else :
game_round += 1
#in the next round the other player plays first
turn = next_player(first)
first = turn
main()
问题:
这个答案远未完成,但当你试图学习时,它至少应该让你开始思考下一步的工作 一,。 我不认为你需要把“董事会”定义为一个全球性的名单。如果希望函数接受板状态,那么像在main()中那样传递它们“theBoard”就足够了 程序仍然接受提交的另一个原因是您没有执行任何类型的检查以查看空间是否已被占用
def place_marker(board, marker, position):
board[position] = marker
#places marker (X or O) in position
应该是这样的:
def place_marker(board, marker, position):
if board[position] == ' ':
board[position] = marker
else:
print("That space is already occupied.")
然后,您需要考虑当有人试图将一块放在另一块上时,您希望程序做什么
如果棋盘检查应该检查棋盘是否已满(因此游戏结束但没有人赢),那么实现这一点的一种方法是循环检查空格是否等于“”,如果它们都等于“”,则棋盘已满。但是,更快捷的方法是:
def board_full(board):
if ' ' not in board:
return True
else:
return False
二,。
我想你指的是我为你的位置标记功能提出的建议
三,。
显示分数:好的,这里你们的错误是,你们的函数只把分数作为参数,然后使用黑板。Board全局定义为空Board(实际上它充满了空格,这也是不必要的,您可以使用空引号)
这个函数也在改变分数的值,但我认为你真正想做的是在你进行赢球检查时改变分数的值。如果玩家1获胜,则在玩家1的分数上加1
最后,由于没有打印功能,“显示分数”不会显示任何内容。打印(分数)可能就是你想要的 这个答案还远未完成,但在你努力学习的过程中,至少应该让你开始思考下一步的工作 一,。 我不认为你需要把“董事会”定义为一个全球性的名单。如果希望函数接受板状态,那么像在main()中那样传递它们“theBoard”就足够了 程序仍然接受提交的另一个原因是您没有执行任何类型的检查以查看空间是否已被占用
def place_marker(board, marker, position):
board[position] = marker
#places marker (X or O) in position
应该是这样的:
def place_marker(board, marker, position):
if board[position] == ' ':
board[position] = marker
else:
print("That space is already occupied.")
然后,您需要考虑当有人试图将一块放在另一块上时,您希望程序做什么
如果棋盘检查应该检查棋盘是否已满(因此游戏结束但没有人赢),那么实现这一点的一种方法是循环检查空格是否等于“”,如果它们都等于“”,则棋盘已满。但是,更快捷的方法是:
def board_full(board):
if ' ' not in board:
return True
else:
return False
二,。
我想你指的是我为你的位置标记功能提出的建议
三,。
显示分数:好的,这里你们的错误是,你们的函数只把分数作为参数,然后使用黑板。Board全局定义为空Board(实际上它充满了空格,这也是不必要的,您可以使用空引号)
这个函数也在改变分数的值,但我认为你真正想做的是在你进行赢球检查时改变分数的值。如果玩家1获胜,则在玩家1的分数上加1
最后,由于没有打印功能,“显示分数”不会显示任何内容。打印(分数)可能就是你想要的 电路板电路板太长了:你的网格应该是3x3,但它有十个单元 更准确地说,您可以通过访问单元格及其索引来为单元格指定标记,索引由
1
和9
组成。但是,Python的列表是从0
索引的,因此从不分配索引0
处的第一个单元格
board\u check
功能的问题在于,它测试board
是否等于['']
,而您想检查board
是否仍然包含'
。因此,您的功能应该是:
def board_check(board):
if ' ' not in board:
return False
else:
return True
def board_check(board):
return ' ' not in board
但是Pythons支持布尔值,所以应该是:
def board_check(board):
if ' ' not in board:
return False
else:
return True
def board_check(board):
return ' ' not in board
回到电路板
电路板,现在电路板检查
正常。您需要将theBoard=['']*10
更改为theBoard=['']*9
。然后,在访问单元格时,必须传递索引减号1
,因为元素将向左移动1
由于代码在其他位置的依赖性,最简单的修复方法是将theBoard[1:][/code>而不是theBoard
传递到board\u check
。这意味着“从1到结尾的电路板的子列表”
此外,您的choose\u first
函数错误,有可能返回None
,以异常结束。您的函数定义如下:
def choose_first():
if random.randint(0, 1) == 0:
return 'Player 1 '
elif random.randint(0, 1) == 1:
return 'Player 2 '
从语义上讲,您的代码意味着:
掷硬币。如果是头部,玩家1开始。如果是尾巴,你再掷一枚硬币。如果第二枚硬币是反面,玩家2开始
如果第一个randint
给你1
,第二个给你0
,会怎么样?没有人开始
您需要生成一个随机数(掷一枚硬币),并根据该值做出决定:
def choose_first():
value = random.randint(0, 1)
if value == 0:
return 'Player 1 '
else:
return Player 2 '
电路板太长了:你的网格应该是3x3,但它有十个单元
更准确地说,您可以通过访问单元格及其索引(由1
和