# ono_bot4.tcl
# Developed by Leo van Iersel (Ono)
# implements a slightly intelligent dominategame.com robot

# set version
set version 0.4

# initialise global variables

# the continent we want to conquer
set gContinentConquer 0
# the continent we want to kick
set gContinentsKick [list]
# are we busy placing
set gPlacing 0
# how much countries do we have
set gCountries 0
# do we want to attack
set gAttack 0
# with what country do we want to attack
set gFromCountry 0
# what country do we want to attack
set gToCountry 0
# how many dice do we want to use
set gDice 0

# return a random element from a list
proc lrand {list} {
    return [lindex $list [expr int(floor(rand() * [llength $list]))]]
}

# counts enemy neighbours
proc enemy_neighbours {country} {
    set number 0
    
    # store neighbours in variable neighbours
    set neighbours [country neighbours $country]
    
    # loop through the neighbours, looking for enemy-countries
    foreach neighbour $neighbours {
        if {[expr [country owner $neighbour] != [player me]]} then { 
            incr number
        }
    }
    
    return $number
}

# is your country on the border of a controlled continent?
proc border_of_continent {country} {
    
    # find the continent where this country lies in
    set continent [country continent $country]
    
    # do you control this continent?
    foreach contCountry [continent countries $continent] {
    	if {[expr [country owner $contCountry] != [player me]]} then {
    	    return 0
    	}
    }
    
    # store neighbours in variable neighbours
    set neighbours [country neighbours $country]
       
    # loop through the neighbours, looking for countries in not controlled continents
    foreach neighbour $neighbours {
         
         # find the continent where this neigbour lies in
         set continent [country continent $neighbour]
         # do you control this continent?
         foreach contCountry [continent countries $continent] {
    	if {[expr [country owner $contCountry] != [player me]]} then {
    	    return 1
    	}
         }
         
    }
    
    return 0
}

# what continent do you want to conquer
proc 2conquer {} {

    global gContinentConquer
    
    set countries [map countries]
    
    # remove the continents we control
    set tcontinents [map continents] 
    set continents [list]
    foreach continent $tcontinents {
        foreach country [continent countries $continent] {
       	if {[expr [country owner $country] != [player me]]} {
    	    lappend continents $continent
    	    break
    	}
        }
    }
    
    set control -1000
    set best_continent 0
    
    foreach continent $continents {
    	
        # count your and total number of armies in continent
        set contCountries [continent countries $continent]
        set mine 0
        set total 0
        foreach country $contCountries {
            # check if this is one of our own countries
            if {[expr [country owner $country] == [player me]]} {
                set mine [expr [country armies $country] + $mine]
            }
            set total [expr [country armies $country] + $total]
        }
        
        # count your armies on neighbours of the continent
        set nbarmies 0
        foreach country $countries {
            # skip if not our own country
            if {[expr [country owner $country] != [player me]]} {
                continue
            }
            # search for neighbors in the right continent
            set neighbours [country neighbours $country]
            foreach neighbour $neighbours {
                # skip if neighbour is not on the right continent
                if {[expr [country continent $neighbour] != $continent]} {
                    continue
                }
                # count these armies if the neighbour is of a enemy
                if {[expr [country owner $neighbour] != [player me]]} {
                    set nbarmies [expr $nbarmies + [country armies $country] - 1]
                    break
                }
            }
        }
        
        # calculate your "control"
        if {[expr [expr $mine + $nbarmies] > 0]} {
            set tcontrol [expr [expr 100 * $mine] / $total + $nbarmies - 5 * [continent bonus $continent] - $total]
        } else {
            set tcontrol -1000
        } 
                
        if {[expr $tcontrol > $control]} {
            set best_continent $continent
            set control $tcontrol
        }
    }
    
    set gContinentConquer $best_continent
}

# what continent do we want to kick
proc 2kick {} {
   
    global gContinentsKick
    
    set kickContinents [list]
    
    set continents [map continents]
    
    foreach continent $continents {
        set contCountries [continent countries $continent]
        if {[expr [llength $contCountries] == 0]} {
            continue
        }
        set country [lrand $contCountries]
        set owner [country owner $country]
        set 2kick 1
        foreach contCountry $contCountries {
            if {[expr [country owner $contCountry] != $owner]} {
                set 2kick 0
            }
        }
        if {[expr $2kick == 1]} {
            lappend kickContinents $continent
        }
    }
    
   set gContinentsKick $kickContinents
   
}

### FIND A ATTACK ###
#################
proc find_attack {} {
    
    # ---> aims
    # conquer a continent
    # kick other continents
    # get a card
    
    # global variables used
    global gContinentConquer
    global gContinentsKick
    global gFromCountry
    global gToCountry
    global gDice
    global gCountries
    global gAttack
    
    set gAttack 0
    
    # create a list with all countries in the map
    set countries [map countries]
    
    # find attacks on the continent we want to conquer
    set continentFromCountries [list]
    foreach fromCountry $countries {
        
        # check if country represents one of our own countries
        if {[expr [country owner $fromCountry] != [player me]]} then { 
            continue 
        }
        
        # check if there are enough armies on the country
        if {[expr [country armies $fromCountry] <= 1]} then {
            continue 
        }
        
        # store neighbours of the country in a variable
        set toCountries [country neighbours $fromCountry]
        
        # loop through the neighbours
        foreach toCountry $toCountries {
        
            # continue when the neighbour is owned by the robot
            if {[expr [country owner $toCountry] == [player me]]} then { 
                continue 
            }
                
            # continue when you cant use more dice then your opponent
            if {[expr [country armies $fromCountry] < 3]} then {
                continue
            }
            if {[expr [country armies $fromCountry] < 4] && [expr [country armies $toCountry] > 1]} then {
        	    continue
            }
    	
    	# continue when the toCountry is on the wrong continent
             if {[expr [country continent $toCountry] != $gContinentConquer]} then {
                 continue
             }
    	
             # attack when only one opponent
             if {[expr [llength [player list]] == 2]} {
                lappend continentFromCountries $fromCountry
                # stop this loop
                break
             }
            
    	# do not attack with your continent borders when army < 15
    	if {[expr [country armies $fromCountry] < 15] && [expr [border_of_continent $fromCountry] == 1]} then {
        	    continue
    	}
            
            # this is a good attack, append country to list of possible from countries
            lappend continentFromCountries $fromCountry
            
            # stop this loop
            break
        }
    }
    
    # did you find a good from_country?
    if {[expr [llength $continentFromCountries] > 0]} then {
    	
        # pick a random country
        set continentFromCountry [lrand $continentFromCountries]
    	
        # now find country with largest army
        foreach fromCountry $continentFromCountries {
            if {[expr [country armies $fromCountry] > [country armies $continentFromCountry]]} then {
                set continentFromCountry $fromCountry
            }
        }
        
        # create a list with all possible to countries
        set toCountries [list]
    
        # store neighbours in variable neighbours
        set neighbours [country neighbours $continentFromCountry]
        
        # loop through the neighbours, looking for valid destinations
        foreach toCountry $neighbours {
        
            # check if toCountry represents one of our own countries
            if {[expr [country owner $toCountry] == [player me]]} then { 
                continue 
            }
            
            # check if toCountry lies on the right continent
            if {[expr [country continent $toCountry] != $gContinentConquer]} then { 
                continue 
            }
        
            # valid to country
            lappend toCountries $toCountry
        }
        
        # pick a random country
        set continentToCountry [lrand $toCountries]
    	
        # now find country with smallest army
        foreach toCountry $toCountries {
    	if {[expr [country armies $toCountry] < [country armies $continentToCountry]]} then {
    	    set continentToCountry $toCountry
       	}
        }
        
        # number of dice is number of armies on from country minus 1
    	set dice [expr [country armies $continentFromCountry] - 1]
    	
    	# when dice > 3, make it 3
    	if {[expr $dice > 3]} then { 
            set dice 3 
        }
        
        # adjust global variables
        set gToCountry $continentToCountry
        set gFromCountry $continentFromCountry
        set gDice $dice
        set gAttack 1        
    	
        # stop this procedure
        return
    }
    
    # now we want to kick other continents
    if {[expr [llength $gContinentsKick] > 0]} {
        set kickFromCountries [list]
        foreach fromCountry $countries {
            
            # check if handle represents one of our own countries
            if {[expr [country owner $fromCountry] != [player me]]} then { 
                continue 
            }
            
            # check if there are enough armies on the country
            if {[expr [country armies $fromCountry] <= 1]} then {
                continue 
            }
            
            # store neighbours of the country in a variable
            set toCountries [country neighbours $fromCountry]
            
            # loop through the neighbours
            foreach toCountry $toCountries {
            
                # continue when the neighbour is owned by the robot
                if {[expr [country owner $toCountry] == [player me]]} then { 
                    continue 
                }
                    
                # continue when you cant use more dice then your opponent
                if {[expr [country armies $fromCountry] < 3]} then {
                    continue
                }
                if {[expr [country armies $fromCountry] < 4] && [expr [country armies $toCountry] > 1]} then {
        	       continue
                }
    	
                # continue when toCountry doesnt lie on a continent that should be kicked
                set ok 0
                foreach continent $gContinentsKick {
                    if {[expr [country continent $toCountry] == $continent]} then {
            	           set ok 1
        	       }
        	   }
        	   if {[expr $ok == 0]} {
        	       continue
        	   }
        	   
        	   # attack when only one opponent
                if {[expr [llength [player list]] == 2]} {
                    lappend kickFromCountries $fromCountry
                    # stop this loop
                    break
                }
                
                # continue when you dont have (7 - bonus) more armies then your opponent
                set bonus [continent bonus [country continent $toCountry]]
                if {[expr [country armies $fromCountry] <= [expr [country armies $toCountry] + 7 - $bonus]]} then {
            	       continue
        	   }
        	   
        	   # dont attack with the border of a controlled continent when army <10
        	   if {[expr [country armies $fromCountry] < 10] && [expr [border_of_continent $fromCountry] == 1]} then {
        	       continue
    	   }
                
                # this is a good attack, append country to list of possible from countries
                lappend kickFromCountries $fromCountry
                
                # stop this loop
                break
            }
        }
        
        # did you find a good from_country?
        if {[expr [llength $kickFromCountries] > 0]} then {
        	
            # pick a random country
            set kickFromCountry [lrand $kickFromCountries]
        	
            # now find country with largest army
            foreach fromCountry $kickFromCountries {
                if {[expr [country armies $fromCountry] > [country armies $kickFromCountry]]} then {
        	       set kickFromCountry $fromCountry
           	   }
            }
            
            # create a list with all possible to countries
            set toCountries [list]
        
            # store neighbours in variable neighbours
            set neighbours [country neighbours $kickFromCountry]
        
            # loop through the neighbours, looking for valid destinations
            foreach toCountry $neighbours {
            
                # check if toCountry represents one of our own countries
                if {[expr [country owner $toCountry] == [player me]]} then { 
                    continue 
                }
                
                # continue when toCountry doesnt lie on a continent that should be kicked
                set ok 0
                foreach continent $gContinentsKick {
                    if {[expr [country continent $toCountry] == $continent]} then {
            	           set ok 1
        	       }
        	   }
        	   if {[expr $ok == 0]} {
        	       continue
        	   }
            
                # valid to country
                lappend toCountries $toCountry
            }
            
            # pick a random country
            set kickToCountry [lrand $toCountries]
        	
            # now find country with smallest army
            foreach toCountry $toCountries {
                if {[expr [country armies $toCountry] < [country armies $kickToCountry]]} then {
        	       set kickToCountry $toCountry
           	   }
            }
            
            # number of dice is number of armies on from country minus 1
        	set dice [expr [country armies $kickFromCountry] - 1]
        	
        	# when dice > 3, make it 3
        	if {[expr $dice > 3]} then { 
                set dice 3 
            }
            
            # adjust global variables
            set gToCountry $kickToCountry
            set gFromCountry $kickFromCountry
            set gDice $dice
            set gAttack 1        
        	
            # stop this procedure
            return
        }
    }
    
    # now we want to get a card if needed
    # count our countries
    set number 0
    foreach country $countries {
        if {[expr [country owner $country] == [player me]]} {
            incr number
        }
    }
    # check if we need to attack for a card
    if {[expr $number == $gCountries]} {
        
        # choose the attack with the best winchance
        set bestChance -1000
        foreach fromCountry $countries {
            
            # check if handle represents one of our own countries
            if {[expr [country owner $fromCountry] != [player me]]} then { 
                continue 
            }
            
            # check if there are enough armies on the country
            if {[expr [country armies $fromCountry] <= 1]} then {
                continue 
            }
            
            # store neighbours of the country in a variable
            set toCountries [country neighbours $fromCountry]
            
            # loop through the neighbours
            foreach toCountry $toCountries {
            
                # continue when the neighbour is owned by the robot
                if {[expr [country owner $toCountry] == [player me]]} then { 
                    continue 
                }
                    
                # continue when you cant use more dice then your opponent
                if {[expr [country armies $fromCountry] < 3]} then {
                    continue
                }
                if {[expr [country armies $fromCountry] < 4] && [expr [country armies $toCountry] > 1]} then {
        	       continue
                }
    	    
    	   # continue when we already found a attack with a better "winchance"
                if {[expr [expr [country armies $fromCountry] - 2 * [country armies $toCountry]] < $bestChance]} then {
            	       continue
        	   }
                
                # continue when you dont have more more armies then your opponent
                if {[expr [country armies $fromCountry] <= [country armies $toCountry]]} then {
            	       continue
        	   }
        	   
        	   # do not attack with your continent borders when army < 15
    	   if {[expr [country armies $fromCountry] < 15] && [expr [border_of_continent $fromCountry] == 1]} then {
        	       continue
    	   }
        	
        	   # this is a good attack, append country to list of possible from countries
                set cardFromCountry $fromCountry
                set cardToCountry $toCountry
                
                # adjust bestChance
                set bestChance [expr 100 * [country armies $fromCountry] / [country armies $toCountry]]
            }
        }
        
        # did you find a good attack?
        if {[expr $bestChance > 0]} then {
            
            # number of dice is number of armies on from country minus 1
            set dice [expr [country armies $cardFromCountry] - 1]
        	
            # when dice > 3, make it 3
            if {[expr $dice > 3]} then { 
                set dice 3 
            }
            
            # adjust global variables
            set gToCountry $cardToCountry
            set gFromCountry $cardFromCountry
            set gDice $dice
            set gAttack 1
        }
    }
}

