File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Loop for players Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Loop for players" Watch "Loop for players" New topic
Author

Loop for players

Dheesan Dabycharun
Greenhorn

Joined: Nov 16, 2009
Posts: 3
Hi guys,

Currently programming a game in java in which i want to have the below:-
1. user can play with computer.
2. user can choose to select from 2 to 4 players.

So far i can only make 2 players work using the following method.

public void changePlayer () {

// loop for currentPlayer

if (currentPlayer == player1) {
currentPlayer = player2;
} else if (currentPlayer == player2) {
currentPlayer = player1;
}

1. How can make the computer be the player?
2. How can i add up to 4 players in this loop?
(tried to make more than 2 player in the above but the token is not moving.
3. Thought of using a loop like but bit confused how to make it.
for (int i = 0; i<4; i++)

Thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

1) Make it a Player instance, or the instance of a special subclass that does not need user interaction.
2+3) Use an array (or even better, a List) with an index that specifies the current index. In pseudo code:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dheesan Dabycharun
Greenhorn

Joined: Nov 16, 2009
Posts: 3
yeah, thanks a lot for your quick reply. gotta try it out and be back.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loop for players