From e93e520593d3a5cd7409232a0b982ed3c329a3b9 Mon Sep 17 00:00:00 2001 From: Erica Murdock-Waters Date: Wed, 15 Apr 2020 22:34:39 +0000 Subject: [PATCH 1/3] Game.start now working for 0, 1, and 2 player games --- bin/tictactoe | 4 +- lib/board.rb | 65 +++++++++++++++++++++++++++++ lib/game.rb | 92 +++++++++++++++++++++++++++++++++++++++++ lib/player.rb | 8 ++++ lib/players/computer.rb | 8 ++++ lib/players/human.rb | 10 +++++ 6 files changed, 186 insertions(+), 1 deletion(-) diff --git a/bin/tictactoe b/bin/tictactoe index 7ddd394b19b..70c389e9c83 100755 --- a/bin/tictactoe +++ b/bin/tictactoe @@ -1,3 +1,5 @@ #!/usr/bin/env ruby -require_relative '../config/environment' +require_relative '../config/environment' # Loads environment and code + +Game.start.play diff --git a/lib/board.rb b/lib/board.rb index e69de29bb2d..dfe83f0929f 100644 --- a/lib/board.rb +++ b/lib/board.rb @@ -0,0 +1,65 @@ +require 'pry' +class Board + attr_accessor :cells + + def initialize + @cells = [" ", " ", " ", " ", " ", " ", " ", " ", " "] + # Commented out 8 & 9 and replaced with line 6. Not sure I need 8 & 9 anymore. + #@cells = [] + #self.reset! + end + + def reset! #resets cells of the board to a 9 element array of " " + @cells.replace([" ", " ", " ", " ", " ", " ", " ", " ", " "]) #refactorMe + 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(user_input) # takes in user input and returns the value of the board cell + self.cells[(user_input.to_i) - 1] + end + + def full? + !self.cells.include?(" ") + end + + def turn_count + count = 0 + self.cells.each do |cell| + if cell != " " + count += 1 + end + end + count + end + + def taken?(position) + if @cells[position.to_i - 1] == "X" + true + elsif @cells[position.to_i - 1] == "O" + true + else + false + end + end + + def valid_move?(user_input) + if taken?(user_input) + false + elsif !(1..9).include?(user_input.to_i) + false + else + true + end + end + + def update(position, player) + @cells[position.to_i-1] = "#{player.token}" + end +end diff --git a/lib/game.rb b/lib/game.rb index e69de29bb2d..3d93196bbbf 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -0,0 +1,92 @@ +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) + @player_1 = player_1 + @player_2 = player_2 + @board = board + end + + def current_player + (board.turn_count % 2) == 0 ? self.player_1 : self.player_2 + end + + def won? + WIN_COMBINATIONS.detect do |win_combo| + position_1 = board.cells[win_combo[0]] + position_2 = board.cells[win_combo[1]] + position_3 = board.cells[win_combo[2]] + + position_1 == "X" && position_2 == "X" && position_3 == "X" || position_1 == "O" && position_2 == "O" && position_3 == "O" + end + end + + def draw? + !won? && !board.cells.include?(" ") + end + + def over? + won? || draw? + end + + def winner + if winner = won? + board.cells[winner[0]] + end + end + + def turn + board.display + input = current_player.move(board) + if board.valid_move?(input) + board.update(input, current_player) + else + puts "Invalid move. Please try again." + turn + end + end + + def play + until over? + turn + end + + if won? + puts "Congratulations #{winner}!" + else + puts "Cat's Game!" + end + end + + def self.start + puts "Welcome to Tic Tac Toe!" + puts "How many players will be playing? Enter 0, 1, or 2" + number_of_players = gets.chomp + if number_of_players.to_i == 0 + Game.new(player_1 = Players::Computer.new("X"), player_2 = Players::Computer.new("O"), board = Board.new) + elsif number_of_players.to_i == 1 + puts "Would you like to be X and go first or be O and go second? Enter X or O" + player_choice = gets.chomp + if player_choice.capitalize == "X" + Game.new(player_1 = Players::Human.new("X"), player_2 = Players::Computer.new("O"), board = Board.new) + else + Game.new(player_1 = Players::Computer.new("X"), player_2 = Players::Human.new("O"), board = Board.new) + end + else + Game.new + 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..e30f0d3dd61 100644 --- a/lib/players/computer.rb +++ b/lib/players/computer.rb @@ -0,0 +1,8 @@ +module Players + class Computer < Player + def move(board) + rand(1..9).to_s + end + end + +end diff --git a/lib/players/human.rb b/lib/players/human.rb index e69de29bb2d..28f16ff8619 100644 --- a/lib/players/human.rb +++ b/lib/players/human.rb @@ -0,0 +1,10 @@ +module Players + class Human < Player + + def move(board) + puts "Enter your move 1 - 9" + move = gets.chomp + end + end + +end From b8da8e1d50a7543e7495e7d256f4ccd84a97d0dc Mon Sep 17 00:00:00 2001 From: Erica Murdock-Waters Date: Wed, 15 Apr 2020 22:38:15 +0000 Subject: [PATCH 2/3] replaced strings with player_1.token or player_2.token in Game #won? method --- lib/game.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/game.rb b/lib/game.rb index 3d93196bbbf..dccdf98aa1b 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -28,7 +28,7 @@ def won? position_2 = board.cells[win_combo[1]] position_3 = board.cells[win_combo[2]] - position_1 == "X" && position_2 == "X" && position_3 == "X" || position_1 == "O" && position_2 == "O" && position_3 == "O" + position_1 == player_1.token && position_2 == player_1.token && position_3 == player_1.token || position_1 == player_2.token && position_2 == player_2.token && position_3 == player_2.token end end From bcc52f7d5ae5e7e941170b129170728922f4c7e1 Mon Sep 17 00:00:00 2001 From: Erica Murdock-Waters Date: Wed, 15 Apr 2020 22:52:18 +0000 Subject: [PATCH 3/3] added layer of strategy to Players::Computer #move method --- lib/players/computer.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/players/computer.rb b/lib/players/computer.rb index e30f0d3dd61..010da74815f 100644 --- a/lib/players/computer.rb +++ b/lib/players/computer.rb @@ -1,7 +1,14 @@ module Players class Computer < Player + def move(board) - rand(1..9).to_s + puts "Computer select move 1 - 9" + if !board.taken?(5) + "5" + else + rand(1..9).to_s + end + end end