Connect 4 Game

13 views (last 30 days)
nick
nick on 1 Dec 2011
I am attempting to make a simple connect four game. I have made a board but dont know how to do the game play or chips. Could anyone write the code for me or give some hints on how to do this for two players?
Here is my code for the board:
figure('Units','normalized','Position',[.2 .2 .6 .6], 'Name','Connect Four','Color','w');
axes('Units','normalized','Position',[.1 .1 .8 .8],'Color','y','LineWidth',1,'Box','on');
set(gca, 'XTick',[],'YTick',[],'XLim',[0,160],'YLim',[0,160]);
hold on;
for row = 1:7
for col = 1:7
board(row,col) = plot(col*20,row*20, 'wo','MarkerSize',40,'LineWidth',1,'MarkerFaceColor',[1 1 1]','MarkerEdgeColor', [0 0 0]);
end
end

Answers (1)

Sean de Wolski
Sean de Wolski on 1 Dec 2011
A few thoughts:
  • You know how to make white circles, so it should be fairly straight forward to make black/red ones.
  • The users only have to pick the column, so I would have 7 buttons on the top so whoever's turn it is just clicks the button.
  • I would keep the values in a matrix of -1s 0s and 1s for red unused and black.
  • Whatever column is selected the zero in the biggest row is turned to the respective color
  • You can use conv2 or bwhitmiss (I like conv2) with the ' valid' option and four different kernels (one for each way to win: horizontal 4 vertical 4, diagonal and antidiagonal) call conv2 four times per turn to see if that player won.
  • Enjoy writing the game and good luck. Logic games are fun to write but the progress slows greatly when you have a working version and can start playing!

Categories

Find more on Board games in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!