Click to See Complete Forum and Search --> : Creating a Java game
daveandrews123
December 17th, 2003, 06:54 AM
I am creating a board game but am experiencing a few problems during creation.
When trying to initialise the board not all squares are populated as should be.
I have begun by creating an array of arrays to represent the board, ie
public int p[][] = new int[24][16];
But when initialising not all squares are filled:
public void newGame() {
for (int i=0; i<=23; i++) {
for (int j=0; j<=15; j++) {
p[i][j] = blueTeam;
(where blueTeam prints the blue team member on screen)
This results in printing 15 blue players down the screen, but only the one column. I want to be able to print 24 columns of 16 rows (adding players when required). I’ve tried messing about with the figures (eg 5 columns, 6 columns, etc) but this still only ever prints the one column. Also, it doesn’t offer me row 0, only row 1. Can anyone please suggest where I am going wrong as it’s driving me mad!
If any more code is needed I can add to this post...
Thanks in advance!
cjard
December 17th, 2003, 09:23 AM
im lost.. your fragment:
public void newGame() {
for (int i=0; i<=23; i++) {
for (int j=0; j<=15; j++) {
p[i][j] = blueTeam;
it is better to write loops like this:
public void newGame() {
for (int i=0; i<p.length; i++) {
for (int j=0; j<p[i].length; j++) {
p[i][j] = blueTeam;
that way your array sizes can change and you dont have to rewrite code. also use the and tags when posting code. makes it readable
now, you say:
blueTeam prints the Blue Team member
your statement implies a method call, i.e., an action.. but the line of code:
p[i][j] = blueTeam;
is merely an assignment. nothing is printed.
all you are doing in this code fragment is putting values into an array, i need more info (Not more code) of what you are trying to do and the exact errors you are experiencing
daveandrews123
December 18th, 2003, 06:35 AM
Thanks very much for the reply.
The statement
p[i][j] = blueTeam
is an assignment, but in another class is the method call that prints the team member. I know that this works, as if I try to print a player in, say p[1][1] it works. Using the loop I have at the moment it prints the players, but only in column 1, no more (it also only prints 15 instead of the required 16, ie o up to <=15). So the method seems to be fine - I can paste that if you require?
Basically, the Game class (where this code is from) assigns the values and the Display class prints them when required (using an if statment, ie if p[x][x] == blueTeam, print blueTeam). Compilation is fine and the game runs, only I experience the problems above - it doesn't print all of the required players. Any ideas?!!
dlorde
December 18th, 2003, 06:40 AM
If your print routine doesn't print the way you want it to, there's probably something wrong with it. Why not post it here so we can have a look?
Computers do not solve problems -computers carry out solutions, specified by people, to problems...
D. D. Spencer
daveandrews123
December 18th, 2003, 07:02 AM
OK, this is the code for the printing:
for (int i=1; i<=24; i++)
for (int j=1; j<=16; j++) {
if (game.p[i][j] == game.redTeam) {
g.setColor(Color.red);
g.fillOval(xCenter(i) - redRadius, yCenter(j) - redRadius,
2*redRadius, 2*redRadius);
}
(is that right for the [CODE] bit?)
(and the same again for printing the blue players)
The xCenter and redRadius etc bits have been worked out and they work, as I say the game will print for example 15 blue squares in one column downwards but no more. Hope this helps!
dlorde
December 18th, 2003, 07:59 AM
OK, when you said 'printing' I was expecting some 'print' statements :rolleyes: The xCenter and redRadius etc bits have been worked out and they workThat's as may be, but given the code, I can only assume that method xCenter(i) is returning the same value each time, because if it returned different values each time, the ovals would have different horizontal positions...
Your array indexing is still incorrect (did you not read cjard's post?) - arrays start at 0 and are iterated like this:for (int i=0; i < array.length; i++) { ...It would also be sensible to give the array a name - you say it represents the board, so why not call it 'board'?
Also, if the objects in the array each have a display position, why is the display position not held in each object? Properties of an object should be held in the object...
OO = Encapsulation, Polymorphism, & Inheritance
daveandrews123
December 18th, 2003, 09:08 AM
While I was offline I have been working away on my problem...
...yes I did read the earlier posts, and this was one of the things that I was changing...
...anyway, it was a problem with my print statement - messing about with that has corrected the problem, so it is now sorted.
Oh, and p is for (football) pitch - I will change it to pitch thgroughour my code now.
Thank you both for your help in looking at my problem - much appreciated.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.