/* * (c) 1998-2000 The Brookings Institution, All Rights Reserved * * Permission to use this software and its documentation for non-commercial * purposes and without fee is hereby granted, provided this copyright statement * is included. Please contact us for permission for redistribution and other uses. *  * BROOKINGS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,  * OR NON-INFRINGEMENT. BROOKINGS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY * LICENSEE AS A RESULT OF USING THIS SOFTWARE OR ITS DERIVATIVES. * * _Sugarscape_ * See _Growing Artificial Societies: Social Science from the Ground Up_ * Brookings Institution Press, The MIT Press * Joshua Epstein * jepstein@brook.edu * Robert Axtell * raxtell@brook.edu * * Miles Parker * mparker@brook.edu * http://www.brook.edu/es/dynamics/models/ascape * The Brookings Institution * Washington, D.C. */package edu.brook.sugarscape;import java.lang.*;import java.util.*;import java.awt.Color;import edu.brook.ascape.model.*;import edu.brook.ascape.util.*;public class CulturalSexualAgent extends SexualAgent {        protected static int tagLength;        protected boolean[] tag;        public void initialize() {        super.initialize();        tag = new boolean[tagLength];        for (int i = 0; i < tag.length; i++) {            //We do a draw for each random value instead of using values from large integers            //to ensure that there are no artifacts.            tag[i] = randomIs();        }    }        public void play(Agent neighbor) {        int position = randomToLimit(tag.length);        ((CulturalSexualAgent) neighbor).setTagPosition(position, this.getTagPosition(position));        //In case we changed majority...        requestUpdate();    }        public static int getTagLength() {        return tagLength;    }        public static void setTagLength(int _tagLength) {        tagLength = _tagLength;    }        public boolean getTagPosition(int position) {        return tag[position];    }        public void setTagPosition(int position, boolean value) {        tag[position] = value;    }        public boolean[] getTag() {        return tag;    }        public void setTag(boolean[] tag) {        this.tag = tag;    }        public boolean getMajority() {        int count0 = 0;        int count1 = 0;        int majoritySize = (int) Math.ceil(tag.length / 2);        for (int i = 0; i < tag.length; i++) {            if (!tag[i]) {                count0++;                if (count0 > majoritySize) {                    return false;                }            }            else {                count1++;                if (count1 > majoritySize) {                    return true;                }            }        }        throw new RuntimeException("Internal Error in CulturalSexualAgent.getMajority");    }}
