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

Done #1441

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Done #1441

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
32 changes: 31 additions & 1 deletion bin/tictactoe
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
#!/usr/bin/env ruby

require 'pry'
require_relative '../config/environment'

game_mode = 0
until game_mode >= 1 && game_mode <= 4
puts "Please select game mode:"
puts "1. Human vs. Human"
puts "2. Human vs. Computer"
puts "3. Computer vs. Computer"
puts "4. War Games Mode"
game_mode = gets.strip.to_i
end

case game_mode
when 1
new_game = Game.new
new_game.play
when 2
new_game = Mixed_Game.new
new_game.play
when 3
new_game = Computer_Game.new
new_game.play
when 4
counter = 0
until counter == 100
new_game = War_Game.new
new_game.play
counter += 1
end
puts War_Game.all
end
46 changes: 46 additions & 0 deletions lib/board.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'pry'

class Board
attr_accessor :cells

def initialize
@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(input)
cells[input.to_i - 1]
end

def full?
turn_count == 9
end

def turn_count
cells.select{|c| c != " "}.count
end

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

def valid_move?(input)
input.to_i >= 1 && input.to_i <= 9 && !taken?(input)
end

def update(input, player)
cells[input.to_i - 1] = player.token
end

end
77 changes: 77 additions & 0 deletions lib/computer_game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require 'pry'

class Computer_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]
]

@@all_winners = {"X": 0, "O": 0}

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

end

def current_player
@board.turn_count.even?? @player_1 : @player_2
end

def won?
WIN_COMBINATIONS.detect do |combo|
@board.cells[combo[0]] == @board.cells[combo[1]] &&
@board.cells[combo[0]] == @board.cells[combo[2]] &&
@board.cells[combo[0]] != " "
end
end

def draw?
@board.full? && !won?
end

def over?
draw? || won?
end

def winner
if won?
@@all_winners[@board.cells[won?[0]]] += 1
@board.cells[won?[0]]
end
end

def turn
input = current_player.move(board).to_i
remember_player = current_player.token
if @board.valid_move?(input)
@board.cells[input - 1] = current_player.token
@board.display
puts "Computer player #{remember_player} has selected position #{input}."
sleep 3
else
turn
end
end

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

end
70 changes: 70 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'pry'

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.even?? @player_1 : @player_2
end

def won?
WIN_COMBINATIONS.detect do |combo|
@board.cells[combo[0]] == @board.cells[combo[1]] &&
@board.cells[combo[0]] == @board.cells[combo[2]] &&
@board.cells[combo[0]] != " "
end
end

def draw?
@board.full? && !won?
end

def over?
draw? || won?
end

def winner
if won?
@board.cells[won?[0]]
end
end

def turn
input = current_player.move(board).to_i
if @board.valid_move?(input)
@board.cells[input - 1] = current_player.token
@board.display
else
turn
end
end

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

end
88 changes: 88 additions & 0 deletions lib/mixed_game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require 'pry'

class Mixed_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]
]

@@all_winners = {"X": 0, "O": 0, "Draw": 0}

def initialize(player_1 = Players::Computer.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.even?? @player_1 : @player_2
end

def won?
WIN_COMBINATIONS.detect do |combo|
@board.cells[combo[0]] == @board.cells[combo[1]] &&
@board.cells[combo[0]] == @board.cells[combo[2]] &&
@board.cells[combo[0]] != " "
end
end

def draw?
@board.full? && !won?
end

def over?
draw? || won?
end

def winner
if won?
@board.cells[won?[0]]
end
end

def turn
input = current_player.move(board).to_i
remember_player = current_player.token
if @board.valid_move?(input)
@board.cells[input - 1] = current_player.token
@board.display
puts "Computer player #{remember_player} has selected position #{input}."
else
turn
end
end

def play

until over?
turn
end

if won?
puts "Congratulations #{winner}!"
if winner == "X"
@@all_winners[:X] += 1
elsif winner == "O"
@@all_winners[:O] += 1
end
elsif draw?
puts "Cat's Game!"
@@all_winners[:Draw] += 1
end

end

def self.all
@@all_winners
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 @@
require 'pry'

class Player
attr_reader :token

def initialize(token)
@token = token
end

end
57 changes: 57 additions & 0 deletions lib/players/computer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'pry'

module Players
class Computer < 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]
]

def move(board)
current = nil
turn_two = [1,3,7,9]
all_options = [1,2,3,4,5,6,7,8,9]
case board.turn_count
when 0
"5"
when 1
current = turn_two[rand(4)]
current.to_s
when 2
current = board.cells.index("O") + 1
turn_three = turn_two
turn_three.delete(current)
turn_three.delete(10 - current)
current = turn_three[rand(2)]
current.to_s
when 3..8
close_to_winning = WIN_COMBINATIONS.detect do |combo|
((board.cells[combo[0]] == board.cells[combo[1]] && board.cells[combo[2]] == " ") ||
(board.cells[combo[0]] == board.cells[combo[2]] && board.cells[combo[1]] == " ") ||
(board.cells[combo[1]] == board.cells[combo[2]] && board.cells[combo[0]] == " ")) &&
!(board.cells[combo[0]] == board.cells[combo[1]] && board.cells[combo[0]] == board.cells[combo[2]])
end

if close_to_winning
win_blocker = close_to_winning.detect{|cell| board.cells[cell] == " "}
next_move = win_blocker + 1
elsif !close_to_winning
all_options = []
board.cells.each_with_index do |cell, index|
all_options << (index + 1) if cell == " "
end

next_move = all_options[rand(all_options.length)]
end
next_move.to_s
end
end
end
end
Loading