{"id":135,"date":"2024-10-25T14:08:45","date_gmt":"2024-10-25T14:08:45","guid":{"rendered":"https:\/\/theroyalscode.com\/students\/a_carpenter\/?p=135"},"modified":"2024-10-25T14:08:45","modified_gmt":"2024-10-25T14:08:45","slug":"free-friday-10-25","status":"publish","type":"post","link":"https:\/\/theroyalscode.com\/students\/a_carpenter\/2024\/10\/25\/free-friday-10-25\/","title":{"rendered":"Free Friday 10\/25"},"content":{"rendered":"\n<p><strong><span style=\"text-decoration: underline;\">I committed and synced the code for a turn-based combat game I made with a friend. Here is the code for the main loop and game class:<\/span><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\nimport random\nfrom player import Player\n\n\n\n\n\n\nclass Game:\n    def __init__(self):\n        self.player_turn = True\n        self.player = None\n        self.computer = None\n\n        warrior = Player(\"Waldo\", 10, 5, 100, &#91;\"stab\", \"slash\"], &#91;20, 30])\n        archer = Player(\"Alex\", 5, 3, 100, &#91;\"stab\", \"shoot\"], &#91;15, 25])\n        wizard = Player(\"Wilmoth\", 5, 1, 100, &#91;\"slap\", \"magicmissile\"], &#91;1, 60])\n        rogue = Player(\"Remy\", 15, 7, 200, &#91;\"stab\", \"rararatsputin\"], &#91;20, 80])\n\n\n\n    def turn(self):\n        if self.player_turn:\n            player_move_index = int(input(\"Choose a move (0: first move, 1: second move): \"))\n            player_move = self.player.moves&#91;player_move_index]\n            if player_move == \"quit\":\n                self.exit()\n            self.attack(player_move, self.computer)\n            self.player_turn = False\n        else:\n            computer_move = '1'\n            self.attack(computer_move, self.player)\n            self.player_turn = True\n        print(self.player.moves)\n        print(self.computer.moves)\n        self.player.hp -= 10\n        print(self.player.hp)\n        print(self.computer.hp)\n\n\n    def attack(self, move, target):\n        if move == 'q':\n            exit()\n        elif move in self.player.moves:\n            move_index = self.player.moves.index(move)\n            move_power = self.player.defn&#91;move_index]\n            target.hp -= 10 + move_power + self.player.atk - target.defn&#91;1]\n        else:\n            print(\"Invalid move selected. Please choose a valid move.\")\n\n    def check_winner(self):\n        if self.player.hp &lt;= 0:\n            print(\"Computer AI wins!\")\n            self.playing == False\n        elif self.computer.hp &lt;= 0:\n            print(\"Player wins!\")\n            self.playing == False\n\n    def restart(self):\n        self.player.hp = 100\n        self.computer.hp = 100\n        print(\"Game restarted.\")\n\n    def exit(self):\n        print(\"Exiting Game...\")\n        print(\"Game Closed Successfully.\")\n        sys.exit()\n\n    def run(self):\n        player_choice = int(input(\"Choose your character (0: Waldo, 1: Alex, 2: Wilmoth, 3: Remy:) > \"))\n        characters = &#91;\n            Player(\"Waldo\", 10, 5, 100, &#91;\"stab\", \"slash\"], &#91;20, 30]),\n            Player(\"Alex\", 5, 3, 100, &#91;\"stab\", \"shoot\"], &#91;15, 25]),\n            Player(\"Wilmoth\", 5, 1, 100, &#91;\"slap\", \"magicmissile\"], &#91;1, 60]),\n            Player(\"Remy\", 15, 7, 200, &#91;\"stab\", \"rararatsputin\"], &#91;20, 80]),\n            Player(\"HitMan\", 20, 15,  900, &#91;\"shoot\",\"Tactical nuke\"], &#91;90,169])\n        ]\n\n        self.player = characters&#91;player_choice]\n        characters.remove(self.player)  # Remove the player's choice from available characters\n        self.computer = random.choice(characters)\n\n\n\n        print(f\"Player: {self.player.name}\")\n        print(f\"Computer: {self.computer.name}\")\n\n        self.playing = True\n        while self.playing:\n            self.turn()\n            self.check_winner()\n\n\ndef main():\n    game = Game()\n    game.run()\n\nmain()<\/code><\/pre>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\">And the player code:<\/span><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Player:\n    def __init__(self, name, atk, pwr, hp, moves, defn):\n        self.name = name\n        self.atk = atk\n        self.pwr = pwr\n        self.hp = hp\n        self.moves = moves\n        self.defn = defn\n \ndef attack(self, move, target):\n    move_index = self.player.moves.index(move)\n    move_power = self.player.defn&#91;move_index]\n    target.hp -= 10 + move_power + self.player.atk - target.defn&#91;1]\n    \n \n    def display_stats(self):\n        print(self.hp)\n        <\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I committed and synced the code for a turn-based combat game I made with a friend. Here is the code for the main loop and game class: And the player code:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-135","post","type-post","status-publish","format-standard","hentry","category-friday"],"_links":{"self":[{"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/posts\/135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":1,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":136,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/posts\/135\/revisions\/136"}],"wp:attachment":[{"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}