Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Sandbox

Post History

60%
+1 −0
#1: Initial revision by user avatar luap42‭ · 2021-08-10T15:22:17Z (over 3 years ago)
A two player tic-tac-toe
Your task is to develop a two player tic-tac-toe.

The game rules:

* You have two players, one is called "X" and the other "O".
* You have a field of 3&times;3 positions; any position may be marked as "." (empty), "X" or "O". We start with every position being "." (empty).
* The player called "X" starts by marking any "." position with an "X". Then it's the other player's turn, who has to mark any "." position with an "O". They continue alternating until we have a winner or a tie.
* We have a winner if there are three consecutive "X"s or "O"s in any row, column or diagonal. Example:

    ```text
    O.X
    OX.
    X.O
    ```
* We have a tie if the board has been completely filled without any player having won.

The fields are enumerated according to the following pattern

```text
012
345
678
```

In every turn the game outputs the full board as shown above followed by a new-line and a prompt consisting of the player active and the following sequence: `> `. Example:

```text
...
.X.
.O.

X> (cursor here)
```

The player inputs the number of the position they want to add their mark to. If the input is valid (a valid number between 0 and 8 and the position is yet empty), the game applies the input and starts a new turn (for the other player). Otherwise it'll print `invalid.` followed by a new line and a new prompt; the game may print a new board too.

After every turn, the game checks if a win or a tie has been created. In this case the game shows "X won.", "O won." or "tie." respectively.

Your task is to write the game in your language of choice. You have to follow this specification exactly as written above. Additional new-lines are allowed around game boards and messages (invalid, win, tie) and at the end.

There's an ungolfed example implementation at [Try it online!](https://tio.run/##nVRRb4IwEH6/X9H1RYjMCCgqyfYXfDUxhHRatRmWBqqOX89awEmZGLc@lNxx33fftdcThTyk3H8VhSjKck@ONP5ISbZFbwiPrguDSEhBs1x7V0sMZCPZmca1VznHAJcDS6jCYMQ4uvGEgNQSGePSsqFl3ELW49CP7Pu//DDo@xWEi8hgbIyMiqRQmhgXJ2k1wteG4ggNEX5HuGauNrZrgDyVuoIjEVYuMwdlhO@ptbDtupJbOsz4mSRsO2po9NqkXDJ@orWSzSHNKY93jCbbSpC0qhz2NWOrmnZshF706eO/ZzTur8UemvRD1HssPZKGbhhdjzehZ8JlTL6o7of1jxTjSiOnTeWaphfZzl2Yb8ZNTHPaBwvMuJlpzvtgHZGd5EEfzH0octYH87rF/Evk5ElY8BDmNY@quVPVitZgNXDQdbP1EzBu@lcvrtAl5e1O/Mgo@az5aFIxLjVZsz3BuHzA2B04rtbccaoxpDLnVE8j/W2PHiwZVdRlOQYXPPBhAlMIYAZz@AY "Python 3 (PyPy) – Try It Online")


Example inputs:

```text
Tie:
    4, 2, 8, 0, 1, 7, 3, 5, 6
Win for X:
    0, 1, 2, 3, 4, 5, 6
Win for O:
    6, 4, 0, 3, 1, 5
```

You may get your input via any way you want, including via stdin or arguments. Output to stdout. Standard loopholes apply. This is code-golf, so the shortest code in any language wins.