Paste: payoff matrices for ayo

Author: Rex
Mode: ml
Date: Tue, 2 Jun 2009 19:13:01
Plain Text |
% A toolkit for calculating payoff matrices
% for the game of Ayo.
% Created by Rex Ford, 2009
% James Madison University

% my bin numbering system...
% this would be 3 choices:
%   6 5 4
%   1 2 3

board=zeros(1,15);

% are these two positions on the same side?
function is_it =same_side(board,x,y)
	is_it=false;
	n=length(board)/2;
	if(x>n && y>n)
		is_it=true;
	end
	if(x<=n && y<=n)
		is_it=true;
	end
end


% calculates the number of stones obtained
% by making a certain choice on a board
function [score,board] = advantage(board,choice)
	number_there=board(choice);
	board(choice)=0;
	pos=choice;
	for x=1:number_there
		pos=mod(pos,length(board))+1;
		if(pos==choice)
			pos=mod(pos,length(board))+1;
		end
		board(pos)=board(pos)+1;
	end
	% now, we must steal!
	score=0;
	while((board(pos)==2 || board(pos)==3) && ~same_side(board,choice,pos))
		score=score+board(pos);
		board(pos)=0;
		pos=pos-1;
		if(pos==0)
			pos=length(board);
		end
	end
end

% calculates the payoff matrix
% for a given board
function payoff=pom(board)
	n=length(board)/2;
	payoff1=zeros(n,n);
	for x=1:n
		[p,board2]=advantage(board,x);
		for y=1:n
			q=advantage(board2,y+n);
			payoff1(x,y)=p;
		end
	end
	
	payoff2=zeros(n,n);
	for x=1:n
		[p,board2]=advantage(board,x);
		for y=1:n
			q=advantage(board2,y+n);
			payoff2(x,y)=q;
		end
	end
	
	payoff3=zeros(n,n);
	for x=1:n
		 for y=1:n
		 	payoff3(x,y)=payoff1(x,y)-payoff2(x,y);
		 end
	end
	payoff1
	payoff2
	payoff3
end


function show_board(board)
	n=length(board)/2;
	[board(n+1:2*n);board(1:n)]
end



% This is the cadillac of payoff matrices
% for Ayo. The numbering is shifted for q's turns
% so that both players are choosing within one range.
function play
	p_score = 0;
	q_score = 0;
	choices = input("how many bins on each side?");
	stones = input("how many stones in each bin?");
	
	the_board=stones*ones(1,choices*2);
	while(sum(abs(the_board(1:choices)))*sum(abs(the_board(choices+1:2*choices)))~=0)
		% the dashboard...
		pom(the_board)
		show_board(the_board);
		p_score
		q_score
		p=input("what choice does p make? ");
		while(p<1 || p>choices || the_board(p)==0)
			p=input("Pick another hole. ");
		end
		[p_theft,the_board]=advantage(the_board,p);
		p_score=p_score+p_theft;
		q=input("what choice does q make? ");
		while(q<1 || q>choices || the_board(q)==0)
			q=input("Pick another hole. ");
		end
		q=q+choices; % this shifts q's choice to their side
		[q_theft,the_board]=advantage(the_board,q);
		q_score=q_score+q_theft;
	end
	show_board(the_board);
	p_score
	q_score
end

New Annotation

Summary:
Author:
Mode:
Body: