hi There,

i have made an AI player for the game Oware, i am looking to improve my programming in order to make my player stronger. can anyone help?

here is my code

import random

#pots = [[1,2,8,4,2,6], [4,4,4,4,3,4]]
def ai_player(pots, scoring_pits, turn, valid_moves):


    """
    Input:
        pots: list of pot values for each player, e.g:
              [[4, 4, 4, 4, 4, 4],   <-- always current player
               [4, 4, 4, 4, 4, 4]]   <-- always opponent
        scoring_pits: list of scoring pit values - [0, 0]
        turn: integer turn counter (starts from 1)
        valid_moves: list of valid moves (indexes), e.g:
                     [0, 1, 2, 3, 4, 5]

    Return:
        pot index --> 0 to 5
                  --> pot value can't be zero
                  --> must be an integer object
    """

    points = [0,0,0,0,0,0]                    

    opp_1 = pots[1][0]
    opp_2 = pots[1][1]
    opp_3 = pots[1][2]
    opp_4 = pots[1][3]
    opp_5 = pots[1][4]
    opp_6 = pots[1][5]

    if 1<= opp_1 <= 2:
        points[0] = points[0] + 1



    if 1<= opp_2 <= 2:
          points[1] = points[1] + 1
          if points[0] != 0:
                points[1] = points[1] + 1

    if 1<= opp_3 <= 2:
        points[2] = points[2] + 1
        if points[1] != 0:
                points[2] = points[2] +1
                if points[0] != 0:    
                    points[2] = points[2] +1    

    if 1<= opp_4 <= 2:
        points[3] = points[3] + 1
        if points[2] != 0:
                points[3] = points[3] +1
                if points[1] != 0:    
                    points[3] = points[3] +1 
                    if points[0] != 0:    
                        points[3] = points[3] +1

    if 1<= opp_5 <= 2:
        points[4] = points[4] + 1
        if points[3] != 0:
                points[4] = points[4] +1
                if points[2] != 0:    
                    points[4] = points[4] +1 
                    if points[1] != 0:    
                        points[4] = points[4] +1
                        if points[0] != 0:    
                            points[4] = points[4] +1


    if 1<= opp_6 <= 2:
        points[5] = points[5] + 1
        if points[4] != 0:
                points[5] = points[5] +1
                if points[3] != 0:    
                    points[5] = points[5] +1 
                    if points[2] != 0:    
                        points[5] = points[5] +1
                        if points[1] != 0:    
                            points[5] = points[5] +1
                            if points[0] != 0:    
                                points[5] = points[5] +1

    play_max = 0 
    max_points = max(points)
    if max_points != 0:
        for i in range(5):
            if points[i] == max_points:
                position_max = i  
                play_max = position_max + 1 

#print position_max, "pos"
#print points, "points"        
#print max_points , "max"
#print play_max, "play_max"

    ours = [0,0,0,0,0,0]        
    ours[5] = pots[0][0]        
    ours[4] = pots[0][1]
    ours[3] = pots[0][2]
    ours[2] = pots[0][3]
    ours[1] = pots[0][4]
    ours[0] = pots[0][5]      

#print ours

    for p in range (5):
        if ours[p] == play_max + p:
            n = len(valid_moves)
            for z in range(n):
                        if p == valid_moves[z]:
                            ind = p
                            return ind



    else:
       ind = random.choice(valid_moves)
       return ind

#miooprint ind, "ind"   
#print pots

"""
    def_points = [0,0,0,0,0,0]

    if 1<= ours[5] <=2:
        def_points[5] = def_points[5] + 2

    if 1<= ours[4] <=2:
        def_points[4] = def_points[4] + 2    
        if def_points[4] == 2:
            def_points[5] += 1 

    if 1<= ours[3] <=2:
        def_points[3] = def_points[4] + 1    
        if def_points[3] == 2:
            def_points[4] += 1
            if def_points[5] == 2:
                def_points[5] += 1       

    if 1<= ours[2] <=2:
        def_points[2] = def_points[2] + 1    
        if def_points[2] == 2:
            def_points[3] += 1
            if def_points[4] == 2:
                def_points[4] += 1

    if 1<= ours[1] <=2:
        def_points[1] = def_points[1] + 1    
        if def_points[1] == 2:
            def_points[2] += 1
            if def_points[3] == 2:
                def_points[3] += 1

    if 1<= ours[0] <=2:
        def_points[0] = def_points[0] + 1    
        if def_points[0] == 2:
            def_points[1] += 1
            if def_points[2] == 2:
                def_points[2] += 1

    #print def_points

    m = len(valid_moves)    
    def_move = max(def_points)
    if def_move != 0:
        for c in range(5):
            if pots[0][c] == def_move:
                for z in range(m):
                        if c == valid_moves[z]:
                            ind = int(c)
                            return ind

""" 
"""          
    else:
        ind = random.choice(valid_moves)
        return ind                

"""

I'd start by refactoring for concision and readability; this will make your algorithm easier to analyze.

The most obvious improvement to me is with the nested if statements that start out looking like this:

if 1<= opp_6 <= 2:

These could be turned into a function that takes each of the opp_x variables and updates the points array using a simple for loop. Moving another step past that, it could even loop through pots[1][x] directly, so you wouldn't have to copy out the array values.

At the algorithmic level, it looks like you're doing a simple static analysis of the board position. Are you familiar with optimization approaches like minimax?

Unfortunately have only learnt the basics of python , and need to use these basics to create this ai player with the best offense and defense .. This is what I have come up with so far and am trying to make the player stronger or less prone to defeat

Does "need to use these basics" include/permit writing separate functions to reuse inside of ai_player?

you could do that but would have to show the function you have made in the ai player programing

Excellent. Yes, the code would have to be right there next to ai_player, but that's fine; it's just a handy organizational tool.

So on the "make code better" front, I'd start here:

if 1<= opp_6 <= 2:
    points[5] = points[5] + 1
    if points[4] != 0:
            points[5] = points[5] +1
            if points[3] != 0:    
                points[5] = points[5] +1 
                if points[2] != 0:    
                    points[5] = points[5] +1
                    if points[1] != 0:    
                        points[5] = points[5] +1
                        if points[0] != 0:    
                            points[5] = points[5] +1

This can be simplified (from a syntax perspective) into a single loop. Have a try at writing one that cycles down through points[n]. Meanwhile, I'll ponder which kind of loop makes the most sense.

For the "play the game better" goal... it looks like you're picking the move that improves your immediate position. A next step there would be to look ahead a bit to consider what opportunities each possible move would open up for your opponent. Anecdotal example: My grandfather always played a very short-sighted game of Scrabble, so playing directly after him was always the favorite seat; he would invariably leave at least one triple word score wide open over the course of the game.

Thanks so much for the help really appreciate it !!

I see what you saying about looking at what the opponents next move .. But that's also where I have come for abit of help . I don't know where to start in programming the Ai player to select the best possible move with gaining the highest points , but also stopping the opponent from having a "better" move to follow , at same time ..

I don't know where to start in programming the Ai player to select the best possible move with gaining the highest points , but also stopping the opponent from having a "better" move to follow , at same time

Have you read the minimax article I linked? It's one of the fundamental decision techniques for games like this.

This article has been dead for over six months. Start a new discussion instead.