Created
December 6, 2022 21:29
-
-
Save matthewdeanmartin/0b2d796241c03c46a586d283a860780d to your computer and use it in GitHub Desktop.
ChatGPT version of corewars
This file contains hidden or 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 random | |
| # Constants for the game | |
| MEMORY_SIZE = 100 | |
| INSTRUCTION_SET = ['DAT', 'MOV', 'ADD', 'SUB', 'JMP', 'JMZ', 'CMP'] | |
| # Define the player class | |
| class Player: | |
| def __init__(self, name, program): | |
| self.name = name | |
| self.program = program | |
| self.ip = 0 # instruction pointer | |
| # Initialize the game | |
| memory = [None] * MEMORY_SIZE | |
| players = [] | |
| # Create the players | |
| p1 = Player('Player 1', ['MOV #0, @0', 'ADD #1, @0', 'JMP @0']) | |
| p2 = Player('Player 2', ['MOV #0, @99', 'SUB #1, @99', 'JMZ @99']) | |
| players.append(p1) | |
| players.append(p2) | |
| # Main game loop | |
| while True: | |
| # Randomly shuffle the order of the players each turn | |
| random.shuffle(players) | |
| # Execute one instruction for each player | |
| for player in players: | |
| # Get the instruction from the player's program | |
| instruction = player.program[player.ip] | |
| # Parse the instruction | |
| parts = instruction.split() | |
| if len(parts) == 2: | |
| op, a = parts | |
| b = None | |
| elif len(parts) == 3: | |
| op, a, b = parts | |
| # Execute the instruction | |
| if op == 'DAT': | |
| # End the player's program | |
| player.program = None | |
| elif op == 'MOV': | |
| # Move the value in "a" to the address in "b" | |
| value = eval(a, {}, memory) | |
| address = eval(b, {}, memory) | |
| memory[address] = value | |
| elif op == 'ADD': | |
| # Add the value in "a" to the address in "b" | |
| value = eval(a, {}, memory) | |
| address = eval(b, {}, memory) | |
| memory[address] += value | |
| elif op == 'SUB': | |
| # Subtract the value in "a" from the address in "b" | |
| value = eval(a, {}, memory) | |
| address = eval(b, {}, memory) | |
| memory[address] -= value | |
| elif op == 'JMP': | |
| # Jump to the address in "a" | |
| address = eval(a, {}, memory) | |
| player.ip = address | |
| elif op == 'JMZ': | |
| # Jump to the address in "b" if the value in "a" is zero | |
| value = eval(a, {}, memory) | |
| address = eval(b, {}, memory) | |
| if value == 0: | |
| player.ip = address | |
| elif op == 'CMP': | |
| # Compare the value in "a" to the address in "b" | |
| # | |
| value = eval(a, {}, memory) | |
| address = eval(b, {}, memory) | |
| if value == memory[address]: | |
| player.program = None | |
| # Move to the next instruction | |
| player.ip += 1 | |
| # Check if only one player remains | |
| remaining_players = [p for p in players if p.program is not None] | |
| if len(remaining_players) == 1: | |
| winner = remaining_players[0] | |
| print(f'{winner.name} wins!') | |
| break | |
| elif len(remaining_players) == 0: | |
| print('The game ended in a tie!') | |
| break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment