Class: Chess::Game

Inherits:
Object
  • Object
show all
Includes:
Display, Mouse, Save, ValidMoves, WinAndDraw
Defined in:
lib/chess/game/game.rb

Overview

Game class

Constant Summary

Constants included from Save::Serializer

Save::Serializer::SERIALIZER

Constants included from Display

Display::COLORS

Instance Method Summary collapse

Methods included from WinAndDraw

#any_legal_move?, #checkmate?, #detect_win_or_draws, #fifty_move_draw?, #game_exit, #save_and_exit, #save_game, #stalemate?

Methods included from ValidMoves

#castling_king_passes_check?, #invalid_castling_move?, #invalid_king_side_castle?, #invalid_normal_move?, #invalid_queen_side_castle?, #king_comes_in_check?, #valid_moves, #valid_moves_for_king, #valid_moves_from_possible_moves

Methods included from Save

#read, #save

Methods included from Save::FenCodeFromBoard

#fen_piece_placement, #fen_piece_placement_by_rank, #generate_fen_code, #letter_of

Methods included from Save::Serializer

#serialize, #unserialize

Methods included from Mouse

#button_type, #buttons_touched?, #clicked_element, #disable_mouse_tracking, #enable_mouse_tracking, #game_exit?, #generate_file_coords, #generate_rank_coords, #input_loop, #read_clicked, #read_input, #start_mouse_input, #warn_tmux_users

Methods included from Display

#bg_color, #color, #display_board, #display_buttons, #king_check_dot, #move_dots, #print_board_data, #print_files, #print_piece_square, #print_square, #prompt_enter_code, #prompt_pawn_promotion_choices, #prompt_save_name, #prompt_select_save, #prompt_start_choices, #redraw_display, #square_bg_color_name, #square_string

Constructor Details

#initialize(player = Chess::Player) ⇒ Game

Returns a new instance of Game.



13
14
15
16
# File 'lib/chess/game/game.rb', line 13

def initialize(player = Chess::Player)
  @white_player = player.new
  @black_player = player.new
end

Instance Method Details

#board_action(board, board_pos) ⇒ nil, Integer

actions for clicks on board

Parameters:

  • board (Chess:Board)
  • board_pos (Array)

Returns:

  • (nil, Integer)

    nil or Exit Code 0



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chess/game/game.rb', line 38

def board_action(board, board_pos)
  player = if board.current_player == 'w'
             @white_player
           else
             @black_player
           end
  update_castling_rights(board)
  valid_moves = player_turn(player, board, board_pos)
  update_king_status(board)
  redraw_display(board.data, valid_moves)
  detect_win_or_draws(board)
end

#button_action(board, mouse_coord) ⇒ nil, Integer

actions for clicks on buttons

Parameters:

Returns:

  • (nil, Integer)

    nil or Exit Code 0



55
56
57
58
59
60
61
62
# File 'lib/chess/game/game.rb', line 55

def button_action(board, mouse_coord)
  type = button_type(mouse_coord)
  if type == 'save&exit'
    save_and_exit(board)
  else
    game_exit
  end
end

#player_turn(player, board, board_pos) ⇒ void

This method returns an undefined value.

player turn to select move

Parameters:



69
70
71
72
73
74
75
76
77
# File 'lib/chess/game/game.rb', line 69

def player_turn(player, board, board_pos)
  piece = board.piece_at(*board_pos)
  if player.selected_piece == '' || same_color?(board.current_player, piece)
    select_piece(piece, player, board)
  else
    select_move(player, board, board_pos)
    []
  end
end

#same_color?(current_player, piece) ⇒ Boolean

checks if current_player and piece have same color

Parameters:

Returns:

  • (Boolean)


109
110
111
# File 'lib/chess/game/game.rb', line 109

def same_color?(current_player, piece)
  piece.color.chr == current_player unless piece == ''
end

#select_click_action(board, clicked, board_pos, mouse_coord) ⇒ nil, Integer

select different actions based on what was clicked

Parameters:

  • board (Chess::Board)
  • clicked (String)
  • board_pos (Array)
  • mouse_coord (Array)

Returns:

  • (nil, Integer)

    nil or Exit Code 0



24
25
26
27
28
29
30
31
32
# File 'lib/chess/game/game.rb', line 24

def select_click_action(board, clicked, board_pos, mouse_coord)
  return if clicked == 'outside'

  if clicked == 'board'
    board_action(board, board_pos)
  else
    button_action(board, mouse_coord)
  end
end

#select_move(player, board, move_pos) ⇒ void

This method returns an undefined value.

selects move to play

Parameters:



99
100
101
102
103
# File 'lib/chess/game/game.rb', line 99

def select_move(player, board, move_pos)
  player.select_move(board, move_pos)
  player.selected_piece.valid_moves = []
  player.selected_piece = ''
end

#select_piece(piece, player, board) ⇒ void

This method returns an undefined value.

player turn to select move

Parameters:



84
85
86
87
88
89
90
91
# File 'lib/chess/game/game.rb', line 84

def select_piece(piece, player, board)
  player.selected_piece = piece if same_color?(board.current_player, piece)
  if player.selected_piece == ''
    []
  else
    player.selected_piece.valid_moves = valid_moves(piece, board)
  end
end

#update_castling_rights(board) ⇒ void

This method returns an undefined value.

updates castling_rights for board

Parameters:



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/chess/game/game.rb', line 130

def update_castling_rights(board)
  return if board.castling_rights == ''

  location_piece_color_and_rights = {
    h0: [Chess::Pieces::Rook, 'w', 'K'],
    a0: [Chess::Pieces::Rook, 'w', 'Q'],
    e0: [Chess::Pieces::King, 'w', 'KQ'],
    h7: [Chess::Pieces::Rook, 'b', 'k'],
    a7: [Chess::Pieces::Rook, 'b', 'q'],
    e7: [Chess::Pieces::King, 'b', 'kq']
  }
  update_castling_rights_for_each_pos(board, location_piece_color_and_rights)
end

#update_castling_rights_for_each_pos(board, location_piece_color_and_rights) ⇒ void

This method returns an undefined value.

helper method for #update_castling_rights

Parameters:

  • board (Chess::Board)
  • location_piece_color_and_rights (Hash)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/chess/game/game.rb', line 149

def update_castling_rights_for_each_pos(board, location_piece_color_and_rights) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  location_piece_color_and_rights.each_key do |pos|
    type = location_piece_color_and_rights[pos].first
    color = location_piece_color_and_rights[pos][1]
    rights = location_piece_color_and_rights[pos].last
    position = pos.to_s.chars
    file = position.first
    rank = position.last.to_i
    piece = board.piece_at(file, rank)
    rights = rights.chars
    rights.each do |right|
      board.castling_rights.sub!(right, '') unless piece.is_a?(type) && same_color?(color, piece)
    end
  end
end

#update_king_status(board) ⇒ void

This method returns an undefined value.

updates king’s @in_check attribute

Parameters:



117
118
119
120
121
122
123
124
# File 'lib/chess/game/game.rb', line 117

def update_king_status(board)
  current_player = board.current_player
  board.current_player = 'b'
  board.white_king.in_check?(board, board.black_pieces)
  board.current_player = 'w'
  board.black_king.in_check?(board, board.white_pieces)
  board.current_player = current_player
end