Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AI #8

Merged
merged 14 commits into from
Mar 12, 2024
Merged

AI #8

Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added AI/__init__.py
Empty file.
29 changes: 19 additions & 10 deletions AI/ai_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(self, second_player: bool):
self._good_game_message_lost = "Good game! You played well."
self._good_game_message_won = "Good game! I'll have better luck next time."
self._good_game_message_draw = "Good game! We are evenly matched."
self._current_uuid = None
self._rulebase = ai_rulebase.AIRulebase()
self._ip = "127.0.0.1"
self._port = 8765
Expand Down Expand Up @@ -54,10 +55,10 @@ async def _message_handler(self, message_type: str):
# AI does not need this
pass
case "game/start":
logger.info("start")
if self._current_player == self._player:
logger.info(f"start {self._starting_player.uuid}")
if self._starting_player == self._player:
await self.do_turn()
#await self.wish_good_luck()
await self.wish_good_luck()

case "game/end":
await self.say_good_game()
Expand All @@ -83,7 +84,7 @@ async def wish_good_luck(self):
await self.chat_message(self._good_luck_message)

async def say_good_game(self):
if self._winner.uuid == self._ai_uuid:
if self._winner.uuid == self._current_uuid:
await self.chat_message(self._good_game_message_won)
elif self._winner.uuid == None:
await self.chat_message(self._good_game_message_draw)
Expand Down Expand Up @@ -113,12 +114,16 @@ def __init__(self, second_player: bool = False):
self._ai_uuid = "108eaa05-2b0e-4e00-a190-8856edcd56a5"
self._ai_uuid2 = "108eaa05-2b0e-4e00-a190-8856edcd56a6"
JM-Lemmi marked this conversation as resolved.
Show resolved Hide resolved
HOOK-Hawkins marked this conversation as resolved.
Show resolved Hide resolved
self._strength = "Weak"
self._current_uuid = self._ai_uuid if not second_player else self._ai_uuid2
self._player = Player(f"{self._strength} AI", random.randint(0, 0xFFFFFF), uuid=self._current_uuid)
super().__init__(second_player)
# set it again because the parent's init overwrites it
self._current_uuid = self._ai_uuid if not second_player else self._ai_uuid2
self._strength = "Weak"
self._good_luck_message = "Good luck! I'm still learning so please have mercy on me."
self._good_game_message_lost = "Good game! I will try to do better next time."
self._good_game_message_won = "Good game! I can't believe I won!"
self._good_game_message_draw = "Good game! I' happy I didn't lose."
self._player = Player(f"{self._strength} AI", random.randint(0, 0xFFFFFF), uuid=(self._ai_uuid if not second_player else self._ai_uuid2))
super().__init__(second_player)

async def do_turn(self):

Expand All @@ -132,12 +137,16 @@ def __init__(self, second_player: bool = False):
self._ai_uuid = "108eaa05-2b0e-4e00-a190-8856edcd56a5"
self._ai_uuid2 = "108eaa05-2b0e-4e00-a190-8856edcd56a6"
HOOK-Hawkins marked this conversation as resolved.
Show resolved Hide resolved
self._strength = "Advanced"
self._current_uuid = self._ai_uuid if not second_player else self._ai_uuid2
self._player = Player(f"{self._strength} AI", random.randint(0, 0xFFFFFF), uuid=self._current_uuid)
super().__init__(second_player)
# set it again because the parent's init overwrites it
self._strength = "Advanced"
self._current_uuid = self._ai_uuid if not second_player else self._ai_uuid2
self._good_luck_message = "Good luck! I hope you are ready for a challenge."
self._good_game_message_lost = "Good game! I admire your skills."
self._good_game_message_won = "Good game! I hope you learned something from me."
self._good_game_message_draw = "Good game! I hope you are ready for a rematch."
self._player = Player(f"{self._strength} AI", random.randint(0, 0xFFFFFF), uuid=(self._ai_uuid if not second_player else self._ai_uuid2))
super().__init__(second_player)

def check_winning_move(self, empty_cells: list, player: int):
"""
Expand Down Expand Up @@ -168,12 +177,12 @@ async def do_turn(self):
empty_cells = self.get_empty_cells(self._playfield)

# Check for own winning move
if winning_move:= self.check_winning_move(empty_cells, self._player_number) != None:
if (winning_move:= self.check_winning_move(empty_cells, self._player_number)) != None:
await self.game_make_move(winning_move[0], winning_move[1])
return

# Check for opponent winning move
if winning_move:= self.check_winning_move(empty_cells, self._opponent_number):
if (winning_move:= self.check_winning_move(empty_cells, self._opponent_number)) != None:
await self.game_make_move(winning_move[0], winning_move[1])
return

Expand Down
File renamed without changes.
11 changes: 9 additions & 2 deletions Client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
from uuid import UUID

# Set up logging
logging.basicConfig(level=logging.INFO)
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
logger = logging.getLogger(__name__)

class GameClient:
Expand Down Expand Up @@ -170,7 +173,11 @@ async def _preprocess_message(self, message:str) -> str:
logger.error("Game start message received, but lobby does not contain 2 players. This should not happen and should be investigated.")
raise ValidationError("Game start message received, but lobby does not contain 2 players. This should not happen and should be investigated.")

self._opponent = self._lobby_status[0] if self._lobby_status[0].uuid is not str(self._player.uuid) else self._lobby_status[1]

self._opponent = self._lobby_status[0] if self._lobby_status[0] != self._player else self._lobby_status[1]

if self._opponent == self._player:
logger.info("ERROR WTF ALTER")
JM-Lemmi marked this conversation as resolved.
Show resolved Hide resolved

if str(self._player.uuid) == message_json["starting_player_uuid"]:
self._current_player = self._player
Expand Down
11 changes: 7 additions & 4 deletions Server/websocket_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,14 @@ async def handler(self, websocket):

# check for winning state
if self._game.state.finished:
websockets.broadcast(self._connections, json.dumps({
finished_dict = {
"message_type": "game/end",
"winner_uuid": self._game.state.winner.uuid,
"final_playfiled": self._game.state.playfield,
}))
"final_playfield": self._game.state.playfield,
}
if self._game.state.winner != 0:
finished_dict["winner_uuid"] = str(self._game.players[self._game.state.winner].uuid)

websockets.broadcast(self._connections, json.dumps(finished_dict))
self._inprogress = False

# announce new game state
Expand Down
Loading