我是新来的Python和编程,我试图使基于特定准则井字游戏。井字:显示比分,防止重复条目,印刷画
这是我的代码:
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()
问题:
- 的高清board_check工作不正常,当板子全程序仍然接受投稿。
- 我想阻止用户在提交一个已经被另一个值内占据高清player_choice块的价值。
- 我不能得到高清display_score工作。
非常感谢您对您的回复@ Charmader35它是一个很大的帮助。 。我在** def place_marker **中更改了代码并且它可以正常工作,但是当玩家在占用的位置上提交了一个值时,他就会错过他。我忘记了打印(分数)在之前打印..但我仍然无法在每个球员.. –
我可以通过检查'def player_choice(board,turn)'中的占用点来避免缺失的转向吗? –
您可以让place_marker返回True或False,并且不会切换到下一个玩家,直到它返回True。 – Charmander35