Class: Cliptic::Main::Player::Board

Inherits:
Object
  • Object
show all
Includes:
Cliptic::Main::Puzzle, Windows
Defined in:
lib/cliptic/main.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date: Date.today) ⇒ Board

, state:)



428
429
430
431
432
433
434
# File 'lib/cliptic/main.rb', line 428

def initialize(date:Date.today)#, state:)
  @puzzle = Puzzle::Puzzle.new(date:date)
  @grid   = Grid.new(puzzle:puzzle)
  @box    = Cluebox.new(grid:grid)
  @cursor = Cursor.new(grid:grid)
  #@state  = state
end

Instance Attribute Details

#boxObject (readonly)

Returns the value of attribute box.



427
428
429
# File 'lib/cliptic/main.rb', line 427

def box
  @box
end

#clueObject (readonly)

Returns the value of attribute clue.



427
428
429
# File 'lib/cliptic/main.rb', line 427

def clue
  @clue
end

#cursorObject (readonly)

Returns the value of attribute cursor.



427
428
429
# File 'lib/cliptic/main.rb', line 427

def cursor
  @cursor
end

#dirObject (readonly)

Returns the value of attribute dir.



427
428
429
# File 'lib/cliptic/main.rb', line 427

def dir
  @dir
end

#gridObject (readonly)

Returns the value of attribute grid.



427
428
429
# File 'lib/cliptic/main.rb', line 427

def grid
  @grid
end

#puzzleObject (readonly)

Returns the value of attribute puzzle.



427
428
429
# File 'lib/cliptic/main.rb', line 427

def puzzle
  @puzzle
end

#stateObject (readonly)

Returns the value of attribute state.



427
428
429
# File 'lib/cliptic/main.rb', line 427

def state
  @state
end

Instance Method Details

#advance_cursor(n: 1) ⇒ Object



523
524
525
526
527
528
# File 'lib/cliptic/main.rb', line 523

def advance_cursor(n:1)
  case dir
  when :a then move(x:n)
  when :d then move(y:n)
  end
end

#clear_all_cellsObject



519
520
521
522
# File 'lib/cliptic/main.rb', line 519

def clear_all_cells
  grid.cells.flatten.each{|cell| cell.clear}
  puzzle.clues.each{|clue| clue.done = false}
end

#clear_clueObject



511
512
513
514
# File 'lib/cliptic/main.rb', line 511

def clear_clue
  clue.clear
  clue.activate
end

#delete_char(advance: true) ⇒ Object



467
468
469
470
471
# File 'lib/cliptic/main.rb', line 467

def delete_char(advance:true)
  addch(char:" ").underline
  advance_cursor(n:-1) if advance && 
    !on_first_cell?
end

#goto_cell(n:) ⇒ Object



506
507
508
509
510
# File 'lib/cliptic/main.rb', line 506

def goto_cell(n:)
  if n > 0 && n <= clue.length
    cursor.set(**clue.cells[n-1].sq)
  end
end

#goto_clue(n:) ⇒ Object



503
504
505
# File 'lib/cliptic/main.rb', line 503

def goto_clue(n:)
  set_clue(clue:get_clue_by_index(i:n), mv:true)
end

#insert_char(char:, advance: true) ⇒ Object



462
463
464
465
466
# File 'lib/cliptic/main.rb', line 462

def insert_char(char:, advance:true)
  addch(char:char)
  move_after_insert(advance:advance)
  check_current_clue
end

#move(y: 0, x: 0) ⇒ Object



454
455
456
457
458
459
460
461
# File 'lib/cliptic/main.rb', line 454

def move(y:0, x:0)
  cursor.move(y:y, x:x)
  if current_cell.blocked
    move(y:y, x:x)
  elsif outside_clue?
    set_clue(clue:get_clue_at(**cursor.pos))
  end
end

#next_clue(n: 1) ⇒ Object



472
473
474
475
476
477
# File 'lib/cliptic/main.rb', line 472

def next_clue(n:1)
  n.times do 
    set_clue(clue:clue.next, mv:true)
  end
  next_clue if clue.done && !puzzle.complete?
end

#prev_clue(n: 1) ⇒ Object



478
479
480
481
482
483
484
485
# File 'lib/cliptic/main.rb', line 478

def prev_clue(n:1)
  n.times do 
    on_first_cell? ?
      set_clue(clue:clue.prev, mv:true) :
      to_start
  end
  prev_clue if clue.done && !puzzle.complete?
end

#redrawObject



446
447
448
449
450
451
452
453
# File 'lib/cliptic/main.rb', line 446

def redraw
  grid.draw
  grid.cells.flatten
    .find_all{|c| c.buffer != " "}
    .each{|c| c.unlock.write}
  puzzle.check_all if $config[:auto_mark]
  clue.activate
end

#reveal_clueObject



515
516
517
518
# File 'lib/cliptic/main.rb', line 515

def reveal_clue
  clue.reveal
  next_clue(n:1)
end

#save_stateObject



496
497
498
499
500
501
502
# File 'lib/cliptic/main.rb', line 496

def save_state
  [].tap do |state|
    grid.cells.flatten.map do |cell|
      state << { sq:cell.sq, char:cell.buffer } unless cell.blocked || cell.buffer == " "
    end
  end
end

#setup(state: nil) ⇒ Object



435
436
437
438
439
440
441
# File 'lib/cliptic/main.rb', line 435

def setup(state:nil)
  grid.draw
  load_state(state:state)
  set_clue(clue:puzzle.first_clue, mv:true) if !@clue
  update
  self
end

#swap_directionObject



492
493
494
495
# File 'lib/cliptic/main.rb', line 492

def swap_direction
  set_clue(clue:get_clue_at(
    **cursor.pos, dir:Pos.change_dir(dir)))
end

#to_endObject



489
490
491
# File 'lib/cliptic/main.rb', line 489

def to_end
  cursor.set(**clue.coords.last)
end

#to_startObject



486
487
488
# File 'lib/cliptic/main.rb', line 486

def to_start
  cursor.set(**clue.coords.first)
end

#updateObject



442
443
444
445
# File 'lib/cliptic/main.rb', line 442

def update
  cursor.reset
  grid.refresh
end