취미생활/CheckIO

[checkIO]Home 12.Xs and Os Referee

우주먼지의하루 2020. 2. 19. 16:40
728x90

Tic-Tac-Toe, sometimes also known as Xs and Os, is a game for two players (X and O) who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal rows (NW-SE and NE-SW) wins the game.

But we will not be playing this game. You will be the referee for this games results. You are given a result of a game and you must determine if the game ends in a win or a draw as well as who will be the winner. Make sure to return "X" if the X-player wins and "O" if the O-player wins. If the game is a draw, return "D".

A game's result is presented as a list of strings, where "X" and "O" are players' marks and "." is the empty cell.

Input: A game result as a list of strings (unicode).

Output: "X", "O" or "D" as a string.

Example:

checkio([

"X.O",

"XX.",

"XOO"]) == "X"

checkio([

"OO.",

"XOX",

"XOX"]) == "O"

checkio([

"OOX",

"XXO",

"OXX"]) == "D"

* 가로, 세로, 대각선이 모두 O이면 O출력, X면 X출력

* 세개 다 같은 것이 없다면 D를 출력

 

 

- 풀이

 

from typing import List

def checkio(data):
    
    # Checking rows
    i=0
    while i<3:
        line=data[i]
        if (line=="XXX"): return "X"
        if (line=="OOO"): return "O"
        i+=1
        
    # Checking collumns
    j=0
    while j<3:
        column= data[0][j] +  data[1][j] +  data[2][j]
        if (column=="XXX"): return "X"
        if (column=="OOO"): return "O"
        j+=1
    
    # Checking diagonals
    d1=data[0][0] +  data[1][1] +  data[2][2]
    d2=data[2][0] +  data[1][1] +  data[0][2]
    print("D1: " + d1 + " - D2: " + d2)
    diagonals=[d1,d2]
    k=0
    while k<2:
        diagonal=diagonals[k]
        if (diagonal=="XXX"): return "X"
        if (diagonal=="OOO"): return "O"
        k+=1
    
    return "D"
반응형

'취미생활 > CheckIO' 카테고리의 다른 글

[checkIO]Element 02.Easy Unpack  (0) 2020.02.22
[checkIO]Element 01.Say Hi  (0) 2020.02.21
[checkIO]Home 11.Bird Language  (0) 2020.02.17
[checkIO]Home 10.Sun Angle  (0) 2020.02.17
[checkIO]Home 08. Long Repeat  (0) 2020.02.16