reorx
11/16/2012 - 3:58 PM

hackrank_1.py


#!/bin/python
import random


X_SYMBOL = 'X'

O_SYMBOL = 'O'


# Complete the function below to print 2 integers separated by a single space which will be your next move
# Refer section <i>Output format</i> for more details
def nextMove(player, board):
    # restruct matrix from board
    matrix = [list(i) for i in board]

    # init players' tic
    Xs, Os = [], []

    for i, row in enumerate(matrix):
        for j, point in enumerate(row):
            if point == X_SYMBOL:
                Xs.append((i, j))
            elif point == O_SYMBOL:
                Os.append((i, j))
            else:
                pass
    print Xs, Os

    # check for logicability
    X_num = len(Xs)
    O_num = len(Os)
    if player == X_SYMBOL:
        assert X_num == O_num,\
            'Logic problem, player is %s, %s X, %s O' % (player, X_num, O_num)
    else:
        assert X_num - O_num == 1,\
            'Logic problem, player is %s, %s X, %s O' % (player, X_num, O_num)

    # calculate ways to win


#If player is X, I'm the first player.
#If player is O, I'm the second player.
player = raw_input()

#Read the board now. The board is a 3x3 array filled with X, O or _.
board = []
for i in xrange(0, 3):
    board.append(raw_input())

nextMove(player, board)