### PLACE A ATTACK ###
######################
proc robot_attack {} {
    global gFromCountry
    global gToCountry
    global gDice
    return [list $gFromCountry $gToCountry $gDice]
}

### ATTACK OR END ###
#####################
proc robot_attack_or_end {} {
    
    global gPlacing
    global gAttack
    
    find_attack
    
    set gPlacing 0
    
    if {[expr $gAttack == 1]} then {
        return attack
    } else {
        error "damn dice"
        return end
    }
}

### PLACE OR ATTACK ###
#######################
# place or attack at random
proc robot_place_or_attack {} {

    # fifty-fifty
    if {[expr rand() < 0.5]} then {
        return place
    } else {
        return attack
    }
}

### EXCHANGE CARDS ###
######################
proc robot_exchange_cards {} {

    # check most valuable combinations
    if {[cards contain artillery infantry cavalry]} then {
        return [list artillery infantry cavalry]
    }
    if {[cards contain artillery infantry wildcard]} then {
        return [list artillery infantry wildcard]
    }
    if {[cards contain artillery wildcard cavalry]} then {
        return [list artillery wildcard cavalry]
    }
    if {[cards contain wildcard infantry cavalry]} then {
        return [list wildcard infantry cavalry]
    }
    
    # exchange when we got 4 of a kind
    if {[cards contain infantry infantry infantry infantry]} then {
        return [list infantry infantry infantry]
    }
     if {[cards contain cavalry cavalry cavalry cavalry]} then {
        return [list cavalry cavalry cavalry]
    }
     if {[cards contain artillery artillery artillery artillery]} then {
        return [list artillery artillery artillery]
    }
    
    # do not exchange cards when less than five cards
    if {[expr [cards size] < 5]} then { 
        
        # less than five cards
        return
    }
    
    if {[cards contain cavalry cavalry cavalry]} then {
        return [list cavalry cavalry cavalry]
    }
    if {[cards contain cavalry cavalry wildcard]} then {
        return [list cavalry cavalry wildcard]
    }
    if {[cards contain infantry infantry infantry]} then {
        return [list infantry infantry infantry]
    }
    if {[cards contain infantry infantry wildcard]} then {
        return [list infantry infantry wildcard]
    }
    if {[cards contain artillery artillery artillery]} then {
        return [list artillery artillery artillery]
    }
    if {[cards contain artillery artillery wildcard]} then {
        return [list artillery artillery wildcard]
    }
    
    # this place should be unreachable
}

