Connect Four

From UBC Wiki

Connect Four

Authors: Jenny Ngo & Chris Ren

What is the problem?

We will be building a two-player Connect Four game written in Haskell. We want to create a game that gives the players another chance at their turn because mistakes do happen. So if the player types anything other than the columns on the board, the game will ask the player to try again and continue the game instead of terminating.

The code we are referring to can be found here: https://gist.github.com/jaspervdj/3238203

What is the something extra?

The board is hard to see in the linked code. We want to fix this. Also, when someone reaches a connect 4, we will notify that a player has won and highlight the winning 4 pieces by changing the symbol used for that placement into something that stands out more ex. # or @ instead of the X or O piece. Secondly we’ll ask the player to try again if the inputs are outside of the scope of the board instead of terminating.

What did we learn from doing this?

What did we learn?

We learned that Haskell can support games from basic character input. We were able to build a game in less than 300 lines. There was a lot of things we wanted to do but couldn't. For example, variables are immutable, so we couldn't change them from within a function. Also, we couldn't have global variables we could call in each function. This restricted a lot of things we wanted to do.

In order to highlight the winning 4 pieces, we needed to be able to have a global variable. Since we didn't, we couldn't find another way to pass the winning coins to the function that prints the board.

What is the bottom line?

The bottom line is that Haskell is not a very suitable language for this task. Although it is great for simple games, it's very difficult to do things such as create and print elements in a 2D array as Haskell is limited to just returning one argument and printing would result the type to be of incomparable IO, whereas in Java or C++ that would be a simple nested for loop with print statements. We had to go through recursion and multiple methods instead.

Is functional programming suitable for (part-of) the task? Make sure you include the evidence for your claims.

Yes, for part of the game. Pattern-matching was very helpful in functions where we had to traverse through a list. It was also useful for checking if there was a win. Functional programming was not suitable for printing the four winning coins in a different shape, since variables were not stored globally and we could not easily make changes to the variables. Also, we wanted to have more than one output for our functions, but that was impossible. For example, we wanted to print out the winning piece in checkHorizontal, but that would conflict with the output type.

Links to code etc

https://github.com/jennyyngo/CPSC312_Project1