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

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 37 additions & 1 deletion bin/tictactoe
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
#!/usr/bin/env ruby

require_relative '../config/environment'

def game_chooser(input_a, input_b)
if (input_a == "1") && (input_b != nil)
Game.new(player_1 = Players::Computer.new, player_2 = Players::Computer.new("O"), board = Board.new).play
elsif (input_a == "2") && (input_b== "X")
Game.new(player_1 = Players::Human.new, player_2 = Players::Computer.new("O"), board = Board.new).play
elsif (input_a == "2") && (input_b == "O")
Game.new(player_1 = Players::Computer.new, player_2 = Players::Human.new("O"), board = Board.new).play
elsif (input_a == "3") && (input_b == "X")
Game.new(player_1 = Players::Human.new, player_2 = Players::Human.new, board = Board.new).play
elsif (input_a == "2") && (input_b == "O")
Game.new(player_1 = Players::Human.new, player_2 = Players::Computer.new("O"), board = Board.new).play
end
end


puts "Hello! Welcome to Tic Tac Toe."
puts "Please enter your name:"
input_d = gets.strip

puts "Hi, #{input_d}, what kind of game would you like to play?"
puts "1. 0 Player"
puts "2. 1 Player"
puts "3. 2 Player"
input_a = gets.strip
puts "Would you like to be X or O?"
input_b = gets.strip.upcase
game_chooser(input_a, input_b)


puts "Would you like to play again?"
input_c = gets.strip
if input_c == "Yes"
game_chooser(input_a, input_b)
elsif input_c == "No"
puts "Thanks for playing!"
end
57 changes: 57 additions & 0 deletions lib/board.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Board
attr_accessor :cells
def initialize
@cells = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
end

def board
@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(x)
y = x.to_i - 1
@cells[y]
end

def full?
@cells.none? (" ")
end

def turn_count
counter = [ ]
@cells.each do |item|
if item == " "
counter << item
end
end
9 - counter.length
end

def taken?(x)
@cells[x.to_i- 1] == "X" || @cells[x.to_i - 1] == "O"
end

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

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

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], [1, 4, 7]]

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

def board
@board
end

def current_player
arr_1 = []
arr_2 = []
board.cells.each do |item|
if item == "X"
arr_1 << item
elsif item == "O"
arr_2 << item
end
end
if arr_1.length > arr_2.length
self.player_2
elsif arr_1.length < arr_2.length
self.player_1
elsif arr_1.length == arr_2.length
self.player_1
end
end

def won?
y = []
WIN_COMBINATIONS.each do |combo|
if (board.cells[combo[0]] == "X" && board.cells[combo[1]] == "X" && board.cells[combo[2]] == "X")
y << combo
elsif (board.cells[combo[0]] == "O" && board.cells[combo[1]] == "O" && board.cells[combo[2]] == "O")
y << combo
end
end
if y.length > 0
y[0]
else
false
end
end

def draw?
if self.won? == false && board.cells.include?(" ") == false
true
end
end

def over?
self.won? || self.draw?
end

def winner
x = self.won?
if x == false
nil
else
board.cells[x[0]]
end
end

def turn
a = current_player.move(board)
b = " "
while !board.valid_move?(a)
a = current_player.move(board)
end
if board.valid_move?(a)
board.update(a, current_player)
board.display
end
end

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



#def turn
#a = current_player.move(board)
#if board.valid_move?(a)
#board.update(input, current_player)
#elsif !board.valid_move?(a)
#until board.valid_move?(input)
#puts "Enter input again:"
#input = gets.strip
#if board.valid_move?(input)
#board.update(input, current_player)
#end
#end
#end
#end

#def turn
#a = current_player.move(board)
#if board.valid_move?(a)
#board.update(a, current_player)
#<<<<<<< HEAD
#else
#self.turn

#else
#turn


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

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

#def game_chooser(input)
#input_b = " "
#if (input == "2") || (input == "3")
#puts "Would you like to be X (Player 1), or O (Player 2)?"
#puts "Enter your selection(X or O):"
#input_b = gets.strip
#end

#if (input == "1")
#Game.new(player_1 = Players::Computer.new, player_2 = Players::Computer.new("O"), board = Board.new).play
#elsif (input == "2") && (input_b== "X")
#Game.new(player_1 = Players::Human.new, player_2 = Players::Computer.new("O"), board = Board.new).play
#elsif (input == "2") && (input_b == "O")
#Game.new(player_1 = Players::Computer.new, player_2 = Players::Human.new("O"), board = Board.new).play
#elsif (input == "3") && (input_b == "X")
#Game.new(player_1 = Players::Human.new, player_2 = Players::Human.new, board = Board.new).play
#elsif (input == "2") && (input_b == "O")
#Game.new(player_1 = Players::Human.new, player_2 = Players::Computer.new("O"), board = Board.new).play
#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

@@players = []

def initialize(token = "X")
@token = token
end
end
Loading