Class: ScoreTable

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

Constant Summary collapse

@@line =
'|||||||||||||||||||||||||||||||||||'.colorize(:light_blue)

Instance Method Summary collapse

Instance Method Details

#actualize(timesToDelete, value, valuesDice) ⇒ Object

To delete the dices that make points and return just the once that doesn’t



98
99
100
101
# File 'lib/ScoreTable.rb', line 98

def actualize(timesToDelete,value,valuesDice)
  timesToDelete.times { valuesDice.delete_at(valuesDice.index(value)) }
  return valuesDice
end

#celebrate(winner, listOfPlayers) ⇒ Object

To celebrate the winner of the game



114
115
116
117
118
119
120
# File 'lib/ScoreTable.rb', line 114

def celebrate (winner,listOfPlayers)
  system('clear')
  3.times { puts "========= C O N G R A T U L A T I O N S =========".colorize(:green) }
  puts "------------------>   #{listOfPlayers[ winner ].name}   <------------------".upcase.colorize(:red)
  puts "================ You deserve it! ================".colorize(:green)
  gets.chomp
end

#checkName(listPlayer, newPlayer) ⇒ Object

To check if the name is available



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

def checkName(listPlayer,newPlayer)
  checkName = []
  checkName = listPlayer.map {|player| player.name == newPlayer}
  if checkName.include?(true) and newPlayer != nil
    puts  'This name it\'s already took it.'.colorize(:red)
    return false
  else
    return true
  end
end

#countMatches(valuesDice) ⇒ Object

To count the matches of each value of the array of values



45
46
47
48
49
50
51
52
53
54
# File 'lib/ScoreTable.rb', line 45

def countMatches (valuesDice)
  matches = []
   matches <<  valuesDice.count(1)
   matches <<  valuesDice.count(2)
   matches <<  valuesDice.count(3)
   matches <<  valuesDice.count(4)
   matches <<  valuesDice.count(5)
   matches <<  valuesDice.count(6)
   return matches
end

#displayListPlayer(listPlayer) ⇒ Object

To display all the players



18
19
20
21
22
23
# File 'lib/ScoreTable.rb', line 18

def displayListPlayer(listPlayer)
  system('clear')
  puts '|||||||||| P L A Y E R S ||||||||||'.colorize(:light_blue)
  listPlayer.each  { |player| puts "\t      #{player.name}" }
  puts @@line
end

#displayPoints(listPlayer) ⇒ Object

To display the name of the player and their points



7
8
9
10
11
12
13
14
15
# File 'lib/ScoreTable.rb', line 7

def displayPoints(listPlayer)
  system('clear')
  puts '||||||||||| P O I N T S |||||||||||'.colorize(:light_blue)
  listPlayer.each  do |player|
    print "\t      #{player.name}\t | "
    puts "#{player.score}".colorize(:green)
  end
  puts @@line
end

#displayTurn(listPlayer, player) ⇒ Object

To display who is gonna roll



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ScoreTable.rb', line 123

def displayTurn(listPlayer,player)
  self.displayPoints( listPlayer )
  2.times { puts "\n"}
  print "--> Your turn "
  puts " #{listPlayer[player].name}".colorize(:green)
  puts "Roll the dices! "
  print "Press "
  print "enter ".colorize(:red)
  print "to roll: "
  gets.chomp
end

#evaluate(listPlayer, player) ⇒ Object

To control the workflow of getting the score and actualize the values of the dice



38
39
40
41
42
# File 'lib/ScoreTable.rb', line 38

def evaluate(listPlayer,player)
 matches = countMatches(listPlayer[player].arrayV)
 arrayOfValues = getPoints(matches,listPlayer[player].arrayV)
 return arrayOfValues
end

#getPoints(matches, valuesDice) ⇒ Object

To get the score of the array of values and return the actualize array with the score



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ScoreTable.rb', line 57

def getPoints(matches,valuesDice)
  score = 0
  matches.each_with_index do |number,index|
    case index
      when 0
        score = 1000 if number >= 3
        valuesDice = actualize(3,1,valuesDice) if number >= 3
        puts  "Three 1's => 1000 points".colorize(:blue) if number >=3
        score += 100 if number == 1
        puts " One   1   =>  100 points".colorize(:blue) if number == 1
        valuesDice = actualize(1,1,valuesDice) if number == 1
      when 1
        score += 200 if number >= 3
        valuesDice = actualize(3,2,valuesDice) if number >= 3
        puts " Three 2's =>  200 points".colorize(:blue) if number >=3
      when 2
        score += 300 if number >= 3
        valuesDice = actualize(3,3,valuesDice) if number >= 3
        puts " Three 3's =>  300 points".colorize(:blue) if number >=3
      when 3
        score += 400 if number >= 3
        valuesDice = actualize(3,4,valuesDice) if number >= 3
        puts "  Three 4's =>  400 points".colorize(:blue) if number >=3
      when 4
        score += 500 if number >= 3
        valuesDice = actualize(3,5,valuesDice) if number >= 3
        puts " Three 5's =>  500 points".colorize(:blue) if number >=3
        score += 50 if number == 1
        valuesDice = actualize(1,5,valuesDice) if number == 1
        puts " One   5   =>   50 points".colorize(:blue) if number == 1
      when 5
        score += 600 if number >= 3
        valuesDice = actualize(3,6,valuesDice) if number >= 3
        puts " Three 6's =>  600 points".colorize(:blue) if number >=3
    end
  end
  valuesDice << score
  return valuesDice
end

#winner(listPlayers) ⇒ Object

To verify if in the game exist a winners



104
105
106
107
108
109
110
111
# File 'lib/ScoreTable.rb', line 104

def winner (listPlayers)
  winner = -1
  listPlayers.each_with_index do |player,index|
    winner = index if player.score >= 1500
    break if winner >= 0
  end
  return winner
end