bashrw_ttt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. data/bin/play.rb +10 -0
  2. data/lib/ttt/ai.rb +33 -0
  3. data/lib/ttt/ai_easy.rb +17 -0
  4. data/lib/ttt/ai_hard.rb +21 -0
  5. data/lib/ttt/ai_medium.rb +21 -0
  6. data/lib/ttt/ai_medium_back.rb +58 -0
  7. data/lib/ttt/board.rb +37 -0
  8. data/lib/ttt/config_helper.rb +53 -0
  9. data/lib/ttt/config_options.rb +27 -0
  10. data/lib/ttt/context.rb +214 -0
  11. data/lib/ttt/four_by_four.rb +62 -0
  12. data/lib/ttt/game.rb +94 -0
  13. data/lib/ttt/game_history.rb +37 -0
  14. data/lib/ttt/game_interactor.rb +126 -0
  15. data/lib/ttt/human.rb +12 -0
  16. data/lib/ttt/interfaces/cli/lib/cli/board_selection.rb +34 -0
  17. data/lib/ttt/interfaces/cli/lib/cli/cli_game.rb +186 -0
  18. data/lib/ttt/interfaces/cli/lib/cli/cli_presenter.rb +216 -0
  19. data/lib/ttt/interfaces/cli/lib/cli/clio.rb +17 -0
  20. data/lib/ttt/interfaces/cli/lib/cli/play_again.rb +38 -0
  21. data/lib/ttt/interfaces/cli/lib/cli/player_selection.rb +43 -0
  22. data/lib/ttt/interfaces/cli/lib/cli/selection.rb +8 -0
  23. data/lib/ttt/interfaces/cli/lib/cli/walk_history.rb +48 -0
  24. data/lib/ttt/interfaces/cli/spec/cli/board_selection_spec.rb +29 -0
  25. data/lib/ttt/interfaces/cli/spec/cli/cli_game_spec.rb +376 -0
  26. data/lib/ttt/interfaces/cli/spec/cli/cli_presenter_spec.rb +147 -0
  27. data/lib/ttt/interfaces/cli/spec/cli/play_again_spec.rb +39 -0
  28. data/lib/ttt/interfaces/cli/spec/cli/player_selection_spec.rb +62 -0
  29. data/lib/ttt/interfaces/cli/spec/cli/walk_history_spec.rb +115 -0
  30. data/lib/ttt/interfaces/cli/spec/spec_helper.rb +14 -0
  31. data/lib/ttt/interfaces/limelight/four_by_four/props.rb +1 -0
  32. data/lib/ttt/interfaces/limelight/four_by_four/styles.rb +74 -0
  33. data/lib/ttt/interfaces/limelight/four_by_four_finished/props.rb +1 -0
  34. data/lib/ttt/interfaces/limelight/four_by_four_finished/styles.rb +74 -0
  35. data/lib/ttt/interfaces/limelight/game_list/players/load_button.rb +10 -0
  36. data/lib/ttt/interfaces/limelight/game_list/props.rb +17 -0
  37. data/lib/ttt/interfaces/limelight/game_list/styles.rb +8 -0
  38. data/lib/ttt/interfaces/limelight/main_menu/players/exit_ttt.rb +3 -0
  39. data/lib/ttt/interfaces/limelight/main_menu/players/load_game.rb +3 -0
  40. data/lib/ttt/interfaces/limelight/main_menu/players/new_game.rb +3 -0
  41. data/lib/ttt/interfaces/limelight/main_menu/props.rb +17 -0
  42. data/lib/ttt/interfaces/limelight/new_game/players/board_type.rb +1 -0
  43. data/lib/ttt/interfaces/limelight/new_game/players/new_game.rb +5 -0
  44. data/lib/ttt/interfaces/limelight/new_game/players/player_type.rb +1 -0
  45. data/lib/ttt/interfaces/limelight/new_game/players/setup_button.rb +21 -0
  46. data/lib/ttt/interfaces/limelight/new_game/props.rb +21 -0
  47. data/lib/ttt/interfaces/limelight/new_game/styles.rb +23 -0
  48. data/lib/ttt/interfaces/limelight/partials/board_partial.rb +14 -0
  49. data/lib/ttt/interfaces/limelight/partials/menu_button_partial.rb +5 -0
  50. data/lib/ttt/interfaces/limelight/partials/move_history_partial.rb +6 -0
  51. data/lib/ttt/interfaces/limelight/partials/title_history_partial.rb +10 -0
  52. data/lib/ttt/interfaces/limelight/players/board.rb +3 -0
  53. data/lib/ttt/interfaces/limelight/players/game.rb +4 -0
  54. data/lib/ttt/interfaces/limelight/players/generic_move_history.rb +5 -0
  55. data/lib/ttt/interfaces/limelight/players/generic_player.rb +8 -0
  56. data/lib/ttt/interfaces/limelight/players/left_button.rb +3 -0
  57. data/lib/ttt/interfaces/limelight/players/main_menu.rb +3 -0
  58. data/lib/ttt/interfaces/limelight/players/right_button.rb +3 -0
  59. data/lib/ttt/interfaces/limelight/playscripts/game_playscript.rb +169 -0
  60. data/lib/ttt/interfaces/limelight/production.rb +14 -0
  61. data/lib/ttt/interfaces/limelight/spec/game_playscript_spec.rb +218 -0
  62. data/lib/ttt/interfaces/limelight/spec/main_menu/main_menu_spec.rb +38 -0
  63. data/lib/ttt/interfaces/limelight/spec/new_game/new_game_spec.rb +45 -0
  64. data/lib/ttt/interfaces/limelight/spec/spec_helper.rb +8 -0
  65. data/lib/ttt/interfaces/limelight/stages.rb +6 -0
  66. data/lib/ttt/interfaces/limelight/styles.rb +119 -0
  67. data/lib/ttt/interfaces/limelight/three_by_three/props.rb +1 -0
  68. data/lib/ttt/interfaces/limelight/three_by_three/styles.rb +39 -0
  69. data/lib/ttt/interfaces/limelight/three_by_three_by_three/props.rb +1 -0
  70. data/lib/ttt/interfaces/limelight/three_by_three_by_three/styles.rb +119 -0
  71. data/lib/ttt/interfaces/limelight/three_by_three_by_three_finished/props.rb +1 -0
  72. data/lib/ttt/interfaces/limelight/three_by_three_by_three_finished/styles.rb +119 -0
  73. data/lib/ttt/interfaces/limelight/three_by_three_finished/props.rb +1 -0
  74. data/lib/ttt/interfaces/limelight/three_by_three_finished/styles.rb +39 -0
  75. data/lib/ttt/interfaces/rails/app/controllers/application_controller.rb +3 -0
  76. data/lib/ttt/interfaces/rails/app/controllers/ttt_games_controller.rb +86 -0
  77. data/lib/ttt/interfaces/rails/app/helpers/application_helper.rb +2 -0
  78. data/lib/ttt/interfaces/rails/app/models/ttt_game.rb +34 -0
  79. data/lib/ttt/interfaces/rails/app/presenters/web_game_presenter.rb +159 -0
  80. data/lib/ttt/interfaces/rails/autotest/discover.rb +1 -0
  81. data/lib/ttt/interfaces/rails/config/application.rb +67 -0
  82. data/lib/ttt/interfaces/rails/config/boot.rb +6 -0
  83. data/lib/ttt/interfaces/rails/config/environment.rb +5 -0
  84. data/lib/ttt/interfaces/rails/config/environments/development.rb +37 -0
  85. data/lib/ttt/interfaces/rails/config/environments/production.rb +67 -0
  86. data/lib/ttt/interfaces/rails/config/environments/test.rb +37 -0
  87. data/lib/ttt/interfaces/rails/config/initializers/backtrace_silencers.rb +7 -0
  88. data/lib/ttt/interfaces/rails/config/initializers/inflections.rb +15 -0
  89. data/lib/ttt/interfaces/rails/config/initializers/mime_types.rb +5 -0
  90. data/lib/ttt/interfaces/rails/config/initializers/secret_token.rb +7 -0
  91. data/lib/ttt/interfaces/rails/config/initializers/session_store.rb +8 -0
  92. data/lib/ttt/interfaces/rails/config/initializers/wrap_parameters.rb +14 -0
  93. data/lib/ttt/interfaces/rails/config/routes.rb +49 -0
  94. data/lib/ttt/interfaces/rails/db/schema.rb +28 -0
  95. data/lib/ttt/interfaces/rails/db/seeds.rb +7 -0
  96. data/lib/ttt/interfaces/rails/spec/controllers/ttt_games_controller_spec.rb +211 -0
  97. data/lib/ttt/interfaces/rails/spec/models/ttt_game_spec.rb +76 -0
  98. data/lib/ttt/interfaces/rails/spec/presenters/web_game_presenter_spec.rb +52 -0
  99. data/lib/ttt/interfaces/rails/spec/spec_helper.rb +13 -0
  100. data/lib/ttt/interfaces/web_interface/html_generator.rb +76 -0
  101. data/lib/ttt/interfaces/web_interface/web_game_presenter.rb +0 -0
  102. data/lib/ttt/minimax.rb +46 -0
  103. data/lib/ttt/move_history.rb +10 -0
  104. data/lib/ttt/move_traverser.rb +49 -0
  105. data/lib/ttt/player.rb +15 -0
  106. data/lib/ttt/riak_db.rb +53 -0
  107. data/lib/ttt/setup.rb +59 -0
  108. data/lib/ttt/three_by_three.rb +35 -0
  109. data/lib/ttt/three_by_three_by_three.rb +37 -0
  110. data/spec/spec_helper.rb +22 -0
  111. data/spec/ttt/ai_easy_spec.rb +15 -0
  112. data/spec/ttt/ai_hard_spec.rb +76 -0
  113. data/spec/ttt/ai_medium_spec.rb +65 -0
  114. data/spec/ttt/ai_spec.rb +50 -0
  115. data/spec/ttt/board_spec.rb +96 -0
  116. data/spec/ttt/config_helper_spec.rb +54 -0
  117. data/spec/ttt/context_spec.rb +398 -0
  118. data/spec/ttt/four_by_four_spec.rb +112 -0
  119. data/spec/ttt/game_history_spec.rb +41 -0
  120. data/spec/ttt/game_interactor_spec.rb +197 -0
  121. data/spec/ttt/game_spec.rb +246 -0
  122. data/spec/ttt/human_spec.rb +18 -0
  123. data/spec/ttt/move_history_spec.rb +13 -0
  124. data/spec/ttt/move_traverser_spec.rb +75 -0
  125. data/spec/ttt/player_spec.rb +13 -0
  126. data/spec/ttt/riak_db_spec.rb +64 -0
  127. data/spec/ttt/setup_spec.rb +63 -0
  128. data/spec/ttt/three_by_three_by_three_spec.rb +130 -0
  129. data/spec/ttt/three_by_three_spec.rb +110 -0
  130. metadata +238 -0
