Skip to content

Commit

Permalink
everything?
Browse files Browse the repository at this point in the history
  • Loading branch information
hoornet committed Feb 2, 2010
1 parent 1db5b62 commit 1b75d10
Show file tree
Hide file tree
Showing 11 changed files with 9,813 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .idea/runConfigurations/Week7.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9,638 changes: 9,638 additions & 0 deletions FORPC101/LECTURE SOURCE/INDEX.whole

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions FORPC101/Week 6/gameboard.rb
@@ -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
34 changes: 34 additions & 0 deletions FORPC101/Week 6/testgameboard.rb
@@ -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 added FORPC101/Week 7/COTNC application.pdf
Binary file not shown.
Binary file added FORPC101/Week 7/TOTSSapplication.pdf
Binary file not shown.
Binary file added FORPC101/Week 7/image.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added FORPC101/Week 7/song.mp3
Binary file not shown.
71 changes: 71 additions & 0 deletions FORPC101/Week 7/w7e5.rb
@@ -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.
7 changes: 7 additions & 0 deletions PlayArounds/Rubytemplate.rb
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

=BEGIN



=END

0 comments on commit 1b75d10

Please sign in to comment.