### INITIAL PLACEMENT ###
#########################
# first try to place in aus or sa
# then to place on coutry next to aus or sa
# group the other armies
proc robot_initial_placement {armies} {
    
    set countries [map countries]
    set continents [map continents]
    
    set control -1000
    set bestContinent 0
    
    # loop through all continents
    foreach continent $continents {
        
        # count our countries in and around this continent
        set in 0
        set around 0
        foreach country $countries {
            # continue if not our own country
            if {[expr [country owner $country] != [player me]]} {
                continue
            }
            # count this country if it is on the continent
            if {[expr [country continent $country] == $continent]} {
                incr in
                continue
            }
            # loop through neighbours
            set neighbours [country neighbours $country]
            foreach neighbour $neighbours {
                # check if the neighbour is on the right continent
                if {[expr [country continent $neighbour] == $continent]} {
                    incr around
                    break
                }
            }
        }
        # count the total number of countries in the continent
        set inCont [continent countries $continent]
        set total [llength $inCont]
        
        # calculate your "control"
        if {[expr $total > 0]} {
            set tcontrol [expr [expr 100 * $in] / $total + 10 * $around - 10 * [continent bonus $continent]  - $total * $total]
        } else {
            set tcontrol -1000
        }
        if {[expr $tcontrol > $control]} {
            set bestContinent $continent
            set control $tcontrol
        }
    }
    
    # first try to place in or around this continent
    set inCountries [list]
    set aroundCountries [list]
    foreach country $countries {
        # continue if not our own country
        if {[expr [country owner $country] != [player me]]} {
            continue
        }
        # continue if country has already 4 armies
        if {[expr [country armies $country] == 4]} {
            continue
        }
        # check if country in the right continent
        if {[expr [country continent $country] == $bestContinent]} {
            lappend inCountries $country
            continue
        }
        # loop through neighbours
        set neighbours [country neighbours $country]
        foreach neighbour $neighbours {
            # add if neighbour is in the right continent
            if {[expr [country continent $neighbour] == $bestContinent]} {
                lappend aroundCountries $country
                break
            }
        }
    }
    # smallest country in the continent
    if {[expr [llength $inCountries] > 0]} {
        set inCountry [lrand $inCountries]
        foreach country $inCountries {
            if {[expr [country armies $country] < [country armies $inCountry]]} {
                set inCountry $country
            }
        }
        return $inCountry
    }
    # smallest country around the continent
    if {[expr [llength $aroundCountries] > 0]} {
        set aroundCountry [lrand $aroundCountries]
        foreach country $aroundCountries {
            if {[expr [country armies $country] < [country armies $aroundCountry]]} {
                set aroundCountry $country
            }
        }
        return $aroundCountry
    }
    # now find armies with 3, 2 and 1 armies
    set 1armie [list]
    set 2armies [list]
    set 3armies [list]
    foreach country $countries {
        # check if our own country
        if {[expr [country owner $country] != [player me]]} {
            continue
        }
        if {[expr [country armies $country] == 3]} {
            lappend 3armies $country
        }
        if {[expr [country armies $country] == 2]} {
            lappend 2armies $country
        }
        if {[expr [country armies $country] == 1]} {
            lappend 1armies $country
        }
    }
    # try country with max number of armies
    if {[expr [llength $3armies] > 0]} {
        set returnCountry [lrand $3armies]
    } elseif {[expr [llength $2armies] > 0]} {
        set returnCountry [lrand $2armies]
    } elseif {[expr [llength $1armies] > 0]} {
        set returnCountry [lrand $1armies]
    }
    return $returnCountry
}

