Noughts & Crosses

No attempts yetTime limit1sMemory limit128 MB

Problem

Noughts and crosses (also known as tic-tac-toe) is a simple game for two players. The players take turns placing their own symbol on a 3×3 board; one player uses X and the other uses O. The aim is to be the first to line up three of your own symbols in a row — horizontally, vertically, or diagonally.

Three example games of noughts and crosses

  • In Game 1, X wins with a horizontal row.
  • In Game 2, O wins with a diagonal row.
  • In Game 3 neither side completes a row, so the game is a draw.

Given the record of the moves in one or more games, determine the result of each game.

Input

You are given one or more games to replay, and you must decide the result of each one.

Each game is written on its own line. The line begins with the letter X or O, showing which player takes the first turn. That player makes the first move, and the two players then alternate turns.

After the leading letter come the moves, all on the same line and separated by spaces. Each move is a number from 1 to 9 that names the board square being marked, numbered as follows:

1 | 2 | 3
---------
4 | 5 | 6
---------
7 | 8 | 9

The 3×3 board with its squares numbered 1 to 9

For example, Game 1 above is recorded as the leading letter X followed by the moves 1 5 9 7 3 6 2.

The input ends with a line containing only the character #. That line does not belong to any game and must not be processed.

Output

For each game, print exactly one line.

  • If a player completes a row, print the winner's uppercase letter: X or O.
  • If neither player completes a row, print the word Draw.

A player wins the moment they complete a row of three; any moves listed after that on the same line do not change the result.