Skip to content

Instantly share code, notes, and snippets.

@boy-jer
Last active April 21, 2016 04:13
Show Gist options
  • Save boy-jer/38e432ce21c34376ac2c5aaa6b4131dc to your computer and use it in GitHub Desktop.
Save boy-jer/38e432ce21c34376ac2c5aaa6b4131dc to your computer and use it in GitHub Desktop.
http://codereview.stackexchange.com/questions/88429/tic-tac-toe-implementation-in-ruby
**https://codequizzes.wordpress.com/2013/10/25/creating-a-tic-tac-toe-game-with-ruby/
ruby ~/dev-gt/test/ttt/lib/ttt.rb
For example, a Tic Tac Toe program requires data structures for storing
the board and conditional logic for knowing whose turn it is or if someone has won.
http://stackoverflow.com/questions/3233278/how-do-i-test-if-all-items-in-an-array-are-identical?rq=1
You can use Enumerable#all? which returns true if the given block returns true for all the elements in the collection.
array.all? {|x| x == array[0]}
(If the array is empty, the block is never called, so doing array[0] is safe.)
http://blog.kyletolle.com/deep-each-in-ruby/
https://github.com/alexbtlv/tic_tac_toe/tree/master/lib
To play a game run:
$ ruby example/example_game.rb
http://danielallendeutsch.com/blog/1-tic-tac-toe.html
https://github.com/learn-co-students/tic-tac-toe-rb-q-000
https://github.com/learn-co-curriculum/tic-tac-toe-rb
https://learn.co/lessons/ttt-5-move-rb
https://learn.co/lessons/oo-tic-tac-toe
https://github.com/search?q=user%3Alearn-co-curriculum+ttt&type=Repositories
https://learn.co/lessons/ttt-1-welcome-rb
https://github.com/learn-co-curriculum/ttt-2-board-rb
https://learn.co/lessons/ttt-2-board-rb
https://github.com/learn-co-curriculum/ttt-2-board-rb/tree/solution
https://learn.co/lessons/ttt-3-display_board-example
https://github.com/learn-co-curriculum/ttt-5-move-rb
https://learn.co/lessons/intro-to-tic-tac-toe-rb
For example, a Tic Tac Toe program requires data structures for storing
the board and conditional logic for knowing whose turn it is or if
someone has won.
###############
board = [" "," "," "," "," "," "," "," "," "]
def display_board(board)
puts " #{board[0]} | #{board[1]} | #{board[2]} "
puts "-----------"
puts " #{board[3]} | #{board[4]} | #{board[5]} "
puts "-----------"
puts " #{board[6]} | #{board[7]} | #{board[8]} "
end
def move(board, location, current_player = "X")
board[location.to_i-1] = current_player
end
move(board, 1, "X")
display_board(board)
move(board, 5, "O")
def display_board
puts " #{grid[0][0]} | #{grid[0][1]} | #{grid[0][2]} "
puts "-----------"
puts " #{grid[1][0]} | #{grid[1][1]} | #{grid[1][2]} "
puts "-----------"
puts " #{grid[2][0]} | #{grid[2][1]} | #{grid[2][2]} "
end
########################
'@@@@@@@@@@@@@@@@@@@@@@@
////////////////
$> irb
require '~/dev-gt/test/ttt/lib/ttt/cell'
require '~/dev-gt/test/ttt/lib/ttt/player'
require '~/dev-gt/test/ttt/lib/ttt/board'
#load all 3 files above by running
$> require '~/dev-gt/test/ttt/lib/ttt'
$> Ttt::Cell.new
$> Ttt::Player.new
xyz = Struct.new(:value)
x_cell = xyz.new('X')
y_cell = xyz.new('Y')
empty = xyz.new
x_cell = Ttt::Cell.new("X")
y_cell = Ttt::Cell.new("Y")
empty = Ttt::Cell.new
grid = [
[x_cell, empty, empty],
[y_cell, x_cell, y_cell],
[y_cell, x_cell, x_cell]
]
board = Ttt::Board.new(grid: grid)
board.game_finished
Effectively, the single outer array has three inner arrays.
returns winner
grid = [
[x_cell, empty, empty],
[y_cell, x_cell, y_cell],
[y_cell, x_cell, x_cell]
]
##returns a draw
grid = [
[x_cell, y_cell, x_cell],
[y_cell, x_cell, y_cell],
[y_cell, x_cell, y_cell]
]
##no winner
grid = [
[x_cell, empty, empty],
[y_cell, empty, empty],
[y_cell, empty, empty]
]
board = Ttt::Board.new(grid: grid)
board.game_finished
###
http://stackoverflow.com/questions/13249872/rubys-any-and-all-methods-behaviour-on-empty-arrays-and-hashes
all?() public
Passes each element of the collection to the given block. The method
returns true if the block never returns false or nil. If the block is
not given, Ruby adds an implicit block of {|obj| obj} (that is all?
will return true only if none of the collection members are false or nil.)
#method that returns true if all elements of an array are empty and
false otherwise.
b = [1, 2, 3]
b.all? {|s| s.to_s.empty?}
b.all?
=> true
a = ['', '']
a[0].to_s.empty?
==> true
returns true if all elements of an Array are the same and
false otherwise.
b = [1, 2, 3]
b.all? {|s| s == b[0]}
=> false
c = [1, 2, 3]
c.all? {|s| s == c[0]}
=> true
###
any?() public
Passes each element of the collection to the given block. The method
returns true if the block ever returns a value other than false or
nil. If the block is not given, Ruby adds an implicit block
of {|obj| obj} (that is any? will return true if at least one of
the collection members is not false or nil.
##
returns true if any elements of the array are empty and false
otherwise.
b = [1, 2, 3]
b.any? {|s| s.to_s.empty?}
=> false
c = [1, 1, 1, ""]
c.any? {|s| s.to_s.empty?}
=> true
def draw?
p = Ttt::Board.new
k = p.grid.flatten.map { |cell| cell.value }
#any? here is used to check if any elements of the array are empty and false otherwise.
u = k.any? {|s| s.to_s.empty?}
end
def winning_positions
grid + # rows
grid.transpose + # columns
diagonals # two diagonals
end
def diagonals
[
[fetch_cell(0, 0), fetch_cell(1, 1), fetch_cell(2, 2)],
[fetch_cell(0, 2), fetch_cell(1, 1), fetch_cell(2, 0)]
]
end
def winner?
winning_positions.each do |winning_position|
#.all? is checking is all_empty?
next if winning_position_values(winning_position).all? { |element| element.to_s.empty? }
#all? here is used to check if all the array rows are thesame
return true if winning_position_values(winning_position).all? { |element| element == self[0] }
end
false
end
def winning_position_values(winning_position)
winning_position.map { |cell| cell.value }
end
##############################
??????????????????????????????
in console
@grid = [
[x_cell, empty, empty],
[y_cell, x_cell, y_cell],
[y_cell, x_cell, x_cell]
]
def fetch_cell(x, y)
@grid[y][x]
end
def draw?
k = @grid.flatten.map { |cell| cell.value }
u = k.any? {|s| s.to_s.empty?}
end
def draw?
@grid.flatten.map { |cell| cell.value }.any? {|s| s.to_s.empty?}
end
def winner?
winning_positions.each do |winning_position|
next if winning_position_values(winning_position).all? { |element| element.to_s.empty? }
#return true if winning_position_values(winning_position).all? { |element| element == self[0] }
#return true if winning_position_values(winning_position).all? { |element| element == winning_position_values[0] }
k = winning_position_values(winning_position)
puts "are u an array #{k.is_a? Array}"
w = winning_position_values(winning_position)
puts "k's content is #{w.all? { |element| element == w[0] }}"
end
false
end
def winning_position_values(winning_position)
winning_position.map { |cell| cell.value }
end
def winning_positions
@grid + @grid.transpose + diagonals
end
def diagonals
[
[fetch_cell(0, 0), fetch_cell(1, 1), fetch_cell(2, 2)],
[fetch_cell(0, 2), fetch_cell(1, 1), fetch_cell(2, 0)]
]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment