Python 스크립팅 기초 - Tic-tac-toe 게임.

내용물


  • Summary

  • Code
  • Let's talk logic
  • Building the score board and displaying it
  • Inputs from players
  • Validating player provided name
  • Validating player provided symbol
  • Setting the symbol for the computer
  • Checking which cells are available to add input to
  • Validating if a player is providing correct input for cell value
  • Auto populating the last cell
  • Checking who won


  • Bonus
  • Adding a time delay
  • Ascii art

  • Putting it all together

  • 요약

    Okay, who hasn't played tic-tac-toe? Okay, try noughts and crosses? Still no? This is one of the classic games out there, quick, short and fun. Take a look at the wikipedia page .
    오늘 우리는 처음부터 게임을 빌드하고 터미널에서 플레이할 것입니다. 예, 파이썬을 사용할 것입니다. 그럼 시작할까요?

    LINK TO GITHUB VIDEO


    암호

    논리를 말하자

    Tic-tac-toe is played between two players on a 3x3 grid. One playes chooses 'X' and the other player chooses 'O'. Anyone can play first, but for our game, let us consider whoever chooses 'X' gets to play first. Any player who gets three 'X' or 'O' in a row or column or diagonally wins the game. If at the end of the game, no player is able to achieve that, the game ends in a draw.

    The following table demostrates how a 3x3 grid would look like. Each row and its associated column number is displayed as the cell value. For row 1 column 1 it is R1C1, row 2 column 2 it is R2C2, etc.

    R1C1 R1C2 R1C3
    R2C1 R2C2 R2C3
    R3C1 R3C2 R3C3

    For a player to win, they have to get their chosen symbol of 'X' or 'O' in one of the following series. It doesn't matter the order which they went for one first.

    Winning Combinations
    R1C1,R1C2,R1C3
    R2C1,R2C2,R2C3
    R3C1,R3C2,R3C3
    R1C1,R2C1,R3C1
    R1C2,R2C2,R3C2
    R1C3,R2C3,R3C3
    R1C1,R2C2,R3C3
    R3C1,R2C2,R1C3

    We will do the following in order

    • Display the set of rules to the player.

    • Ask the player to enter their name, and if they don't we will assign one, say Bob. How does Bob sound?

    • Ask the player if they want 'X' as their playing symbol or 'O', and assign the other one to the computer.

    • Let our computer side player green our human.

    • Design a pretty looking score board to keep track of their scores.

    • While the game is being played we will

      • Display the cell locations which are not yet occupied by 'X' or 'O'.
      • Let the computer decide randomly which cell value to pick as its choice. I am keeping this basic. At a later point of time, will add difficulty levels to the game, and will let the computer play offensive.
      • After both the player and computer has provided their input, check if anyone has won.

    스코어 보드 구축 및 표시

    We will use two modules here, pandas, and tabulate . Pandas는 데이터를 저장할 데이터 프레임을 제공할 것이고, 우리는 그것을 예쁘게 만들기 위해 표로 만들 것입니다.

    다음 명령을 사용하여 환경에 둘 다 설치할 수 있습니다.

    python3.10 -m pip install pandas
    python3.10 -m pip install tabulate
    



    #This is how our dataframe will look like
    >>> import pandas
    >>> pandas.DataFrame([['11','12','13'],['21','22','23'],['31','32','33']],columns=['C1','C2','C3'],index=['R1','R2','R3'])
        C1  C2  C3
    R1  11  12  13
    R2  21  22  23
    R3  31  32  33
    >>>
    



    # This is how our final scoreboard will look like on display
    >>> import tabulate
    >>> d = pandas.DataFrame([['11','12','13'],['21','22','23'],['31','32','33']],columns=['C1','C2','C3'],index=['R1','R2','R3'])
    >>> print(tabulate.tabulate(d, tablefmt = 'fancy_grid', showindex=False))
    ╒════╤════╤════╕
     11  12  13 
    ├────┼────┼────┤
     21  22  23 
    ├────┼────┼────┤
     31  32  33 
    ╘════╧════╧════╛
    


    플레이어의 입력

    플레이어 제공 이름 확인 중

    Store the user provided name in a global variable. If the name is empty assign a default name Bob.

    >>> validate_user_input_name()
    Enter your name: SB
    >>> print(user_input_name)
    Sb
    >>> validate_user_input_name()
    Enter your name: 
    >>> print(user_input_name)
    Bob
    

    플레이어 제공 기호 확인 중

    Ask the player if they want to choose 'X' as their symbol or 'O' and assign the opposite to the computer to play with. Any invalid attempts, a total of three will end the game.

    >>> validate_user_input_name()
    Enter your name: sb
    >>> user_input_name
    'SB'
    >>> validate_user_input_symbol()
    Player SB enter either ['X', 'O'] without the quotes : x
    >>> user_input_symbol
    'X'
    >>> validate_user_input_symbol()
    Player SB enter either ['X', 'O'] without the quotes : z
    Player SB enter either ['X', 'O'] without the quotes : a
    Player SB enter either ['X', 'O'] without the quotes : b
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 13, in validate_user_input_symbol
    ValueError: Maximum '3' invalid symbol choice attempts reached. Thank you for playing.
    >>> cpu_input_symbol
    'O'
    >>> validate_user_input_symbol()
    Player SB enter either ['X', 'O'] without the quotes : O
    >>> user_input_symbol
    'O'
    >>> cpu_input_symbol
    'X'
    >>> 
    

    컴퓨터 기호 설정

    In the above section you see that the computer player symbol is automatically set the moment the player chooses a correct symbol. We use a simple if else statement for that.

    if user_input_symbol == 'X':
        cpu_input_symbol = 'O'
    else:
        cpu_input_symbol = 'X'
    

    입력을 추가할 수 있는 셀 확인

    By using list comprehensions we can quickly find out which cells does not have a 'X' or 'O' value in it.

    # score_board is our dataframe
    # symbol choices is a list of ['X','O']
    ([score_board.iloc[a][b] for a in range(3) for b in range(3) if score_board.iloc[a][b] not in symbol_choices])
    

    플레이어가 셀 값에 대한 올바른 입력을 제공하는지 확인

    Display the cells available for input using the previous method then ask for user input. If the input provided by the user is incorrect three times, exit.

    empty_slots = get_empty_slots()
    invalid_input_count = 0
    while invalid_input_count < 3:
        user_input_cell_choice = input(f"\nEnter either {empty_slots} without the quotes : ")
        if user_input_cell_choice not in empty_slots:
            invalid_input_count += 1
        else:
            return user_input_cell_choice
    if invalid_input_count == 3:
        raise ValueError(f"Maximum '{invalid_input_count}' invalid input choice attempts reached. Thank you for playing.")
    

    마지막 셀 자동 채우기

    The game will be played for 4 rounds, if no one has won before that, and at the start of round 5, only one cell will be left, we can assign it the value of 'X'. We are using 'X' since we are assuming that player using the 'X' symbol starts the game.

    X|O|X|O|X|O|X|O|X ← this will always be 'X'

    누가 이겼는지 확인

    As described previously in our code logic 섹션에서 우승 조합은 다음과 같습니다.


    성공적인 조합


    R1C1, R1C2, R1C3

    R2C1, R2C2, R2C3

    R3C1, R3C2, R3C3

    R1C1, R2C1, R3C1

    R1C2, R2C2, R3C2

    R1C3, R2C3, R3C3

    R1C1, R2C2, R3C3

    R3C1, R2C2, R1C3


    'X'와 'O'를 단순 반복하고 데이터 프레임 값과 비교하면 플레이어가 이미 이겼는지 또는 지금 이겼는지 알 수 있습니다.

    # symbol_choices = ['X','O']
    for symbol_choice in symbol_choices:
        for i in range(3):
            if (symbol_choice == score_board.iloc[i][0] == score_board.iloc[i][1] == score_board.iloc[i][2]) or (symbol_choice == score_board.iloc[0][i] == score_board.iloc[1][i] == score_board.iloc[2][i]):
                return(True,symbol_choice,player_choices[symbol_choice])
        if (symbol_choice == score_board.iloc[0][0] == score_board.iloc[1][1] == score_board.iloc[2][2]) or (symbol_choice == score_board.iloc[0][2] == score_board.iloc[1][1] == score_board.iloc[2][0]):
            return(True,symbol_choice,player_choices[symbol_choice])    
    return (False,symbol_choice,player_choices[symbol_choice])
    


    보너스

    시간 지연 추가

    The time 모듈에는 시간 지연을 도입하는 데 사용할 수 있는 sleep 명령이 있어 컴퓨터 플레이어가 실제로 선택에 대해 생각하는 것처럼 보입니다.

    >>>import time
    >>>time.sleep(2) # this is in seconds.
    


    아스키 아트

    
       ###     ######   ######  #### ####       ###    ########  ######## 
      ## ##   ##    ## ##    ##  ##   ##       ## ##   ##     ##    ##    
     ##   ##  ##       ##        ##   ##      ##   ##  ##     ##    ##    
    ##     ##  ######  ##        ##   ##     ##     ## ########     ##    
    #########       ## ##        ##   ##     ######### ##   ##      ##    
    ##     ## ##    ## ##    ##  ##   ##     ##     ## ##    ##     ##    
    ##     ##  ######   ######  #### ####    ##     ## ##     ##    ##    
    
    
    If you do not know this 또는 this 이미 웹 사이트를 방문하십시오. 터미널 코드에 ASCII 그래픽을 추가하는 것이 재미있습니다. 우리의 친구 "The Machinee"은 여기에서 왔습니다.

    함께 모아서

    Visit my GitHub 터미널에서 플레이할 수 있는 tic-tac-toe 게임을 만들기 위한 전체 코드를 확인하려면 페이지를 참조하십시오.

    좋은 웹페이지 즐겨찾기