### MOVE ###
###########
proc robot_movement {} {
    
    # find a country with no enemy neighbours and more then 1 army
    set countries [map countries]
    set moveCountries [list]
    foreach country $countries {
        
        # continue if this is not one of our own countries
        if {[expr [country owner $country] != [player me]]} {
            continue
        }
        # continue if this country has enemy neighbours
        if {[expr [enemy_neighbours $country] > 0]} {
            continue
        }
        # continue if this country has only one army
        if {[expr [country armies $country] == 1]} {
            continue
        }
        # continue if this country is on the border of a controlled continent
        if {[expr [border_of_continent $country] == 1]} {
            continue
        }
        # add this country
        lappend moveCountries $country
    }
    # if we found a country
    if {[expr [llength $moveCountries] > 0]} {
        
        # choose a random country
        set moveCountry [lrand $moveCountries]
        # find the biggest one
        foreach country $moveCountries {
            if {[expr [country armies $country] > [country armies $moveCountry]]} {
                set moveCountry $country
            }
        }
        # put the neighbours in a list
        set neighbours [country neighbours $moveCountry]
        # find a neighbour with enemy neighbours or on the border of a controlled continent
        set toCountries [list]
        foreach neighbour $neighbours {
            if {[expr [enemy_neighbours $neighbour] > 0]} {
                lappend toCountries $neighbour
                continue
            }
            if {[expr [border_of_continent $neighbour] == 1]} {
                lappend toCountries $neighbour
            }
        }
        # if we found such neighbour
        if {[expr [llength $toCountries] > 0]} {
            # choose a random one
            set toCountry [lrand $toCountries]
        } else {
            # else choose a random other one
            set toCountry [lrand $neighbours]
        }
        # calculate number of armies to move
        set armies [expr [country armies $moveCountry] - 1]
        # dont move more then 7 armies
        if {[expr $armies > 7]} {
            set armies 7
        }
        # return this move
        return [list $moveCountry $toCountry $armies]
    }
}

