mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-06 01:57:58 -08:00
Merge pull request #1 from ribtips/ribtips-evenwins-logic-patches
Logic fixes and patches for evenwins in perl and python
This commit is contained in:
@@ -21,7 +21,7 @@ sub game_play {
|
|||||||
if ($choice == 0) {
|
if ($choice == 0) {
|
||||||
until ($marbles == 0) {
|
until ($marbles == 0) {
|
||||||
|
|
||||||
my $computer_choice = &computer_select($marbles,$turn);
|
my $computer_choice = &computer_select($marbles,$turn,$player_total);
|
||||||
$marbles = $marbles - $computer_choice;
|
$marbles = $marbles - $computer_choice;
|
||||||
$computer_total = $computer_total + $computer_choice;
|
$computer_total = $computer_total + $computer_choice;
|
||||||
print "MY TOTAL IS $computer_total\n";
|
print "MY TOTAL IS $computer_total\n";
|
||||||
@@ -52,7 +52,7 @@ sub game_play {
|
|||||||
|
|
||||||
if ($marbles == 0) {&determine_winner($computer_total,$player_total)};
|
if ($marbles == 0) {&determine_winner($computer_total,$player_total)};
|
||||||
|
|
||||||
my $computer_choice = &computer_select($marbles,$turn);
|
my $computer_choice = &computer_select($marbles,$turn,$player_total);
|
||||||
$marbles = $marbles - $computer_choice;
|
$marbles = $marbles - $computer_choice;
|
||||||
$computer_total = $computer_total + $computer_choice;
|
$computer_total = $computer_total + $computer_choice;
|
||||||
print "MY TOTAL IS $computer_total\n";
|
print "MY TOTAL IS $computer_total\n";
|
||||||
@@ -114,6 +114,7 @@ sub player_select {
|
|||||||
sub computer_select {
|
sub computer_select {
|
||||||
my $marbles = shift;
|
my $marbles = shift;
|
||||||
my $turn = shift;
|
my $turn = shift;
|
||||||
|
my $player_total = shift;
|
||||||
my $num = 2;
|
my $num = 2;
|
||||||
my $validity = "invalid";
|
my $validity = "invalid";
|
||||||
if ($turn == 0) {
|
if ($turn == 0) {
|
||||||
@@ -122,14 +123,24 @@ sub computer_select {
|
|||||||
else {
|
else {
|
||||||
until ($validity eq "valid") {
|
until ($validity eq "valid") {
|
||||||
my $R=$marbles-6*int(($marbles/6));
|
my $R=$marbles-6*int(($marbles/6));
|
||||||
if ($marbles < 4.2) {
|
|
||||||
|
if (int($player_total/2) == $player_total/2) {
|
||||||
|
if ($R < 1.5 || $R > 5.3) {
|
||||||
|
$num = 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$num = $R - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
elsif ($marbles < 4.2) {
|
||||||
$num = $marbles;
|
$num = $marbles;
|
||||||
}
|
}
|
||||||
if ($R > 3.4) {
|
elsif ($R > 3.4) {
|
||||||
if ($R < 4.7 || $R > 3.5) {
|
if ($R < 4.7 || $R > 3.5) {
|
||||||
$num = 4;
|
$num = 4;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$num = 1;
|
$num = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -142,17 +142,32 @@ def game_over():
|
|||||||
print('')
|
print('')
|
||||||
|
|
||||||
def computer_turn():
|
def computer_turn():
|
||||||
global marbles_in_middle
|
global marbles_in_middle
|
||||||
global computer_marbles
|
global computer_marbles
|
||||||
|
global human_marbles
|
||||||
|
|
||||||
print("It's the computer's turn ...")
|
marbles_to_take=0
|
||||||
max_choice = min(4, marbles_in_middle)
|
|
||||||
|
|
||||||
# choose at random
|
print("It's the computer's turn ...")
|
||||||
n = random.randint(1, max_choice)
|
r = marbles_in_middle - 6 * int((marbles_in_middle/6)) #line 500
|
||||||
print(f'Computer takes {marbles_str(n)} ...')
|
|
||||||
marbles_in_middle -= n
|
if int(human_marbles/2) == human_marbles/2: #line 510
|
||||||
computer_marbles += n
|
if r < 1.5 or r > 5.3: #lines 710 and 720
|
||||||
|
marbles_to_take = 1
|
||||||
|
else:
|
||||||
|
marbles_to_take = r - 1
|
||||||
|
|
||||||
|
elif marbles_in_middle < 4.2: #line 580
|
||||||
|
marbles_to_take = marbles_in_middle
|
||||||
|
elif r > 3.4: #line 530
|
||||||
|
if r < 4.7 or r > 3.5:
|
||||||
|
marbles_to_take = 4
|
||||||
|
else:
|
||||||
|
marbles_to_take = r + 1
|
||||||
|
|
||||||
|
print(f'Computer takes {marbles_str(marbles_to_take)} ...')
|
||||||
|
marbles_in_middle -= marbles_to_take
|
||||||
|
computer_marbles += marbles_to_take
|
||||||
|
|
||||||
def play_game():
|
def play_game():
|
||||||
global marbles_in_middle
|
global marbles_in_middle
|
||||||
|
|||||||
Reference in New Issue
Block a user