diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 798afd0..0000000 --- a/.gitignore +++ /dev/null @@ -1,344 +0,0 @@ - -# Created by https://www.toptal.com/developers/gitignore/api/react,ruby,java,linux,macos,node,rails -# Edit at https://www.toptal.com/developers/gitignore?templates=react,ruby,java,linux,macos,node,rails - -### Java ### -# Compiled class file -*.class - -# Log file -*.log - -# BlueJ files -*.ctxt - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.nar -*.ear -*.zip -*.tar.gz -*.rar - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* -replay_pid* - -### Linux ### -*~ - -# temporary files which can be created if a process still has a handle open of a deleted file -.fuse_hidden* - -# KDE directory preferences -.directory - -# Linux trash folder which might appear on any partition or disk -.Trash-* - -# .nfs files are created when an open file is removed but is still being accessed -.nfs* - -### macOS ### -# General -.DS_Store -.AppleDouble -.LSOverride - -# Icon must end with two \r -Icon - -# Thumbnails -._* - -# Files that might appear in the root of a volume -.DocumentRevisions-V100 -.fseventsd -.Spotlight-V100 -.TemporaryItems -.Trashes -.VolumeIcon.icns -.com.apple.timemachine.donotpresent - -# Directories potentially created on remote AFP share -.AppleDB -.AppleDesktop -Network Trash Folder -Temporary Items -.apdisk - -### Node ### -# Logs -logs -npm-debug.log* -yarn-debug.log* -yarn-error.log* -lerna-debug.log* -.pnpm-debug.log* - -# Diagnostic reports (https://nodejs.org/api/report.html) -report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json - -# Runtime data -pids -*.pid -*.seed -*.pid.lock - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage -*.lcov - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# Bower dependency directory (https://bower.io/) -bower_components - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (https://nodejs.org/api/addons.html) -build/Release - -# Dependency directories -node_modules/ -jspm_packages/ - -# Snowpack dependency directory (https://snowpack.dev/) -web_modules/ - -# TypeScript cache -*.tsbuildinfo - -# Optional npm cache directory -.npm - -# Optional eslint cache -.eslintcache - -# Optional stylelint cache -.stylelintcache - -# Microbundle cache -.rpt2_cache/ -.rts2_cache_cjs/ -.rts2_cache_es/ -.rts2_cache_umd/ - -# Optional REPL history -.node_repl_history - -# Output of 'npm pack' -*.tgz - -# Yarn Integrity file -.yarn-integrity - -# dotenv environment variable files -.env -.env.development.local -.env.test.local -.env.production.local -.env.local - -# parcel-bundler cache (https://parceljs.org/) -.cache -.parcel-cache - -# Next.js build output -.next -out - -# Nuxt.js build / generate output -.nuxt -dist - -# Gatsby files -.cache/ -# Comment in the public line in if your project uses Gatsby and not Next.js -# https://nextjs.org/blog/next-9-1#public-directory-support -# public - -# vuepress build output -.vuepress/dist - -# vuepress v2.x temp and cache directory -.temp - -# Docusaurus cache and generated files -.docusaurus - -# Serverless directories -.serverless/ - -# FuseBox cache -.fusebox/ - -# DynamoDB Local files -.dynamodb/ - -# TernJS port file -.tern-port - -# Stores VSCode versions used for testing VSCode extensions -.vscode-test - -# yarn v2 -.yarn/cache -.yarn/unplugged -.yarn/build-state.yml -.yarn/install-state.gz -.pnp.* - -### Node Patch ### -# Serverless Webpack directories -.webpack/ - -# Optional stylelint cache - -# SvelteKit build / generate output -.svelte-kit - -### Rails ### -*.rbc -capybara-*.html -.rspec -/db/*.sqlite3 -/db/*.sqlite3-journal -/db/*.sqlite3-[0-9]* -/public/system -/coverage/ -/spec/tmp -*.orig -rerun.txt -pickle-email-*.html - -# Ignore all logfiles and tempfiles. -/log/* -/tmp/* -!/log/.keep -!/tmp/.keep - -# TODO Comment out this rule if you are OK with secrets being uploaded to the repo -config/initializers/secret_token.rb -config/master.key - -# Only include if you have production secrets in this file, which is no longer a Rails default -# config/secrets.yml - -# dotenv, dotenv-rails -# TODO Comment out these rules if environment variables can be committed -.env*.local - -## Environment normalization: -/.bundle -/vendor/bundle - -# these should all be checked in to normalize the environment: -# Gemfile.lock, .ruby-version, .ruby-gemset - -# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: -.rvmrc - -# if using bower-rails ignore default bower_components path bower.json files -/vendor/assets/bower_components -*.bowerrc -bower.json - -# Ignore pow environment settings -.powenv - -# Ignore Byebug command history file. -.byebug_history - -# Ignore node_modules - -# Ignore precompiled javascript packs -/public/packs -/public/packs-test -/public/assets - -# Ignore yarn files -/yarn-error.log - -# Ignore uploaded files in development -/storage/* -!/storage/.keep -/public/uploads - -### react ### -.DS_* -**/*.backup.* -**/*.back.* - -node_modules - -*.sublime* - -psd -thumb -sketch - -### Ruby ### -*.gem -/.config -/InstalledFiles -/pkg/ -/spec/reports/ -/spec/examples.txt -/test/tmp/ -/test/version_tmp/ -/tmp/ - -# Used by dotenv library to load environment variables. -# .env - -# Ignore Byebug command history file. - -## Specific to RubyMotion: -.dat* -.repl_history -build/ -*.bridgesupport -build-iPhoneOS/ -build-iPhoneSimulator/ - -## Specific to RubyMotion (use of CocoaPods): -# -# We recommend against adding the Pods directory to your .gitignore. However -# you should judge for yourself, the pros and cons are mentioned at: -# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control -# vendor/Pods/ - -## Documentation cache and generated files: -/.yardoc/ -/_yardoc/ -/doc/ -/rdoc/ - -/.bundle/ -/lib/bundler/man/ - -# for a library or gem, you might want to ignore these files since the code is -# intended to run in multiple environments; otherwise, check them in: -# Gemfile.lock -# .ruby-version -# .ruby-gemset - -# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: - -# Used by RuboCop. Remote config files pulled in from inherit_from directive. -# .rubocop-https?--* - -# End of https://www.toptal.com/developers/gitignore/api/react,ruby,java,linux,macos,node,rails diff --git a/turtle_graphics_2/turtle.js b/turtle_graphics_2/turtle.js new file mode 100644 index 0000000..97ece77 --- /dev/null +++ b/turtle_graphics_2/turtle.js @@ -0,0 +1,149 @@ +/* In this homework, you will create a simple drawing program inspired +by Turtle Graphics. It will be a much simpler implementation that will +only accept right angles (90 degrees). +*/ + +// Create the Turtle class with initial position +class Turtle { + constructor (x, y) { + this.x = x || 0; + this.y = y || 0; + this.direction = 'east'; + this.angle = 0; + this.steps = [ + [this.x, this.y] + ]; + }; + +/* Forward method that takes a number of steps then updates the Turtle +instance with its new position after moving that many steps. Keep track +of every movement the turtle makes including the first one. +*/ + +forward (step) { + for (let i = 0; i < step; i++) { + if (this.angle === 0) { + this.x++; + } else if (this.angle === 90) { + this.y--; + } else if (this.angle === 180) { + this.x--; + } else if (this.angle === 270) { + this.y++; + } else { + return this; + } + this.steps.push([this.x, this.y]); + } + return this; +} + +/* Create a right method that takes zero arguments. When right is called, +update the Turtle instance to rotate its facing to the right. A turtle +should begin facing east. +*/ + +right () { + this.angle += 90; + if (this.angle === 90) { + this.direction = 'south'; + } else if (this.angle === 180) { + this.direction = 'west'; + } else if (this.angle === 270) { + this.direction = 'north'; + } else { + this.direction = 'east'; + this.angle = 0; + } + return this; +}; + +// Create a left method like right but turns the turtle's facing to the left. + +left () { + this.angle -= 90; + if (this.angle === 90) { + this.direction = 'south'; + } else if (this.angle === 180) { + this.direction = 'west'; + } else if (this.angle === -90 || this.angle === 270) { + this.angle = 270; + this.direction = 'north'; + } else { + this.direction = 'east'; + this.angle = 0; + } + return this; +}; + +/* Create an allPoints method which returns an array containing all +coordinates the turtle has walked over. +*/ + +allPoints () { + return this.steps; +}; + +/*Create a print method that draws the path that the turtle walked over +as a string and logs it to the console. +*/ + +print () { + let maxX = -Infinity; + let maxY = -Infinity; + let minX = Infinity; + let minY = Infinity; + for (let step of this.steps) { + if (step[0] > maxX) { + maxX = step[0]; + } + if (step[1] > maxY) { + maxY = step[1]; + } + if (step[0] < minX) { + minX = step[0]; + } + if (step[1] < minY) { + minY = step[1]; + } + }; + +const turtleFootprint = (x, y) => { + for (let step of this.steps) { + if (step[0] === x && step[1] === y) + return true; + } + return false; +}; + +console.log('-- BEGIN LOG'); +for (let y = maxY + 1; y >= minY; y--) { + let row = ''; + for (let x = minX; x <= maxX + 1; x++ ) { + if (turtleFootprint (x, y)) { + row += ' ○ '; + } else { + row += ' - '; + } + } + console.log(row); +} +console.log('-- END LOG'); +}; +}; + +const ringo = new Turtle(); +ringo.forward(3) + .left() + .forward(3) + .right() + .forward(5) + .right() + .forward(8) + .right() + .forward(5) + .right() + .forward(3) + .left() + .forward(3) + .print();