### PICK CAPITAL ###
################
# pick a capital at random
proc robot_pick_capital {} {

    # store all countries in a variable
    set countries [map countries]
    
    # find a random country
    while {1} {
        
        # pick a random country
        set country [lrand $countries]
    
        # check if country is valid
        if {[country owner $country] == [player me]} then {
        
            # valid country
            return $country
        }
    }
    
    # unreachable code
}

### PLACE ARMIES ###
#################
# place armies on country with smallest or biggest army
# only place on border-countries
proc robot_placement {armies} {
    
    # --> aims
    # defend controlled continents
    # conquer other continents
    # kick a continent
    # get a card
    
    set continentDefendCountry 0
    set conquerCountry 0
    set kickCountry 0
    set cardCountry 0
    
    global gContinentConquer
    global gContinentsKick
    global gPlacing
    global gCountries
    
    # store all countries in a variable
    set countries [map countries]
        
    # count our countries
    set number 0
    foreach country $countries {
        if {[expr [country owner $country] == [player me]]} {
            incr number
        }
    }
    set gCountries $number
    
    if {[expr $gPlacing == 0]} {
        # check what continent we want to conquer
        2conquer
        # check what continent we want to kick
        2kick
        # do not check again this turn
        set gPlacing 1
    }
    
    # defend controlled continents
    
    set placeCountries [list]
    foreach country $countries {
        # check if this is our own country
        if {[expr [country owner $country] != [player me]]} {
            continue
        }
        # check if the country is on the border of a controlled continent
        if {[expr [border_of_continent $country] == 0]} {
            continue
        }
        # add country if less then 5 armies
        if {[expr [country armies $country] < 5]} {
            lappend placeCountries $country
            continue
        }
        # add country if less then 15 armies and more than 2 players
        if {[expr [llength [player list]] > 2] && [expr [country armies $country] < 15]} {
            lappend placeCountries $country
            continue
        }
        # add country if there is a enemy neighbour with more armies
        set neighbours [country neighbours $country]
        foreach neighbour $neighbours {
            if {[expr [country owner $neighbour] != [player me]]} {
                if {[expr [country armies $neighbour] > [country armies $country]]} {
                    lappend placeCountries $country
                    break
                }
            }
        }
    }
    # if we did find a country...
    if {[expr [llength $placeCountries] > 0]} {
        # find the smallest one
        set continentDefendCountry [lrand $placeCountries]
        foreach country $placeCountries {
            if {[expr [country armies $country] < [country armies $continentDefendCountry]]} {
                set continentDefendCountry $country
            }
        }
    }
    
    # conquer other continents
    set conquerCountries [list]
    foreach country $countries {
        # continue if not our own country
        if {[expr [country owner $country] != [player me]]} {
            continue
        }
        # loop through neighbours
        set neighbours [country neighbours $country]
        foreach neighbour $neighbours {
            # continue if neighbour is our own country
            if {[expr [country owner $neighbour] == [player me]]} {
                continue
            }
            # continue is neighbour is not in the right continent
            if {[expr [country continent $neighbour] != $gContinentConquer]} {
                continue
            }
            # add this country
            lappend conquerCountries $country
            # stop this loop
            break
        }
    }
    # if we did find a good country
    if {[expr [llength $conquerCountries] > 0]} {
        # choose a random country
        set conquerCountry [lrand $conquerCountries]
    }
    
    # kick a continent
    set kickCountries [list]
    foreach country $countries {
        # continue if not our own country
        if {[expr [country owner $country] != [player me]]} {
            continue
        }
        # loop through neighbours
        set neighbours [country neighbours $country]
        foreach neighbour $neighbours {
            # continue if neighbour is our own country
            if {[expr [country owner $neighbour] == [player me]]} {
                continue
            }
            # continue when neighbour doesnt lie on a continent that should be kicked
            set ok 0
            foreach continent $gContinentsKick {
                 if {[expr [country continent $neighbour] == $continent]} then {
                     set ok 1
        	    }
        	}
        	if {[expr $ok == 0]} {
        	     continue
            }
            # add this country
            lappend kickCountries $country
            # stop this loop
            break
        }
    }
    # if we did find a good country
    if {[expr [llength $kickCountries] > 0]} {
        # choose a random country
        set kickCountry [lrand $kickCountries]
    }
    
    # get a card
    set cardCountry 0
    set minNumber 1000
    foreach country $countries {
        # continue if not our own country
        if {[expr [country owner $country] != [player me]]} {
            continue
        }
        # loop through neighbours
        set neighbours [country neighbours $country]
        foreach neighbour $neighbours {
            # continue if neighbour is our own country
            if {[expr [country owner $neighbour] == [player me]]} {
                continue
            }
            # continue if we already found a neighbour with less armies
            if {[expr [country armies $neighbour] >= $minNumber]} {
                continue
            }
            # stop if we already got enough armies to get a card here
            if {[expr [expr [country armies $country] - [country armies $neighbour]] > 2]} {
                set cardCountry 0
                break
            }
            # take this country
            set cardCountry $country
            # store the number of armies
            set minNumber [country armies $neighbour]
        }
    }
        
    # choose a country to place
    if {[expr $continentDefendCountry > 0]} {
        return $continentDefendCountry
    } elseif {[expr $kickCountry > 0] && [expr rand() < 0.3]} {
        return $kickCountry
    } elseif {[expr $cardCountry > 0] && [expr rand() < 0.3]} {
        return $cardCountry
    } elseif {[expr $conquerCountry > 0]} {
        return $conquerCountry
    } else {
        # find random country
        foreach country $countries {
            if {[expr [country owner $country] == [player me]]} {
                lappend randomCountries $country
            }
        }
        return [lrand $randomCountries]
    }
    
}

### TAKE COUNTRY ###
####################
proc robot_take_country {from to min} {
    
    # with only two players:
    # keep only 1 army on from-country
    # if from is on the border of a controlled continent:
    #	try to keep 5 armies on this country
    if {[expr [llength [player list]] == 2]} {
        if {[expr [border_of_continent $from] == 1] && [expr [country armies $from] > [expr 4 + $min]]} then {
        	return [expr [country armies $from] - 5]
        } elseif {[expr [border_of_continent $from] == 1]} then {
            return $min
        } else {
            return [expr [country armies $from] - 1]
        }
    }
        
    # with more than two players:
    # try to keep 4 armies on from-country
    # if from is on the border of a controlled continent:
    #	try to keep 10 armies on this country
    if {[expr [border_of_continent $from] == 1] && [expr [country armies $from] > [expr 9 + $min]]} then {
    	return [expr [country armies $from] - 10]
    } elseif {[expr [border_of_continent $from] == 1]} then {
        return $min
    } elseif { [expr [country armies $from] > [expr 3 + $min]] } then {
    	return [expr [country armies $from] - 4]
    } else {
    	return $min
    }
}