• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Loop for players

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22563
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Dheesan Dabycharun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, thanks a lot for your quick reply. gotta try it out and be back.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic