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

tic tac toe #31

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1ce7a5a
initial commit
matthewlee07 Oct 11, 2018
c57596f
initial commit
matthewlee07 Jan 2, 2019
fc2d186
js to ruby view
matthewlee07 Jan 2, 2019
e93de65
static board
matthewlee07 Jan 2, 2019
0c06987
.
matthewlee07 Jan 3, 2019
8943a51
seperated into MVC, clean up and test
matthewlee07 Jan 7, 2019
4e5cad9
.
matthewlee07 Jan 9, 2019
78f85ad
ask user questions. switch turns for game over... default is O. compu…
matthewlee07 Jan 10, 2019
c4b54b3
reporting correct winner
matthewlee07 Jan 10, 2019
69b13f2
.
matthewlee07 Jan 11, 2019
c5eef59
.
matthewlee07 Jan 11, 2019
7c29fd3
.
matthewlee07 Jan 11, 2019
30bdd94
try to initialize available_spaces
matthewlee07 Jan 11, 2019
b6dcb7b
successfully added winner&tie, replaced board with @board, still need…
matthewlee07 Jan 11, 2019
ae900d5
.
matthewlee07 Jan 13, 2019
2557947
odd side effect from get_available_spacess, need to simplify get_best…
matthewlee07 Jan 13, 2019
0e56cbd
moved original into model successfully
matthewlee07 Jan 13, 2019
446e930
ready to ask user questions
matthewlee07 Jan 13, 2019
88cfc3d
need to fix impossible
matthewlee07 Jan 13, 2019
ac69950
get_best_space iteration needs tweeking
matthewlee07 Jan 13, 2019
74754a7
before reorganizing get_best_move
matthewlee07 Jan 14, 2019
58faa4e
successfully ordered winner vs block win, but need to block fork stil…
matthewlee07 Jan 14, 2019
4283b9e
.
matthewlee07 Jan 14, 2019
b0ffcd7
.
matthewlee07 Jan 14, 2019
9e954f9
successfully added settings.mode and .difficulty. todo: add order and…
matthewlee07 Jan 14, 2019
496ab6a
.
matthewlee07 Jan 14, 2019
692f7d5
successfully added game.order, .symbol.. need difficulty=impossible
matthewlee07 Jan 14, 2019
0977509
no impossible difficulty, space_available? needs tweeking, next: tests
matthewlee07 Jan 14, 2019
63a7c9a
final
matthewlee07 Jan 15, 2019
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
9 changes: 3 additions & 6 deletions Jays-Game/controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require_relative 'view'
require_relative 'model'
require_relative "view"
require_relative "model"

class GameController
include GameView
Expand All @@ -10,9 +10,6 @@ def run!
Print::run_spinner
Print::title_screen

#Make todoList methods like RESTful endpoints (new/edit/update/delete)
#Think Backbone Model & View

loop do
Print::menu
case Print::fetch_user_input
Expand All @@ -25,7 +22,7 @@ def run!
when "D"
todoList.delete_todo(Print::deleted_id.to_i)
when "Q"
puts "We're done"
puts "We're done"
exit
else
Print::error_message
Expand Down
66 changes: 32 additions & 34 deletions Jays-Game/view.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
module GameView
module Print
class << self
def run_spinner
print "Loading (please wait) "
5.times { print "."; sleep 1 }
print "\n"
end

module Print

class << self
def run_spinner
print "Loading (please wait) "
5.times { print "."; sleep 1; }
print "\n"
end

def error_message
def error_message
puts "That's not a command key. Try again!"
end
def title_screen
title = <<TITLE
end

def title_screen
title = <<-EOS

********** || **********
* TODO MAGIC *
********** || **********
********** || **********
* TODO MAGIC *
********** || **********

TITLE
puts title
end
EOS
puts title
end

def menu
menu = <<EOS
menu = <<-EOS

***** Welcome *****
- (V)iew your todos
- (A)dd a todo
- (C)omplete a todo
- (D)elete a todo
- (Q)uit program
***** *****
***** Welcome *****
- (V)iew your todos
- (A)dd a todo
- (C)omplete a todo
- (D)elete a todo
- (Q)uit program
***** *****

