Advertisement
MonkaGaming

Tic Tac Toe Gemini Google AI

Nov 7th, 2024
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.52 KB | Source Code | 0 0
  1. extends Node2D
  2.  
  3. # ... other node setup code ... (Replace this comment with your actual code for creating the board visuals and cells)
  4.  
  5. var current_player = 1  # 1 for player, 2 for AI
  6. var board = [
  7.     [0, 0, 0],
  8.     [0, 0, 0],
  9.     [0, 0, 0]
  10. ]
  11.  
  12. # Get the click position relative to the scene
  13. var click_pos = event.position
  14.  
  15. # Convert click position to grid coordinates
  16. var grid_x = floor(click_pos.x / cell_size)  # Replace "cell_size" with your actual cell size
  17. var grid_y = floor(click_pos.y / cell_size)
  18.  
  19. # Check if the clicked cell is empty
  20. if board[grid_y][grid_x] == 0:
  21.     # Update the board state
  22.     board[grid_y][grid_x] = current_player
  23.  
  24.     # Update the visual representation of the board (add code to display X or O on the clicked cell)
  25.  
  26.     # Check for win/draw condition (add code to check if the current player has won or if the game is a draw)
  27.  
  28.     # Switch to AI's turn
  29.     current_player = 2
  30.  
  31. func _input(event):
  32.     # Check for left mouse click
  33.     if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
  34.         # Handle player's click
  35.         pass  # Replace with your actual click handling code
  36.  
  37. func _process(delta):
  38.     if current_player == 2:
  39.         # AI's turn
  40.         var ai_move = get_ai_move()
  41.         # Make the AI's move
  42.         # ...
  43.         current_player = 1
  44.  
  45. func get_ai_move():
  46.     # Simple AI: choose a random empty cell
  47.     var empty_cells = []
  48.     for y in range(3):
  49.         for x in range(3):
  50.             if board[y][x] == 0:
  51.                 empty_cells.append([x, y])
  52.    
  53.     var random_index = randi() % empty_cells.size()
  54.     return empty_cells[random_index]
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement