Class: GamesAndRpgParadise::VierGewinnt

Inherits:
Base
  • Object
show all
Includes:
GUI::SharedCode
Defined in:
lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb

Overview

GamesAndRpgParadise::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 included from Base::Extensions::Colours

Base::Extensions::Colours::ARRAY_AVAILABLE_KONSOLE_COLOURS

Constants included from CommonExtensions

CommonExtensions::CONTROL_C_CODE, CommonExtensions::N

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base::Extensions::Colours

ecomment, #efancy, #eparse, #forestgreen, #gold, #grey, #lightblue, #mediumseagreen, #mediumslateblue, #peru, #rev, sdir, sfancy, #sfile, simp, #teal, #yellow

Methods included from Base::Extensions::CommandlineArguments

#commandline_arguments?, #filter_away_commandline_arguments, #first_argument?, #first_non_hyphened_argument?, #set_commandline_arguments

Methods included from CommonExtensions

#cat, #cd, #cliner, #copy_file, #delete, #dirname_but_retains_the_trailing_slash, #disable_colours, #ensure_that_the_log_directory_exists, #esystem, #get_user_input, #infer_the_namespace, #is_on_roebe?, #log_dir?, #mkdir, #mkdir_then_cd_into_it, #mv, #namespace?, #opne, #opnn, #project_base_directory?, #project_image_directory?, #project_yaml_directory?, #rds, #register_sigint, #remove_this_directory, #rename_file, #reset_the_internal_hash, #return_pwd, #return_today, #touch_file, #wrap, #write_what_into

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_and_rpg_paradise/games/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

#

GamesAndRpgParadise::VierGewinnt[]

#


669
670
671
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 669

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.

#


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 136

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.

#


355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 355

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)


303
304
305
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 303

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

#array?Boolean Also known as: game_map?

#

array?

#

Returns:

  • (Boolean)


116
117
118
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 116

def array?
  @array
end

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

#

check_for_win_conditions (win tag)

#


467
468
469
470
471
472
473
474
475
476
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
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 467

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)


196
197
198
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 196

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

#


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 166

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)

#


395
396
397
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
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 395

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

#


444
445
446
447
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 444

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

#


98
99
100
101
102
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 98

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)


434
435
436
437
438
439
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 434

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)


452
453
454
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 452

def is_the_game_over?
  !@correct_solution.nil?
end

#is_there_a_correct_solution?Boolean

#

is_there_a_correct_solution?

#

Returns:

  • (Boolean)


203
204
205
206
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 203

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)


382
383
384
385
386
387
388
389
390
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 382

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

#


107
108
109
110
111
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 107

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

#


211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 211

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
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 57

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

#reset_the_game_mapObject

#

reset_the_game_map

#


89
90
91
92
93
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 89

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

#


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 70

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)

#


662
663
664
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 662

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.

#


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 249

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.

#


125
126
127
128
129
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 125

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

#


459
460
461
462
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 459

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

#


290
291
292
293
294
295
296
297
298
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 290

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

#


310
311
312
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
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 310

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

#


278
279
280
281
282
283
284
285
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 278

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

#


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/games_and_rpg_paradise/games/vier_gewinnt/vier_gewinnt.rb', line 228

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