#ROBOT JOUEUR #gère la gestion du jeu #VARIABLES var i=0 #variable de boucle 1 var j=0 #variable de boucle 2 var temp1=0 #variables temporaires var temp2=0 var player_turn=0 #varibale de gestion du tour du joeur 0=player1 1=player2 var first_press_center=0 var game_has_started=0 #gestion de début du jeu var turn_has_started=0 #gestion de début de tour 0: pas commencé, 1: colonne, 2: ligne var won=-1 #qui a gagné 0: player 1, 1: player 2, 2: draw var game_finished=0 #tant qu'on décide de jouer var turn_counter=0 #pour transmission avec l'autre robot var key_flag=0 #pour survivre avec un onevent buttons qui fait des siennes var restart_sound=0 var error_sound=0 #tableau de gestion des cases var case[9] #représente les cases du jeu var case_modified=-1 var col_row[2]=[0,0] #colonne/ligne choisie par le joueur var col_row_counter=0 #gestion de la position var col_row_value=1 #gestion des valeurs de compteur var col_row_sound_to_play=0 #son à jouer call math.fill(case, 0) #activation de la communication entre Thymio call prox.comm.enable(1) prox.comm.tx=0 #timer de gestion des boutons timer.period[0]=200 sub light_indicator #debug visuel :p #info sur player if player_turn==0 then #joueur 1 call leds.bottom.right(0,0,32) else #joueur 2 call leds.bottom.right(32,0,0) end #info sur col_row if col_row_counter==0 then #colonne call leds.bottom.left(0,32,0) else call leds.bottom.left(32,0,32) end #info sur col_row_value if col_row_value==1 then #1 call leds.circle(32,0,0,0,0,0,0,0) elseif col_row_value==2 then call leds.circle(32,32,0,0,0,0,0,0) elseif col_row_value==3 then call leds.circle(32,32,32,0,0,0,0,0) end sub play_turn #routine de lancement du tour de jeu if turn_has_started==0 then call leds.top(0,32,0) turn_has_started=1 col_row_counter=0 col_row_value=1 if error_sound==0 then if player_turn==0 then #play player 1 sound call sound.play(7) else #play player 2 sound call sound.play(8) end else error_sound=0 end callsub light_indicator end sub check_draw #test du match nul temp1=0 for i in 0:8 do temp1+=abs(case[i]) end if temp1==9 then won=2 end sub check_case_win #boucles de test si des col_rows for i in 0:2 do #Test lignes/colonne temp1=0 temp2=0 for j in 0:2 do temp1+=case[i*3+j] #somme ligne temp2+=case[i+j*3] #somme colonne end if abs(temp1)==3 or abs(temp2)==3 then #Si 3 croix/cercles dans lignes/colonnes if temp1==3 or temp2==3 then won=0 #joueur 1 gagne else won=1 #joueur 2 gagne end return end end temp1=case[0]+case[4]+case[8] #diagonale 1 temp2=case[2]+case[4]+case[6] #diagonale 2 if abs(temp1)==3 or abs(temp2)==3 then #Si 3 croix/cercles dans diagonales if temp1==3 or temp2==3 then won=0 #joueur 1 gagne else won=1 #joueur 2 gagne end return end callsub check_draw sub check_entry #verification si l'entrée est correct call leds.top(0,32,32) case_modified=(col_row[0]-1)+(col_row[1]-1)*3 if case[case_modified]!=0 then #play error sound call sound.play(9) error_sound=1 callsub play_turn callsub light_indicator else #call pour faire aller dessiner le thymio prox.comm.tx=col_row[0]*10+col_row[1] #valeur à transmettre au robot call leds.temperature(32,0) #modification interne if player_turn==0 then case[case_modified]=1 else case[case_modified]=-1 end callsub check_case_win if won==-1 then #pas gagner player_turn=(player_turn+1)%2 callsub light_indicator turn_has_started=0 else game_has_started=0 game_finished=1 call leds.top(32,32,32) if won==0 then call sound.play(10) elseif won==1 then call sound.play(11) elseif won==2 then call sound.play(12) end end end sub col_row_sound col_row_sound_to_play=col_row_counter*3+col_row_value-1 #0:A 1:B 2:C 3:1 4:2 5:3 call sound.play(col_row_sound_to_play) sub confirm_entry #confirmation de la coordonnée if turn_has_started==1 then col_row[col_row_counter]= col_row_value col_row_value=1 if col_row_counter==0 then col_row_counter++ #si premier passage => incremente pour avoir la deuxième coordonnée callsub col_row_sound callsub light_indicator else turn_has_started=0 #fin du tour callsub check_entry #sinon check la valeur entrée end end sub decrement if turn_has_started==1 then col_row_value-- if col_row_value<1 then col_row_value=3 end callsub col_row_sound callsub light_indicator end sub increment if turn_has_started==1 then col_row_value++ if col_row_value>3 then col_row_value=1 end callsub col_row_sound callsub light_indicator end sub start_game #routine de lancement de jeu if game_has_started==0 then game_has_started=1 call sound.play(6) end sub restart_game if game_has_started==0 and game_finished==1 and restart_sound==1 then #reset des variables #player_turn=0 #A TEST ! turn_has_started=0 game_has_started=1 won=-1 game_finished=0 restart_sound=0 error_sound=0 case_modified=-1 col_row_counter=0 col_row_value=1 col_row_sound_to_play=0 call math.fill(case, 0) call math.fill(col_row, 0) prox.comm.tx=0 end onevent timer0 #l'event buttons semble être beaucoup trop réactif (1 press = 2-4 activations de l'event buttons) if key_flag==0 then if button.center==1 then if game_has_started==1 and first_press_center==0 then first_press_center=1 callsub play_turn elseif turn_has_started==1 and game_has_started==1 then callsub confirm_entry end key_flag++ return end if button.left==1 then callsub decrement key_flag++ return end if button.right==1 then callsub increment key_flag++ return end if button.forward==1 then callsub restart_game key_flag++ return end else key_flag=0 end if prox.comm.rx>turn_counter then #Je n'utilise pas de when parce qu'il semblerait qu'il fasse des siennes aussi turn_counter++ #on incrémente la valeur à recevoir la prochaine fois call leds.temperature(0, 32) if game_finished==0 then callsub play_turn else restart_sound=1 #playsound de comment restart end end if first_press_center==0 and game_has_started==0 then callsub start_game end