3-i-rad
InterviewTime limit1sMemory limit1024 MB
Play tic-tac-toe interactively against an opponent, reading the board after each of their moves and printing your move; end the game once either side wins or draws.
- Level
Medium5 of 10
- Topics
- Simulation, Game theory, Implementation, Backtracking
- Solved
- No attempts yet
Problem
Mårten is extremely good at 3-i-rad (three in a row). So good that you never manage to beat him! Write a computer program that plays 3-i-rad for you, so that you might have a chance.
If you do not know what 3-i-rad is, you can read about how the game works on Wikipedia.
Input
This problem is interactive. You should first read a line containing either first or second, which tells you whether you move first or not.
Each time it is your turn to move, you should read in a 3-i-rad board with the move Mårten made (or a blank board for the very first move), and then print a 3-i-rad board with the move you made.
If you print a board where you win or draw, your program should terminate. If you read in a board where Mårten has won or drawn, your program should terminate.
A board should be printed and read as 3 lines of 3 characters each: either . for an empty square, o for Mårten's pieces, or x for your pieces (note: lowercase letters).
Output
In the example below, Mårten plays first. Mårten's output is in bold.
second
..o
...
...
..o
.x.
...
.oo
.x.
...
xoo
.x.
...
xoo
.xo
...
xoo
.xo
..x
After the last move you won against Mårten, and should therefore terminate your program.