Last active
April 2, 2024 16:53
-
-
Save zack-anthropic/25444526153d5d4246869bb24a0bfccb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import anthropic | |
client = anthropic.Anthropic( | |
api_key="my_api_key", | |
) | |
message = client.messages.create( | |
model="claude-3-opus-20240229", | |
max_tokens=1000, | |
temperature=0, | |
messages=[ | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "text", | |
"text": "Can you please write some code that will allow us to play Tic Tac Toe against each other?" | |
} | |
] | |
}, | |
{ | |
"role": "assistant", | |
"content": [ | |
{ | |
"type": "text", | |
"text": "Certainly! Here's a Python script that allows two players to play Tic Tac Toe against each other:\n\n```python\ndef print_board(board):\n print(\"-------------\")\n for i in range(3):\n print(\"|\", board[i][0], \"|\", board[i][1], \"|\", board[i][2], \"|\")\n print(\"-------------\")\n\ndef check_winner(board):\n for i in range(3):\n if board[i][0] == board[i][1] == board[i][2] != \" \":\n return board[i][0]\n if board[0][i] == board[1][i] == board[2][i] != \" \":\n return board[0][i]\n if board[0][0] == board[1][1] == board[2][2] != \" \":\n return board[0][0]\n if board[0][2] == board[1][1] == board[2][0] != \" \":\n return board[0][2]\n return None\n\ndef play_game():\n board = [[\" \", \" \", \" \"], [\" \", \" \", \" \"], [\" \", \" \", \" \"]]\n player = \"X\"\n while True:\n print_board(board)\n print(\"Player\", player, \"turn.\")\n row = int(input(\"Enter row (0-2): \"))\n col = int(input(\"Enter column (0-2): \"))\n if board[row][col] != \" \":\n print(\"Invalid move. Try again.\")\n continue\n board[row][col] = player\n winner = check_winner(board)\n if winner:\n print_board(board)\n print(\"Player\", winner, \"wins!\")\n break\n if \" \" not in board[0] and \" \" not in board[1] and \" \" not in board[2]:\n print_board(board)\n print(\"It's a tie!\")\n break\n player = \"O\" if player == \"X\" else \"X\"\n\nplay_game()\n```\n\nThis script does the following:\n\n1. The `print_board` function takes the current state of the board and prints it in a formatted way.\n\n2. The `check_winner` function checks if there is a winner by examining the rows, columns, and diagonals of the board. If a winner is found, it returns the winning player (\"X\" or \"O\"). If there is no winner, it returns `None`.\n\n3. The `play_game` function is the main game loop. It initializes an empty board and starts with player \"X\".\n\n4. Inside the game loop:\n - The current state of the board is printed.\n - The current player is prompted to enter the row and column where they want to make a move.\n - If the selected position is already occupied, an invalid move message is displayed, and the player is prompted again.\n - The player's move is recorded on the board.\n - The `check_winner` function is called to check if there is a winner. If a winner is found, the board is printed, the winner is announced, and the game ends.\n - If the board is full (all nine squares have been taken) and there is no winner, it's a tie, and the game ends.\n - The player alternates between \"X\" and \"O\" for each turn.\n\n5. The `play_game` function is called to start the game.\n\nThis script provides a basic implementation of Tic Tac Toe where two players can take turns entering their moves until a winner is determined or the game ends in a tie." | |
} | |
] | |
}, | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "text", | |
"text": "Great, please run the code so we can play against each other. After each of our moves, please list X's squares and O's squares by doing the following 2 actions before stating whether or not anyone has won:\n\n1. State the following to clarify what it means for there to be a winner: \"In order for there to be a winner, the given row, column, or diagonal must have all three elements in the array occupied by the same letter. Therefore any array with both an O and an X is not a winning array.\"\n2. Run `check_winner` line by line, checking each row, column, and diagonal\n\nAfter each move I make, think step by step in <thinking> tags about the best move, then play the move that gives you the best chance of winning (or avoiding a loss)." | |
} | |
] | |
}, | |
{ | |
"role": "assistant", | |
"content": [ | |
{ | |
"type": "text", | |
"text": "Sure! Let's play Tic Tac Toe. I'll run the code and we can take turns making moves. After each move, I'll define what it means to have a winner and list X's squares and O's squares while running the `check_winner` function line by line before stating if there's a winner. And before each of my moves, I'll think carefully about the best choice.\n\n-------------\n| | | |\n-------------\n| | | |\n-------------\n| | | |\n-------------\nPlayer X turn." | |
} | |
] | |
}, | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "text", | |
"text": "Row: 1\nColumn: 1" | |
} | |
] | |
}, | |
] | |
) | |
print(message.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment