0

This is a very rudimentary code that my team has developed for a connect 4 game in Matlab. We can currently display the board, ask for user input and correctly display the chips, but are having issues writing code that says who the winner is. We are currently trying loops within each case to check, but are still running into problems.

Code:

clear;close all;
load Connect
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
Array = zeros(6,7);
Player = 1;
Winner = 0;
row1 = 6;
row2 = 6;
row3 = 6;
row4 = 6;
row5 = 6;
row6 = 6;
row7 = 6;
while sum(sum(Array == 0)) > 0 && Player == 1
switch Player
   %%%%%%%% Player 1 %%%%%%%%%%
    case 1 
        choice = menu('Player 1: Choose a column','Col 1','Col 2','Col 3','Col 4','Col 5','Col 6','Col 7');
        switch choice 
            case 1 
                if(Array(row1,1))~=0
                   row1=row1-1;
                end
                if(row1 < 1)
                    error('Column is full. Choose a new column.');
                end
                Board{row1,1} = blackchip;
                imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                Array(row1,1) = 1;

            case 2
                if(Array(row2,2))~=0
                    row2= row2-1;
                end
                if(row2 < 1)
                    error('Column is full. Choose a new column.');
                end
                Board{row2,2} = blackchip;
                imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                Array(row2,2) = 1;

            case 3
                if(Array(row3,3))~=0
                    row3= row3-1;
                end
                if(row3 < 1)
                    error('Column is full. Choose a new column.');
                end
                Board{row3,3} = blackchip;
                imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                Array(row3,3) = 1;

            case 4
                if(Array(row4,4))~=0
                   row4= row4-1;
                end
                if(row4 < 1)
                    error('Column is full. Choose a new column.');
                end
                Board{row4,4} = blackchip;
                imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                Array(row4,4) = 1;

            case 5
                if(Array(row5,5))~=0
                    row5= row5-1;
                end
                if(row5 < 1)
                    error('Column is full. Choose a new column.');
                end
                Board{row5,5} = blackchip;
                imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                Array(row5,5) = 1;

            case 6
                if(Array(row6,6))~=0
                    row6 = row6-1;
                end
                if(row6 < 1)
                    error('Column is full. Choose a new column.');
                end
                Board{row6,6} = blackchip;
                imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                Array(row6,6) = 1;

            case 7
                if(Array(row7,7))~=0
                    row7= row7-1;
                end
                if(row7 < 1)
                    error('Column is full. Choose a new column.');
                end
                Board{row7,7} = blackchip;
                imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                Array(row7,7) = 1;
                check = 0;
                while check == 0
                      if(row7 < 1)
                         fprintf('Please select another column');
                         if(Array(row7,7))~=0
                              row7 = row7-1;
                         end
                            Board{row7,7} = redchip;
                            imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                            Array(row7,7) = -1;
                         else
                           check = 1;
                      end
                end
end

        %%%%%%%%%% Player 2 %%%%%%%%%%%
        Player = 2;
        while sum(sum(Array == 0)) > 0 && Player == 2
            switch Player
                 case 2 %Player 1
                    choice = menu('Player 2: Choose a column:','Col 1','Col 2','Col 3','Col 4','Col 5','Col 6','Col 7');
                    switch choice 
                        case 1
                            if(Array(row1,1))~=0
                                row1= row1-1;
                            end
                            if(row1 < 1)
                                error('Column is full. Choose a new column.');
                            end
                            Board{row1,1} = redchip;
                            imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                            Array(row1,1) = -1;

                        case 2
                            if(Array(row2,2))~=0
                                row2= row2-1;
                            end
                            if(row2 < 1)
                                error('Column is full. Choose a new column.');
                            end
                            Board{row2,2} = redchip;
                            imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                            Array(row2,2) = -1;

                        case 3
                            if(Array(row3,3))~=0
                              row3= row3-1;
                            end
                            if(row3 < 1)
                                error('Column is full. Choose a new column.');
                            end
                            Board{row3,3} = redchip;
                            imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                            Array(row3,3) = -1;

                        case 4
                            if(Array(row4,4))~=0
                                row4= row4-1;
                            end
                            if(row4 < 1)
                                error('Column is full. Choose a new column.');
                            end
                            Board{row4,4} = redchip;
                            imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                            Array(row4,4) = -1;

                        case 5
                            if(Array(row5,5))~=0
                                row5 = row5-1;
                            end
                            if(row5 < 1)
                                error('Column is full. Choose a new column.');
                            end
                            Board{row5,5} = redchip;
                            imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                            Array(row5,5) = -1;

                        case 6
                            if(Array(row6,6))~=0
                               row6 = row6-1;
                            end
                            if(row6 < 1)
                                error('Column is full. Choose a new column.');
                            end
                            Board{row6,6} = redchip;
                            imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                            Array(row6,6) = -1;

                        case 7
                            if(Array(row7,7))~=0
                               row7 = row7-1;
                            end
                            if(row7 < 1)
                                error('Column is full. Choose a new column.');
                            end
                            Board{row7,7} = redchip;
                            imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                            Array(row7,7) = -1;
                            check = 0;
                            while check == 0
                                if(row7 < 1)
                                    fprintf('Please select another column');
                                    if(Array(row7,7))~=0
                                        row7 = row7-1;
                                    end
                            Board{row7,7} = redchip;
                            imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
                            Array(row7,7) = -1;
                                else
                                    check = 1;
                                end
                            end
                    end 
            end
            Player = 1;
        end
end
end

Output: Displays board, asks the player which column they want to drop the chip, places the chip, and then asks the next player. Currently, the board fills up until there are no more spaces, because we can't figure the code out to choose a winner.

1 Answers1

0

In general, you need to add a routine for checking the board for a winning combination after every move. You can see examples of brute force solutions on MATLAB Central; for example this one has a solution for checking for a winner that would fit your current paradigm.

But for a really elegant solution, see the second answer to this SO question, which uses a bitboard representation of the game.

Community
  • 1
  • 1
mhopeng
  • 1,063
  • 1
  • 7
  • 17