  // written by: Mike Puntolillo
  // assisted by: Juan Bazurto, Rich Pullose
  // debugged by: Ehud Cohen, Justin Phalon

package minoritygame;

import java.lang.Math;
/**
 *
 * @author PuntolMi
 */
public class Strategy
{

    private final int M = gameParameters.M;
    private final int strategyID;
    private final int rows=(int)Math.pow(2, M);//number of rows in strategy table
    private char [][]table = new char [rows][M+1];
    private double score = 0;//strategy's score
    
    public Strategy(int num)
    {
        int cnt=0;
        strategyID=num;
        for(int x=0; x<rows; x++)
        {
            String s = Integer.toBinaryString(x);
            String n=Integer.toBinaryString(num);
            while(s.length()<M)
            {
                s='0'+s;
            }

            while(n.length()<rows)
            {
                n='0'+n;
            }
            
            for(int y=0; y<M; y++)
            {   
                table[x][y]=s.charAt(y);
            }

            table[x][M]=n.charAt(cnt);
            cnt++;
             
        }

    }

    public int decision(char [] mem)
    {   int row=0;
        for(int x=0; x<M; x++)
        {
            
            while(mem[x]!=table[row][x])
            {
                row++;
            }

        }

        char r=table[row][M];

        if(r=='0')
    {
            return -1;
        }
        else if(r=='1')
        {
            return 1;
        }

        return -1;
    }

    public void adjustScore()
    {
        score=score*gameParameters.ALPHA;
    }
    public void incrementScore()
    {
        score++;
    }

    public double getScore()
    {
        return score;
    }
    public void setScore(double s)
    {
        score=s;
    }

    public void printStrategy()
    {
          for(int x =0; x<rows; x++)
        {
            for(int y=0; y<=M; y++)
            {
                System.out.print(table[x][y]);
            }
            System.out.println();
        }

    }

    public int getStrategyID()
    {
        return strategyID;
    }

}

