#!/usr/bin/perl # this program creates a text based maa board with text input ######################################### ########### DON'T PANIC ######################################### # bins for each player (should be 7 - 6 bins + 1 mancala) @p1 = (4,4,4,4,4,4,0); @p2 = (4,4,4,4,4,4,0); while (\$in ne "Q"){ # send the routine to draw the board the number of stones in each bin &draw_board(\$p1[0],\$p1[1],\$p1[2],\$p1[3],\$p1[4],\$p1[5],\$p1[6], \$p2[0],\$p2[1],\$p2[2],\$p2[3],\$p2[4],\$p2[5],\$p2[6]); # ask for input print "enter bin to distribute or Q to exit:"; # get keyboard input \$in = <>; # remove the carriage return from the entry chomp \$in; # edit check the input # return to top of loop if Q is entered next if (\$in eq "Q"); # display error message if a non digit or bin number is out of range # then erturn to the top of the loop if (\$in =~ /\D/ || \$in < 1 || \$in > 12){ print " bin can only be 1 through 12 or Q\n\n"; next; } # we have a valid entry, so distribute stones &distribute_stones(\$in); } sub distribute_stones{ # set variable \$bin to the user input bin t be distribuetd \$bin = \$_[0]; # set the variable \$stones_2_distribute equal to the number of stones # in the array for player 1 or player 2 represented by the bin number # the user entered. Then set that array entry to 0. (because all of # the stones in that bin have been picked up to be distributed) # # remember - perl arrays are indexed from 0 - or simply stated, the # first entry used in a per array is number 0. # this code is for the player 1 array (@p1) if (\$bin == 1){ \$stones_2_dist = \$p1[0]; \$p1[0] = 0; } if (\$bin == 2){ \$stones_2_dist = \$p1[1]; \$p1[1] = 0; } if (\$bin == 3){ \$stones_2_dist = \$p1[2]; \$p1[2] = 0; } if (\$bin == 4){ \$stones_2_dist = \$p1[3]; \$p1[3] = 0; } if (\$bin == 5){ \$stones_2_dist = \$p1[4]; \$p1[4] = 0; } if (\$bin == 6){ \$stones_2_dist = \$p1[5]; \$p1[5] = 0; } # this code is for player 2 array (@p2) if (\$bin == 7){ \$stones_2_dist = \$p2[0]; \$p2[0] = 0; } if (\$bin == 8){ \$stones_2_dist = \$p2[1]; \$p2[1] = 0; } if (\$bin == 9){ \$stones_2_dist = \$p2[2]; \$p2[2] = 0; } if (\$bin == 10){ \$stones_2_dist = \$p2[3]; \$p2[3] = 0; } if (\$bin == 11){ \$stones_2_dist = \$p2[4]; \$p2[4] = 0; } if (\$bin == 12){ \$stones_2_dist = \$p2[5]; \$p2[5] = 0; } # variable \$bin_switch will help us determine where # to start distributing stones, then will be set to 0 # so the remaining stones can be distributed in order # \$bin_switch = \$bin; while (\$stones_2_dist > 0){ # please see coding notes below # this piece of code will only be accessed when stone distribution # comes around the corner - it will never be the first bin a # stone is distributed to if (\$bin_switch < 1){ \$p1[0]++; \$stones_2_dist--; \$bin_switch = 0; } next if (\$stones_2_dist < 1); if (\$bin_switch < 2){ # add 1 to player 1 array first entry # the folloing line is shorthand for \$p1[0] = \$p1[0] + 1 \$p1[1]++; \$stones_2_dist--; # subtract one from our pile of stones to distribute \$bin_switch = 0; # set the bin switch to zero so remainder of stones can be distribnuted } # this line will cause control to return to the top of the # loop, if there are no more stones to distribute, where # the while statement will terminate the loop next if (\$stones_2_dist < 1); if (\$bin_switch < 3){ \$p1[2]++; \$stones_2_dist--; \$bin_switch = 0; } next if (\$stones_2_dist < 1); if (\$bin_switch < 4){ \$p1[3]++; \$stones_2_dist--; \$bin_switch = 0; } next if (\$stones_2_dist < 1); if (\$bin_switch < 5){ \$p1[4]++; \$stones_2_dist--; \$bin_switch = 0; } next if (\$stones_2_dist < 1); if (\$bin_switch < 6){ \$p1[5]++; \$stones_2_dist--; \$bin_switch = 0; } next if (\$stones_2_dist < 1); #determine if a stone should be dropped in player 1's #mancala (bins 1-6 belong to player 1) if (\$bin > 0 && \$bin <7){ \$p1[6]++; \$stones_2_dist--; \$bin_switch = 0; } next if (\$stones_2_dist < 1); ## begin player 2 array # this piece of code will only be accessed when stone distribution # comes around the corner - it will never be the first bin a # stone is distributed to if (\$bin_switch < 7){ \$p2[0]++; \$stones_2_dist--; \$bin_switch = 0; } next if (\$stones_2_dist < 1); if (\$bin_switch < 8){ \$p2[1]++; \$stones_2_dist--; \$bin_switch = 0; } next if (\$stones_2_dist < 1); if (\$bin_switch < 9){ \$p2[2]++; \$stones_2_dist--; \$bin_switch = 0; } next if (\$stones_2_dist < 1); if (\$bin_switch < 10){ \$p2[3]++; \$stones_2_dist--; \$bin_switch = 0; } next if (\$stones_2_dist < 1); if (\$bin_switch < 11){ \$p2[4]++; \$stones_2_dist--; \$bin_switch = 0; } next if (\$stones_2_dist < 1); if (\$bin_switch < 12){ \$p2[5]++; \$stones_2_dist--; \$bin_switch = 0; } next if (\$stones_2_dist < 1); #determine if a stone should be dropped in player 2's #mancala (bins 7-12 belong to player 2) if (\$bin > 6 && \$bin <13){ \$p2[6]++; \$stones_2_dist--; \$bin_switch = 0; } # the following line is not technically required # but will save trouble if you ever need to add # some code between this line and the end of the loop next if (\$stones_2_dist < 1); } } sub draw_board{ # display the board print "\n\n\n"; printf " Bin=| %3d | %3d | %3d | %3d | %3d | %3d | \n\n" ,12,11,10,9,8,7; printf "-------------------------------------------------\n"; printf " %3d | %3d | %3d | %3d | %3d | %3d | %3d | \n" ,\$_[13],\$_[12], \$_[11],\$_[10],\$_[9],\$_[8],\$_[7]; printf " |-----+-----+-----+-----+-----+-----| \n"; printf " | %3d | %3d | %3d | %3d | %3d | %3d | %3d\n" ,\$_[0], \$_[1],\$_[2],\$_[3],\$_[4],\$_[5],\$_[6]; printf "-------------------------------------------------\n"; printf "\n Bin=| %3d | %3d | %3d | %3d | %3d | %3d | \n\n" ,1,2,3,4,5,6; }