import java.util.Scanner;

public class TicTacToe {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        
        char[][] board = new char[3][3];
        //clear the board
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                board[i][j] = ' ';
            }           
        }
        
        char player = 'X';
        boolean hasWinner = false;
        for (int turn = 0; turn < 9 && !hasWinner; turn++) {
            System.out.println("Current player is " + player);
            
            //print the board
            for (char[] row : board) {
                for (char c : row) {
                    System.out.print(c);
                }    
                System.out.println("");
            }  
            
            System.out.println("Please enter the position to place the piece at(1 to 9 like on the numpad)");
        
            int x;
            int y;
            do {
                int number = myScanner.nextInt() - 1;
                
                //calculate the position
                x = number % 3;
                y = 2 - number / 3;               
            }while (x < 0 || y < 0 || x > 2 || y > 2 || board[y][x] != ' ');
        
            board[y][x] = player;
            
            //horizontal victory
            for (int i = 0; i < board.length; i++) {
                char[] row = board[i];
                if (row[0] == row[1] && row[1] == row[2] && row[2] != ' ') {
                    hasWinner = true;
                    break;
                }   
            }
            
            if (!hasWinner) {
                //swap the player
                player = player == 'X' ? 'O' : 'X';
            }
        }
        
        //print winner message
        if (hasWinner) {
            System.out.println(player + " won");
        }else{
            System.out.println("Draw");
        }
    }
}