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

Project submitted #1433

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b5ec3fd
Done.
parisdolfman Oct 25, 2019
7e1fad4
Done.
parisdolfman Oct 25, 2019
d94f024
Done.
parisdolfman Oct 25, 2019
3de87a5
Done.
parisdolfman Oct 25, 2019
f885329
Done.
parisdolfman Oct 25, 2019
83f051c
Done.
parisdolfman Oct 25, 2019
f985640
Done.
parisdolfman Oct 25, 2019
b52c6a8
Done.
parisdolfman Oct 25, 2019
2803898
Done.
parisdolfman Oct 25, 2019
0e71a9d
Done.
parisdolfman Oct 27, 2019
cb230ef
Done.
parisdolfman Oct 27, 2019
b49aaf2
Done.
parisdolfman Oct 27, 2019
da046e5
Done.
parisdolfman Oct 27, 2019
b30e194
Done.
parisdolfman Oct 27, 2019
22f9b7a
Done.
parisdolfman Oct 27, 2019
a0617f1
Done.
parisdolfman Oct 27, 2019
5fec2e0
Done.
parisdolfman Oct 27, 2019
5813cde
Done.
parisdolfman Oct 27, 2019
8177a8b
Done.
parisdolfman Oct 27, 2019
e619950
Done.
parisdolfman Oct 27, 2019
5b90a39
Done.
parisdolfman Oct 27, 2019
3d96624
Done.
parisdolfman Oct 28, 2019
fe5e222
Done.
parisdolfman Oct 28, 2019
23a8592
Done.
parisdolfman Oct 28, 2019
a7fab96
Done.
parisdolfman Oct 28, 2019
fcce21f
Done.
parisdolfman Oct 28, 2019
566d4c4
Done.
parisdolfman Oct 28, 2019
e6dab7e
Done.
parisdolfman Oct 28, 2019
a8d28b5
Done.
parisdolfman Oct 28, 2019
55f2446
Done.
parisdolfman Oct 28, 2019
3671943
Done.
parisdolfman Oct 28, 2019
3411dc1
Done.
parisdolfman Oct 28, 2019
e689518
Done.
parisdolfman Oct 28, 2019
3e0e7f2
Done.
parisdolfman Oct 29, 2019
0e92f51
Done.
parisdolfman Nov 3, 2019
7f3a627
Done.
parisdolfman Nov 3, 2019
3afeb20
Done.
parisdolfman Nov 3, 2019
a284f53
Done.
parisdolfman Nov 3, 2019
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
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'

def start
input = ""
while input != "exit"
puts "Welcome to Tic Tac Toe!"
puts "What kind of game would you like to play? Enter '0 player', '1 player', or '2 player'."


input = gets.strip

case input

when input == "0 player"
Game.new(Players::Computer.new("X"), Players::Computer.new("O"), Board.new).play

when input == "1 player"
puts "Would you like to go first? Enter 'yes' or 'no'."
if input == "yes"
Game.new(Players::Human.new("X"), Players::Computer.new("O"), Board.new).play
else Game.new(Players::Computer.new("O"), Players::Human.new("X"), Board.new).play
end

when input == "2 player"
Game.new(Players::Human.new("X"), Players::Human.new("O"), Board.new).play
end

puts "Would you like to play again? If not enter 'exit'."

game_start until input == "exit"
end

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

attr_accessor :cells

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

def initialize
@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)
@cells[input.to_i - 1]
end

def full?
@cells.all?{ |char| char == "X" || char == "O"}
end

def turn_count
counter = 0
@cells.each do |cell|
if cell == "X" || cell == "O"
counter += 1
end
end
counter
end


def taken?(input)
position(input) == "X" || position(input) == "O" ? true : false
end

def valid_move?(input)
input.to_i.between?(1, 9) && !taken?(input)
end

def update(input, player)
@cells[input.to_i - 1] = player.token
end


end
68 changes: 68 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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],
[6,4,2]
]

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 ? player_1 : player_2
end

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

def over?
@board.full?
end

def draw?
@board.full? && !won?
end

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


def turn
player = current_player
current = player.move(@board)
@board.valid_move?(current) ? @board.update(current, player) : turn
end

def play
while !won? && !draw? && !over?
turn
end
if won?
puts "Congratulations #{winner}!"
elsif draw?
puts "Cat's Game!"
end
end



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

attr_reader :token

def initialize(token)
@token = token
end


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

def move(board)
if board.cells[4] == " "
"5"
elsif board.cells[6] == " "
"7"
elsif board.cells[7] == " "
"8"
elsif board.cells[0] == " "
"1"
elsif board.cells[2] == " "
"3"
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:"
gets.strip
end

end
end