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

A two player tic-tac-toe

+1
−0

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×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:

    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

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:

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

Example inputs:

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads