### Week 20 Tic-tac-toe v1.0

posted Apr 12, 2017, 7:46 AM by Attila Lengyel   [ updated Apr 12, 2017, 4:40 PM ]
 import randomdef drawBoard(board):  print("    |   |   ")  print(" ",board[0],"|",board[1],"|",board[2])  print("    |   |   ")    print("-------------")  print("    |   |   ")  print(" ",board[3],"|",board[4],"|",board[5])  print("    |   |   ")    print("-------------")  print("    |   |   ")  print(" ",board[6],"|",board[7],"|",board[8])  print("    |   |   ")  def win(board, player):  if board[0] == player and board[1] == player and board[2] == player:    return True  if board[3] == player and board[4] == player and board[5] == player:    return True  if board[6] == player and board[7] == player and board[8] == player:    return True  if board[0] == player and board[3] == player and board[6] == player:    return True  if board[1] == player and board[4] == player and board[7] == player:    return True  if board[2] == player and board[5] == player and board[8] == player:    return True  if board[0] == player and board[4] == player and board[8] == player:    return True  if board[2] == player and board[4] == player and board[6] == player:    return True    return False############## Get computer mark ############def opponent(sUser):  if sUser == "O":    return "X"  return "O"############## Strategy to move random ############def randommove(board, sUser):  emptystep = []  for x in range(0,9):    if board[x] == " ":      emptystep.append(x)    move = random.randint(0,len(emptystep)-1)  move = emptystep[move]  return move############## Strategy to win or block ############def nextMoveWin(board, sUser):  for x in range(0,9):    if board[x] == " ":      board[x] = sUser      isWin = win(board, sUser)      board[x] = " "      if isWin:        return x  return -1    ############## Move center ##################def moveCenter(board):  if board[4] == " ":    return 4  return -1############## Move Corner ##################def moveCorner(board):  if board[1] == " " and board[3] == " " and board[0] == " ":    return 0  if board[1] == " " and board[5] == " " and board[2] == " ":    return 2  if board[5] == " " and board[7] == " " and board[8] == " ":    return 8  if board[3] == " " and board[7] == " " and board[6] == " ":    return 6    if board[0] == " ":    return 0      if board[2] == " ":    return 2  if board[6] == " ":    return 6  if board[8] == " ":    return 8  return -1############## Check for tie ##################def checktie(board):  isTie = True  for x in range(0,9):    if board[x] == " ":      isTie = False    return isTie############## Head or Tail ######################def headtail():  bet = input("Head or Tail:")  flip = random.randint(0,1)  if flip == 0:    coin = "Head"  else:    coin = "Tail"    if bet == coin:    return True    return False############## User move ##################def usermove(board, user):  move = input("Make your move (1~9):")  if board[int(move) - 1] != " ":    print("You lost your move!!!")  else:    board[int(move) - 1] = user    drawBoard(board)    if win(board, user):    print("The user ", user, "won!!!")    return True    if checktie(board):    print("It is a tie")    return True      return False  ############## 2 ways block ##################def twowaysblock(steps, board, user):  empty = 0  x = 0  o = 0    for i in steps:    if board[i] == ' ': empty += 1    if board[i] == 'X': x += 1    if board[i] == 'O': o += 1      if user == 'X' and empty == 3 and x == 2:    for i in steps:      if board[i] == ' ':        return i    if user == 'O' and empty == 3 and o == 2:    for i in steps:      if board[i] == ' ':        return i    return -1  ############## Scanning for 2 ways ############def scan2ways(board, user):  steps = [0,2,8,1,5]  move = twowaysblock(steps, board, user)  if move != -1: return move    steps = [2,8,6,5,7]  move = twowaysblock(steps, board, user)  if move != -1: return move    steps = [8,6,0,7,3]  move = twowaysblock(steps, board, user)  if move != -1: return move  steps = [6,0,2,3,1]  move = twowaysblock(steps, board, user)  if move != -1: return move  return -1  ############## Computer move ##################def computermove(board, user):    movetype = ""    move = nextMoveWin(board, opponent(user))  if move == -1:    move = nextMoveWin(board, user)  else:    if movetype == "": movetype = "Computer moves to win!"    if move == -1:    move = scan2ways(board, user)  else:    if movetype == "": movetype = "Computer moves to win!"  if move == -1:    move = moveCenter(board)  else:    if movetype == "": movetype = "Computer blocks your win!"  if move == -1:    move = moveCorner(board)  else:    if movetype == "": movetype = "Computer moves center!"  if move == -1:    move = randommove(board,opponent(user))  else:    if movetype == "": movetype = "Computer moves corner!"  if movetype == "": movetype = "Computer moves random!"  print(movetype)  board[move] = opponent(user)  print("Computer moves", move + 1)    drawBoard(board)    if win(board, opponent(user)):    print("The computer", opponent(user), "won!!!")    return True    if checktie(board):    print("It is a tie!")    return True    return False################## Main Program ##################tttBoard = [" "] * 9drawBoard(tttBoard)#if headtail():#  print("You won, you will be X")user = "X"#else:#  print("You lost, you will be O")# user = "O"#print("The computer is", opponent(user))while(True):    if user == "X":    if usermove(tttBoard, user):break    if computermove(tttBoard, user): break  else:    if computermove(tttBoard, user):break    if usermove(tttBoard, user):break
Comments