为什么我的Tic-Tac_Toe电脑Ai在我刷新浏览器的时候又多了一个动作?
Why does my Tic-Tac_Toe computer Ai makes another move when I refresh the browser?
我正在开发我的 TicTacToe Game.This 正在部署到 Heroku,我已经修复了一个错误,当一个人正在玩游戏而另一个人开始从另一台计算机玩游戏时,它会在秒玩家屏幕第一玩家在做什么。然后我意识到,当我在浏览器上点击刷新时,AI 会再次移动,如果你继续刷新浏览器,它会一次又一次地移动。关于为什么会发生这种情况的任何想法?
post '/game' do
session[:choice] = params[:choice].to_i
choice = params[:choice].to_i
player_marker = players.current_player()
if play_board.square_available?(choice - 1) == true
play_board.board[choice - 1] = player_marker
redirect to('/status')
else
erb :squares, :locals => {:p1 => players.player1,
:p2 => players.player2,
:invaild => "Hey #{players.current} #{choice} is already taken",
:message2 => "Please choose again.",
:current => players.current,
:board => play_board.board,
:type => players.type}
end
end
get '/computerai' do
player_marker = players.current_player()
move = ai.computer_move()
play_board.board[move] = player_marker
redirect to('/status')
end
get '/status' do
if play_board.winner?(players.current_player) == true
redirect to('/win')
elsif play_board.board_full?() == true
redirect to('/tie')
end
players.current = players.change()
redirect to('/computerai') if players.type == "1" && players.current == 2
erb :squares, :locals => {:p1 => players.player1,
:p2 => players.player2,
:invaild => "",
:message2 => "",
:current => players.current,
:board => play_board.board,
:type => players.type}
end
这是 link 到 Github 页面,如果您需要更多代码,我的源代码就在这里。 https://github.com/josephmckenzie/Tic_tac_toe_online.
Called from: C:/Ruby21/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:86:in `new
WARN: tilt autoloading 'tilt/erb' in a non thread-safe way; explicit require 'tilt/erb' suggested
127.0.0.1 - - [04/Jan/2016:00:38:04 -0500] "GET /tictactoe HTTP/1.1" 200 1544 0.3104
127.0.0.1 - - [04/Jan/2016:00:38:07 -0500] "POST /tictactoe HTTP/1.1" 200 891 0.0165
127.0.0.1 - - [04/Jan/2016:00:38:10 -0500] "POST /marker HTTP/1.1" 200 1521 0.0085
127.0.0.1 - - [04/Jan/2016:00:38:11 -0500] "POST /squares HTTP/1.1" 200 944 0.0130
127.0.0.1 - - [04/Jan/2016:00:38:14 -0500] "POST /game HTTP/1.1" 303 - 0.0030
127.0.0.1 - - [04/Jan/2016:00:38:15 -0500] "GET /status HTTP/1.1" 302 - 0.0122
127.0.0.1 - - [04/Jan/2016:00:38:15 -0500] "GET /computerai HTTP/1.1" 302 - 0.0104
127.0.0.1 - - [04/Jan/2016:00:38:15 -0500] "GET /status HTTP/1.1" 200 914 0.0135
127.0.0.1 - - [04/Jan/2016:00:38:17 -0500] "GET /status HTTP/1.1" 302 - 0.0025
127.0.0.1 - - [04/Jan/2016:00:38:18 -0500] "GET /computerai HTTP/1.1" 302 - 0.0131
127.0.0.1 - - [04/Jan/2016:00:38:18 -0500] "GET /status HTTP/1.1" 200 914 0.0110
127.0.0.1 - - [04/Jan/2016:00:38:18 -0500] "GET /style.css HTTP/1.1" 304 - 0.0030
127.0.0.1 - - [04/Jan/2016:00:38:18 -0500] "GET /mm.png HTTP/1.1" 304 - 0.0035
127.0.0.1 - - [04/Jan/2016:00:38:18 -0500] "GET /TicTacToePowerPoint1.jpg HTTP/1.1" 304 - 0.0025
这看起来不对:
redirect to('/computerai') if players.type == "1" && players.current == 2
erb :squares, :locals => {:p1 => players.player1,
除非我误解了重定向的工作原理,否则您将继续并仍然生成 erb 响应。
我想你想要这个
if players.type == "1" && players.current == 2
redirect ...
else
erb ...
end
如果这不能解决问题,请 post 当您刷新浏览器时正在发出什么请求...我假设它是 /status ,但我不确定...
修复了我的刷新问题 :) 我所做的是取出人类移动逻辑并将其放入它自己的 /get 中。所以现在我可以在我的浏览器上点击刷新,人工智能不会再采取行动。我问过我的老师,他基本上告诉我要遵循逻辑并分离出 get/status 的一部分以及其他一些东西。
get '/status' do
if play_board.winner?(players.current_player) == true
redirect to('/win')
elsif play_board.board_full?() == true
redirect to('/tie')
end
players.current = players.change()
if players.type == "1" && players.current == 2
redirect to('/computerai')
else
redirect to('/humanmove')
end
end
get '/humanmove' do
players.type == "1" && players.current == 1
erb :squares, :locals => {:p1 => players.player1,
:p2 => players.player2,
:invaild => "",
:message2 => "",
:current => players.current,
:board => play_board.board,
:type => players.type}
end
我正在开发我的 TicTacToe Game.This 正在部署到 Heroku,我已经修复了一个错误,当一个人正在玩游戏而另一个人开始从另一台计算机玩游戏时,它会在秒玩家屏幕第一玩家在做什么。然后我意识到,当我在浏览器上点击刷新时,AI 会再次移动,如果你继续刷新浏览器,它会一次又一次地移动。关于为什么会发生这种情况的任何想法?
post '/game' do
session[:choice] = params[:choice].to_i
choice = params[:choice].to_i
player_marker = players.current_player()
if play_board.square_available?(choice - 1) == true
play_board.board[choice - 1] = player_marker
redirect to('/status')
else
erb :squares, :locals => {:p1 => players.player1,
:p2 => players.player2,
:invaild => "Hey #{players.current} #{choice} is already taken",
:message2 => "Please choose again.",
:current => players.current,
:board => play_board.board,
:type => players.type}
end
end
get '/computerai' do
player_marker = players.current_player()
move = ai.computer_move()
play_board.board[move] = player_marker
redirect to('/status')
end
get '/status' do
if play_board.winner?(players.current_player) == true
redirect to('/win')
elsif play_board.board_full?() == true
redirect to('/tie')
end
players.current = players.change()
redirect to('/computerai') if players.type == "1" && players.current == 2
erb :squares, :locals => {:p1 => players.player1,
:p2 => players.player2,
:invaild => "",
:message2 => "",
:current => players.current,
:board => play_board.board,
:type => players.type}
end
这是 link 到 Github 页面,如果您需要更多代码,我的源代码就在这里。 https://github.com/josephmckenzie/Tic_tac_toe_online.
Called from: C:/Ruby21/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:86:in `new
WARN: tilt autoloading 'tilt/erb' in a non thread-safe way; explicit require 'tilt/erb' suggested
127.0.0.1 - - [04/Jan/2016:00:38:04 -0500] "GET /tictactoe HTTP/1.1" 200 1544 0.3104
127.0.0.1 - - [04/Jan/2016:00:38:07 -0500] "POST /tictactoe HTTP/1.1" 200 891 0.0165
127.0.0.1 - - [04/Jan/2016:00:38:10 -0500] "POST /marker HTTP/1.1" 200 1521 0.0085
127.0.0.1 - - [04/Jan/2016:00:38:11 -0500] "POST /squares HTTP/1.1" 200 944 0.0130
127.0.0.1 - - [04/Jan/2016:00:38:14 -0500] "POST /game HTTP/1.1" 303 - 0.0030
127.0.0.1 - - [04/Jan/2016:00:38:15 -0500] "GET /status HTTP/1.1" 302 - 0.0122
127.0.0.1 - - [04/Jan/2016:00:38:15 -0500] "GET /computerai HTTP/1.1" 302 - 0.0104
127.0.0.1 - - [04/Jan/2016:00:38:15 -0500] "GET /status HTTP/1.1" 200 914 0.0135
127.0.0.1 - - [04/Jan/2016:00:38:17 -0500] "GET /status HTTP/1.1" 302 - 0.0025
127.0.0.1 - - [04/Jan/2016:00:38:18 -0500] "GET /computerai HTTP/1.1" 302 - 0.0131
127.0.0.1 - - [04/Jan/2016:00:38:18 -0500] "GET /status HTTP/1.1" 200 914 0.0110
127.0.0.1 - - [04/Jan/2016:00:38:18 -0500] "GET /style.css HTTP/1.1" 304 - 0.0030
127.0.0.1 - - [04/Jan/2016:00:38:18 -0500] "GET /mm.png HTTP/1.1" 304 - 0.0035
127.0.0.1 - - [04/Jan/2016:00:38:18 -0500] "GET /TicTacToePowerPoint1.jpg HTTP/1.1" 304 - 0.0025
这看起来不对:
redirect to('/computerai') if players.type == "1" && players.current == 2
erb :squares, :locals => {:p1 => players.player1,
除非我误解了重定向的工作原理,否则您将继续并仍然生成 erb 响应。
我想你想要这个
if players.type == "1" && players.current == 2
redirect ...
else
erb ...
end
如果这不能解决问题,请 post 当您刷新浏览器时正在发出什么请求...我假设它是 /status ,但我不确定...
修复了我的刷新问题 :) 我所做的是取出人类移动逻辑并将其放入它自己的 /get 中。所以现在我可以在我的浏览器上点击刷新,人工智能不会再采取行动。我问过我的老师,他基本上告诉我要遵循逻辑并分离出 get/status 的一部分以及其他一些东西。
get '/status' do
if play_board.winner?(players.current_player) == true
redirect to('/win')
elsif play_board.board_full?() == true
redirect to('/tie')
end
players.current = players.change()
if players.type == "1" && players.current == 2
redirect to('/computerai')
else
redirect to('/humanmove')
end
end
get '/humanmove' do
players.type == "1" && players.current == 1
erb :squares, :locals => {:p1 => players.player1,
:p2 => players.player2,
:invaild => "",
:message2 => "",
:current => players.current,
:board => play_board.board,
:type => players.type}
end