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

Refactor code in app.py and awaleboard.py, and fix error handling in game_awale.js #44

Merged
merged 1 commit into from
Apr 25, 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
6 changes: 3 additions & 3 deletions src/main/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ def awale_place_piece():
pitid = data['pitid']
current_player = data['current_player']
id = int(pitid)
print("current_player",current_player)

if board_awale is not None:
try:
Expand All @@ -305,13 +304,14 @@ def awale_place_piece():
#print("values",values)
winner = board_awale.check_winner()
print("Gagnant joueur",winner)
if winner == 1 or winner == 2:
if winner:

return jsonify({'winner': current_player, 'game_over': True, 'current_player': current_player,'values':values,'pitid':pitid,'score_1':scores[0],'score_2':scores[1]})
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
except Exception as e:
# Handle the exception here
error_message = str(e) # Get the error message
board_awale.display_board() # Display the game board in the console
print("error: ", error_message)
return jsonify({'error': "An error has occured"}), 400
return jsonify({'result': 'Success', 'game_over': False,'current_player': current_player,'values':values,'score_1':scores[0],'score_2':scores[1],'pitid':pitid})
Expand Down
50 changes: 16 additions & 34 deletions src/main/game_logic/awalegame/board/awaleboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ def __init__(self):
self.score_1=0
self.score_2=0


def display_board(self):
print("|\t0\t1\t2\t3\t4\t5\t|")
print(f"|\t{self.board[0]}\t{self.board[1]}\t{self.board[2]}\t{self.board[3]}\t{self.board[4]}\t{self.board[5]}\t|")
print(f"|\t{self.board[11]}\t{self.board[10]}\t{self.board[9]}\t{self.board[8]}\t{self.board[7]}\t{self.board[6]}\t|\n")
print("|\t11\t10\t9\t8\t7\t6\t|")


def is_legal_move(self, position, player):
# Check if the position is valid
if position < 0 or position > 11:
Expand Down Expand Up @@ -67,29 +69,7 @@ def is_legal_move(self, position, player):


def make_move(self, position, player):
# 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")
self.is_legal_move(position, player)

# Sow the seeds
position = self.sow_seeds(position, player)
Expand All @@ -98,9 +78,6 @@ def make_move(self, position, player):

# Check if any capture is possible
self.capture(position, player)

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


def affamer(self, position, player):
Expand All @@ -119,7 +96,6 @@ def affamer(self, position, player):
return True



def nourrir(self, position, player):
# Check if the opponent's side is empty
if player == 1:
Expand Down Expand Up @@ -206,11 +182,9 @@ def get_board(self):
def check_winner(self):

if self.board[0:5] == [0,0,0,0,0,0]:
self.score_2 = 48
return 2

if self.board[6:11] == [0,0,0,0,0,0]:
self.score_1 = 48
return 1

if self.score_1 > 24:
Expand All @@ -220,11 +194,19 @@ def check_winner(self):
return 2

if sum(self.board) <= 3:
if self.score_1 > self.score_2:
return 1
if self.score_2 > self.score_1:
return 2
#return 0
return 2 - self.score_1 > self.score_2

try:
self.nourrir(0, 1)
except CannotFeedError:
return 1

try:
self.nourrir(6, 2)
except CannotFeedError:
return 2

return None


def get_possible_moves(self,player):
Expand Down
13 changes: 12 additions & 1 deletion src/main/game_ui/static/js/game_awale.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,18 @@ window.onload = function () {
.then(response => response.json())
.then(data => {
if (data.error) {
alert(data.error);
// briefly change the color of the pit to red
pit.style.backgroundColor = 'red';

// disable the cell
pit.setAttribute('disabled', true);

setTimeout(() => {
pit.style.backgroundColor = '#cc945b';

// remove the disabled attribute from the hex cell
pit.removeAttribute('disabled');
}, 500);
}
else {
values = []
Expand Down