• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Games in java

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know, I have a game in java about the snake, I have snake with 40 Points and I put all points in a variable called MAX_POINT who is the body the snake and the tail I´ll put inside the variable i = tail of snake. I want to know What´s this expression does: i = (i+1) % MAX_POINTS in the JPanel I know i will increment the tail point and one, but why need to take a rest of division in the body of snake, I have this doubt.

Thank you,

Freakjar.
 
Bartender
Posts: 2590
124
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fernando hiar wrote: I´ll put inside the variable i = tail of snake.


What value does 'i' hold ? I am not able to understand...
 
Fernando hiar
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fernando hiar wrote:I want to know, I have a game in java about the snake, I have snake with 40 Points and I put all points in a variable called MAX_POINT who is the body the snake and the tail I´ll put inside the variable i = tail of snake. I want to know What´s this expression does: i = (i+1) % MAX_POINTS in the JPanel I know i will increment the tail point and one, but why need to take a rest of division in the body of snake, I have this doubt.

Thank you,

Freakjar.




The value is an int, i´ll put a body snake inside of array e I have a variable tail and the head the rest is the body I´ll put a variable tail in i and I did the expression, I want to know what´s this expression does: i = (i+1) % MAX_POINTS, the max points is the body snake and i is the tail snake..... i want to know because I get the rest of division, but what´s behave in the JPanel this expression.
 
salvin francis
Bartender
Posts: 2590
124
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lets take a simple example:



output



as you might understand, the i value increments itself... but when it crosses MAX_POINTS - 1, it returns to 0

a similar output is seen when changing the expression to:



output:


 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually % symbol returns the remainder value. That is, if you put 5%2 means it will return 1. Because the quotient is 2 here and the remainder is 1.
Same way, if you put 2%5 means, you will get 2 as the answer. Because here the quotient is 0 & the remainder is 2. So your (i+1)%MAX_VALUE always return i+1 upto i=38. When i=39, the expression (i+1)%MAX_VALUE becomes (39+1)%40 which is equal to 0. Also when i=40, the result is 41%40=1

Thank you
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Raj AbigQuestion wrote:


Please check your private messages for an important administrative matter
 
Their achilles heel is the noogie! Give them noogies tiny ad!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic