본문 바로가기

카테고리 없음

#3. TicTacToe GameChallenge

def GameChallenge(strArr):
  temp = [e for e in strArr if e != "<>"]
  #board = [temp[i:i+3] for i in range(0, len(temp), 3)]
  #ans = -1
  for i in range(0,9,4):
    row = [strArr[i],strArr[i+1],strArr[i+2]]
    if row.count("X") == 2 and row.count("-") == 1:
      return i + row.index("-")
    if row.count("O") == 2 and row.count("-") == 1:
      return i + row.index("-")

  for i in range(3):
    col = [strArr[i],strArr[i+4],strArr[i+8]]
    if col.count("X") == 2 and col.count("-") == 1:
      return i + 4 * col.index("-")
    if col.count("O") == 2 and col.count("-") == 1:
      return i + 4 * col.index("-")


  diag1 = [strArr[2],strArr[5],strArr[8]]
  dic1 = {0:2, 1:5, 2:8}
  if diag1.count("X") == 2 and diag1.count("-") == 1:
    return dic1[diag1.index("-")]
  if diag1.count("O") == 2 and diag1.count("-") == 1:
    return dic1[diag1.index("-")]

  diag2 = [strArr[0],strArr[5],strArr[10]]
  dic2 = {0:0, 1:5, 2:10}
  if diag2.count("X") == 2 and diag2.count("-") == 1:
    return dic2[diag2.index("-")]
  if diag2.count("O") == 2 and diag2.count("-") == 1:

    return dic2[diag2.index("-")]
  return -1
# keep this function call here
string = ["-","X","-","<>","O","X","O","<>","-","-","-"]

print(GameChallenge(string))​
def GameChallenge(strArr):
  temp = [e for e in strArr if e != "<>"]
  board = [temp[i:i+3] for i in range(0, len(temp), 3)]
  ans = -1
  for i in range(0,9,4):
    row = [strArr[i],strArr[i+1],strArr[i+2]]
    if row.count("X") == 2 and row.count("-") == 1:
      return i + row.index("-")
    if row.count("O") == 2 and row.count("-") == 1:
      return i + row.index("-")

  for i in range(3):
    col = [strArr[i],strArr[i+4],strArr[i+8]]
    if col.count("X") == 2 and col.count("-") == 1:
      return i + 4 * col.index("-")
    if col.count("O") == 2 and col.count("-") == 1:
      return i + 4 * col.index("-")


  diag1 = [strArr[2],strArr[5],strArr[8]]
  dic1 = {0:2, 1:5, 2:8}
  if diag1.count("X") == 2 and diag1.count("-") == 1:
    return dic1[diag1.index("-")]
  if diag1.count("O") == 2 and diag1.count("-") == 1:
    return dic1[diag1.index("-")]

  diag2 = [strArr[0],strArr[5],strArr[10]]
  dic2 = {0:0, 1:5, 2:10}
  if diag2.count("X") == 2 and diag2.count("-") == 1:
    return dic2[diag2.index("-")]
  if diag2.count("O") == 2 and diag2.count("-") == 1:

    return dic2[diag2.index("-")]
  return -1
# keep this function call here
string = ["-","X","-","<>","O","X","O","<>","-","-","-"]

print(GameChallenge(string))