andretafta

2.2.3 Intro to AI Python CodeHS

Feb 6th, 2026 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.31 KB | None | 0 0
  1. #Global Variables
  2. board = [['-', '-', '-'],
  3.         ['-', '-', '-'],
  4.         ['-', '-', '-']]
  5. player = "X"
  6. flag = False
  7. flag2 = False
  8.  
  9. #Copy your functions from the previous activities here:
  10. #print_board, is_valid_move, place_player, take_turn
  11. def print_board():
  12.    
  13.     n = 0
  14.     print()
  15.     print('   0   1   2')
  16.     print()
  17.    
  18.     for i in board:
  19.         print(n, end='  ')
  20.         n += 1
  21.         for j in i:
  22.             print(j, end='   ')
  23.         print()
  24.         print()
  25.  
  26. #returns true if a row,col on the board is open
  27. def is_valid_move(row, col):
  28.     if row not in (0,1,2) or col not in (0,1,2):
  29.         print("Please enter a valid move")
  30.         return False
  31.     if board[row][col] != "-":
  32.         print("That spot is already taken")
  33.         return False
  34.     return True
  35.  
  36.        
  37. #places player on row,col on the board
  38. def place_player(player, row, col):
  39.  
  40.     board[row][col] = player
  41.    
  42. #Asks the user to enter a row and col until the user enters a valid location
  43. #Adds user location to the board, and prints the board
  44. def take_turn(player):
  45.     print(f"{player}'s Turn")
  46.     valid = False
  47.     while not valid:
  48.         row = int(input("Enter a row    : "))
  49.         col = int(input("Enter a column : "))
  50.         valid = is_valid_move(row, col)
  51.     place_player(player, row, col)
  52.     print_board()
  53.  
  54.  
  55. #Write your check win functions here:
  56. def check_col_win(player):
  57.     for c in range(3):
  58.         if board[0][c] == board[1][c] == board[2][c] == player:
  59.             return True
  60.     return False
  61.  
  62.        
  63. def check_row_win(player):
  64.     for r in range(3):
  65.         if board[r][0] == board[r][1] == board[r][2] == player:
  66.             return True
  67.     return False
  68.  
  69.    
  70.    
  71. def check_diag_win(player):
  72.     if board[0][0] == board[1][1] == board[2][2] == player:
  73.         return True
  74.     if board[0][2] == board[1][1] == board[2][0] == player:
  75.         return True
  76.     return False
  77.  
  78.            
  79. def check_win(player):
  80.     return (
  81.         check_row_win(player) or
  82.         check_col_win(player) or
  83.         check_diag_win(player)
  84.     )
  85.  
  86.    
  87.  
  88.  
  89. def check_tie():
  90.     for row in board:
  91.         if "-" in row:
  92.             return False
  93.     return True
  94.  
  95.        
  96.        
  97. #Start of program
  98. print("\t\tWelcome to Tic Tac Toe!")
  99. print_board()
  100.  
  101. '''
  102. DO NOT CHANGE: Tests
  103. each test should print true if your function is written correctly:
  104. '''
  105. #Check row win
  106. place_player(player, 0,0)
  107. place_player(player, 0,1)
  108. place_player(player, 0,2)
  109. print_board()
  110. print("Check Row Win: ", check_win(player))
  111.  
  112. #Check col win
  113. board = []
  114. for i in range(3):
  115.     board.append(["-","-","-"])
  116. place_player(player, 0,0)
  117. place_player(player, 1,0)
  118. place_player(player, 2,0)
  119. print_board()
  120. print("Check Col Win: ", check_win(player))
  121.  
  122. #check diag win
  123. board = []
  124. for i in range(3):
  125.     board.append(["-","-","-"])
  126. place_player(player, 0,0)
  127. place_player(player, 1,1)
  128. place_player(player, 2,2)
  129. print_board()
  130. print("Check Diag Win: ", check_win(player))
  131.  
  132. #Check Tie
  133. board = []
  134. for i in range(3):
  135.     board.append(["-","-","-"])
  136. place_player("X", 1,1)
  137. place_player("X", 0,2)
  138. place_player(player, 0,0)
  139. place_player("O", 0,1)
  140. place_player("O", 2,2)
  141. place_player("O", 1,0)
  142. place_player("X", 1,2)
  143. place_player("X", 2,1)
  144. place_player("O", 2,0)
  145. print_board()
  146. print("Check Tie: ", check_tie())
Advertisement
Add Comment
Please, Sign In to add comment