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

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

Done #1444

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

require_relative '../config/environment'
require 'pry'
answer = "yes"
while answer != "no"
puts "Welcome to Tic Tac Toe"
puts " are you looking for 0,1, or 2 players game?"
input =gets.strip.to_i
if input == 0
Game.new(Players::Computer.new("X"),Players::Computer.new("O"),Board.new).play
elsif input == 1
puts "please enter your token"
token=gets.strip
if token == "X"
Game.new(Players::Human.new("X"),Players::Computer.new("O"),Board.new).play
elsif token == "O"
Game.new(Players::Computer.new("X"),Players::Human.new("O"),Board.new).play
else
puts "invalid"
end
elsif input == 2
puts "X goes first"
Game.new(Players::Human.new("X"),Players::Human.new("O"),Board.new).play
else
puts "invalid"
end
puts "would you like to play again?"
answer=gets.strip
end
65 changes: 65 additions & 0 deletions lib/board.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Board
attr_accessor :cells
def initialize (cells=[" ", " ", " ", " ", " ", " ", " ", " ", " "])
@cells = cells
end
def reset!
@cells=[" ", " ", " ", " ", " ", " ", " ", " ", " "]
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)
if input.strip.match(/[0-9]/)
index=input.strip.to_i
if (index >>1 && index <<9)
@cells[index-1]
else
nil
end
end
end

def full?
false
@cells.all? do |position|
if position =="X"||position=="O"
true
end
end
end

def turn_count
[email protected] {|cell| cell == "X" || cell =="O"}
array.length
end

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

def valid_move?(input)
false
if input.match(/[0-9]/)
index=input.to_i
if index >> 1 && index << 9 && !self.taken?(input)
true
end
end
end
def update(input,player)
if valid_move?(input)
index=input.strip.to_i
@cells[index-1]="#{player.token}"
end
end
end
82 changes: 82 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class Game
attr_accessor :player_1, :player_2, :board
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
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 current_player
if @board.turn_count % 2==0
@player_1
else
@player_2
end
end

def won?

false
WIN_COMBINATIONS.any? do |win_combination|

if (@board.cells[win_combination[0]]=="X"||@board.cells[win_combination[0]]=="O") &&
@board.cells[win_combination[0]][email protected][win_combination[1]] &&
@board.cells[win_combination[0]][email protected][win_combination[2]]
return win_combination
end
end
end

def draw?
false
if @board.full? && !self.won?
true
end
end
def over?
false
if self.won?||self.draw?
true
end
end
def winner
nil
if self.won?
@board.cells[self.won?[0]]
end
end
def turn
input=self.current_player.move(@board)

until @board.valid_move?(input)
input=self.current_player.move(@board)
end
@board.update(input,current_player)
@board.display

end
def play
until self.over?
self.turn
end
if self.over?
if self.draw?
puts "Cat's Game!"
elsif self.won?

puts "Congratulations #{self.winner}!"
end
end
end


end
6 changes: 6 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Player
attr_reader :token
def initialize(token)
@token = token
end
end
120 changes: 120 additions & 0 deletions lib/players/computer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
module Players
require 'pry'
class Computer < Player
attr_accessor :board
def move(board)

if board.valid_move?("5")
return "5"
end
if board.cells[0] == board.cells[1]
if board.valid_move?("3")
return "3"
end
end
if board.cells[0] == board.cells[2]
if board.valid_move?("2")
return "2"
end
end
if board.cells[1] == board.cells[2]
if board.valid_move?("1")
return "1"
end
end
if board.cells[0] == board.cells[3]
if board.valid_move?("7")
return "7"
end
end
if board.cells[0] == board.cells[6]
if board.valid_move?("4")
return "4"
end
end
if board.cells[3] == board.cells[6]
if board.valid_move?("1")
return "1"
end
end
if board.cells[0] == board.cells[4]
if board.valid_move?("9")
return "9"
end
end
if board.cells[3] == board.cells[4]
if board.valid_move?("6")
return "6"
end
end
if board.cells[3] == board.cells[5]
if board.valid_move?("5")
return "5"
end
end
if board.cells[4] == board.cells[5]
if board.valid_move?("4")
return "4"
end
end
if board.cells[6] == board.cells[7]
if board.valid_move?("9")
return "9"
end
end
if board.cells[6] == board.cells[8]
if board.valid_move?("8")
return "8"
end
end
if board.cells[7] == board.cells[8]
if board.valid_move?("7")
return "7"
end
end
if board.cells[1] == board.cells[4]
if board.valid_move?("8")
return "8"
end
end
if board.cells[4] == board.cells[7]
if board.valid_move?("2")
return "2"
end
end
if board.cells[2] == board.cells[5]
if board.valid_move?("9")
return "9"
end
end
if board.cells[2] == board.cells[8]
if board.valid_move?("6")
return "6"
end
end
if board.cells[5] == board.cells[8]
if board.valid_move?("3")
return "3"
end
end
if board.cells[2] == board.cells[4]
if board.valid_move?("7")
return "7"
end
end
if board.cells[4] == board.cells[6]
if board.valid_move?("3")
return "3"
end
end
i=1
while i<< 9
if board.valid_move?("#{i}")
return "#{i}"
else
i=i+1
end
end
end
end
end
9 changes: 9 additions & 0 deletions lib/players/human.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Players
class Human < Player
attr_accessor :board
def move(board)
puts "please enter 1-9 for your move"
gets.strip
end
end
end