From ad20cec062ce1bece3a36476ce4522fc13dfa58a Mon Sep 17 00:00:00 2001 From: Olivia Hanson Date: Mon, 18 Jun 2018 19:51:52 +0000 Subject: [PATCH 1/3] Done. --- bin/tictactoe | 17 +++++++++ lib/board.rb | 63 ++++++++++++++++++++++++++++++ lib/game.rb | 85 +++++++++++++++++++++++++++++++++++++++++ lib/player.rb | 8 ++++ lib/players/computer.rb | 11 ++++++ lib/players/human.rb | 10 +++++ 6 files changed, 194 insertions(+) diff --git a/bin/tictactoe b/bin/tictactoe index 7ddd394b19b..b4ffc9d7bab 100755 --- a/bin/tictactoe +++ b/bin/tictactoe @@ -1,3 +1,20 @@ #!/usr/bin/env ruby require_relative '../config/environment' + +puts "Hey, there! Let's play some 'Toe!" +game = Game.new +until game.won? + game.play +end + +play_again = "" +until play_again.downcase == "n" || play_again.downcase == "no" + puts "Wanna do this ish again?" + play_again = gets.strip + play_again.downcase == "y" || play_again.downcase == "yes" + game = Game.new + until game.won? + game.play + end +end diff --git a/lib/board.rb b/lib/board.rb index e69de29bb2d..de5f2064446 100644 --- a/lib/board.rb +++ b/lib/board.rb @@ -0,0 +1,63 @@ +class Board + attr_accessor :cells + + def initialize() + reset! + end + + def reset! + @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? + if @cells.include?(" ") + false + else + true + end + end + + def turn_count + count = 0 + @cells.collect do |x| + if x != " " + count += 1 + end + end + count + end + + # def turn_count + # cells.count{|token| token == "X" || token == "O"} + # end + + def taken?(position) + if @cells[position.to_i - 1] == "X" || @cells[position.to_i - 1] == "O" + true + else + false + end + 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 diff --git a/lib/game.rb b/lib/game.rb index e69de29bb2d..d9929d6ac61 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -0,0 +1,85 @@ +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], + [2,4,6] + ] + + 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 + @board.turn_count % 2 == 0 ? @player_1 : @player_2 + 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.taken?(combo[0] + 1) + end + end + + def draw? + @board.full? && !won? + end + + def over? + won? || draw? + end + + def winner + xcount = 0 + ocount = 0 + if won? + @board.cells.collect do |token| + if token == "X" + xcount += 1 + elsif token == "O" + ocount += 1 + end + end + if xcount > ocount + "X" + else + "O" + end + end + end + + def turn + player = current_player + current_move = player.move(@board) + if !@board.valid_move?(current_move) + turn + else + puts "Turn: #{@board.turn_count + 1}\n" + @board.display + @board.update(current_move, player) + puts "#{player.token} moved #{current_move}" + @board.display + puts "\n\n" + end + end + + def play + while !over? + turn + end + if won? + puts "Congratulations #{winner}!" + elsif draw? + puts "Cat's Game!" + end + end + + +end diff --git a/lib/player.rb b/lib/player.rb index e69de29bb2d..c2c7537c360 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -0,0 +1,8 @@ +class Player + attr_reader :token + + def initialize(token) + @token = token + end + +end diff --git a/lib/players/computer.rb b/lib/players/computer.rb index e69de29bb2d..86205e3f5dd 100644 --- a/lib/players/computer.rb +++ b/lib/players/computer.rb @@ -0,0 +1,11 @@ +module Players + + class Computer < Player + + def move(board) + "1" + end + + end + +end diff --git a/lib/players/human.rb b/lib/players/human.rb index e69de29bb2d..2b7fbe4012e 100644 --- a/lib/players/human.rb +++ b/lib/players/human.rb @@ -0,0 +1,10 @@ +module Players + class Human < Player + + def move(board) + puts "Please enter 1-9:" + gets.strip + end + + end +end From 29a191dd0326f53dabbf6761e5ec8c6f8bdf65de Mon Sep 17 00:00:00 2001 From: Olivia Hanson Date: Wed, 11 Nov 2020 19:19:20 +0000 Subject: [PATCH 2/3] Done. --- lib/board.rb | 38 +++++++++++--------------------------- lib/game.rb | 35 +++++++---------------------------- lib/players/computer.rb | 20 +++++++++++++------- lib/players/human.rb | 5 ++++- 4 files changed, 35 insertions(+), 63 deletions(-) diff --git a/lib/board.rb b/lib/board.rb index de5f2064446..6bc721be83b 100644 --- a/lib/board.rb +++ b/lib/board.rb @@ -1,14 +1,18 @@ +require 'pry' + class Board attr_accessor :cells - def initialize() + + + def initialize reset! end - def reset! @cells = Array.new(9, " ") end + def display puts " #{cells[0]} | #{cells[1]} | #{cells[2]} " puts "-----------" @@ -22,33 +26,15 @@ def position(input) end def full? - if @cells.include?(" ") - false - else - true - end + !cells.include?(" ") end def turn_count - count = 0 - @cells.collect do |x| - if x != " " - count += 1 - end - end - count + cells.count("X") + cells.count("O") end - # def turn_count - # cells.count{|token| token == "X" || token == "O"} - # end - - def taken?(position) - if @cells[position.to_i - 1] == "X" || @cells[position.to_i - 1] == "O" - true - else - false - end + def taken?(input) + position(input) != " " end def valid_move?(input) @@ -56,8 +42,6 @@ def valid_move?(input) end def update(input, player) - @cells[input.to_i-1] = player.token + cells[input.to_i-1] = player.token end - - end diff --git a/lib/game.rb b/lib/game.rb index d9929d6ac61..410203c96f8 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -24,12 +24,12 @@ def current_player def won? WIN_COMBINATIONS.detect do |combo| - @board.cells[combo[0]] == @board.cells[combo[1]] && @board.cells[combo[1]] == @board.cells[combo[2]] && @board.taken?(combo[0] + 1) + board.cells[combo[0]] == board.cells[combo[1]] && board.cells[combo[0]] == board.cells[combo[2]] && board.taken?(combo[0] + 1) end end def draw? - @board.full? && !won? + board.full? && !won? end def over? @@ -37,43 +37,22 @@ def over? end def winner - xcount = 0 - ocount = 0 if won? - @board.cells.collect do |token| - if token == "X" - xcount += 1 - elsif token == "O" - ocount += 1 - end - end - if xcount > ocount - "X" - else - "O" - end + @board.cells[won?[0]] end end def turn - player = current_player - current_move = player.move(@board) - if !@board.valid_move?(current_move) + input = current_player.move(board) + if !board.valid_move?(input) turn else - puts "Turn: #{@board.turn_count + 1}\n" - @board.display - @board.update(current_move, player) - puts "#{player.token} moved #{current_move}" - @board.display - puts "\n\n" + board.update(input, current_player) end end def play - while !over? - turn - end + turn until over? if won? puts "Congratulations #{winner}!" elsif draw? diff --git a/lib/players/computer.rb b/lib/players/computer.rb index 86205e3f5dd..d17723f8886 100644 --- a/lib/players/computer.rb +++ b/lib/players/computer.rb @@ -1,11 +1,17 @@ -module Players - - class Computer < Player +class Players::Computer < Player def move(board) - "1" - end - - end + move = nil + if !board.taken?("5") + move = "5" + elsif board.turn_count == 1 + move = "3" + elsif board.turn_count == 2 + move = [1,3,7,9].detect {|open| board.taken?(open) == false }.to_s + else + move = [1, 2, 3, 4, 5, 6, 7, 8, 9].detect {|open| board.taken?(open) == false }.to_s + end + move + end end diff --git a/lib/players/human.rb b/lib/players/human.rb index 2b7fbe4012e..f31b1eddf48 100644 --- a/lib/players/human.rb +++ b/lib/players/human.rb @@ -1,10 +1,13 @@ module Players + class Human < Player def move(board) - puts "Please enter 1-9:" + puts "Please enter 1-9" + #gets.strip.tap{|input| input} gets.strip end end + end From 4598b558004fc5e0bfe340ede43b8e6c88391c53 Mon Sep 17 00:00:00 2001 From: Olivia Hanson Date: Thu, 19 Nov 2020 04:27:07 +0000 Subject: [PATCH 3/3] Done. --- bin/tictactoe | 35 ++++++++------ lib/game.rb | 8 ++-- lib/players/computer.rb | 103 +++++++++++++++++++++++++++++++++++----- lib/players/human.rb | 2 +- 4 files changed, 117 insertions(+), 31 deletions(-) diff --git a/bin/tictactoe b/bin/tictactoe index b4ffc9d7bab..c3da781a44d 100755 --- a/bin/tictactoe +++ b/bin/tictactoe @@ -2,19 +2,26 @@ require_relative '../config/environment' -puts "Hey, there! Let's play some 'Toe!" -game = Game.new -until game.won? - game.play -end -play_again = "" -until play_again.downcase == "n" || play_again.downcase == "no" - puts "Wanna do this ish again?" - play_again = gets.strip - play_again.downcase == "y" || play_again.downcase == "yes" - game = Game.new - until game.won? - game.play + puts "Hey, there! Let's play some 'Toe!" + puts "\nPlease choose player mode: + 0 + 1 + 2\n" + player_mode = gets.strip + + + if player_mode == "0" + Game.new(Players::Computer.new("X"), Players::Computer.new("O"), Board.new).play + + elsif player_mode == "1" + puts "Do you want to go first? [y/ n]" + if gets.strip == "y" + Game.new(Players::Human.new("X"), Players::Computer.new("O"), Board.new).play + else + Game.new(Players::Computer.new("X"), Players::Human.new("O"), Board.new).play + end + + elsif player_mode == "2" + Game.new(Players::Human.new("X"), Players::Human.new("O"), Board.new).play end -end diff --git a/lib/game.rb b/lib/game.rb index 410203c96f8..8581e74ec7e 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -24,12 +24,12 @@ def current_player def won? WIN_COMBINATIONS.detect do |combo| - board.cells[combo[0]] == board.cells[combo[1]] && board.cells[combo[0]] == board.cells[combo[2]] && board.taken?(combo[0] + 1) + @board.cells[combo[0]] == @board.cells[combo[1]] && @board.cells[combo[0]] == @board.cells[combo[2]] && @board.taken?(combo[0] + 1) end end def draw? - board.full? && !won? + @board.full? && !won? end def over? @@ -44,10 +44,10 @@ def winner def turn input = current_player.move(board) - if !board.valid_move?(input) + if !@board.valid_move?(input) turn else - board.update(input, current_player) + @board.update(input, current_player) end end diff --git a/lib/players/computer.rb b/lib/players/computer.rb index d17723f8886..1f4e4c061bc 100644 --- a/lib/players/computer.rb +++ b/lib/players/computer.rb @@ -1,17 +1,96 @@ class Players::Computer < Player - def move(board) - move = nil - if !board.taken?("5") - move = "5" - elsif board.turn_count == 1 - move = "3" - elsif board.turn_count == 2 - move = [1,3,7,9].detect {|open| board.taken?(open) == false }.to_s - else - move = [1, 2, 3, 4, 5, 6, 7, 8, 9].detect {|open| board.taken?(open) == false }.to_s - end - move + def move(board) + move = nil + if board.turn_count == 0 + move = [1,3,7,9].sample.to_s + # elsif board.turn_count == 1 && [1,3,7,9].all?{|cell| !board.taken?(cell.to_s)} + # move = [1,3,7,9].sample.to_s +elsif board.turn_count == 1 && board.taken?("5") + move = [1,3,7,9].sample.to_s + elsif board.turn_count >= 1 && board.turn_count < 6 && !board.taken?("5") + move = "5" + # elsif board.turn_count == 2 && board.taken?("5") && [1,3,7,9].all?{|cell| !board.taken?(cell.to_s)} + # move = [1,3,7,9].sample.to_s + elsif board.turn_count == 2 && board.turn_count < 6 && board.taken?("5") && ([1,3,7,9].select{|cell| board.position("#{cell}") == self.token}) + if board.position("1") == self.token + move = [3,7].sample + elsif board.position("3") == self.token + move = [1,9].sample + elsif board.position("7") == self.token + move = [1,9].sample + elsif board.position("9") == self.token + move = [3,7].sample + end + elsif (board.turn_count > 2 && board.turn_count < 6 && ((board.position("1") != " " && board.position("2") != " " && board.position("1") == board.position("2") && !board.taken?("3")) || (board.position("2") != " " && board.position("3") != " " && board.position("2") == board.position("3") && !board.taken?("1")) || (board.position("4") != " " && board.position("5") != " " && board.position("4") == board.position("5") && !board.taken?("6")) || (board.position("5") != " " && board.position("6") != " " && board.position("5") == board.position("6") && !board.taken?("4")) || (board.position("7") != " " && board.position("8") != " " && board.position("7") == board.position("8") && !board.taken?("9")) || (board.position("8") != " " && board.position("9") != " " && board.position("8") == board.position("9") && !board.taken?("7")) || (board.position("1") != " " && board.position("4") != " " && board.position("1") == board.position("4") && !board.taken?("7")) || (board.position("4") != " " && board.position("7") != " " && board.position("4") == board.position("7") && !board.taken?("1")) || (board.position("2") != " " && board.position("5") != " " && board.position("2") == board.position("5") && !board.taken?("8")) || (board.position("5") != " " && board.position("8") != " " && board.position("5") == board.position("8") && !board.taken?("2")) || (board.position("3") != " " && board.position("6") != " " && board.position("3") == board.position("6") && !board.taken?("9")) || (board.position("6") != " " && board.position("9") != " " && board.position("6") == board.position("9") && !board.taken?("3")) || (board.position("1") != " " && board.position("5") != " " && board.position("1") == board.position("5") && !board.taken?("9")) || (board.position("5") != " " && board.position("9") != " " && board.position("5") == board.position("9") && !board.taken?("1")) || (board.position("7") != " " && board.position("5") != " " && board.position("7") == board.position("5") && !board.taken?("3")) || (board.position("5") != " " && board.position("3") != " " && board.position("5") == board.position("3") && !board.taken?("7")))) || (board.turn_count > 3 && board.turn_count < 7 && ((board.position("1") != " " && board.position("3") != " " && board.position("1") == board.position("3") && !board.taken?("2")) || (board.position("7") != " " && board.position("9") != " " && board.position("7") == board.position("9") && !board.taken?("8")) || (board.position("1") != " " && board.position("7") != " " && board.position("1") == board.position("7") && !board.taken?("4")) || (board.position("3") != " " && board.position("9") != " " && board.position("3") == board.position("9") && !board.taken?("6")))) + if board.position("1") != " " && board.position("2") != " " && board.position("1") == board.position("2") && !board.taken?("3") + move = "3" + elsif board.position("2") != " " && board.position("3") != " " && board.position("2") == board.position("3") && !board.taken?("1") + move = "1" + elsif board.position("4") != " " && board.position("5") != " " && board.position("4") == board.position("5") && !board.taken?("6") + move = "6" + elsif board.position("5") != " " && board.position("6") != " " && board.position("5") == board.position("6") && !board.taken?("4") + move = "4" + elsif board.position("7") != " " && board.position("8") != " " && board.position("7") == board.position("8") && !board.taken?("9") + move = "9" + elsif board.position("8") != " " && board.position("9") != " " && board.position("8") == board.position("9") && !board.taken?("7") + move = "7" + elsif board.position("1") != " " && board.position("4") != " " && board.position("1") == board.position("4") && !board.taken?("7") + move = "7" + elsif board.position("4") != " " && board.position("7") != " " && board.position("4") == board.position("7") && !board.taken?("1") + move = "1" + elsif board.position("2") != " " && board.position("5") != " " && board.position("2") == board.position("5") && !board.taken?("8") + move = "8" + elsif board.position("5") != " " && board.position("8") != " " && board.position("5") == board.position("8") && !board.taken?("2") + move = "2" + elsif board.position("3") != " " && board.position("6") != " " && board.position("3") == board.position("6") && !board.taken?("9") + move = "9" + elsif board.position("6") != " " && board.position("9") != " " && board.position("6") == board.position("9") && !board.taken?("3") + move = "3" + elsif board.position("1") != " " && board.position("5") != " " && board.position("1") == board.position("5") && !board.taken?("9") + move = "9" + elsif board.position("5") != " " && board.position("9") != " " && board.position("5") == board.position("9") && !board.taken?("1") + move = "1" + elsif board.position("7") != " " && board.position("5") != " " && board.position("7") == board.position("5") && !board.taken?("3") + move = "3" + elsif board.position("5") != " " && board.position("3") != " " && board.position("5") == board.position("3") && !board.taken?("7") + move = "7" + elsif board.position("1") != " " && board.position("3") != " " && board.position("1") == board.position("3") && !board.taken?("2") + move = "2" + elsif board.position("1") != " " && board.position("7") != " " && board.position("1") == board.position("7") && !board.taken?("4") + move = "4" + elsif board.position("3") != " " && board.position("9") != " " && board.position("3") == board.position("9") && !board.taken?("6") + move = "6" + elsif board.position("9") != " " && board.position("7") != " " && board.position("9") == board.position("7") && !board.taken?("8") + move = "8" + end +# elsif board.turn_count > 3 && board.turn_count < 7 && ((board.position("1") != " " && board.position("3") != " " && board.position("1") == board.position("3") && !board.taken?("2")) || (board.position("7") != " " && board.position("9") != " " && board.position("7") == board.position("9") && !board.taken?("8")) || (board.position("1") != " " && board.position("7") != " " && board.position("1") == board.position("7") && !board.taken?("4")) || (board.position("3") != " " && board.position("9") != " " && board.position("3") == board.position("9") && !board.taken?("6"))) +# if board.position("1") != " " && board.position("3") != " " && board.position("1") == board.position("3") && !board.taken?("2") +# move = "2" +# elsif board.position("1") != " " && board.position("7") != " " && board.position("1") == board.position("7") && !board.taken?("4") +# move = "4" +# elsif board.position("3") != " " && board.position("9") != " " && board.position("3") == board.position("9") && !board.taken?("6") +# move = "6" +# elsif board.position("9") != " " && board.position("7") != " " && board.position("9") == board.position("7") && !board.taken?("8") +# move = "8" +# end + elsif board.turn_count > 2 && board.turn_count < 6 && board.position("5") == self.token && [1,3,7,9].none?{|cell| board.position("#{cell}") == self.token} + move = [1,3,7,9].detect{|cell| !board.taken?("#{cell}")}.to_s + elsif board.turn_count > 2 && board.turn_count < 6 && board.position("5") == self.token && ((board.position("1") == self.token && !board.taken?("9")) || (board.position("3") == self.token && !board.taken?("7")) || (board.position("7") == self.token && !board.taken?("3")) || (board.position("9") == self.token && !board.taken?("1"))) + if board.position("1") == self.token && !board.taken?("9") + move = "9" + elsif board.position("3") == self.token && !board.taken?("7") + move = "7" + elsif board.position("7") == self.token && !board.taken?("3") + move = "3" + elsif board.position("9") == self.token && !board.taken?("1") + move = "1" + end + # elsif board.turn_count >= 3 + else + move = [1,2,3,4,5,6,7,8,9].detect{|cell| board.taken?(cell.to_s) == false}.to_s end + move + end end diff --git a/lib/players/human.rb b/lib/players/human.rb index f31b1eddf48..e4cbf84463d 100644 --- a/lib/players/human.rb +++ b/lib/players/human.rb @@ -4,7 +4,7 @@ class Human < Player def move(board) puts "Please enter 1-9" - #gets.strip.tap{|input| input} + puts "#{board.display}" gets.strip end