2021-10-25

Introduction to Connect Four

Basic definitions:

type Player = Int
type Posn = (Int, Int)
type Piece = (Player, Posn)
type Board = [Piece]

There are two different ways you could represent a Connect Four board. We will write some functions to convert between the two possible representations. These functions are to warm up; they might not actually be used in your final version of the game.

Representations

There are two different ways to represent the following 2 row, 3 column board:

- O -
O X X

One way would be simply to replace X by 1 and O by 2, letting 0 represent the empty squares.

[[0, 2, 0],
 [2, 1, 1]]

Another way would be to record only the location of the occupied squares, writing a Piece for each one, which is a tuple (Player, Posn). The order does not matter:

[(2,(0,0)), (1,(1,0)), (1,(2,0)),
 (2,(1,1))]

The bottom row of the matrix will be numbered row 0. (Like the first quadrant in math.)

Helper: getPlayer

When using the list of pieces representation of the board, it helps to have a function that takes in a row, column, and board, and returns 0 if the row and column is unoccupied, or the number of the player that occupies it.

getPlayer :: Int -> Int -> [Piece] -> Int
getPlayer x y board = undefined

convertToPieces

Given a matrix representation of the board, produce a list of Pieces.

boardConvertToPieces :: [[Int]] -> [Piece]
boardConvertToPieces board = undefined

convertToMatrix

Given a list of Pieces, produce the matrix. In this case you need the additional information of how many rows and columns are in the board.

boardConvertToMatrix :: Int -> Int -> [Piece] -> [[Int]]
boardConvertToMatrix width height pieces = undefined
Last modified August 18, 2023: 2022-2023 End State (7352e87)