Class: PickANumber

Inherits:
Game
  • Object
show all
Defined in:
lib/pick_a_number.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Game

#display, #reload_position

Constructor Details

#initializePickANumber

Returns a new instance of PickANumber.



7
8
9
10
11
# File 'lib/pick_a_number.rb', line 7

def initialize
  @position = {"1" => 0,"2" => 0, "3" => 0}
  @next_player = 0
  @move_list = []
end

Instance Attribute Details

#move_listObject (readonly)

Returns the value of attribute move_list.



5
6
7
# File 'lib/pick_a_number.rb', line 5

def move_list
  @move_list
end

#next_playerObject (readonly)

Returns the value of attribute next_player.



5
6
7
# File 'lib/pick_a_number.rb', line 5

def next_player
  @next_player
end

#quietObject

Returns the value of attribute quiet.



6
7
8
# File 'lib/pick_a_number.rb', line 6

def quiet
  @quiet
end

#verboseObject

Returns the value of attribute verbose.



6
7
8
# File 'lib/pick_a_number.rb', line 6

def verbose
  @verbose
end

#winnerObject (readonly)

Returns the value of attribute winner.



5
6
7
# File 'lib/pick_a_number.rb', line 5

def winner
  @winner
end

Instance Method Details

#current_positionObject



80
81
82
# File 'lib/pick_a_number.rb', line 80

def current_position
  return @position
end

#drawn?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
# File 'lib/pick_a_number.rb', line 91

def drawn?
  if @position["2"] != 0
    return true
  end
  false
end


17
18
19
20
21
22
23
24
25
# File 'lib/pick_a_number.rb', line 17

def legal_moves
  legal_moves = []
  @position.each do |move|
    if move[1] == 0
      legal_moves << move[0].to_s
    end
  end
  legal_moves
end

#nameObject



13
14
15
# File 'lib/pick_a_number.rb', line 13

def name
  "PickANumber"
end

#play_move(player, move) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pick_a_number.rb', line 38

def play_move(player, move)
  unless player == @next_player
    display "not player #{player}'s turn"
    return false
  end
  unless @position[move] == 0
    display "illegal move by player #{player}"
    return false
  end
  if won?
    display "player #{winner} has already won"
    return false
  elsif drawn?
    display "game was drawn"
    return false
  end

  @move_list << [player,move]
  
  if player == 1
    @position[move] = 1
  else
    @position[move] = -1
  end
  if won?
    display "player #{player} wins!"
    @winner = player
  elsif drawn?
    display "game drawn"
  end
  
  if @next_player == 1
    @next_player = 0
  else
    @next_player = 1
  end
  if @verbose
    display @position
  end
  true      
end

#show_boardObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/pick_a_number.rb', line 27

def show_board
  board = "Please Pick from the following Numbers:\n"

  @position.each do |move|
    if move[1] == 0
      board += "#{move[0]},"
    end  
  end
  board +"\n"
end

#won?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
# File 'lib/pick_a_number.rb', line 84

def won?
  if @position["3"] != 0
    return true
  end
  false
end