Everyday Game Coding

I am a simply game-addicted guy
a little bit special about me is that I start learning ‘Java coding’
so here I am, start coding a simple game step by step everyday for you ❤ :) (Using Java of cause)

Before we begin

Requirements

NetBeans 8.2 or newer download it for free right here!

Choose File/New Project then a small window looks like this should appears. Choose Java Folder on the left and Java Application on the right then click Next>
Name your Project anything you like then simply click Finish
Ta-da! Congrats! Your new java file is now ready for you to rock and roll!

Day 1 : Let’s play “Hangman”!

Everybody love hangman, then why not let it be our first game !

Game’s rules

This Hangman will be a bit different with the others, no drawing head, leg,..
We will just set how many turns can the player guess, and they will lose when they spend all their turns.

Let’s Start with the idea!

Remember, every programs are not gonna pop out of nowhere. All comes from your head!

  • We are going to need 2 Strings Arrays. The Strings we use as key words, and the String of underscores (_ _ _ _ _) we use to show the player during the game.
  • We are going to use lots of ‘for’ loops. 
    so let me explain for you a bit.
    Example : ‘for loop command’
     for(int x = 0;x<10;x++){…}
    means we set integer x ,values it zero, then do command in the {…} 
    plus x = 1 when end of command then start doing all the command 
    in the {…}again until x<10
    (until x = 9 in this case because the highest integer below 10 = 9)
  • we will show the player with underscore (_) and replace it with the correct character they input. If they input the wrong one then remain underscore.

Here goes the program!

Just delete all their grey suggestions.You only need mine (*wink*)

Let’s start with the header

  • package everydaygames is your folder name the file is located in.
  • import java.util.*; 
    You have to import some data for this game ,I will tell you more about it later.

Now comes to the body part, the heart of the program!

Create two Arrays of String, hangman[] for our keyword, hangman2[] for what we show the player during the game.
  • the word I choose for today is “Java”
  • this two Strings is the part you have to customize before you play the game.
  • the quantity of the underscores in hangman2[] and quantity of the characters in hangman[] has to be the same
here comes our first ‘for’
  • for(String s : hangman2)
    is the new way to use ‘for’ 
    this means the command in the for loop will run for every Strings there are in the hangman2 Array.
  • I set the command to print underscore
  • “\t” is for tab, try running the program without it and see what happens
  • now we should get _ _ _ _ on the Output screen
Remember good indents make your program easy to understand
  • let’s make another for loop here
     for(int count=1; count<=quantity; count++)
    the quantity is for how many times you allow the player to try to input 
    in this case I values it 10.
  • I create ‘quantity’ variable because it is easier for the programmer to customize the data later ,the programmer can customize three variable on the top.
  • Scanner reader = new Scanner(System.in)
    This “reader” is our new function ‘Scanner’ to make an input command
    this where we need ‘import java.util.*; in the header
  • String y = reader.next();
    is the command where the player input String into y
  • now there will be “please input your (number) letter”
    the number refer to how many times the player input the letter 
    then the player can input the letter 
    this will run for a loop for 10(quantity) times
  • “\n” is for the new line
for in a for! calm down its not that hard to understand!
  • hangman.length 
    is the length of hangman Array
  • for in a for ! The program will run the inner for first until it’s done then the outter for.
  • the inner for is for comparing the player’s answer and the keyword if it’s correct then…
  • hangman2[b]=y;
    this mean now the String number ‘b’ in hangman2 Array now keep the value of ‘y’ variable
  • ** int x [] ={1,2,3,4,5}
    x[0] = 1
    x[1] = 2
    x[2] = 3
    x[3] = 4
    x[4] = 5
    Arrays order start with the 0
  • x++ means x=x+1
    x is for counting how many characters the player answer correctly
    if x=hangman’s length then it means the player correct it all 
    that make the player win
this is about to finish! we nearly get our first game!
  • equals in programming is not “=” but “==”
  • break; 
    command that break out of every loop
  • now we creating the condition for winning and losing
it looks like it’s finished but nah, not yet
  • for(String s2 : hangman2)

{System.out.print(s2+”\t”);}

is for print out every members of hangman2[] after player input a character
so this has to be in the 
for(int count=1;count<=quantity;count++}

if you are not care enough there could be lots of bugs, so do test a lot ,everyway you can!
  • what if the player put the same character! He will ruin our program!!
  • create String z to keep the value of y at the end of the loop to compare with the new value of y 
    if they are the same then means the player input the same character
    count = count -1 // this make the program will not count the same character as a new one
    x = x-1 // if the player put 4 same correct character then he can not win
    (ex. j j j j)

It’s done Let’s test!!!!

when the player win
when the player lose
when the player input the same character

What you can improve

  • Decorate the program,
    give the instruction
  • Try to use easier way than mine.
    Keep it in mind, there are various ways to code!