2017-06-02 52 views
0

我是新来的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() 

问题:

  1. 高清board_check工作不正常,当板子全程序仍然接受投稿。
  2. 我想阻止用户在提交一个已经被另一个值内占据高清player_choice块的价值。
  3. 我不能得到高清display_score工作。

回答

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 

2. 我想你指的是什么,我建议你place_marker功能?

3. 显示评分:好的,在这里你的错误是你的功能只将分数作为参数,然后使用板子。董事会是全球定义为空板(实际上它充满了空间,这也是不必要的,你可以使用空引号)。

此功能也改变了比分的价值,但真的是我想你想要做的是改变分数的值,当你执行你的win_check。如果玩家1获胜,则将1加入玩家1的得分。

最后,由于没有打印功能,display_score不显示任何内容。打印(分数)可能是你以后的样子。

+0

非常感谢您对您的回复@ Charmader35它是一个很大的帮助。 。我在** def place_marker **中更改了代码并且它可以正常工作,但是当玩家在占用的位置上提交了一个值时,他就会错过他。我忘记了打印(分数)在之前打印..但我仍然无法在每个球员.. –

+0

我可以通过检查'def player_choice(board,turn)'中的占用点来避免缺失的转向吗? –

+0

您可以让place_marker返回True或False,并且不会切换到下一个玩家,直到它返回True。 – Charmander35

1

theBoard板子太长了:你的网格应该是3x3,但它有10个单元......

更确切地说,您可以通过访问其索引(其索引位于19之间)来为单元指定标记。但是,Python的列表是从0索引的,因此索引0上的第一个单元从不分配。

board_check功能的问题是,它测试,如果board等于[' '],同时要检查是否board仍含有' '。因此,你的功能应该是:

def board_check(board): 
    if ' ' not in board: 
     return False 
    else: 
     return True 

但蟒蛇支持布尔,所以这应该是:

def board_check(board): 
    return ' ' not in board 

回过头来看看这个theBoard板,现在board_check是好的。您需要将theBoard = [' '] * 10更改为theBoard = [' '] * 9。然后,在访问单元格时,您必须通过索引减号1,因为元素将向左移动一个元素。

由于您代码中其他地方的依赖关系,最简单的修复方法是将theBoard[1:]而不是theBoard改为board_check。这意味着“从1到结束的theBoard的子列表”。


此外,您choose_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 ' 
+0

非常感谢你@你的时间。我永远不会想到我的'def choose_first()'问题,它是一个很大的帮助。伟大的消除,你为我清除了很多东西。我使用了10个单元而不是9个,以方便用户(提交值1-9而不是0-8)。 –

+0

@Mike_R在这种情况下,一个好的解决方案是对核心功能进行编码(制作电路板,在电池中放置一个标记......),并编写充当核心与控制器之间接口的功能(即主要功能/循环/脚本)。例如,你可以定义一个'convert_index'函数,它取1到9之间的一个整数,并在0和8之间取回。这样,主代码是自然的(从人的角度来看它是有意义的),但是正确的数据被提供给核心。最好的方法(IMO,尤其是Python)是将核心封装在一个类中,这里是'Board'。 –