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

J'ai tout cassé #41

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
59 changes: 18 additions & 41 deletions src/main/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,43 +229,19 @@ def game_awaleiaia():



@app.route('/awale_place_piece', methods=['POST']) # player place a piece on the board
def awale_place_piece():
global board_awale, current_player

data = request.get_json()
pitid = data['pitid']
current_player = data['current_player']
id = int(pitid)

if board_awale.make_move(id, current_player):
scores = board_awale.get_scores()
values = board_awale.get_board()
winner = board_awale.check_winner()

board_awale.display_board() # Display the game board in the console

if winner:
return jsonify({'winner': winner, 'game_over': True, 'current_player': current_player,'values':values,'pitid':pitid,'score_1':scores[0],'score_2':scores[1]})
current_player = 1 if current_player == 2 else 2
return jsonify({'result': 'Success', 'current_player': current_player,'values':values,'score_1':scores[0],'score_2':scores[1]})
else:
return jsonify({'error': "An error has occured"}), 400


@app.route('/players_awaleia', methods=['POST']) # Return player's and IA's values
def players_awaleia():
global player, IA
return jsonify({'result': 'Success','player': player,'IA':IA})

@app.route('/first_move_IA_awale',methods=['POST']) #Return IA's first move if player=2
def first_move_IA_awale():
global game_board, IA
move = game_board.get_best_move(depth_awale,IA)
game_board.make_move(move, IA)
global board_awale, IA
move = board_awale.get_best_move(depth_awale,IA)
board_awale.make_move(move, IA)
iamove = move
values = game_board.get_board()
scores = game_board.get_scores()
values = board_awale.get_board()
scores = board_awale.get_scores()
return jsonify({'result': 'Success','values':values,'score_1':scores[0],'score_2':scores[1]})


Expand All @@ -287,7 +263,7 @@ def awaleia_place_piece():

# check if current_IA won

winner = game_board.check_winner()
winner = board_awale.check_winner()
print(winner)
if winner == 1 or winner == 2:
print(winner,"OUI!!")
Expand All @@ -299,9 +275,9 @@ def awaleia_place_piece():
# Handle the exception here
error_message = str(e) # Get the error message

game_board.display_board()
values = game_board.get_board()
scores = game_board.get_scores()
board_awale.display_board()
values = board_awale.get_board()
scores = board_awale.get_scores()

print("error: ", error_message)
return jsonify({'error': "An error has occured"}), 400
Expand All @@ -311,24 +287,25 @@ def awaleia_place_piece():

@app.route('/awale_place_piece', methods=['POST']) # player place a piece on the board
def awale_place_piece():
global game_board, current_player
global board_awale, current_player

data = request.get_json()
pitid = data['pitid']
current_player = data['current_player']
id = int(pitid)
print("current_player",current_player)

if game_board is not None:
if board_awale is not None:
try:
game_board.make_move(id, current_player) # Try to place the piece
scores = game_board.get_scores()
game_board.display_board() # Display the game board in the console
values = game_board.get_board()
board_awale.make_move(id, current_player) # Try to place the piece
scores = board_awale.get_scores()
board_awale.display_board() # Display the game board in the console
values = board_awale.get_board()
#print("values",values)
winner = game_board.game_over()
winner = board_awale.check_winner()
print("Gagnant joueur",winner)
if winner:
winner = 2 - (game_board.score_1 > game_board.score_2)
winner = 2 - (board_awale.score_1 > board_awale.score_2)
return jsonify({'winner': current_player, 'game_over': True, 'current_player': current_player,'values':values,'pitid':pitid,'score_1':scores[0],'score_2':scores[1]})
current_player = 1 if current_player == 2 else 2
except Exception as e:
Expand Down
55 changes: 25 additions & 30 deletions src/main/game_logic/awalegame/board/awaleboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,29 @@ def is_legal_move(self, position, player):


def make_move(self, position, player):
# Check if the move is legal
try:
self.is_legal_move(position, player)
except Exception as e:
print(str(e))
if str(e) == "Game should be over after this move":
# force the game to end
if player == 1:
for i in range(0, 12):
self.score_1 += self.board[i]
self.board[i] = 0
else:
for i in range(0, 12):
self.score_2 += self.board[i]
self.board[i] = 0
return False
# Check if the position is valid
if position < 0 or position > 11:
raise InvalidPositionError("Invalid position, position should be between 1 and 12")

# Check if the position is empty
if self.board[position] == 0:
raise PositionEmptyError("Position is empty")

# Check if the position is on the opponent's side
if player == 1 and position > 5:
raise InvalidPositionError("Invalid position, position is on the opponent's side")
if player == 2 and position <= 5:
raise InvalidPositionError("Invalid position, position is on the opponent's side")


# Check rule "affamer"
if self.affamer(position, player):
raise AffamerError("Player cannot play this move, it will starve the opponent")


# Check rule "nourrir"
if not self.nourrir(position, player):
raise NourrirError("Player can feed the opponent")

# Sow the seeds
position = self.sow_seeds(position, player)
Expand All @@ -92,7 +99,8 @@ def make_move(self, position, player):
# Check if any capture is possible
self.capture(position, player)

return True
# Check if the game is over
return self.check_winner()


def affamer(self, position, player):
Expand Down Expand Up @@ -186,19 +194,6 @@ def capture(self, origin, player):
return finished_capture


def check_winner(self):
# Check if any player has a score of 25 or more
if self.score_1 >= 25 or self.score_2 >= 25:
return 1 if self.score_1 > self.score_2 else 2
# Check if there are less than 6 seeds left on the board
seeds_left = 0
for i in range(12):
seeds_left += self.board[i]
if seeds_left < 6:
return 1 if self.score_1 > self.score_2 else 2
return None


def get_scores(self):
return[self.score_1,self.score_2]

Expand Down