EOS
EOS
puts menu
end

Expand All @@ -44,16 +42,16 @@ def menu
def print_list(todos)
todos.each do |todo|
if todo.completed?
puts "[X] #{todo.id} || #{todo.title} - #{todo.description}"
puts "[X] #{todo.id} || #{todo.title} - #{todo.description}"
else
puts "[_] #{todo.id} || #{todo.title} - #{todo.description}"
puts "[_] #{todo.id} || #{todo.title} - #{todo.description}"
end
end
end

def serialize_todo
{}.tap do |obj|
["\nEnter the title:", "\nEnter the description:"].each do |t|
{}.tap do |obj|
["\nEnter the title:", "\nEnter the description:"].each do |t|
if obj.empty?
obj[:title] = fetch_user_input(t)
else
Expand All @@ -73,7 +71,7 @@ def completed_id
fetch_user_input(gimme_id)
end

def fetch_user_input(question=nil)
def fetch_user_input(question = nil)
puts question if question
print "> "
gets.chomp
Expand Down
120 changes: 120 additions & 0 deletions ttt/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
require_relative "view"
require_relative "model"

class Controller
include View

def start_game
game = Game.new
Print::welcome
handle_mode_setting(game)
if game.mode != game.CvC
handle_order_setting(game)
handle_symbol_setting(game)
handle_difficulty_setting(game) if game.mode == game.HvC
end
Print::report_settings(game)
Print::render_board(game)
play(game)
end

def handle_mode_setting(game)
Print::tell_user("Choose game mode:\n[0] #{game.HvC} [1] #{game.HvH}, or [2] #{game.CvC}")
mode = gets.chomp
case mode
when "0"
game.set_mode(game.HvC)
when "1"
game.set_mode(game.HvH)
when "2"
game.set_mode(game.CvC)
else
Print::tell_user("Invalid input, please enter 0, 1 or 2")
handle_mode_setting(game)
end
end

def handle_order_setting(game)
Print::tell_user("Choose your order:\n[0] #{game.first} or [1] #{game.second}")
order = gets.chomp
case order
when "0"
game.set_order(game.first)
when "1"
game.set_order(game.second)
else
Print::tell_user("Invalid input, please enter 0 or 1")
handle_order_setting(game)
end
end

def handle_symbol_setting(game)
Print::tell_user("Choose your symbol:\n[0] #{game.X} or [1] #{game.O}")
symbol = gets.chomp
case symbol
when "0"
game.set_symbol(game.X)
when "1"
game.set_symbol(game.O)
else
Print::tell_user("Invalid input, please enter 0 or 1")
handle_symbol_setting(game)
end
end

def handle_difficulty_setting(game)
Print::tell_user("Choose computer difficulty:\n[0] #{game.easy} or [1] #{game.hard}")
difficulty = gets.chomp
case difficulty
when "0"
game.set_difficulty(game.easy)
when "1"
game.set_difficulty(game.hard)
# when "2"
# Print::tell_user("Difficulty: Impossible")
# game.set_difficulty(game.impossible)
else
Print::tell_user("Invalid input, please enter 0 or 1") #or 2
handle_difficulty_setting(game)
end
end

def play(game)
game.set_first_turn if game.mode != game.CvC
if game.mode == game.HvC && game.order == game.second
handle_computer_move(game)
game.switch_turns
end
until game.game_is_over
if game.mode != game.CvC
handle_human_move(game)
Print::report_move(game.current_turn, game.space)
Print::render_board(game)
game.switch_turns if !game.game_is_over
end
if game.mode != game.HvH
handle_computer_move(game) if !game.game_is_over
game.switch_turns if !game.game_is_over
end
end
handle_game_over(game)
end

def handle_human_move(game)
Print::acceptable_moves
space = gets.chomp
game.valid_move(space) ? game.move(space.to_i) : handle_human_move(game)
end

def handle_computer_move(game)
game.get_computer_move
Print::report_move(game.current_turn, game.space)
Print::render_board(game)
end

def handle_game_over(game)
game.winner ? Print::report_winner(game.current_turn) : Print::report_tie
end
end

Controller.new.start_game
Loading