Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done #1438

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Done #1438

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bin/tictactoe
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env ruby

require_relative '../config/environment'
require_relative '../config/environment' # Loads environment and code

Game.start.play
65 changes: 65 additions & 0 deletions lib/board.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'pry'
class Board
attr_accessor :cells

def initialize
@cells = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
# Commented out 8 & 9 and replaced with line 6. Not sure I need 8 & 9 anymore.
#@cells = []
#self.reset!
end

def reset! #resets cells of the board to a 9 element array of " "
@cells.replace([" ", " ", " ", " ", " ", " ", " ", " ", " "]) #refactorMe
end

def display
puts " " + @cells[0] + " | " + @cells[1] + " | " + @cells[2] + " "
puts "-----------"
puts " " + @cells[3] + " | " + @cells[4] + " | " + @cells[5] + " "
puts "-----------"
puts " " + @cells[6] + " | " + @cells[7] + " | " + @cells[8] + " "
end

def position(user_input) # takes in user input and returns the value of the board cell
self.cells[(user_input.to_i) - 1]
end

def full?
!self.cells.include?(" ")
end

def turn_count
count = 0
self.cells.each do |cell|
if cell != " "
count += 1
end
end
count
end

def taken?(position)
if @cells[position.to_i - 1] == "X"
true
elsif @cells[position.to_i - 1] == "O"
true
else
false
end
end

def valid_move?(user_input)
if taken?(user_input)
false
elsif !(1..9).include?(user_input.to_i)
false
else
true
end
end

def update(position, player)
@cells[position.to_i-1] = "#{player.token}"
end
end
92 changes: 92 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
class Game
attr_accessor :board, :player_1, :player_2

WIN_COMBINATIONS = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]

def initialize(player_1 = Players::Human.new("X"), player_2 = Players::Human.new("O"), board = Board.new)
@player_1 = player_1
@player_2 = player_2
@board = board
end

def current_player
(board.turn_count % 2) == 0 ? self.player_1 : self.player_2
end

def won?
WIN_COMBINATIONS.detect do |win_combo|
position_1 = board.cells[win_combo[0]]
position_2 = board.cells[win_combo[1]]
position_3 = board.cells[win_combo[2]]

position_1 == player_1.token && position_2 == player_1.token && position_3 == player_1.token || position_1 == player_2.token && position_2 == player_2.token && position_3 == player_2.token
end
end

def draw?
!won? && !board.cells.include?(" ")
end

def over?
won? || draw?
end

def winner
if winner = won?
board.cells[winner[0]]
end
end

def turn
board.display
input = current_player.move(board)
if board.valid_move?(input)
board.update(input, current_player)
else
puts "Invalid move. Please try again."
turn
end
end

def play
until over?
turn
end

if won?
puts "Congratulations #{winner}!"
else
puts "Cat's Game!"
end
end

def self.start
puts "Welcome to Tic Tac Toe!"
puts "How many players will be playing? Enter 0, 1, or 2"
number_of_players = gets.chomp
if number_of_players.to_i == 0
Game.new(player_1 = Players::Computer.new("X"), player_2 = Players::Computer.new("O"), board = Board.new)
elsif number_of_players.to_i == 1
puts "Would you like to be X and go first or be O and go second? Enter X or O"
player_choice = gets.chomp
if player_choice.capitalize == "X"
Game.new(player_1 = Players::Human.new("X"), player_2 = Players::Computer.new("O"), board = Board.new)
else
Game.new(player_1 = Players::Computer.new("X"), player_2 = Players::Human.new("O"), board = Board.new)
end
else
Game.new
end

end

end
8 changes: 8 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Player
attr_reader :token

def initialize(token)
@token = token
end

end
15 changes: 15 additions & 0 deletions lib/players/computer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Players
class Computer < Player

def move(board)
puts "Computer select move 1 - 9"
if !board.taken?(5)
"5"
else
rand(1..9).to_s
end

end
end

end
10 changes: 10 additions & 0 deletions lib/players/human.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Players
class Human < Player

def move(board)
puts "Enter your move 1 - 9"
move = gets.chomp
end
end

end