Last active
May 1, 2023 18:11
-
-
Save takuyadev/4cbb502d430a57ab109b742d54a67ae4 to your computer and use it in GitHub Desktop.
Math Game (Ruby)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Game | |
# Manages the state of the game | |
# Can change turns, ask questions to players, check answers | |
def initialize(players, max_points) | |
@index = 0 | |
@players = players | |
@max_points = max_points | |
@is_start = false | |
end | |
# Start game, and loop questions until game ends | |
def start_game | |
@is_start = true | |
# Keep asking questions until game has ended | |
while (@is_start) | |
puts "----- NEW TURN -----" | |
self.display_points | |
self.ask_question | |
end | |
end | |
protected | |
# Change turn by changing index | |
def change_turn | |
@index += 1 | |
# Loop back to 0 if index is equal or exceeds the total array length | |
if (@index >= @players.length()) | |
@index = 0 | |
end | |
end | |
# Check if game has ended | |
def end_game | |
puts "----- GAME OVER -----" | |
@is_start = false | |
end | |
# Display total points of each player | |
def display_points | |
message = "" | |
# Message for displaying points for each player | |
@players.each_with_index do |player, index| | |
message += "#{player.name}: #{player.points}/#{@max_points} " | |
# Add vs. unless there are no more players to go through | |
unless (index + 1 == @players.length()) | |
message += "vs. " | |
end | |
end | |
puts message | |
end | |
# Ask random question to player | |
def ask_question | |
# Generate random number | |
player_name = @players[@index].name | |
num1 = rand(20) | |
num2 = rand(20) | |
# Ask current question | |
puts "#{player_name}: What does #{num1} plus #{num2} equal?" | |
# Setup player and actual calculated answer | |
actual_answer = num1 + num2 | |
player_answer = $stdin.gets.chomp.to_i | |
# If player is correct, then add point to user | |
if (player_answer === actual_answer) | |
@players[@index].add_points(1) | |
puts "#{player_name}: YES! You are correct." | |
else | |
# Else, no point is added for current player | |
puts "#{player_name}: Seriously? No!" | |
end | |
# Check winner for every round | |
self.check_winner | |
end | |
# Check if current player has won | |
def check_winner | |
# Check if current player points matches or is over max points | |
if (@players[@index].points >= @max_points) | |
self.end_game | |
puts "Winner is: #{@players[@index].name}!" | |
end | |
# Change turn if winner is not decided yet | |
self.change_turn | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require ("./game.rb") | |
require ("./player.rb") | |
# Initialize players | |
player1 = Player.new("Player 1") | |
player2 = Player.new("Player 2") | |
player3 = Player.new("Player 3") | |
player4 = Player.new("Player 4") | |
player5 = Player.new("Player 5") | |
game = Game.new([player1, player2, player3, player4, player5], 10) | |
# Start the actual game | |
game.start_game |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Player | |
attr_accessor :name, :points | |
# Manages state of player | |
# name of player, how many points the player has in the game | |
def initialize(name) | |
@name = name | |
@points = 0 | |
end | |
# Add points to current player | |
def add_points(points) | |
@points += points | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment