Class: GamesParadise::VierGewinnt

Inherits:
Base
  • Object
show all
Includes:
BaseModule, GUI::SharedCode
Defined in:
lib/games_paradise/vier_gewinnt/vier_gewinnt.rb

Overview

GamesParadise::VierGewinnt

Constant Summary

Constants included from GUI::SharedCode

GUI::SharedCode::HEIGHT, GUI::SharedCode::NAMESPACE, GUI::SharedCode::N_ELEMENTS_IN_THE_X_AXIS, GUI::SharedCode::N_ELEMENTS_IN_THE_Y_AXIS, GUI::SharedCode::RED, GUI::SharedCode::TITLE, GUI::SharedCode::UNICODE_BIG_DOT, GUI::SharedCode::WIDTH, GUI::SharedCode::YELLOW

Constants inherited from Base

Base::CONTROL_C_CODE, Base::N

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseModule

#cliner, #commandline_arguments?, #first_argument?, #infer_the_namespace, #namespace?, #rename_file, #reset_the_internal_hash, #return_pwd, #set_commandline_arguments

Methods inherited from Base

#cat, #commandline_arguments?, #efancy, #eparse, #first_argument?, #forestgreen, #gold, #lightblue, #lightgreen, #mediumorchid, #mediumslateblue, #opnn, #peru, #register_sigint, #rev, #royalblue, #set_commandline_arguments, #sfile, #steelblue, #teal, #tomato, #yellow

Constructor Details

#initialize(commandline_arguments = nil, run_already = true, &block) ⇒ VierGewinnt

#

initialize

#


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 32

