I am building a tic-tac-toe game on RoR. At the moment, the whole board is set up, with a form below it that tracks the moves. I have been told to use the model to figure out such things as if there is a winner(3 in a row), a tie(full board), etc. I thought I had it, but apparently not. Not sure what I'm doing wrong with the players
method in the model.
我正在RoR上构建一个tic-tac-toe游戏。此刻,整个电路板都已设置好,其下方有一个跟踪移动的表格。我被告知使用该模型来弄清楚如果有胜利者(连续3个),领带(全板)等等,我以为我拥有它,但显然不是。不确定我在模型中使用player方法做错了什么。
show.html.erb
:
<div id="board" align = center>
<table>
<tr>
<td data-position="0" class="square <%= class_for_move(0)%>"></td>
<td data-position="1" class="square v <%= class_for_move(1)%>"></td>
<td data-position="2" class="square <%= class_for_move(2)%>"></td>
</tr>
<tr>
<td data-position="3" class="square h <%= class_for_move(3)%>"></td>
<td data-position="4" class="square v h <%= class_for_move(4)%>"></td>
<td data-position="5" class="square h <%= class_for_move(5)%>"></td>
</tr>
<tr>
<td data-position="6" class="square <%= class_for_move(6)%>"></td>
<td data-position="7" class="square v <%= class_for_move(7)%>"></td>
<td data-position="8" class="square <%= class_for_move(8)%>"></td>
</tr>
</table>
</div>
<table>
<tr>
<td><%= @game.player_1 %></td>
<td><%= @game.player_2 %></td>
</tr>
<tr>
<td>X</td>
<td>O</td>
</tr>
</table>
<%= link_to "Play Again", games_path %>
<%= nested_form_for @game do |f| %>
<%= f.fields_for :moves do |move_form| %>
<div id="table" data-current-player="<%=session[:current_player] %>">
<%= move_form.label :position %><br>
<%= move_form.text_field :player, data: {position: move_form.object.position} %>
<%= move_form.hidden_field :id %>
</div>
<% end %>
<input type="Submit">
<% end %>
game.rb
:
class Game < ActiveRecord::Base
has_many :moves
after_create :initialize_moves
accepts_nested_attributes_for :moves
def initialize_moves
9.times do |i|
Move.create(position: i, game:self)
end
end
def players(number)
move = moves.find_by(position: number)
player = move.player
end
def tie?
end
def winner?
if players(0) == players(1) && players(1) == players(2)
return players(0)
end
end
end
games_controller
:
class GamesController < ApplicationController
def create
@game = Game.new(game_params)
@game.save
redirect_to @game
end
def update
@game = Game.find(params[:id])
@game.update(game_params)
if @game.tie?
flash[:error] = "Game over. 'Tis a tie."
elsif @game.winner?
flash[:notice] = "Winner is #{session[:current_player]}"
else
switch_player
end
redirect_to @game
end
def show
@game = Game.find(params[:id])
end
def switch_player
session[:current_player] = session[:current_player] == "X" ? "O" : "X"
end
private
def game_params
params.require(:game).permit(:player_1, :player_2, moves_attributes: [:player, :id])
end
end
When I run what I have at the moment, the game plays out until I have a three-in-a-row(only doing slots 0,1,2 as test at the moment), switching players and acting normal. The moment the three-in-a-row is true, it stays on whatever the current player is, and won't switch. It's not ending the game, or giving me the flash notice. Just sticks to the current player. Same goes with if I try to add an elsif trying to put in another winning position combo.
当我运行我现在所拥有的东西时,游戏将一直播放,直到我有一个三排(此刻只进行0,1,2的测试),切换玩家并表现正常。在三连一的情况下,它会保持在当前播放器的任何位置,并且不会切换。它没有结束游戏,也没有给我闪光通知。只是坚持当前的球员。同样如果我试图添加一个elsif试图放入另一个获胜位置组合。
I realize this is probably elementary level stuff, but I just cannot seem to wrap my head around it, and haven't found anything useful online so far. And, if the if
statement route isn't the right way to go, I am willing to go another way. I just can't think of a different way at the moment.
我意识到这可能是初级的东西,但我似乎无法绕过它,到目前为止还没有在网上找到任何有用的东西。并且,如果if语句路线不是正确的方法,我愿意采取另一种方式。我现在想不出一个不同的方式。
Any help is much appreciated.
任何帮助深表感谢。
1 个解决方案
#1
1
Figured it out. Just needed to set up each combo in the winner?
method with !players(0).blank? &&
. With the respective number for each players
after ==
.
弄清楚了。只需要在获胜者中设置每个组合?使用!players(0).blank的方法? &&。 = =后每个玩家的相应数量。
Was going to delete the question, but just in case anyone was actually curious of the answer, there it is.
是要删除这个问题,但万一有人真的好奇答案,那就是。
#1
1
Figured it out. Just needed to set up each combo in the winner?
method with !players(0).blank? &&
. With the respective number for each players
after ==
.
弄清楚了。只需要在获胜者中设置每个组合?使用!players(0).blank的方法? &&。 = =后每个玩家的相应数量。
Was going to delete the question, but just in case anyone was actually curious of the answer, there it is.
是要删除这个问题,但万一有人真的好奇答案,那就是。