CODE
a game similiar to monopoly. squares
represent continents and oceans. Each player
spins a spinner that returns a value range 1 to 3.
The player moves that many places around the
board. If player lands on North America, the
player wins the game and game is over. If
not play passes to next player
public class Player
{
private String name;
private int location;
//sets and gets
public void setName(String newName){name = newName;}
public void setLocation(int newLoc){location = newLoc;}
public String getName(){return name;}
public int getLocation(){return location;}
}
public Player(String aName)
{
name = aName
}
//-------------------------------------------------------------------
import javax.swing.JOptionPane;
import java.util.*;
import java.io.*;
public class GlobeTrotter
{
private Player[] player;
public static void main(String[] args) throws IOException, ClassNotFoundException{
// get the destinations
String[] destinations = new String[12];
Scanner in = new Scanner(new File("Destinations.txt"));
int i = 0;
try{
while(in.hasNext()){
destinations[i++] = in.nextLine();
}//end while in.hasNext
}
finally{
}
// create the players
Player player[] = new Player[4];
player[0].setName("Jerry");
player[1].setName("Gary");
player[2].setName("Mary");
player[3].setName("Larry");
int id = 0;
int winner = -1;
// Loop to play the game
while(winner<0){
move();
//Find out if there was a winner
if(winner<0){
id = (++id) % 4;
}else{
JOptionPane.showMessageDialog(null, "Congratulations, " + player[id].getName()
+ "you have landed on North America and have won the game!");
}//end if winner<0
}//end while
}
//----
private static int move(player[]){
//spin the spinner
int steps = ((int)(Math.random() * 100) %3 +1);
//move the player
int newLoc=player.move(steps);
//tell the player where they are
JOptionPane.showMessageDialog(null, player.getName() + "has rolled a " + steps
+ "and has landed on " + destinations[player.getLocations()]);
if(newLoc == 0){
return i;
}else {
return -1;
}//end if newLoc == 0
}//end Method move
}//end class
This post has been edited by learningjava: 13 Oct, 2008 - 09:30 AM