Xs and OS referee

Source: Internet
Author: User
Xs and OS referee
 1 def checkio(game_result): 2     winner = ‘D‘ 3  4     for row in game_result: 5         if row[0] == row[1] == row[2] and row[0] != ‘.‘: 6             winner = row[0] 7  8     for col in range(0, 3): 9         if game_result[0][col] == game_result[1][col] == game_result[2][col] and game_result[0][col] != ‘.‘:10             winner = game_result[0][col]11 12     if game_result[0][0] == game_result[1][1] == game_result[2][2] and game_result[0][0] != ‘.‘:13         winner = game_result[0][0]14 15     if game_result[0][2] == game_result[1][1] == game_result[2][0] and game_result[0][2] != ‘.‘:16         winner = game_result[0][2]17 18     return winner

The conclusion of this question is that python supports the judgment of the same mode: Row [0] = row [1] = row [2], that is, supports connections, etc.

Let's take a look at the great god code.

 

1 def checkio(result):2     rows = result3     cols = map(‘‘.join, zip(*rows))4     diags = map(‘‘.join, zip(*[(r[i], r[2 - i]) for i, r in enumerate(rows)]))5     lines = rows + list(cols) + list(diags)6  7     return ‘X‘ if (‘XXX‘ in lines) else ‘O‘ if (‘OOO‘ in lines) else ‘D‘

 

The zip function softens two arrays, such as the student name = ['bob', 'jenny '], grade = [80, 90], zip (name, grade) = [('bob', 80), ('jenny ', 90)]. In function calls, use * List/tuple to separate list/tuple, pass the parameter as a location parameter to the corresponding function (provided that the corresponding function supports an indefinite number of location parameters), such as test = ["XXX", "ooo ","... "], zip (* test) = [('x', 'O ','. '), ('x', 'O ','. '), ('x', 'O ','. ')]

The map function accepts two parameters. The first parameter is the function name, and the second parameter is an iterative object, for example, array = [1, 2, 3], map (STR, array) = ['1', '2', '3'], that is, the first function is applied to the second object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.