Skip to content

Commit

Permalink
Merge pull request #31 from Ivan23BG/random-first-move
Browse files Browse the repository at this point in the history
Random first move
  • Loading branch information
Bon-Monsieur authored Apr 15, 2024
2 parents 134c142 + 501b4a4 commit 162a796
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/main/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def hexiaia_place_piece():
try:
if game_board is not None:

move_IA = game_board.get_best_move(3,current_IA)
move_IA = game_board.get_best_move(2,current_IA)
game_board.place_piece(current_IA, move_IA) # Try to place the piece
iamove = "hex" + str(move_IA[0]) + "-" + str(move_IA[1])

Expand Down Expand Up @@ -156,6 +156,16 @@ def first_move_IA():
iamove = "hex" + str(move[0]) + "-" + str(move[1])
return jsonify({'result': 'Success','iamove':iamove})

@app.route('/hexiaia_random',methods=['POST']) #Return IA's first move if player=2
def hexiaia_random():
global game_board, current_IA
data = request.get_json()
current_IA = data['current_IA']
move = game_board.random_move()
game_board.place_piece(current_IA, move)
iamove = "hex" + str(move[0]) + "-" + str(move[1])
return jsonify({'result': 'Success','iamove':iamove})


@app.route('/undo_move', methods=['POST']) # Undo last move on the board
def undo_move():
Expand Down
9 changes: 9 additions & 0 deletions src/main/game_logic/hexgame/board/hexboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,3 +682,12 @@ def minimax(self, depth, player, alpha, beta):
def get_best_move(self, depth, player):
_ , best_move = self.minimax(depth, player, float('-inf'), float('inf'))
return best_move

def random_move(self):
Trouve = False
while(not Trouve):
x = random.randint(0,self.size-1)
y = random.randint(0,self.size-1)
if not(self.is_position_occupied((x,y))):
Trouve = True
return (x,y)
22 changes: 21 additions & 1 deletion src/main/game_ui/static/js/game_hexiaia.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,36 @@ async function fetchIAMoveJSON(current_IA) {
return data;
}

async function fetchRandomMoveJSON(current_IA) {
const response = await fetch('/hexiaia_random', {method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify({'current_IA': current_IA})});
const data = response.json();
return data;
}

window.onload = async function () {
let current_IA = 1;

let game_over = false;
let short_path = [];
let winner = 0;
let stopped = false;
let stopped = false; // While condition

const game_history = []; // stack to store game_history
const cells = document.querySelectorAll('.hex'); // Get all hex cells

const data1 = await fetchRandomMoveJSON(1); // Get current_IA's move
let iamove = data1.iamove;
var iahex = document.getElementById(iamove);

game_history.push(iamove);
toggle_colour(iahex,1);

const data2 = await fetchRandomMoveJSON(2); // Get current_IA's move
iamove = data2.iamove;
iahex = document.getElementById(iamove);

game_history.push(iamove);
toggle_colour(iahex,2);

while(!stopped){
const data = await fetchIAMoveJSON(current_IA); // Get current_IA's move
Expand Down

0 comments on commit 162a796

Please sign in to comment.