# Python Tic Tac Toe | Competitive Coding Questions

Python Competitive Coding Questions | Tic Tac Toe: This practice is based on Tic Tac Toe in Python programming language.
Submitted by Prerana Jain, on April 24, 2020

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.

Input:

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

```    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.

To solve this question, we are using Python3.

Code:

```arr = []

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

if ((arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X')):
print("Player 1")# player 1 win
elif((arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == '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
```

Preparation

What's New

Top Interview Coding Problems/Challenges!
{"ft_lang_label":"__label__en","ft_lang_prob":0.8393791,"math_prob":0.9626863,"size":2738,"snap":"2022-27-2022-33","text_gpt3_token_len":1068,"char_repetition_ratio":0.32297,"word_repetition_ratio":0.26611227,"special_character_ratio":0.46128562,"punctuation_ratio":0.056514915,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99055225,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-30T08:07:03Z\",\"WARC-Record-ID\":\"<urn:uuid:b41c5970-b670-4bef-a160-9fb7c3ce6408>\",\"Content-Length\":\"189470\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a05e4e72-5da7-4360-a0fe-f4ff7f949c85>\",\"WARC-Concurrent-To\":\"<urn:uuid:56d12a80-3e70-4163-ad69-ac66e21fc5a0>\",\"WARC-IP-Address\":\"104.21.23.12\",\"WARC-Target-URI\":\"https://www.includehelp.com/python/tic-tac-toe-competitive-coding-questions.aspx\",\"WARC-Payload-Digest\":\"sha1:4HXPMD6OUYOQ2ESPQGWKYXGQYCXQ6ZR2\",\"WARC-Block-Digest\":\"sha1:DFP6JXT4V5QFQER7KNYR4WJMAUCSB5DQ\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103669266.42_warc_CC-MAIN-20220630062154-20220630092154-00366.warc.gz\"}"}