Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. $(document).ready(function(){
  2.  
  3. var currentPlayer = $("#table").data("current-player");
  4.  
  5. $(".square").click(function(){
  6. // Gather the position that was clicked
  7. var number = $(this).data("position");
  8. // Locate the game form
  9. var form = $("form");
  10. // Locate the input field corresponding to that position
  11. var input = $("input[data-position='" + number + "']");
  12. // Set the value of that input field to "X" or "O"
  13. input.val(currentPlayer);
  14. // Submit the form
  15. form.submit();
  16. });
  17. });
  18.  
  19. def update
  20. @game = Game.find(params[:id])
  21. @game.update(game_params)
  22. redirect_to @game
  23. switch_player
  24. end
  25.  
  26. def switch_player
  27. session[:current_player] = session[:current_player] == 'X' ? 'O' : 'X'
  28. end
  29.  
  30. <%= nested_form_for @game do |f| %>
  31.  
  32. <%= f.fields_for :moves do |move_form| %>
  33. <p id="table" data-current-player: <%=session[:current_player] %>>
  34. <%= move_form.label :position %><br>
  35. <%= move_form.text_field :player, data: {position: move_form.object.position} %>
  36. <%= move_form.hidden_field :id %>
  37. </p>
  38. <% end %>
  39.  
  40. <input type="Submit">
  41. <% end %>
  42.  
  43. <div id="board" align = center>
  44. <table>
  45. <tr>
  46. <td data-position="0" class="square <%= class_for_move(0)%>"></td>
  47.  
  48. def class_for_move(number)
  49. move = @game.moves.find_by(position: number)
  50. player = move.player
  51. player.downcase + '_move' unless player.blank?
  52. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement