DEV Community

loading...
Cover image for JavaScript Baseball

JavaScript Baseball

denvercoder profile image Tim Myers ・3 min read

Do It Because It's Fun

Some advice I give to people a lot is once you have a good grasp on your preferred language you should build something you're passionate about or something you enjoy.

With new programmers a lot of the struggle is habit building. Getting into the groove of studying and learning takes time. And it's a lot easier if you're having fun while you're doing it.

Now the key like I said before is to try and create a project that you find interesting. You may be thinking, "But I only like nuclear physics... how am I going to write a program about that?"

The key here is not to build some elaborate project... at least not at first. You could create a project that asks you Nuclear Physics questions and you can keep track of correct answers and provide a scoreboard.

Then you can build on that idea. You can add multiplayer support. A timer to answer the questions. Some fun animations, etc. Just add onto it piece by piece until you have something.

Play Ball

I'm not particularly fond of Baseball. I played Little League as a kid, I umpired games for the really small kids, and I played in High School. But I sort of lost interest in it after that.

I decided to do this project because I had given this project to people that I had mentored in the past and I wanted to see how difficult it really was, (maybe I was asking too much of them). Now, I didn't build a Baseball game with full 3D graphics and controller support. It's basically just a bunch of HTML output in the browser:

A baseball scoreboard with Away Team and Home team, outs, strikes, balls, who is at bat, and runners on base

I've abstracted away a lot of the complexity. You don't swing at a pitch or anything. You just pitch the ball. Then a random number generator will either make that pitch a "strike", or a "ball". Once you get to 4 balls the batter is "walked". If you get 3 "strikes" the batter is out. Basically I just use the fundamental rules of baseball to keep track of everything.

A code file showing a switch statement

I picked baseball because it's a relatively slow paced game but you can really do this with anything:

  • Horse Racing
  • Tennis
  • A simple solar system simulator
  • Cricket
  • A (Pet) Program where there is a creature of some sort that you have to take care of.

There are tons of options. Pick something you enjoy and simulate it. Start small, and build the project up. Do you think that Twitter or Dev.to started out the way they are now? Nope. Twitter probably had the ability to login and send a message and that was it. You probably couldn't like, follow, retweet, block, mute, etc. They built an app with minimal functionality and build up from there.

Here is the link to my easy-baseball repo if you'd like to take a look at the whole project. You can run it yourself and try and figure out how it works. It's actually not a lot of code so don't be intimidated. Just go have a look.

If you want you have my full permission and encouragement to build on this project. There isn't a lot of functionality and there are a few bugs. See if you can find them and fix them.

Thanks for reading and that's all y'all.

Discussion (7)

pic
Editor guide
Collapse
raddevus profile image
raddevus

This is an interesting idea. With just a bit of enhancement you could have it so it will (in increasing amount of difficulty) :
1) randomly calculate for a hit also -- so it's not just a strike-out or walk
2) automatically select two real MLB teams and list them as playing
3) put a timer in so it "plays" an entire game and the user can just watch.
Nice idea and article. thanks for sharing

Collapse
denvercoder profile image
Tim Myers Author

It already calculates for a hit. How would there be any runs if there are not hits. 😂

There is a random chance of:

  1. hit
  2. Strike
  3. Ball
  4. Foul

It will actually advance innings and score runs if there are more than 3 hits in an inning.

Collapse
raddevus profile image
raddevus

Yeah, that makes sense. I think it's because the article didn't mention the possibility of a hit, just this :

Then a random number generator will either make that pitch a "strike", or a "ball". Once you get to 4 balls the batter is "walked". If you get 3 "strikes" the batter is out.

Collapse
denvercoder profile image
Tim Myers Author

One of the things I thought of doing is have a "Team" array with Players that all have different ratings. So like in REAL baseball a batter with a .500 average is going to get more hits than a batter with a .300 average. I thought about factoring in that percentage. Then you could let them pick their own team by having a maximum number of "points". Maybe you start with 20 points. A batter with a .500 average is 10 points so that would be almost all of your points so you would have to fill your roster with .125 average batters. So there would be some skill in optimizing your roster of players.

Collapse
douggrubba profile image
Doug Grubba

This is a fun idea. I've had an idea for making a baseball sim for a long time. I love baseball and I just feel I'll regret not doing it.

With that said it is actually extremely tricky to really replicate all that is going on between a batter and pitcher. Breaking it down to these smaller interactions is a great idea. I would definitely less likely loose momentum if that was the case. Thanks for sharing. 🍻

Collapse
code2rithik profile image
Rithik Samanthula

Hey Bro, I loved the blog. BTW, which VSCode theme are you using?

Collapse
denvercoder profile image
Tim Myers Author

Dracula Pro