Difference between revisions of "Connect Four Final Report"
|Line 14:||Line 14:|
Revision as of 14:57, 16 December 2012
This project consisted of creating a MATLAB script for the game Connect Four. We created code that set up a 6 by 7 board. The rules for the game are exactly the same as any Connect Four game. Two players play against each other: black versus red. The player hovers the mouse over the board and then clicks on any column and their chip will fall to the appropriate row. This means that if there are no chips in that column, it will fall to the bottom, but if there are already chips in that column, it will fall to the row directly above it. The code detects if you try and place a chip in an already taken spot, and it does not allow you to do that. We also made code that determines when there is four in a row and someone has won the game.
The goal of this project was to make a computer game using MATLAB, and Connect Four seemed like the most fun. We wanted our MATLAB version of Connect Four to be as realistic to the actual board game as possible. We wanted all the same rules to apply. The motivation for this project came from the desire to give Professor Cheever a fun game to play over winter break, and from the aspiration to make a game people can really play on the computer. This project is interesting because the code involves some complicated nested for loops and if then statements. This project took a lot of problem solving, but with lots of trial and error we were successful with creating Connect Four on MATLAB.
In order to understand this project, you need to be at least somewhat acquainted with MATLAB. The code has three parts: setting up the board, playing, and checking for four in a row. The latter two parts are repeated for the second player. MATLAB statements that we use include for loops, if then else statements, ginput, round, and patch. The patch statement sets up the 6 by 7 board with yellow circles. The ginput statement allows the player to add their chip to the board, and the round function rounds the x and y positions of where they clicked to a round number, so that their chip falls directly in the column. We set up a matrix called bval, which is composed of zeros. Wherever the red player plays, the matrix value is changed to one, and wherever the black player plays the matrix value is changed to negative one. This is so that MATLAB knows where all the black and red chips are. The nested for loops along with the if then statements help guide the chip to the correct position. Once the chip is settled, the “break” statement at the end of the if statement terminated the turn.