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

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

Done #1429

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: 4 additions & 0 deletions bin/tictactoe
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#!/usr/bin/env ruby

require_relative '../config/environment'


TicTacToe.new.start

87 changes: 87 additions & 0 deletions lib/board.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
class Board

attr_accessor :cells


def initialize
@cells = Array.new(9, " ")
end

def reset!
@cells.clear
@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(input)
index = input.to_i-1
@cells[index]
end


def taken?(input)
if position(input) == "" || position(input) == " " || position(input) == nil
false
elsif position(input) == "X" || position(input) == "O"
true
end
end

def valid_move?(input)

if input.to_i.between?(1,9) == true && taken?(input) == false
true
else
false
end
end

def update(input, player)
@cells[input.to_i-1] = player.token
end
# def move(index, value)
# @board[index] = value
# return value
# end
# def turn
# puts "Please enter 1-9:"
# position_taken = gets.strip
# index = input_to_index(position_taken)
# if !valid_move?(index)
# turn
# else
# move(index, current_player)
# display_board
# end
# end


def full?
if @cells.include?(' ') || @cells.include?('')
return false
else
return true
end
end


def turn_count
counter = 0
@cells.each do |place|
if place == 'X' || place == 'O'
counter += 1
end
end
return counter
end



end
113 changes: 113 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
require 'pry'
class Game

attr_accessor :board, :player_1, :player_2
WIN_COMBINATIONS = [
[0,1,2], # Top row
[3,4,5], # Middle row
[6,7,8], #lower row
[0,4,8], #horizontal row_a
[2,4,6], #horizontal_row_b
[1,4,7], #vertical_row_a
[0,3,6], #vertical_row_b
[2,5,8], #vertical_row_c
]

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

def current_player
if @board.turn_count % 2 == 0
return player_1
else
return player_2
end
end


def over?
won? || draw?
end

def full?
if @board.cells.include?(' ') || @board.cells.include?('')
return false
else
return true
end
end

def won?
WIN_COMBINATIONS.detect do |combo|
@board.cells[combo[0]] == @board.cells[combo[1]] &&
@board.cells[combo[1]] == @board.cells[combo[2]] && ( @board.cells[combo[0]] == "X" || @board.cells[combo[0]] == "O")
end #end def
end



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


def draw?
if !won? && full?
return true
end
end

def turn
player = current_player
current_move = player.move(@board)
if [email protected]_move?(current_move)
turn
elsif @board.valid_move?(current_move)
@board.update(current_move, player)
end
end


def play
turn until over?
if won?

puts "Congratulations #{winner}!"
elsif draw?
puts "Cat's Game!"

puts "Congratulations #{winner}"
elsif draw?
puts "There has been a draw"

end
end

def start
puts "Welcome to Tic Tac Toe!"
puts "What player would you like to be? X or O?"
player.token = gets.chomp
puts "Who should go first and be X ?"
player = Player.new(token)
game = Game.new(player_1 = Players::Human.new("X"), player_2 = Players::Human.new("O"), board = Board.new)
game
game.over?
puts "Would you like to play again?"
answer = gets.chomp
if answer == "Yes"
start
game
else
end
end




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

attr_reader :token

def initialize(token)
@token = token
end



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

class Computer < Player

#////////Random Mode\\\\\\\#
#def move(board)
#rand(1..9).to_s
#end

#------------------------------------------------#

#///////Hard-coded Moves by AI\\\\\\\\#
attr_accessor :board, :cells

def move(board)
if board.cells[0] == " " && board.cells[4] == " "
"1"
elsif board.cells[4] == " "
"5"
elsif board.cells[2] == " "
"3"
elsif board.cells[6] == " "
"7"
elsif board.cells[8] == " "
"9"
elsif board.cells[1] == " "
"2"
elsif board.cells[3] == " "
"4"
elsif board.cells[5] == " "
"6"
elsif board.cells[7] == " "
"8"
end
end

#------------------------------------------------#
end #class end

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



def move(board)
puts "Please enter your selection"
input = gets.chomp
input
end

end
end