Created
November 27, 2012 19:46
-
-
Save thefonso/4156537 to your computer and use it in GitHub Desktop.
Answer to SO question...http://stackoverflow.com/posts/13590227/edit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby code: | |
spec code: | |
All test are green now. this is leading me to create a new mechanism for game state in my app. | |
[ | |
the new game_state will have | |
if returned value == 1 => win | |
elsif returned value == -1 => loose | |
else => draw | |
] | |
Below is the code and test file.... | |
ruby code: gamestate.rb | |
module GameState | |
# win = 1 | |
# draw = 0 | |
# lose = -1 | |
# return 1 if current human wins, return -1 if computer wins | |
# TODO win loose draw goes here | |
def is_a_human_win(board) | |
win_moves = { | |
:wm01 => {:a1=>"X", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"X", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>"X"}, | |
:wm02 => {:a1=>" ", :a2=>"X", :a3=>" ", :b1=>" ", :b2=>"X", :b3=>" ", :c1=>" ", :c2=>"X", :c3=>" "}, | |
:wm03 => {:a1=>" ", :a2=>" ", :a3=>"X", :b1=>" ", :b2=>"X", :b3=>" ", :c1=>"X", :c2=>" ", :c3=>" "}, | |
:wm04 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>"X", :b2=>"X", :b3=>"X", :c1=>" ", :c2=>" ", :c3=>" "}, | |
:wm05 => {:a1=>"X", :a2=>"X", :a3=>"X", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}, | |
:wm06 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>"X", :c2=>"X", :c3=>"X"}, | |
:wm07 => {:a1=>"X", :a2=>" ", :a3=>" ", :b1=>"X", :b2=>" ", :b3=>" ", :c1=>"X", :c2=>" ", :c3=>" "}, | |
:wm08 => {:a1=>" ", :a2=>" ", :a3=>"X", :b1=>" ", :b2=>" ", :b3=>"X", :c1=>" ", :c2=>" ", :c3=>"X"} | |
} | |
x_on_the_gameboard = board.grid.select{ |k, v| v == "X" }.keys | |
win_moves.each do |k,v| | |
win_moves_keys = v.select{ |k, v| v == "X"}.keys | |
matching_moves = win_moves_keys & x_on_the_gameboard | |
if matching_moves.length >= 3 | |
p 'key: '+k.to_s | |
string_contains = k.to_s | |
if string_contains =~ /wm/ | |
puts "Human Wins" | |
# return 1 | |
# exit | |
else | |
end | |
end | |
end | |
end | |
def is_a_computer_win(board) | |
ai_winmoves = { | |
:wm01 => {:a1=>"O", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>"O"}, | |
:wm02 => {:a1=>" ", :a2=>"O", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>"O", :c3=>" "}, | |
:wm03 => {:a1=>" ", :a2=>" ", :a3=>"O", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>"O", :c2=>" ", :c3=>" "}, | |
:wm04 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>"O", :b2=>"O", :b3=>"O", :c1=>" ", :c2=>" ", :c3=>" "}, | |
:wm05 => {:a1=>"O", :a2=>"O", :a3=>"O", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}, | |
:wm06 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>"O", :c2=>"O", :c3=>"O"}, | |
:wm07 => {:a1=>"O", :a2=>" ", :a3=>" ", :b1=>"O", :b2=>" ", :b3=>" ", :c1=>"O", :c2=>" ", :c3=>" "}, | |
:wm08 => {:a1=>" ", :a2=>" ", :a3=>"O", :b1=>" ", :b2=>" ", :b3=>"O", :c1=>" ", :c2=>" ", :c3=>"O"} | |
} | |
o_on_the_gameboard = board.grid.select{ |k, v| v == "O" }.keys | |
ai_winmoves.each do |k,v| | |
ai_winmoves_keys = v.select{ |k, v| v == "O"}.keys | |
matching_moves = ai_winmoves_keys & o_on_the_gameboard | |
if matching_moves.length >= 3 | |
p 'key: '+k.to_s | |
test_string_contains = k.to_s | |
if test_string_contains =~ /wm/ | |
puts "Computer Wins" | |
# return -1 | |
# exit | |
else | |
end | |
end | |
end | |
end | |
end | |
spec file: gamestate_spec.rb | |
require 'gamestate' | |
require 'board' | |
require 'game' | |
describe 'Gamestate module method' do | |
before (:each) do | |
@player_human = Player.get_player('X') | |
@player_computer = Player.get_player('O') | |
end | |
describe 'is_a_human_win' do | |
it 'receives current board if win found returns...Human Wins' do | |
class GamestateTester | |
include ::GameState | |
end | |
human_win_test = GamestateTester.new | |
myboard = Board.new | |
myboard.grid[:a1] = "X" | |
myboard.grid[:b2] = "X" | |
myboard.grid[:c3] = "X" | |
human_win_test.should_receive(:puts).with("Human Wins") | |
human_win_test.is_a_human_win(myboard) | |
end | |
end | |
describe 'is_a_computer_win' do | |
it 'receives current board if win found returns...Computer Wins' do | |
class GamestateTester | |
include ::GameState | |
end | |
computer_win_test = GamestateTester.new | |
myboard = Board.new | |
myboard.grid[:a1] = "O" | |
myboard.grid[:b2] = "O" | |
myboard.grid[:c3] = "O" | |
computer_win_test.should_receive(:puts).with("Computer Wins") | |
computer_win_test.is_a_computer_win(myboard) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment