Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gubace committed Apr 16, 2024
2 parents 0dea856 + 162a796 commit c95409a
Show file tree
Hide file tree
Showing 4 changed files with 43 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(4,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 @@ -157,6 +157,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 @@ -644,3 +644,12 @@ def get_best_move(self, depth, player):
a , best_move = self.minimax(depth, player, float('-inf'), float('inf'))
print(a, best_move, player)
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)
2 changes: 2 additions & 0 deletions src/main/game_ui/static/js/game_hexia.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ window.onload = async function () {


if (this.getAttribute('disabled') || playable===false) {
// hide spinner
document.getElementById('spinner').style.display = 'none';
return;
}

Expand Down
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 c95409a

Please sign in to comment.