Public feed #381300

Something

public text v1 · immutable
Raw Edit
·published 2007-03-04 19:31 UTC
rendered paste body
/*
 * Board.java
 *
 * Created on January 22, 2007, 11:29 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Mancala;

/**
 * Keeps all the game's pits and kalahas and relays messages to them
 * @author Sophie Quigley
 */
public class Board {
    
    // CLASS CONSTANTS
    
    // CLASS VARIABLES
    
    // INSTANCE VARIABLES
    /**
     * Swing user interface associated with this board
     */
    public static int mm = 0;
    private MancalaUI ui;
    /**
     * Mancala game associated with this board
     */
    private Game game;
    /**
     * Array of all the kalahas in this board
     */
    private Pit[] kalahas = new Pit[Game.PLAYERS];
    /**
     * Array of all the pits in this board
     */
    private Pit[] pits = new Pit[Game.PITS];
    
    // CLASS METHODS
    /** Creates a new instance of Board */
    public Board() {
        for (int player=0; player<Game.PLAYERS; player++) {
            kalahas[player] = new Pit(player, player,  Pit.SCORING);
            for (int i=0; i<Game.PITSPERPLAYER; i++) {
                pits[pitindex(player,i)] = new Pit(player, pitindex(player,i), Pit.PLAYING);
            }
        }
    }
  
    /**
     * Converts a player number and pit number into an index for an array of all the pits
     * @param player number (0-1) representing the player 
     * @return index (0-11) into an array of all the pits in the game
     * @param pit index (0-6) of one of the player's pits
     */
    public static int pitindex(int player, int pit) {
         return mm = Game.PITSPERPLAYER*player+pit;
    }
    
    // INSTANCE  METHODS
    /**
     * Deals seeds into all the pits at the beginning of the game
     * @param seedsperpit Number of seeds distributed in each pit at the beginning of the game
     */
    public void dealSeeds(int seedsperpit) {
        for (int player=0; player<Game.PLAYERS; player++) {
            kalahas[player].setSeeds(0);
            for (int i=0; i<Game.PITSPERPLAYER; i++) {
                pits[pitindex(player,i)].setSeeds(seedsperpit);
            }
        }
    }
 
    /**
     * Converts board to a String
     * @return String describing the board and its contents
     */
    public String toString() {
        String result = "";
        for (int player=0; player<Game.PLAYERS; player++) {
            for (int i=0; i<Game.PITSPERPLAYER; i++) {
                result += "pits[" + pitindex(player,i) + "]: " + pits[pitindex(player,i)];
                result += '\n';
            }
            result += "kalahas[" + player + "]: " + kalahas[player];
            result +='\n';
        }
        return result;
    }

    /**
     * Sets the ui property: Swing user interface representing this board
     * @param ui New value for ui
     */
    public void setUi(MancalaUI ui) {
        this.ui = ui;
        for (int player=0; player<Game.PLAYERS; player++) {
            kalahas[player].setUi(ui);
            for (int i=0; i<Game.PITSPERPLAYER; i++) {
                pits[pitindex(player,i)].setUi(ui);
            }
        }
    }
    
   // public void MoveSeeds(int seeds) {
        
   //        pits[pitindex(player,i)].setUi(ui);
         
         
   //  }

    /**
     * Sets the game property: Mancala game associated with this board
     * @param game New value for game
     */
    public void setGame(Game game) {
        this.game = game;
    }
    
}