# MATLAB: I’m coding a Connect4 game. Firstmove should play bottom middle of an empty board if I am first, secondmove should play to the left or right of the other player if I am second. Why does the code play bottom middle and to the left of the own move

connect4

function state = play_connect_four_MeisSunnarborg(state, player)if firstmove(state) == true    state(6,4) = player;endif secondmove(state) == true    col = find(state(6,:),1);    state(6,col-1) = player;endif secondmove(state) == false    col = find(state(6,:),1);    state(6,col+1) = player;endendfunction firstmove = firstmove(state)    if state == zeros(6,7)        firstmove = true;    else ,firstmove = false;    endendfunction secondmove = secondmove(state)col = find(state(6,:),1);if col == 2 || 3 || 4 || 5 || 6 || 7    secondmove = true;endif col == 1    secondmove = false;endend

• Several problems:
Replace this line:
    if state == zeros(6,7)
with this to test for matrix equality:
    if isequal(state,zeros(6,7))
Or, if you want generic code that is not tied to any particular matrix size, maybe this instead:
    if( all(state(:)==0) )
Replace this line:
if col == 2 || 3 || 4 || 5 || 6 || 7
with this to test if a number is one of several possible numbers:
if( ismember(col,[2 3 4 5 6 7]) )
Don't name a return variable the same as a function name. So change this line:
function firstmove = firstmove(state)
to something like this instead (and change all variable references to "result" also):
function result = firstmove(state)
Same comment for the "secondmove" function.
The "firstmove" and "secondmove" functions return a value, but you don't do anything with this value in the calling code. So you need to examine your logic and determine what you want to do with these result variables in your calling code.