@@ -0,0 +1 @@
1
+ __install 'partials/move_history_partial.rb', :text => "4x4", :n => 16, :c => 1
@@ -0,0 +1,74 @@
1
+ squares {
2
+ width 415
3
+ horizontal_alignment :center
4
+ vertical_alignment :center
5
+ }
6
+
7
+ square0 {
8
+ extends :square
9
+ extends :left_of_square
10
+ extends :above_square
11
+ }
12
+
13
+ square1 {
14
+ extends :square0
15
+ }
16
+
17
+ square2 {
18
+ extends :square0
19
+ }
20
+
21
+ square3 {
22
+ extends :above_square
23
+ }
24
+
25
+ square4 {
26
+ extends :square0
27
+ }
28
+
29
+ square5 {
30
+ extends :square0
31
+ }
32
+
33
+ square6 {
34
+ extends :square0
35
+ }
36
+
37
+ square7 {
38
+ extends :above_square
39
+ }
40
+
41
+ square8 {
42
+ extends :square0
43
+ }
44
+
45
+ square9 {
46
+ extends :square0
47
+ }
48
+
49
+ square10 {
50
+ extends :square0
51
+ }
52
+
53
+ square11 {
54
+ extends :above_square
55
+ }
56
+
57
+ square12 {
58
+ extends :square
59
+ extends :left_of_square
60
+ }
61
+
62
+ square13 {
63
+ extends :square
64
+ extends :left_of_square
65
+ }
66
+
67
+ square14 {
68
+ extends :square
69
+ extends :left_of_square
70
+ }
71
+
72
+ square15 {
73
+ extends :square
74
+ }
@@ -0,0 +1,10 @@
1
+ on_button_pushed do
2
+ context = TTT::Context.instance
3
+ context.setup = TTT::Setup
4
+ production.context = context
5
+ production.game_id = scene.find("game_list").text
6
+ if production.context.finished?(production.game_id)
7
+ scene_to_load = production.context.which_board(production.game_id) + "_finished"
8
+ production.open_scene(scene_to_load)
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ ttt do
2
+ title :text => "Game Loader"
3
+
4
+ row
5
+
6
+ menu_button_row do
7
+ game_list :text => "Select a game:"
8
+ game_list_dropwdown :players => "drop_down", :id => "game_list", :choices => (context = TTT::Context.instance; context.setup = TTT::Setup; context.game_list)
9
+ end
10
+
11
+ row
12
+
13
+ setup_button_row do
14
+ load_button :players => "button", :id => "load_button", :text => "Load Game"
15
+ main_menu :players => "button", :id => "main_menu", :text => "Main Menu"
16
+ end
17
+ end
@@ -0,0 +1,8 @@
1
+ game_list {
2
+ text_color :white
3
+ font_size 20
4
+ right_margin 20
5
+ }
6
+
7
+ game_list_dropdown {
8
+ }
@@ -0,0 +1,3 @@
1
+ on_button_pushed do
2
+ production.close
3
+ end
@@ -0,0 +1,3 @@
1
+ on_button_pushed do
2
+ production.open_scene("game_list")
3
+ end
@@ -0,0 +1,3 @@
1
+ on_button_pushed do
2
+ production.open_scene("new_game")
3
+ end
@@ -0,0 +1,17 @@
1
+ ttt do
2
+ title :text => "Main Menu"
3
+
4
+ row
5
+
6
+ menu_button_row do
7
+ new_game :players => "button", :text => "New Game", :id => "new_game"
8
+ end
9
+
10
+ menu_button_row do
11
+ load_game :players => "button", :text => "Load Game", :id => "load_game"
12
+ end
13
+
14
+ menu_button_row do
15
+ exit_ttt :players => "button", :text => "Exit", :id => "exit"
16
+ end
17
+ end
@@ -0,0 +1 @@
1
+ backstage_reader :drop_down
@@ -0,0 +1,5 @@
1
+ on_scene_opened do
2
+ context = TTT::Context.instance
3
+ context.setup = TTT::Setup
4
+ production.context = context
5
+ end
@@ -0,0 +1 @@
1
+ backstage_reader :drop_down
@@ -0,0 +1,21 @@
1
+ on_button_pushed do
2
+ production.game_id = production.context.create_game(player1, player2, board)
3
+ production.open_scene(production.context.which_board(production.game_id))
4
+ end
5
+
6
+ private
7
+ def player1
8
+ @player1 ||= player(1)
9
+ end
10
+
11
+ def player2
12
+ @player2 ||= player(2)
13
+ end
14
+
15
+ def player(num)
16
+ scene.find("player#{num}").drop_down.value
17
+ end
18
+
19
+ def board
20
+ scene.find("board").drop_down.value
21
+ end
@@ -0,0 +1,21 @@
1
+ ttt do
2
+ title :text => "Welcome to TTT!"
3
+
4
+ row
5
+
6
+ player1 :text => "Player 1"
7
+ player_type :players => "drop_down", :id => "player1", :choices => TTT::Setup.new.players
8
+
9
+ player2 :text => "Player 2"
10
+ player_type :players => "drop_down", :id => "player2", :choices => TTT::Setup.new.players
11
+
12
+ boards :text => "Boards"
13
+ board_type :players => "drop_down", :id => "board", :choices => TTT::Setup.new.boards
14
+
15
+ row
16
+
17
+ setup_button_row do
18
+ setup_button :players => "button", :id => "setup_button", :text => "Start Game"
19
+ main_menu :players => "button", :id => "main_menu", :text => "Main Menu"
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ player1 {
2
+ extends :setup_list
3
+ }
4
+
5
+ player1_type {
6
+ extends :type_list
7
+ }
8
+
9
+ player2 {
10
+ extends :setup_list
11
+ }
12
+
13
+ player2_type {
14
+ extends :type_list
15
+ }
16
+
17
+ boards {
18
+ extends :setup_list
19
+ }
20
+
21
+ board {
22
+ extends :type_list
23
+ }
@@ -0,0 +1,14 @@
1
+ board_partial do
2
+ game_board_row do
3
+ @c.times do |c|
4
+ squares do
5
+ @n.times do |n|
6
+ square :id => "square#{n + (c*9)}", :players => "board", :styles => "square#{n}" do
7
+ square_text :id => "square_text_#{n + (c*9)}", :text => ""
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ __install "partials/menu_button_partial.rb"
14
+ end
@@ -0,0 +1,5 @@
1
+ menu_button_partial do
2
+ menu_button_row :id => "menu_button_row" do
3
+ main_menu :text => "Main Menu", :id => "main_menu", :players => "button"
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ __ :players => "generic_move_history"
2
+ move_button_row do
3
+ left_button :players => "button", :id => "left_button", :text => "Back"
4
+ right_button :players => "button", :id => "right_button", :text => "Next"
5
+ end
6
+ __install 'partials/title_history_partial.rb'
@@ -0,0 +1,10 @@
1
+ __ :players => "generic_player"
2
+ title_history_partial do
3
+ __ :name => "ttt"
4
+ title :text => "#{@text} Tic Tac Toe"
5
+
6
+ player_turn :text => "Player 1's turn", :id => "player_turn"
7
+
8
+ move_history :text => "Move History", :id => "move_history"
9
+ __install "partials/board_partial.rb"
10
+ end
@@ -0,0 +1,3 @@
1
+ on_mouse_clicked do
2
+ production.limelight_game.player_move(self)
3
+ end
@@ -0,0 +1,4 @@
1
+ on_scene_opened do
2
+ production.limelight_game = GamePlayscript.new(:game => production.game, :context => scene)
3
+ production.limelight_game.process_ai_move
4
+ end
@@ -0,0 +1,5 @@
1
+ on_scene_opened do
2
+ production.limelight_game = GamePlayscript.new(:game_id => production.game_id, :context => scene, :production => production)
3
+ production.limelight_game.initialize_history
4
+ production.limelight_game.display_results
5
+ end
@@ -0,0 +1,8 @@
1
+ on_scene_opened do
2
+ production.limelight_game = GamePlayscript.new(:game_id => production.game_id, :context => scene, :production => production)
3
+ if production.context.finished?(production.game_id)
4
+ production.limelight_game.display_results
5
+ else
6
+ production.limelight_game.process_ai_move
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ on_button_pushed do
2
+ production.limelight_game.view_history(-1)
3
+ end
@@ -0,0 +1,3 @@
1
+ on_button_pushed do
2
+ production.open_scene("main_menu")
3
+ end
@@ -0,0 +1,3 @@
1
+ on_button_pushed do
2
+ production.limelight_game.view_history(1)
3
+ end
@@ -0,0 +1,169 @@
1
+ class GamePlayscript
2
+ attr_accessor :context, :game_id, :production
3
+ def initialize(options)
4
+ self.context = options.fetch(:context)
5
+ self.game_id = options.fetch(:game_id)
6
+ self.production = options.fetch(:production)
7
+ self
8
+ end
9
+
10
+ def initialize_history
11
+ production.context.initialize_history(game_id)
12
+ end
13
+
14
+ def view_history(index_diff)
15
+ game = production.context.get_game(game_id)
16
+ game.adjust_move_index(index_diff)
17
+ production.context.save_game(game_id, game)
18
+ process_game_history
19
+ end
20
+
21
+ def process_game_history
22
+ history_board = production.context.get_history_board(game_id)
23
+ update_context_board(history_board)
24
+ end
25
+
26
+ def display_results
27
+ update_context_board
28
+ display_move_history
29
+ game_over_prompt
30
+ end
31
+
32
+ def game_move_index
33
+ context.get_history(game_id).length
34
+ end
35
+
36
+ def current_move_index
37
+ production.move_index
38
+ end
39
+
40
+ def generate_history_board
41
+ game = context.get_game(game_id)
42
+ game.history.move_traverser.adjust_move_index(production.move_index)
43
+ game.history.move_traverser.history_board_builder(game.board, production.move_index)
44
+ end
45
+
46
+ def player_move(cell)
47
+ @move = cell.id
48
+ return unless valid_move? && not_finished?
49
+ mark_move
50
+ update_context_board
51
+ update_move_history
52
+ prompt_next_player
53
+ display_results if finished?
54
+ process_ai_move unless finished?
55
+ end
56
+
57
+ def process_ai_move
58
+ return unless next_move_is_ai?
59
+ ai_move
60
+ update_context_board
61
+ update_move_history
62
+ prompt_next_player
63
+ display_results if finished?
64
+ if next_move_is_ai? && not_finished?
65
+ sleep 1
66
+ process_ai_move
67
+ end
68
+ end
69
+
70
+ def which_board?
71
+ production.context.which_board(@game_id)
72
+ end
73
+
74
+ def set_move(cell)
75
+ @move = cell.id
76
+ end
77
+
78
+ def move
79
+ cell_location.to_i
80
+ end
81
+
82
+ def cell_location
83
+ return @move[-2..-1] if @move[-2..-1] !~ (/[a-z]/i)
84
+ @move[-1..-1]
85
+ end
86
+
87
+ def game_over_prompt
88
+ if winner?
89
+ @context.find_by_id("player_turn").text = "#{winner} is the winner!"
90
+ else
91
+ @context.find_by_id("player_turn").text = "It's a draw"
92
+ end
93
+ end
94
+
95
+ def update_context_board(new_board = board)
96
+ new_board.each_with_index do |square, index|
97
+ @context.find_by_id("square_text_#{index}").text = square
98
+ end
99
+ end
100
+
101
+ def update_move_history
102
+ history = production.context.get_history(@game_id)
103
+ size = history.size
104
+ value = history[size - 1]
105
+ @context.find_by_id("move_history").text += "\n#{value.side} #{value.move}"
106
+ end
107
+
108
+ def display_move_history
109
+ history = production.context.get_history(@game_id)
110
+ @context.find_by_id("move_history").text = "Move History"
111
+ result_string = ''
112
+ history.each do |value|
113
+ @context.find_by_id("move_history").text += "\n#{value.side} #{value.move}"
114
+ end
115
+ end
116
+
117
+ def prompt_next_player
118
+ @context.find_by_id("player_turn").text = "#{which_current_player?}'s turn"
119
+ end
120
+
121
+ def ai_move
122
+ production.context.ai_move(@game_id)
123
+ end
124
+
125
+ def next_move_is_ai?
126
+ production.context.ai_move?(@game_id)
127
+ end
128
+
129
+ def mark_move
130
+ player = production.context.which_current_player?(@game_id)
131
+ production.context.update_game(@game_id, move, player_side(player))
132
+ end
133
+
134
+ def player_side(player_num)
135
+ if player_num == "Player 1"
136
+ "x"
137
+ else
138
+ "o"
139
+ end
140
+ end
141
+
142
+ def board
143
+ production.context.board(@game_id)
144
+ end
145
+
146
+ def which_current_player?
147
+ production.context.which_current_player?(@game_id)
148
+ end
149
+
150
+ def finished?
151
+ production.context.finished?(@game_id)
152
+ end
153
+
154
+ def not_finished?
155
+ !finished?
156
+ end
157
+
158
+ def winner?
159
+ production.context.winner?(@game_id)
160
+ end
161
+
162
+ def winner
163
+ production.context.winner(@game_id)
164
+ end
165
+
166
+ def valid_move?
167
+ production.context.valid_move?(@game_id, move)
168
+ end
169
+ end