2013 Token Pass

<< 2013 DownloadInfo | APQuestionsTrailIndex | 2013 GridWorldUtilities >>

Click here to see the questions from 2013

TokenPassTester.java

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class TokenPassTester extends Applet implements ActionListener
{
	private Button exBtn, distBtn, newBtn; 
	private TextField num;

	private Label message;
	private TokenPass tp;
	private Panel cp;

	public  void init() 
	{

		exBtn = new Button("Example");
		distBtn = new Button("Distribute");
		newBtn = new Button("New Game");
		exBtn.addActionListener(this);
		distBtn.addActionListener(this);
		newBtn.addActionListener(this);
		num = new TextField("4");
		setLayout(new BorderLayout());
		Panel n=new Panel();
		n.setLayout(new GridLayout(0,5));
		n.add(exBtn);
		n.add(distBtn);
		n.add(num);
		n.add(new Label(" Players"));

		n.add(newBtn);
		add(BorderLayout.NORTH, n);
		message = new Label("hi");
		add(BorderLayout.SOUTH, message);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object btn = e.getSource();
		if (btn==exBtn){
			message.setText("Example Button pressed");
			tp= new TokenPass(4);
			int[] b={3,2,6,10};
			tp.setTokens(b);
			tp.setActive(2);
			repaint();
		}
		if (btn==distBtn){
			message.setText("Distribute Button pressed");
			tp.distributeCurrentPlayerTokens();
			repaint();
		}
		if (btn==newBtn){
			message.setText("New Game Button pressed");
			int p=Integer.parseInt(num.getText());
			tp = new TokenPass(p);
			repaint();
		}
	}

	public void paint(Graphics g) {
		if (tp==null)
			return;
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(Color.BLUE);

		int n = tp.getBoard().length;
		for (int i=0; i<n; i++){
			g.drawString ("Player "+i, i*100+50, 60);
			g.drawString(tp.getBoard()[i]+" tokens", i*100+50, 100);
			if(i==tp.getCurrentPlayer())
				g.drawString("current", i*100+50, 45);
		}

	}

}

TokenPass.java


public class TokenPass {

	private int[] board;
	private int currentPlayer;

	/** Creates the board array to be of size playerCount and fills it with
	* random integer values from 1 to 10, inclusive. Initializes currentPlayer to a
	* random integer value in the range between 0 and playerCount-1, inclusive. 
	* @param playerCount the number of players
	*/
	public TokenPass(int playerCount)
	{ /* to be implemented in part (a) */ 


	}
	/** Distributes the tokens from the current player's position one at a time to each player in
	* the game. Distribution begins with the next position and continues until all the tokens
	* have been distributed. If there are still tokens to distribute when the player at the
	* highest position is reached, the next token will be distributed to the player at position 0.
	* Precondition: the current player has at least one token.
	* Postcondition: the current player has not changed.
	*/
	public void distributeCurrentPlayerTokens()
	{  /* to be implemented in part (b) */

	}
}