GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

how do i strategically code for connect four game using python, that can properly defend and attack.

My board has 10 columns and 8 rows, i can win the game either vertically, horizontally or diagonally.

 

Python Code

 

ai player function

def ai_player(board, turn, valid_moves):

 

My Inputs:

board: numpy array of the disks for each player, e.g.

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],

[0, 0, 0, 1, 2, 0, 0, 0, 0, 0],

[0, 0, 1, 2, 1, 0, 0, 0, 0, 0]]

 

- 0 empty locations

- 1 your disks

- 2 your opponents disks

turn: integer turn counter (starts from 1)

valid_moves: numpy array of valid col index numbers where a disk can be

placed, e.g. [0, 1, 2, 3, 4, 5, 6]

 

Return: col index number --> an integer number of the col where you want to place a disk e.g. 0 (NB: this return value must appear in the valid_moves array)

asked May 19, 2016 by Natasha
...