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 #1449

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

Done #1449

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
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--color
--require spec_helper
--fail-fast
32 changes: 32 additions & 0 deletions bin/tictactoe
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
#!/usr/bin/env ruby

require_relative '../config/environment'

puts "Let's play Tic-Tac-Toe!"
puts "Player 1 is X, Player 2 is O"
puts "You can exit at any time by pressing !"


def begin
puts "How many players? (0,1,or 2)"
user_input = get.strip

if user_input.to_i == 0
Game.new(Players::Computer.new("X"),Players::Computer.new("O")).play
puts "That was fun, would you like to play again?"
elsif user_input.to_i == 1 # option to be first or second x||o
puts "Would you like to be first? (y/n)"
if user_input == "y" || "Y"
Game.new(Players::Human.new("X"),Players::Computer.new("O")).play
elsif user_input == "n" ||"N"
Game.new(Players::Computer.new("X"),Players::Human.new("O")).play
puts "That was fun, would you like to play again?"

elsif user_input.to_i == 2
puts "X, you're first!"
Game.new(Players::Human.new("X"),Players::Human.new("O")).play
puts "That was fun, would you like to play again?"
end
puts "Would you like to play again?"
#When the game is over, the CLI should prompt the user if they would like to play again and allow them to choose
#a new configuration for the game as described above. If the user doesn't want to play again, exit the program.
end
begin until gets.strip == "!"
end
56 changes: 56 additions & 0 deletions lib/board.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'pry'
class Board
attr_accessor :cells

def initialize
reset!

end

def reset!
@cells= Array.new(9," ")
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)
@cells[user_input.to_i-1]
end

def full?
@cells.all? do |char|
if char == "X" || char == "O"
true
else
false
end
end#if position is X or O then return true
end



def turn_count # count all of the cells that have X or O
@cells.count("X") + @cells.count("O")# cells.count("X") + cells.count("O")# cells.all.count(cells) # if its not full => the number of X or O
end

def taken?(position) #returns true if the positon is "X" or "O"
@cells[position.to_i-1] == "X" ||@cells[position.to_i-1] == "O"
end

def valid_move?(position) #returns true for user input 1-9 that is NOT take
position.to_i.between?(1,9) && !taken?(position)
end

def update(position,player) #gets the users input "X" or "O"
@cells[position.to_i-1] = player.token
end



end
89 changes: 89 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require 'pry'
class Game
# extend Players :: Humans (wonder)
# include Players
attr_accessor :board, :player_1, :player_2, :user_input

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
# # return the player whose turn it is
# #if move is odd return player_1 token
if @board.turn_count.even? #if count is even then player 2 jsut moved
player_1
else
player_2
end
end

def won?
WIN_COMBINATIONS.each do |combination| #[0,1,2] which is board range (0-8)
if @board.cells[combination[0]] == @board.cells[combination[1]] &&
@board.cells[combination[1]] == @board.cells[combination[2]] &&
@board.taken?(combination[0]+1)
# user_input range (1-9)
return combination # returns the winner combination
end
end
return false #if there is not a winner
end

def draw? # check if board is full or if anyone won
@board.full? && !self.won? ? true : false
end

def over? # game is over if there is a draw or winner
self.draw? || self.won?
end

def winner #check current player when there is a winning combination
if won? # self.won?.all? == "X" || "O" # check to see which player
combination = won?
@board.cells[combination[0]] # X or O
end
end

def turn
# check to make sure its a valid move
#if valid update board
@user_input = current_player.move(@board)
if @board.valid_move?(@user_input)
@board.update(@user_input, current_player)
else puts "Please enter a number 1-9:"
@board.display
turn
end

end

def play #keeps playing until its over
#congratulates winner
#calls cat's game
until over?
turn
end
if winner
puts "Congratulations #{winner}!"
else draw?
puts "Cat's Game!"
end

end



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] ]

end
7 changes: 7 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Player
attr_reader :token
def initialize(token)
@token = token
end

end
25 changes: 25 additions & 0 deletions lib/players/computer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Players
class Computer < Player
def move(board)
if board.cells[4] == " "
"5"
elsif board.cells[8] == " "
"9"
elsif board.cells[6] == " "
"7"
elsif board.cells[0] == " "
"1"
elsif board.cells[2] == " "
"3"
elsif board.cells[7] == " "
"8"
elsif board.cells[3] == " "
"4"
elsif board.cells[2] == " "
"2"
elsif board.cells[6] == " "
"6"
end
end
end
end
11 changes: 11 additions & 0 deletions lib/players/human.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Players
class Human < Player

#Human will give make a move by inputting an integer that corresponds with the board
#The move should return the value of the user's input

def move(board)
user_input = gets.strip
end
end
end