Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
9,813 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
=begin | ||
Exercise3: | ||
Here are the program specifications for a simple game to be played | ||
against the computer. | ||
a. This game is played at the command-line. | ||
b. The game starts when the computer selects three consecutive cells in a 7 cell | ||
row (from 0 to 6). When that's complete, the game asks for your first guess. | ||
c. Guess the computer's selected cells in the smallest number of guesses. You are | ||
given a rating or level, based on how well you perform. | ||
d. At the command line, the user types in a number from 0 to 6. The computer | ||
checks if it's one of the selected cells. If it's a hit, increment the no_of_hits | ||
variable. In response to your guess, you'll see a result at the command-line: | ||
either "Hit", "Miss" or "End". | ||
e. When you have guessed all three cells, the game ends by printing out your rating | ||
(your number of guesses). | ||
Note: | ||
a. Use only the features we have learned so far, in Ruby. | ||
b. You must design the GameBoard class. | ||
c. The testgameboard.rb program uses your GameBoard class | ||
############################################################################# | ||
=end | ||
|
||
class GameBoard | ||
def set_locations_cells(locations) | ||
@locations = locations | ||
puts "locations: #{@locations[0]}, #{@locations[1]}, #{@locations[2]}" | ||
|
||
end | ||
|
||
def check_yourself(user_guess) | ||
@locations.each { |a| | ||
if (user_guess.to_i == a) | ||
return 'kill' | ||
end | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# testgameboard.rb | ||
require 'gameboard' | ||
|
||
# track how many guesses the user makes | ||
no_of_guesses = 0 | ||
|
||
# instantiate a GameBoard object | ||
gb = GameBoard.new | ||
|
||
# make a random number for the first cell, | ||
# and use it to make the cell locations array | ||
random_no = rand(5) | ||
# make an array for the location of the 3 | ||
# consecutive ints out of a possible 7 | ||
locations = [random_no, random_no+1, random_no+2] | ||
|
||
# invoke the setter method of the GameBoard | ||
gb.set_locations_cells(locations) | ||
|
||
# variable to track if the game is alive | ||
is_alive = true | ||
|
||
while is_alive | ||
puts 'Enter a number: ' | ||
STDOUT.flush | ||
user_guess = gets.chomp | ||
# invoke the check_yourself method on the GameBoard object | ||
result = gb.check_yourself(user_guess) | ||
no_of_guesses += 1 | ||
if (result == 'kill') | ||
is_alive = false | ||
puts "You took #{no_of_guesses} guesses" | ||
end | ||
end |
Binary file not shown.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
=begin | ||
Exercise5. | ||
Here's code for the part of a game that saves the game state to a file. As a deterrent against cheating, | ||
when the game loads a save file it performs a simple check against the file's modification time. If it differs from the | ||
timestamp recorded inside the file, the game refuses to load the save file. | ||
The save_game method is responsible for recording the timestamp: | ||
def save_game(file) | ||
score = 1000 | ||
open(file, "w") do |f| | ||
f.puts(score) | ||
f.puts(Time.new.to_i) | ||
end | ||
end | ||
The load_game method is responsible for comparing the timestamp within the file to the time the filesystem has | ||
associated with the file. Write the load_game(file) method. | ||
This mechanism can detect simple forms of cheating: | ||
save_game("game.sav") | ||
sleep(2) | ||
load_game("game.sav") # => "Your saved score is 1000." | ||
# Now let's cheat by increasing our score to 9000 | ||
open("game.sav", "r+b") { |f| f.write("9") } | ||
load_game("game.sav") # RuntimeError: I suspect you of cheating. | ||
Since it's possible to modify a file's times with tools like the Unix touch command, you shouldn't depend on these | ||
methods to defend you against a skilled attacker actively trying to fool your program. | ||
Read up on sleep method and Time class. | ||
=end | ||
|
||
# The save_game method is responsible for recording the timestamp: | ||
def save_game(file) | ||
score = 1000 | ||
open(file, "w") do |f| | ||
f.puts(score) | ||
f.puts(Time.new.to_i) | ||
end | ||
end | ||
|
||
def load_game(file) | ||
|
||
end | ||
|
||
|
||
|
||
################## | ||
# TEST: | ||
|
||
save_game("game.sav") | ||
sleep(2) | ||
load_game("game.sav") # => "Your saved score is 1000." | ||
|
||
# Now let's cheat by increasing our score to 9000 | ||
open("game.sav", "r+b") { |f| f.write("9") } | ||
load_game("game.sav") # RuntimeError: I suspect you of cheating. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env ruby | ||
|
||
=BEGIN | ||
|
||
|
||
|
||
=END |