This program has everything needed for a classic game of connect 4. The program initially starts as an empty connect 4 board, displays a disclaimer about the program only being a two player version of connect 4, and prompts the users to input a name for the first player. After the first name is inputted the program prompts for a second name, and after that it prompts the users if they have know the rules of connect 4. If the users enter yes the program skips straight to the gameplay. If the users enter no then it explains the rules. After the rules are explained (or not) then the program randomly generates a number between 1 and 2.
The player corresponding to the number generated is then prompted to click on a space of the board. The program recognizes where the user clicked and drops a token in the column selected down to the bottom of the board. After one player drops one token, the other player is prompted to click a space. This then repeats until the game recognizes that there is 4 of one color in a row or the board is full. When the game recognizes that the game is over it displays a winner (if there is one), and prompts the users if they want to play again. If they answer yes, they game board is cleared and the winner is prompted to click a column again. If no the program exits.
Initially the program makes a variable game_over = 0, makes a simpleGameEngine object that has the sprite the connect 4 program uses, declares empty_sprite = 1, red_sprite = 2, and black_sprite = 3, and initializes board_display = empty_sprite * ones(6, 7). The program then creates a figure based on board_display and because it is all empty displays a 7×6 array of empty sprites, and then it changes the name of the figure to ‘Connect 4 like never b4’ and docks it inside of MatLab.
To see if the users know how to play connect 4 the program creates another variable called rules and sets it equal to 2. Then it creates a variable player and randomly generates a number between 1 and 2, this variable keeps track of who’s turn it is. The program creates 2 variables called name1 and name2 and then prompts the user(s) to input their names into the respective variables. After this the user(s) are prompted to input if they know the rules and their answer is stored in rules. While rules is not 0 for no or 1 for yes it will loop repeating the prompt and input. When rules is 0 or 1 the loop will break and if the user(s) entered a 0 for no the program will display the rules as text as well as what color each player is. If the user(s) entered a 1 then the program will display what color each player is in text.
After the rules are shown (or skipped), the program initializes the variable board to equal a 6×7 array of zeroes. The program enters a while loop while game_over does not equal 1. Inside this loop the function prompt_input is called.
The function prompt_input takes input arguments board, player, my_scene, name1, and name 2, while the output of the function is an updated board and move which is a variable that stores the player’s most recent move. This function declares a variable valid as 0, declares a 7×1 array named row, and enters a while loop if valid equals 0. An if statement is used so based on the current player an fprintf statement is used to display the text (‘click a square, name’) where name is based on the name the user(s) inputted. For example if it was player 1’s turn, it would display Click a square, name1. After prompting the user to click a square, it runs the function getMouseInput using my_scene as the input and returning the column clicked in a variable c.
Then the program enters a for loop from i = 1:7, this is done to check if the input is between 1 and 7. Using an if statement c is compared to row(i). If c does not equal row(i) nothing happens and the function continues looping. If c does = row(i) valid is set to 1, and another for loop is entered from k = 1:6. An if/else is used to check the board inside of this loop. If board(6, c) > 0 then the row is full, the program prints “This row is full. Try a different row!”, sets valid to 0 and breaks out of the loop. Else if board(k, in) > 0 do nothing and the program will loop to a new index. Else depending on the current value of player passed into the function it updates the variable board at (k, in) = 1 if it’s player 1’s turn and 2 if it’s player 2’s turn. Outside of every loop, the function sets the variable last_move = [k, c]. Working correctly this function will update the board array with the player’s number at the first place in the column it can and outputs the last move inputted.
After prompt_input, the program runs the function displayMove using input arguments move, player, my_scene, and board_display. DisplayMove outputs into board_display and player.
DisplayMove makes variables move_y and move_x. Move_y is set to 7 – move(1) and move_x is set to move(2). Move_y is set to 7 – move(1) to make gravity for the board. This function then runs sets the function variable display, which is equal to board_display, at the index of (move_y,move_x) = player + 1. This makes the display of the board the correct sprite. Then the function switches the value of player to 1 if it was 2 and vice versa. DisplayMove then outputs the display and the player value.
After displayMove, the program runs the function check_game_over using the variables board, move and player as input arguments board, position, and player. This function updates the variables game_over and player.
This function makes the variable x = position(2), makes the variable y = position(1), makes the variable color = board(y,x), and sets game_over to zero. Color a variable equal to the color of the last token placed. Then it initializes the variables in_a_row_y = 1, in_a_row_x = 1, in_a_row_z = 1, and in_a_row_z_2 = 1. These are used as the counters for the number of tokens in a row.
After all the counters are initialized the programs goes into a for loop i = 1:3. An if/else statement is used to determine if the index of the board will go out of bounds. If y + i > 6, break out of the loop. Else if board (y+i, x) == color, then in_a_row_y = in_a_row_y + 1. Else if board(y + i, x) does not equal color break out of the loop. This loop should check the colors of the tokens in the y positive direction which is a standard y direction in an x-y plane. When the program finds a token of the same color the counter is increased by one. The loop only goes 3 tokens out from the last move because it counts itself as the first token in a row. If the program finds a token of different color or no token it will break out of the loop. There are 7 other loops to check the 7 other possible directions a connect 4 could be formed. They are all implemented similarly to this one.
After all the directions are checked, an if statement is used to check the values of the counters. If the counters have counted 4 or more tokens in a row then set game_over to 1, and set player to 1 if it is 2 or vice versa (the program already switched turns in another function). Check_game_over outputs the new value of game_over and the new value of player.
The program then goes into an if statement. If the variable game_over is 1 then print the name of the player that won, and create a variable play_again to equal the input of the user(s) when prompted if they want to play again. If the value of play_again is not 1 for yes or zero for no, the program will repeat the prompt until the user(s) enter 1 or zero. If a 1 entered into play_again then set game_over to zero, board_display to = empty_sprite * ones(6,7), board to a 6×7 array of zeroes. If play_again is zero then set game_over to 1 and fprintf(‘Goodbye!’). If game_over is 0 continue looping. Working correctly this part should see that the game should be over, print the winner of the game, and ask if the user(s) want to play again. If they answer yes reset variables to their initial values and set game_over to zero. If they answer no, quit the program with a goodbye message.
List Of Variables (main)
- This is the variable that is used to keep looping the functions while the game is not over. When it is true the game is over and breaks out of the loop.
- A simple game engine object that is used to create the visuals of the game.
- A variable that is equal to the empty sprite based on the sprite given to the simple game engine object.
- A variable that is equal to the red sprite based on the sprite given to the simple game engine object.
- A variable that is equal to the black sprite based on the sprite given to the simple game engine object.
- A 6×7 array of the current values of the sprites to be displayed.
- Stores the current figure.
- Used to store the user’s input when asking if they know the rules or not.
- Used to keep track of the current player turn.
- The name inputted for player 1.
- The name inputted for player 2.
- A 6×7 array keeping track of what color tokens have been placed where.
- Used to keep track of the last token placed in the game.
- Used to store the answer from the user(s) when prompted if they want to play again.