diff --git a/bin/tictactoe b/bin/tictactoe index 7ddd394b19b..c878cff16fd 100755 --- a/bin/tictactoe +++ b/bin/tictactoe @@ -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 \ No newline at end of file diff --git a/lib/board.rb b/lib/board.rb index e69de29bb2d..e55c3ebe784 100644 --- a/lib/board.rb +++ b/lib/board.rb @@ -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 \ No newline at end of file diff --git a/lib/game.rb b/lib/game.rb index e69de29bb2d..b45271d86c3 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -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 \ No newline at end of file diff --git a/lib/player.rb b/lib/player.rb index e69de29bb2d..5782d9cfb76 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -0,0 +1,10 @@ +class Player + + attr_reader :token + + @@players = [] + + def initialize(token = "X") + @token = token + end +end \ No newline at end of file diff --git a/lib/players/computer.rb b/lib/players/computer.rb index e69de29bb2d..fab8bbda183 100644 --- a/lib/players/computer.rb +++ b/lib/players/computer.rb @@ -0,0 +1,170 @@ +module Players + class Computer < Player + def move(board) + #index_array = [1, 2, 3, 4, 5, 6, 7, 8, 9] + #options_array = [] + #index_array.each do |item| + #if !board.taken?(item) + #options_array << item + #end + #end + if !board.taken?("5") + "5" + elsif !board.taken?("2") + "2" + elsif !board.taken?("4") + "4" + elsif !board.taken?("6") + "6" + elsif !board.taken?("8") + "8" + elsif !board.taken?("1") + "1" + elsif !board.taken?("3") + "3" + elsif !board.taken?("7") + "7" + elsif !board.taken?("9") + "9" + end + end + end +end + + + + + + #a = options_array.sample + #options_array.delete(a) + #"#{a}" + #end + #end + #end + #end + #elsif !board.taken?(2) + #"2" + #elsif !board.taken?(4) + #"4" + #elsif !board.taken?(1) + #"1" + #elsif !board.taken?(3) + #"3" + #elsif !board.taken?(6) + #"6" + #elsif !board.taken?(7) + #"7" + #elsif !board.taken?(9) + #"9" + #elsif !board.taken?(8) + #"8" + #end + #end + #end +#end + +################################################################################# + + #if !board.taken?(5) + #"5" + #elsif board.taken?(5) + #a = [2, 4, 6, 8].sample + #"#{a}" + #elsif !board.taken?(2) + #"2" + #elsif !board.taken?(4) + #"4" + #elsif !board.taken?(1) + #"1" + #elsif !board.taken?(3) + #"3" + #elsif !board.taken?(6) + #"6" + #elsif !board.taken?(7) + #"7" + #elsif !board.taken?(9) + #"9" + #elsif !board.taken?(8) + #"8" + +########################################################################### + + + #elsif !board.taken?(9) + #@last_board = board.cells + #"9" + #end + #end + + #elsif their_last_move == 4 + #if !board.taken?(1) + #last_board = board.cells + #"1" + #elsif !board.taken?(7) + #last_board = board.cells + #"7" + #end + + #end + + #elsif their_last_move == 6 + #if !board.taken?(3) + #@last_board = board.cells + #"3" + #elsif !board.taken?(9) + #@last_board = board.cells + #"9" + #end + + #elsif their_last_move == 7 + #if !board.taken?(4) + #@last_board = board.cells + #"4" + #elsif ! board.cells.taken?(8) + #@last_board = board.cells + #"8" + #end + + #elsif their_last_move == 8 + #if !board.taken?(7) + #@last_board = board.cells + #"7" + #elsif !board.taken?(9) + #@last_board = board.cells + #"9" + #end + + #elsif their_last_move == 9 + #if !board.taken?(6) + #@last_board = board.cells + #"6" + #elsif !board.taken?(8) + #@last_board = board.cells + #"8" + #end + #end + #end + #end +#end + +#################################################### + + #board.cells.each do |item| + #current_board << "#{item}#{x}" + #x += 1 + #end + + #difference = (current_board - last_board)[0] + #xxxy = current_board.index(difference) + #their_last_move = xxxy.to_i + 1 + + #binding.pry + #end + #end +#end + +################################################# + + #current_board = [] + #last_board = [] + #x = 0 \ No newline at end of file diff --git a/lib/players/human.rb b/lib/players/human.rb index e69de29bb2d..a13471bd7b6 100644 --- a/lib/players/human.rb +++ b/lib/players/human.rb @@ -0,0 +1,9 @@ +module Players + class Human < Player + def move(board) + puts "Enter a move:" + input = gets.strip + input + end + end +end \ No newline at end of file