def initialize(
    commandline_arguments = nil,
    run_already           = true,
    &block
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # == :do_not_run_yet
    # ===================================================================== #
    when :do_not_run_yet
      run_already = false
    end
  end
  run if run_already
end

Class Method Details

.[](i = ARGV) ⇒ Object

#

GamesParadise::VierGewinnt[]

#


672
673
674
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 672

def self.[](i = ARGV)
  new(i)
end

Instance Method Details

#append_red_onto_this_slot(this_slot = 1, starting_y_position = N_ELEMENTS_IN_THE_Y_AXIS-1) ⇒ Object

#

append_red_onto_this_slot

We count via slots starting at 1.

#


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 139

def append_red_onto_this_slot(
    this_slot           = 1,
    starting_y_position = N_ELEMENTS_IN_THE_Y_AXIS-1
  )
  this_slot = this_slot.to_i
  if is_this_slot_already_full?(this_slot)
    e 'Can not place anything on slot number '+this_slot.to_s+' as'
    e 'it is already full.'
    return
  end
  # ======================================================================= #
  # We must determine whether we can put it onto that slot or not.
  # ======================================================================= #
  old_value = @array[starting_y_position][this_slot - 1]
  case old_value
  when 0
    @array[starting_y_position][this_slot - 1] = RED
    @last_position = [starting_y_position, this_slot - 1]
  else
    # already occupied so recursive call here.
    unless starting_y_position < 0
      starting_y_position -= 1
      append_red_onto_this_slot(this_slot, starting_y_position)
    end
  end
end

#append_yellow_onto_this_slot(this_slot = 1, starting_y_position = N_ELEMENTS_IN_THE_Y_AXIS-1) ⇒ Object

#

append_yellow_onto_this_slot

We count via slots starting at 1.

#


358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 358

def append_yellow_onto_this_slot(
    this_slot           = 1,
    starting_y_position = N_ELEMENTS_IN_THE_Y_AXIS-1
  )
  this_slot = this_slot.to_i
  if is_this_slot_already_full?(this_slot)
    e 'Can not place anything on slot number '+this_slot.to_s+' as'
    e 'it is already full.'
    return
  end
  # ======================================================================= #
  # We must determine whether we can put it onto that slot or not.
  # ======================================================================= #
  old_value = @array[starting_y_position][this_slot - 1]
  case old_value
  when 0
    @array[starting_y_position][this_slot - 1] = YELLOW
    @last_position = [starting_y_position, this_slot - 1]
  else
    starting_y_position -= 1
    append_yellow_onto_this_slot(this_slot, starting_y_position)
  end
end

#are_all_slots_completely_occupied?Boolean

#

are_all_slots_completely_occupied?

#

Returns:

  • (Boolean)


306
307
308
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 306

def are_all_slots_completely_occupied?
  !@array.flatten.any? {|entry| entry == 0 }
end

#array?Boolean Also known as: game_map?

#

array?

#

Returns:

  • (Boolean)


119
120
121
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 119

def array?
  @array
end

#check_for_win_conditions(&block) ⇒ Object Also known as: check_for_win_condition

#

check_for_win_conditions (win tag)

#


477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 477

def check_for_win_conditions(
    &block
  )
  if are_all_slots_completely_occupied?
    @correct_solution = :all_slots_are_completely_occupied
  end
  return if is_the_game_over?
  # ======================================================================= #
  # (1) First, we check from top-to-bottom.
  # ======================================================================= #
  n_red    = 0
  n_yellow = 0
  for x_coordinate in 0..(N_ELEMENTS_IN_THE_X_AXIS - 1)
    N_ELEMENTS_IN_THE_Y_AXIS.times {|run_number|
      case @array[run_number][x_coordinate]
      when YELLOW
        n_yellow += 1
        n_red = 0
        if n_yellow == 4
          yellow_has_won_the_game(&block)
          identify_the_correct_solution(
            [
              [run_number-3, x_coordinate],
              [run_number-2, x_coordinate],
              [run_number-1, x_coordinate],
              [run_number,   x_coordinate]
            ]
          )
        end
      when RED
        n_red += 1
        n_yellow = 0
        if n_red == 4
          red_has_won_the_game(&block)
          identify_the_correct_solution(
            [
              [run_number-3, x_coordinate],
              [run_number-2, x_coordinate],
              [run_number-1, x_coordinate],
              [run_number,   x_coordinate]
            ]
          )
        end
      else
        n_red = 0
        n_yellow = 0
      end
    }
  end
  return if is_the_game_over?
  # ======================================================================= #
  # (2) Second, we check from left-to-right. Here we only have to
  #     modify the second part of the Array.
  # ======================================================================= #
  n_red    = 0
  n_yellow = 0
  for x_coordinate in 0..(N_ELEMENTS_IN_THE_Y_AXIS - 1)
    N_ELEMENTS_IN_THE_X_AXIS.times {|run_number|
      case @array[x_coordinate][run_number]
      when YELLOW
        n_yellow += 1
        n_red = 0
        if n_yellow == 4 and ((run_number-3) >= 0)
          yellow_has_won_the_game(&block)
          identify_the_correct_solution(
            [
              [x_coordinate, run_number-3],
              [x_coordinate, run_number-2],
              [x_coordinate, run_number-1],
              [x_coordinate, run_number]
            ]
          )
        end
      when RED
        n_red += 1
        n_yellow = 0
        if n_red == 4 and ((run_number-3) >= 0)
          red_has_won_the_game(&block)
          identify_the_correct_solution(
            [
              [x_coordinate, run_number-3],
              [x_coordinate, run_number-2],
              [x_coordinate, run_number-1],
              [x_coordinate, run_number]
            ]
          )
        end
      else
        n_red = 0
        n_yellow = 0
      end
    }
  end
  return if is_the_game_over?
  # ======================================================================= #
  # (3) Last, check for cross-equality. This has to be done only for
  # two directiones, for any given X - aka / and \.
  # ======================================================================= #
  a, b = @last_position
  if ( (a+3) < (N_ELEMENTS_IN_THE_Y_AXIS) ) and ( (b-3) >= 0 )
    sum = @array[a][b] *
          @array[a+1][b-1] *
          @array[a+2][b-2] *
          @array[a+3][b-3]
    case sum
    when (RED ** 4)
      the_solution_is(
        [
          [a,b],
          [a+1, b-1],
          [a+2, b-2],
          [a+3, b-3]
        ]
      )
      red_has_won(&block)
    when (YELLOW ** 4)
      the_solution_is(
        [
          [a,b],
          [a+1, b-1],
          [a+2, b-2],
          [a+3, b-3]
        ]
      )
      yellow_has_won(&block)
    end
  elsif ( (a-3) < (N_ELEMENTS_IN_THE_Y_AXIS) ) and
         ((b+3) < N_ELEMENTS_IN_THE_X_AXIS) and
         ((b+3) >= 0)
    sum = @array[a][b] *
          @array[a-1][b+1] *
          @array[a-2][b+2] *
          @array[a-3][b+3]
    case sum
    when (RED ** 4)
      the_solution_is(
        [
          [a,b],
          [a-1, b+1],
          [a-2, b+2],
          [a-3, b+3]
        ]
      )
      red_has_won
    when (YELLOW ** 4)
      the_solution_is(
        [
          [a,b],
          [a-1, b+1],
          [a-2, b+2],
          [a-3, b+3]
        ]
      )
      yellow_has_won
    end
  end
  for a in 0..(N_ELEMENTS_IN_THE_Y_AXIS - 1)
    for b in 0 ..(N_ELEMENTS_IN_THE_X_AXIS - 1)
      if ( (a+3) < N_ELEMENTS_IN_THE_Y_AXIS ) and ( (b+3) < N_ELEMENTS_IN_THE_X_AXIS ) and
           ((b+3) >= 0)
        sum = @array[a][b] *
              @array[a+1][b+1] *
              @array[a+2][b+2] *
              @array[a+3][b+3]
        case sum
        when (RED ** 4)
          the_solution_is(
            [
              [a,b],
              [a+1, b+1],
              [a+2, b+2],
              [a+3, b+3]
            ]
          )
          red_has_won
        when (YELLOW ** 4)
          the_solution_is(
            [
              [a,b],
              [a+1, b+1],
              [a+2, b+2],
              [a+3, b+3]
            ]
          )
          yellow_has_won
        end
      end
    end
  end
  return if is_the_game_over?
end

#correct_solution?Boolean

#

correct_solution?

#

Returns:

  • (Boolean)


199
200
201
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 199

def correct_solution?
  @correct_solution
end

#display_this_array(i = @array) ⇒ Object Also known as: display_array, display, show_the_game_map, display_the_game_map

#

display_this_array

#


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 169

def display_this_array(
    i = @array
  )
  i.each {|inner_array|
    inner_array.each {|inner_element|
      case inner_element
      when 1
        inner_element = ::Colours.yellow(UNICODE_BIG_DOT)
      when 2
        inner_element = ::Colours.red(UNICODE_BIG_DOT)
      else
        inner_element = ::Colours.grey(UNICODE_BIG_DOT)
      end
      print "#{inner_element} "
    }
    e
  }
  if @show_numbers_as_hints
    (1 .. N_ELEMENTS_IN_THE_X_AXIS).to_a.each {|entry|
      print lightgreen("#{entry} ")
    }; e
  end
end

#enter_game_loopObject

#

enter_game_loop (loop tag)

#


398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 398

def enter_game_loop
  loop {
    show_the_game_map
    tell_the_current_player_to_place_the_stone
    user_input = STDIN.getch
    case user_input
    when 'q','exit'
      exit
    when '',"\n"
      tell_the_current_player_to_place_the_stone
    else
      if user_input =~ /^\d$/
        if are_all_slots_completely_occupied?
          e 'The game map is full. Exiting now.'
          exit
        elsif is_this_slot_already_full?(user_input)
          e 'Can not place anything on slot number '+user_input.to_s+' as'
          e 'it is already full. Try another slot.'
        else
          place_a_slot_here(user_input, @current_player)
          check_for_win_condition
          toggle_the_current_player
        end
      else
        e 'Please input a number, or "q" to exit (without the "" quotes).'
      end
    end
    if is_the_game_over?
      display_the_game_map # Last display.
      break
    end
  }
end

#feedback_the_current_positionObject

#

feedback_the_current_position

#


454
455
456
457
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 454

def feedback_the_current_position
  e tomato('The current position is: '+
    @last_position.to_s)
end

#identify_the_correct_solution(i = []) ⇒ Object Also known as: the_solution_is

#

identify_the_correct_solution

#


101
102
103
104
105
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 101

def identify_the_correct_solution(
    i = []
  )
  @correct_solution = i
end

#is_cross_equality_possible?(i) ⇒ Boolean

#

is_cross_equality_possible?

Input to this method should be an Array.

#

Returns:

  • (Boolean)


444
445
446
447
448
449
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 444

def is_cross_equality_possible?(i)
  (((i.first+3) < N_ELEMENTS_IN_THE_Y_AXIS) and
   ((i.last+3)  < N_ELEMENTS_IN_THE_X_AXIS)) or
  (((i.first-3) < N_ELEMENTS_IN_THE_Y_AXIS) and
   ((i.last-3)  < N_ELEMENTS_IN_THE_X_AXIS))
end

#is_the_game_over?Boolean Also known as: game_over?

#

is_the_game_over?

#

Returns:

  • (Boolean)


462
463
464
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 462

def is_the_game_over?
  !@correct_solution.nil?
end

#is_there_a_correct_solution?Boolean

#

is_there_a_correct_solution?

#

Returns:

  • (Boolean)


206
207
208
209
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 206

def is_there_a_correct_solution?
  return false if correct_solution?.is_a?(Symbol)
  !correct_solution?.nil?
end

#is_this_slot_already_full?(this_slot = 1) ⇒ Boolean

#

is_this_slot_already_full?

#

Returns:

  • (Boolean)


385
386
387
388
389
390
391
392
393
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 385

def is_this_slot_already_full?(this_slot = 1)
  _ = @array[0][this_slot.to_i - 1]
  case _
  when YELLOW, RED
    true
  else
    false
  end
end

#pp2(i, threshold = 30) ⇒ Object

#

pp2

#


110
111
112
113
114
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 110

def pp2(
    i, threshold = 30
  )
  ::PP.pp(i, $stdout, threshold)
end

#red_has_won(be_verbose = true, &block) ⇒ Object Also known as: red_has_won_the_game

#

red_has_won

#


214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 214

def red_has_won(be_verbose = true, &block)
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :be_quiet
    # ===================================================================== #
    when :be_quiet
      be_verbose = false
    end
  end
  e 'Red has won the game.' if be_verbose
end

#resetObject

#

reset (reset tag)

#


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 57

def reset
  super()
  # ======================================================================= #
  # === @namespace
  # ======================================================================= #
  @namespace = NAMESPACE
  # ======================================================================= #
  # === @show_numbers_as_hints
  # ======================================================================= #
  @show_numbers_as_hints = true
  reset_the_game_state
end

#reset_the_game_mapObject

#

reset_the_game_map

#


92
93
94
95
96
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 92

def reset_the_game_map
  @array = Array.new(N_ELEMENTS_IN_THE_Y_AXIS) {
    Array.new(N_ELEMENTS_IN_THE_X_AXIS, 0)
  }
end

#reset_the_game_stateObject

#

reset_the_game_state

#


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 73

def reset_the_game_state
  # ======================================================================= #
  # === @correct_solution
  # ======================================================================= #
  @correct_solution = nil
  # ======================================================================= #
  # === @current_player
  # ======================================================================= #
  @current_player = 'Red'
  # ======================================================================= #
  # === @last_position
  # ======================================================================= #
  @last_position = nil
  reset_the_game_map
end

#runObject

#

run (run tag)

#


435
436
437
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 435

def run
  enter_game_loop
end

#slot(this_slot = 1, this_colour = :yellow) ⇒ Object Also known as: place_a_slot_here

#

slot

this_colour can be :yellow, :red and :grey.

#


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 252

def slot(
    this_slot   = 1,
    this_colour = :yellow
  )
  this_slot = this_slot.to_i
  if this_slot > N_ELEMENTS_IN_THE_X_AXIS
    this_slot = N_ELEMENTS_IN_THE_X_AXIS
  end
  case this_colour
  # ======================================================================= #
  # === :yellow
  # ======================================================================= #
  when :yellow,
       /Yellow/i
    append_yellow_onto_this_slot(this_slot)
  # ======================================================================= #
  # === :red
  # ======================================================================= #
  when :red,
       /Red/i
    append_red_onto_this_slot(this_slot)
  else
    e "Not yet handled: #{this_colour.to_s}"
  end
end

#slots(i) ⇒ Object

#

slots

Pass an Array to this method.

#


128
129
130
131
132
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 128

def slots(i)
  i.each_slice(2) {|this_slot, this_colour|
    slot(this_slot, this_colour)
  }
end

#start_a_new_gameObject

#

start_a_new_game

#


469
470
471
472
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 469

def start_a_new_game
  reset_the_game_state
  run
end

#tell_the_current_player_to_place_the_stoneObject

#

tell_the_current_player_to_place_the_stone

#


293
294
295
296
297
298
299
300
301
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 293

def tell_the_current_player_to_place_the_stone
  case @current_player
  when /red/i
    _ = tomato(@current_player.to_s+' player')
  when /yellow/i
    _ = yellow(@current_player.to_s+' player')
  end
  e "#{_}: place your stone onto which slot?"
end

#test_this_classObject Also known as: test

#

test_this_class

#


313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 313

def test_this_class
  cliner
  # slot(3, :yellow) # This won't work.
  slots( # slots tag
    [
      1, :yellow,
      1, :red,
      1, :red,
      1, :yellow,
      2, :yellow,
      2, :yellow,
      2, :red,
      2, :yellow,
      3, :red,
      3, :yellow,
      3, :yellow,
      3, :yellow,
      3, :red,
      4, :yellow,
      4, :red,
      4, :red,
      4, :red,
      5, :red,
      5, :yellow,
      5, :red,
      5, :red,
      6, :yellow,
      6, :red,
      6, :red,
      6, :red#,
      # 6, :red
    ]
  )
  check_for_win_conditions
  cliner
  display_this_array
  cliner
end

#toggle_the_current_playerObject

#

toggle_the_current_player

#


281
282
283
284
285
286
287
288
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 281

def toggle_the_current_player
  case @current_player
  when 'Red'
    @current_player = 'Yellow'
  when 'Yellow'
    @current_player = 'Red'
  end
end

#yellow_has_won(be_verbose = true, &block) ⇒ Object Also known as: yellow_has_won_the_game

#

yellow_has_won

#


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/games_paradise/vier_gewinnt/vier_gewinnt.rb', line 231

def yellow_has_won(
    be_verbose = true, &block
  )
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :be_quiet
    # ===================================================================== #
    when :be_quiet
      be_verbose = false
    end
  end
  e 'Yellow has won the game.' if be_verbose
end