From 69cf4f03601fdc0bd4123781f362d996e209c553 Mon Sep 17 00:00:00 2001 From: Valencia Caldwell Date: Fri, 12 Mar 2021 04:37:44 +0000 Subject: [PATCH 1/9] Done. --- .rspec | 1 + lib/board.rb | 46 +++++++++++++++++++++++++++++++++++++++++ lib/game.rb | 3 +++ lib/player.rb | 3 +++ lib/players/computer.rb | 5 +++++ lib/players/human.rb | 5 +++++ 6 files changed, 63 insertions(+) diff --git a/.rspec b/.rspec index 83e16f80447..825b76d0dbd 100644 --- a/.rspec +++ b/.rspec @@ -1,2 +1,3 @@ --color --require spec_helper +--fail-fast diff --git a/lib/board.rb b/lib/board.rb index e69de29bb2d..4a711d0f351 100644 --- a/lib/board.rb +++ b/lib/board.rb @@ -0,0 +1,46 @@ +require 'pry' +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? + # cells.each(&.include?("X"||"O"):true)) + cells.all? do |character| + if character == "X" || character == "O" + true + else + false + end + end#if position is X or O then return true + end + + def turn_count # count all of the cells that have X or O + cells.count{|char| char == "X" || char == "O"}# cells.count("X") + cells.count("O")# cells.all.count(cells) # if its not full => the number of X or O + # binding.pry + end + def taken?(input) #returns true if the positon is "X" or "O" + position.full?(input) + + end + + +end diff --git a/lib/game.rb b/lib/game.rb index e69de29bb2d..4ed2bd45f41 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -0,0 +1,3 @@ +class Game + +end diff --git a/lib/player.rb b/lib/player.rb index e69de29bb2d..b06ea71a56c 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -0,0 +1,3 @@ +class Player + +end diff --git a/lib/players/computer.rb b/lib/players/computer.rb index e69de29bb2d..8e53e790c45 100644 --- a/lib/players/computer.rb +++ b/lib/players/computer.rb @@ -0,0 +1,5 @@ +module Players + class Computer < Player + + end +end diff --git a/lib/players/human.rb b/lib/players/human.rb index e69de29bb2d..fd50c2d1576 100644 --- a/lib/players/human.rb +++ b/lib/players/human.rb @@ -0,0 +1,5 @@ +module Players + class Human < Player + + end +end From 799d0b2e4d0750bd6fc1b0c0ddaad83c3615c16e Mon Sep 17 00:00:00 2001 From: Valencia Caldwell Date: Fri, 12 Mar 2021 17:11:10 +0000 Subject: [PATCH 2/9] Done. --- lib/board.rb | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/board.rb b/lib/board.rb index 4a711d0f351..26a1e1e5fa8 100644 --- a/lib/board.rb +++ b/lib/board.rb @@ -17,30 +17,44 @@ def display 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.each(&.include?("X"||"O"):true)) - cells.all? do |character| - if character == "X" || character == "O" - true + end + + def position(user_input) + self.cells[user_input.to_i-1] + end + + def full? + # cells.each(&.include?("X"||"O"):true)) + self.cells.all? do |char| + if char == "X" || char == "O" + true else - false + false end end#if position is X or O then return true end + + def turn_count # count all of the cells that have X or O - cells.count{|char| char == "X" || char == "O"}# cells.count("X") + cells.count("O")# cells.all.count(cells) # if its not full => the number of X or O - # binding.pry + self.cells.count("X") + self.cells.count("O")# cells.count("X") + cells.count("O")# cells.all.count(cells) # if its not full => the number of X or O end - def taken?(input) #returns true if the positon is "X" or "O" - position.full?(input) + def taken?(position) #returns true if the positon is "X" or "O" + self.cells[position.to_i-1] == "X" ||self.cells[position.to_i-1] == "O" end + def valid_move?(position) #returns true for user input 1-9 that is NOT take + position.to_i.between?(1,9) && !taken?(position) + end + + def update(position,player) #gets the users input "X" or "O" + # self.position(user_input.to_i-1) == "#{player.token}"# put in token in correct position + self.cells[position.to_i-1] = player.token + # binding.pry + # self.position(user_input) = "#{player.token}" end + end + + end From 4c7c531d8bfbf1af1f53ed73f038dae418ebf093 Mon Sep 17 00:00:00 2001 From: Valencia Caldwell Date: Fri, 12 Mar 2021 17:31:50 +0000 Subject: [PATCH 3/9] Done. --- lib/game.rb | 11 +++++++++++ lib/player.rb | 5 +++++ lib/players/human.rb | 6 ++++++ 3 files changed, 22 insertions(+) diff --git a/lib/game.rb b/lib/game.rb index 4ed2bd45f41..856f387279d 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -1,3 +1,14 @@ class Game +attr_accessor :board, :player + + 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] ] end diff --git a/lib/player.rb b/lib/player.rb index b06ea71a56c..21e39defe09 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,3 +1,8 @@ class Player +attr_reader :token + + def initialize(token) + @token = token + end end diff --git a/lib/players/human.rb b/lib/players/human.rb index fd50c2d1576..ae2a8a1f683 100644 --- a/lib/players/human.rb +++ b/lib/players/human.rb @@ -1,5 +1,11 @@ module Players class Human < Player + #Human will give make a move by inputting an integer that corresponds with the board + #The move should return the value of the user's input + + def move(board) + user_input = gets.strip + end end end From 2deb7d91d0108b141b844d895c42d4fff6882cea Mon Sep 17 00:00:00 2001 From: Valencia Caldwell Date: Fri, 12 Mar 2021 22:47:01 +0000 Subject: [PATCH 4/9] Done. --- lib/board.rb | 3 --- lib/game.rb | 23 ++++++++++++++++++++++- lib/player.rb | 1 + 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/board.rb b/lib/board.rb index 26a1e1e5fa8..0e56c1fbd82 100644 --- a/lib/board.rb +++ b/lib/board.rb @@ -49,10 +49,7 @@ def valid_move?(position) #returns true for user input 1-9 that is NOT take end def update(position,player) #gets the users input "X" or "O" - # self.position(user_input.to_i-1) == "#{player.token}"# put in token in correct position self.cells[position.to_i-1] = player.token - # binding.pry - # self.position(user_input) = "#{player.token}" end end diff --git a/lib/game.rb b/lib/game.rb index 856f387279d..2d4467072c9 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -1,5 +1,26 @@ +require 'pry' class Game -attr_accessor :board, :player +attr_accessor :board, :player_1, :player_2 + +def initialize(player_1=Players::Human.new("X"), player_2=Players::Human.new("O"), board=Board.new) + # binding.pry + @player_1 = player_1 + @player_2 = player_2 + @board = board + end + + def current_player + # # return the player whose turn it is + # #if move is odd return player_1 token + + if self.board.turn_count.even? #if count is even then player 2 jsut moved + # binding.pry + player_1 + else + player_2 + end + end + WIN_COMBINATIONS = [ [0,1,2], diff --git a/lib/player.rb b/lib/player.rb index 21e39defe09..511909fbbdb 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,6 +1,7 @@ class Player attr_reader :token + def initialize(token) @token = token end From 5383c9b21ab9a40fc28817fef750933ddb67d550 Mon Sep 17 00:00:00 2001 From: Valencia Caldwell Date: Fri, 12 Mar 2021 22:47:40 +0000 Subject: [PATCH 5/9] Done. --- lib/game.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/game.rb b/lib/game.rb index 2d4467072c9..3b9e9efdd22 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -3,7 +3,6 @@ class Game attr_accessor :board, :player_1, :player_2 def initialize(player_1=Players::Human.new("X"), player_2=Players::Human.new("O"), board=Board.new) - # binding.pry @player_1 = player_1 @player_2 = player_2 @board = board @@ -12,9 +11,7 @@ def initialize(player_1=Players::Human.new("X"), player_2=Players::Human.new("O" def current_player # # return the player whose turn it is # #if move is odd return player_1 token - if self.board.turn_count.even? #if count is even then player 2 jsut moved - # binding.pry player_1 else player_2 From 8ed7649b8412b48cdff571a842d924965c9712fe Mon Sep 17 00:00:00 2001 From: Valencia Caldwell Date: Sat, 13 Mar 2021 17:03:42 +0000 Subject: [PATCH 6/9] Done. --- lib/board.rb | 11 ++-- lib/game.rb | 109 ++++++++++++++++++++++++++++++++++------ lib/players/computer.rb | 22 +++++++- lib/players/human.rb | 6 +-- 4 files changed, 122 insertions(+), 26 deletions(-) diff --git a/lib/board.rb b/lib/board.rb index 0e56c1fbd82..4370661819e 100644 --- a/lib/board.rb +++ b/lib/board.rb @@ -20,12 +20,11 @@ def display end def position(user_input) - self.cells[user_input.to_i-1] + @cells[user_input.to_i-1] end def full? - # cells.each(&.include?("X"||"O"):true)) - self.cells.all? do |char| + @cells.all? do |char| if char == "X" || char == "O" true else @@ -37,11 +36,11 @@ def full? def turn_count # count all of the cells that have X or O - self.cells.count("X") + self.cells.count("O")# cells.count("X") + cells.count("O")# cells.all.count(cells) # if its not full => the number of X or O + @cells.count("X") + @cells.count("O")# cells.count("X") + cells.count("O")# cells.all.count(cells) # if its not full => the number of X or O end def taken?(position) #returns true if the positon is "X" or "O" - self.cells[position.to_i-1] == "X" ||self.cells[position.to_i-1] == "O" + @cells[position.to_i-1] == "X" ||@cells[position.to_i-1] == "O" end def valid_move?(position) #returns true for user input 1-9 that is NOT take @@ -49,7 +48,7 @@ def valid_move?(position) #returns true for user input 1-9 that is NOT take end def update(position,player) #gets the users input "X" or "O" - self.cells[position.to_i-1] = player.token + @cells[position.to_i-1] = player.token end diff --git a/lib/game.rb b/lib/game.rb index 3b9e9efdd22..c5f4677d159 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -1,22 +1,99 @@ require 'pry' class Game -attr_accessor :board, :player_1, :player_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 - # # return the player whose turn it is - # #if move is odd return player_1 token - if self.board.turn_count.even? #if count is even then player 2 jsut moved + # extend Players :: Humans (wonder) + # include Players + attr_accessor :board, :player_1, :player_2, :user_input + + 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 + # # return the player whose turn it is + # #if move is odd return player_1 token + if @board.turn_count.even? #if count is even then player 2 jsut moved player_1 - else - player_2 - end - end + else + player_2 + end + end + + def won? + WIN_COMBINATIONS.each do |combination| #[0,1,2] which is board range (0-8) + if @board.cells[combination[0]] == @board.cells[combination[1]] && + @board.cells[combination[1]] == @board.cells[combination[2]] && + @board.taken?(combination[0]+1) + # user_input range (1-9) + return combination # returns the winner combination + end + end + return false #if there is not a winner + end + + def draw? # check if board is full or if anyone won + @board.full? && !self.won? ? true : false + end + + def over? # game is over if there is a draw or winner + self.draw? || self.won? + end + + def winner #check current player when there is a winning combination + if won? # self.won?.all? == "X" || "O" # check to see which player + combination = won? + @board.cells[combination[0]] # X or O + end + end + + def turn + # check to make sure its a valid move + #if valid update board + @user_input = current_player.move(@board) + if @board.valid_move?(@user_input) + @board.update(@user_input, current_player) + else puts "Please enter a number 1-9:" + @board.display + turn + end + + # @user_input = current_player.move(@board) + # if @board.valid_move?(@user_input) + # @board.update(@user_input, current_player) + # else puts "Please enter a number 1-9:" + # @board.display + # turn + # end + # binding.pry + # move = @board.valid_move?(user_input) + # if move == true + # @board.update(move,current_player) + # _pla + # self.current_player + # else + # false + # end + + # if @board.valid_move?(@board.position,self.current_player) == true + # + # @board.update(self.current_player) + # end + + end + + def play + until over? + turn + end + if winner + puts "Congratulations #{winner}!" + else draw? + puts "Cat's Game!" + end + + end + WIN_COMBINATIONS = [ diff --git a/lib/players/computer.rb b/lib/players/computer.rb index 8e53e790c45..df544fd074b 100644 --- a/lib/players/computer.rb +++ b/lib/players/computer.rb @@ -1,5 +1,25 @@ module Players class Computer < Player - + def move(board) + if board.cells[4] == " " + "5" + elsif board.cells[8] == " " + "9" + elsif board.cells[6] == " " + "7" + elsif board.cells[0] == " " + "1" + elsif board.cells[2] == " " + "3" + elsif board.cells[7] == " " + "8" + elsif board.cells[3] == " " + "4" + elsif board.cells[2] == " " + "2" + elsif board.cells[6] == " " + "6" + end + end end end diff --git a/lib/players/human.rb b/lib/players/human.rb index ae2a8a1f683..58fd52bc330 100644 --- a/lib/players/human.rb +++ b/lib/players/human.rb @@ -4,8 +4,8 @@ class Human < Player #Human will give make a move by inputting an integer that corresponds with the board #The move should return the value of the user's input - def move(board) - user_input = gets.strip - end + def move(board) + user_input = gets.strip + end end end From fb601c4976f2e682c293806724e23a69185c7813 Mon Sep 17 00:00:00 2001 From: Valencia Caldwell Date: Sat, 13 Mar 2021 18:56:13 +0000 Subject: [PATCH 7/9] Done. --- bin/tictactoe | 32 ++++++++++++++++++++++++++++++++ lib/game.rb | 4 +++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/bin/tictactoe b/bin/tictactoe index 7ddd394b19b..f382c02a36a 100755 --- a/bin/tictactoe +++ b/bin/tictactoe @@ -1,3 +1,35 @@ #!/usr/bin/env ruby require_relative '../config/environment' + +puts "Let's play Tic-Tac-Toe!" +puts "Player 1 is X, Player 2 is O" +puts "You can exit at any time by pressing !" + + +def begin +puts "How many players? (0,1,or 2)" + user_input = get.strip + + if user_input.to_i == 0 + Game.new(Players::Computer.new("X"),Players::Computer.new("O")).play + puts "That was fun, would you like to play again?" + elsif user_input.to_i == 1 # option to be first or second x||o + puts "Would you like to be first? (y/n)" + if user_input == "y" || "Y" + Game.new(Players::Human.new("X"),Players::Computer.new("O")).play + elsif user_input == "n" ||"N" + Game.new(Players::Computer.new("X"),Players::Human.new("O")).play + puts "That was fun, would you like to play again?" + + elsif user_input.to_i == 2 + puts "X, you're first!" + Game.new(Players::Human.new("X"),Players::Human.new("O")).play + puts "That was fun, would you like to play again?" + end + puts "Would you like to play again?" + #When the game is over, the CLI should prompt the user if they would like to play again and allow them to choose + #a new configuration for the game as described above. If the user doesn't want to play again, exit the program. + end + begin until gets.strip == "!" +end diff --git a/lib/game.rb b/lib/game.rb index c5f4677d159..67deba3a98e 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -82,7 +82,9 @@ def turn end - def play + def play #keeps playing until its over + #congratulates winner + #calls cat's game until over? turn end From 7d4de60e4b0d402de2b838f57b850b8adefd2ecd Mon Sep 17 00:00:00 2001 From: Valencia Caldwell Date: Sat, 13 Mar 2021 19:00:24 +0000 Subject: [PATCH 8/9] Done. --- lib/player.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/player.rb b/lib/player.rb index 511909fbbdb..6d96ddbdfb9 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,7 +1,5 @@ class Player attr_reader :token - - def initialize(token) @token = token end From 95d7029bfb0e1eca4ac5d97c9d727f9a5b07d900 Mon Sep 17 00:00:00 2001 From: Valencia Caldwell Date: Tue, 30 Mar 2021 00:35:56 +0000 Subject: [PATCH 9/9] Done. --- lib/game.rb | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/lib/game.rb b/lib/game.rb index 67deba3a98e..aa9a63d30c9 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -58,28 +58,6 @@ def turn turn end - # @user_input = current_player.move(@board) - # if @board.valid_move?(@user_input) - # @board.update(@user_input, current_player) - # else puts "Please enter a number 1-9:" - # @board.display - # turn - # end - # binding.pry - # move = @board.valid_move?(user_input) - # if move == true - # @board.update(move,current_player) - # _pla - # self.current_player - # else - # false - # end - - # if @board.valid_move?(@board.position,self.current_player) == true - # - # @board.update(self.current_player) - # end - end def play #keeps playing until its over