class State {
    final static int NR_ROWS = 6, NR_COLUMNS = 7, CELL_EMPTY = -1;

    public int [][] board;	// 0, 1 or -1 (empty)
    public int next_to_move;	// 0 is the max player
    public int depth;		// for searching
    public State() {
	board = new int [NR_ROWS][NR_COLUMNS];
    }
    public String toString() {
	String str = "";
	for (int i = 0; i < NR_ROWS; ++i) {
	    str = str + i + ' ';
	    for (int j = 0; j < NR_COLUMNS; ++j) {
		str = str + ' ' + (board[i][j] == -1 ? ' ' :
				   board[i][j] == 0 ? 'O' : 'X');
	    }
	    str += '\n';
	}
	str += "  ";
	for (int i = 0; i < NR_COLUMNS; ++i)
	    str = str + ' ' + i;
	str += '\n';
	return str;
    }
    // Get counts for a state, and check whether it is the end of the game
    public boolean TerminatedAndGetCounts(Counts c) {
	// Clear the counts
	for (int i = 0; i < 4; ++i)
	    for (int j = 0; j < 2; ++j)
		c.count[i][j] = 0;
	// Find the counts
	for (int i = 0; i < NR_ROWS; ++i)
	    for (int j = 0; j < NR_COLUMNS-4+1; ++j)
		c.AddCount(board[i][j], board[i][j+1],
			   board[i][j+2], board[i][j+3]);
	for (int i = 0; i < NR_ROWS-4+1; ++i)
	    for (int j = 0; j < NR_COLUMNS; ++j)
		c.AddCount(board[i][j], board[i+1][j],
			   board[i+2][j], board[i+3][j]);
	for (int i = 0; i < NR_ROWS-4+1; ++i)
	    for (int j = 0; j < NR_COLUMNS-4+1; ++j)
		c.AddCount(board[i][j], board[i+1][j+1],
			   board[i+2][j+2], board[i+3][j+3]);
	for (int i = 0; i < NR_ROWS-4+1; ++i)
	    for (int j = 0; j < NR_COLUMNS-4+1; ++j)
		c.AddCount(board[i+3][j], board[i+2][j+1],
			   board[i+1][j+2], board[i][j+3]);
	// Check for end of game
	int first_avail_col = 0;
	for (; first_avail_col < NR_COLUMNS; ++first_avail_col)
	    if (board[0][first_avail_col] == CELL_EMPTY)
		break;
	if (first_avail_col == NR_COLUMNS)
	    return true;
	return c.count[3][0]!=0 || c.count[3][1]!=0;
    }
    // Make a move.  Return true if successful, false otherwise
    boolean Apply(int op) {
	int target = 0;
	while (target < NR_ROWS && board[target][op] == CELL_EMPTY)
	    ++target;
	if (target == 0)
	    return false;
	board[target-1][op] = next_to_move;
	next_to_move = 1-next_to_move;
	return true;
    }
}

class Counts {
    public int [][] count;
    Counts() {
	count = new int[4][2];
    }
    public void AddCount(int c1, int c2, int c3, int c4) {
	int count0 = 0, count1 = 0;
	if (c1 == 0)
	    ++count0;
	else if (c1 == 1)
	    ++count1;
	if (c2 == 0)
	    ++count0;
	else if (c2 == 1)
	    ++count1;
	if (c3 == 0)
	    ++count0;
	else if (c3 == 1)
	    ++count1;
	if (c4 == 0)
	    ++count0;
	else if (c4 == 1)
	    ++count1;
	if (count0 == 0 && count1 > 0)
	    ++count[count1-1][1];
	else if (count0 > 0 && count1 == 0)
	    ++count[count0-1][0];
    }
}

public class Fiar {
    public static void main(String [] args) throws java.io.IOException {
	java.io.BufferedReader br =
	    new java.io.BufferedReader(
		new java.io.InputStreamReader(System.in));

	State s = new State();
	for (int i = 0; i < State.NR_ROWS; ++i)
	    for (int j = 0; j < State.NR_COLUMNS; ++j)
		s.board[i][j] = State.CELL_EMPTY;
	s.next_to_move = 0;
	s.depth = 0;

	System.out.println("Play first? ");
	String line = br.readLine();
	java.util.StringTokenizer st =
	    new java.util.StringTokenizer(line);
	char c = st.nextToken().charAt(0);
	int player;
	if (c == 'Y' || c == 'y')
	    player = 0;
	else
	    player = 1;

	Counts cnts = new Counts();
	do {
	    System.out.print(s);
	    System.out.print(s.next_to_move == 0 ? 'O' : 'X');
	    System.out.println("'s move...");
	    // if (s.next_to_move == player) {
	    if (true) {
		for (;;) {
		    System.out.println("Enter move: ");
		    line = br.readLine();
		    st = new java.util.StringTokenizer(line);
		    int op = Integer.parseInt(st.nextToken());
		    if (op >= 0 && op < State.NR_COLUMNS)
			if (s.Apply(op))
			    break;
		    System.out.println("Invalid move!  Please try again.");
		}
	    } else {
		int op = 0;
		// Code the computer's here
		System.out.println("My move: " + op);
		s.Apply(op);
	    }
	} while (!s.TerminatedAndGetCounts(cnts));

	System.out.print(s);
	int winner = -1;
	if (cnts.count[3][0] != 0)
	    winner = 0;
	else if (cnts.count[3][1] != 0)
	    winner = 1;
	if (winner == 0)
	    System.out.println("'O' wins!");
	else if (winner == -1)
	    System.out.println("Draw!");
	else
	    System.out.println("'X' win!");
    }
}

