python井字棋游戏_Python井字游戏| 竞争编码问题

python井字棋游戏

Question:

题:

You will be given a completed board of Tic-Tac-Toe. The cells will be represented by 'X' and 'O'. If there are any empty cells, they will be represented by '#'. 'X' is used by Player 1 and 'O' is used by Player 2. Given the board, print Player 1 if player 1 wins, print Player 2 if player 2 wins, and print "Draw" in case of a Tie. It is guaranteed that all tic-tac-toe boards given in input are valid.

您将获得一个完整的井字游戏董事会。 单元格将由'X''O'表示 。 如果有任何空单元格,它们将由'#'表示。 玩家1使用'X'玩家2使用'O' 。 鉴于主板,打印播放器1如果要是玩家2机1胜,打印播放器2,并打印“画”在平局的情况下。 确保输入中提供的所有井字游戏板均有效。

Input:

输入:

3x3 Tic Tac Toe board. 3 lines containing 3 characters each separated by spaces.

3x3井字游戏板。 3行包含3个字符,每个字符用空格分隔。

    X  O  X
    O  X  X
    O  O  X

Output:

输出:

Print who wins in a single line.

打印谁在单行中获胜。

    Player 1

Explanation/Solution:

说明/解决方案:

In this question we have given 3x3 Tic Tac Toe board and we have to find which player won. The player who wins has the same 3 consecutive symbols like 'X' and 'O'. Both the symbols are represented by the different players like 'X' for player 1 and 'O' for player 2. We can solve this question by making the 3x3 list by using python 3.

在这个问题中,我们提供了3x3的Tic Tac Toe棋盘 ,我们必须找出哪个球员赢了。 获胜的玩家具有相同的3个连续符号,例如'X''O' 。 这两个符号都由不同的玩家表示,例如玩家1的“ X”和玩家2的“ O” 。我们可以通过使用python 3制作3x3列表来解决此问题。

To solve this question, we are using Python3.

为了解决这个问题,我们使用Python3。

Code:

码:

arr = []

# taking input 3 X3 matrix
for i in range(3):
    arr.append(list(input().split()))

if ((arr[0][0] == arr[0][1] and arr[0][1] == arr[0][2] and arr[0][0] == 'X') or
(arr[1][0] == arr[1][1] and arr[1][1] == arr[1][2] and arr[1][0] == 'X') or
(arr[2][0] == arr[2][1] and arr[2][1] == arr[2][2] and arr[2][0] == 'X') or
(arr[0][0] == arr[1][0] and arr[1][0] == arr[2][0] and arr[0][0] == 'X') or
(arr[0][1] == arr[1][1] and arr[1][1] == arr[2][1] and arr[0][1] == 'X') or
(arr[0][2] == arr[1][2] and arr[1][2] == arr[2][2] and arr[0][2] == 'X') or
(arr[0][0] == arr[1][1] and arr[1][1] == arr[2][2] and arr[0][0] == 'X') or
(arr[0][2] == arr[1][1] and arr[1][1] == arr[2][0] and arr[0][2] == 'X')):
    print("Player 1")# player 1 win
elif((arr[0][0] == arr[0][1] and arr[0][1] == arr[0][2] and arr[0][0] == 'O') or
(arr[1][0] == arr[1][1] and arr[1][1] == arr[1][2] and arr[1][0] == 'O') or
(arr[2][0] == arr[2][1] and arr[2][1] == arr[2][2] and arr[2][0] == 'O') or
(arr[0][0] == arr[1][0] and arr[1][0] == arr[2][0] and arr[0][0] == 'O') or
(arr[0][1] == arr[1][1] and arr[1][1] == arr[2][1] and arr[0][1] == 'O') or
(arr[0][2] == arr[1][2] and arr[1][2] == arr[2][2] and arr[0][2] == 'O') or
(arr[0][0] == arr[1][1] and arr[1][1] == arr[2][2] and arr[0][0] == 'O') or
(arr[0][2] == arr[1][1] and arr[1][1] == arr[2][0] and arr[0][2] == 'O')):
    print("Player 2")# player 2 win
else :
    print("Draw")# Match is drawn no body win

Output

输出量

RUN 1:
X X X
O O X
O O #
Player 1

RUN 2:
O X O
X O X
O # #
Player 2


翻译自: https://www.includehelp.com/python/tic-tac-toe-competitive-coding-questions.aspx

python井字棋游戏

你可能感兴趣的:(游戏,列表,python,tensorflow,anaconda)