Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:04:17+00:00 2026-06-14T14:04:17+00:00

I am fairly new to OCaml and I want to implement a game which

  • 0

I am fairly new to OCaml and I want to implement a game which is similar to four-in-a-line.
What I need is some data structure to keep the game state. The game board is a 4×4 square with a total of 16 tiles.
I am looking for a representation for this in OCaml that will make it easy and fast to retrieve (or do some operation on) all the elements in entire column, row or diagonal.
I will be doing minimax search on this game, which is why speed is important.

So far I have considered a one-dimensional list. The problem with a list is that its hard to figure out what elements belong to each row/column/diagonal, and then retrieve them with a List.map for example.

I thought about using Array.make 4 (Array.make 4 Empty);;. This is absolutely perfect when it comes to rows. Its easy to get them and do a pattern match on it. But it is a chore to do pattern matching on individual columns and diagonals.

What I would like to be able to do is have a function that takes a game board and returns a list of lists containing all the rows/columns/diagonals. I would then like to do, for example, match (rows,columns,diagonals) with (Empty, Empty, Empty, Empty) -> something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:04:18+00:00Added an answer on June 14, 2026 at 2:04 pm

    Sometimes matching doesn’t work. Here, I think you should try to use functions as much as possible, and then getting your cells row first or column first won’t be that complex, and you could even move from one representation to the other by reversing the indices order.

    If I use the following type:

    type color = Red | Yellow;;
    type cell  = Empty | Color of color;;
    type board = Array.make 4 (Array.make 4 Empty);;
    

    and decide for column first, then the following functions will get me rows or columns:

    let column (b: board) i j = b.(i).(j)
    let row (b: board) i j = b.(j).(i)
    

    For the diagonals, there are 2 sets of them, one going top-left toward down-right, and the other one in the other direction (top-right to bottom-left):

    let ldiag (b: board) i j = b.((i + j) mod 4).(j)
    let rdiag (b: board) i j = b.((i - j + 4) mod 4).(j)
    

    Then I guess that checking a row, column, or diagonal is just a matter of checking the 4 cells of that line.

    let check predicate linef k = predicate (linef b k 0) && 
                                   predicate (linef b k 1) && 
                                   predicate (linef b k 2) && 
                                   predicate (linef b k 3)
    

    then for instance, checking if there’s a diagonal of red:

    let has_line linef b color = 
        let cmp x = x = color in
        let check k = check cmp linef b k in
        check 0 || check 1 || check 2 || check 3
    
    let has_ldiag b color = has_line ldiag b color
    let has_rdiag b color = has_line rdiag b color
    
    let has_red_diagonal b = has_ldiag b Red | has_rdiag b Red
    

    Etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I fairly new to regular expressions and need some help. I need to filter
Fairly new to CR and i am looking for some help, Basically I want
fairly new to java here. I'm writing a GUI program of which I want
I'm fairly new with OCaml Module and I haven't managed to use my own
Fairly new to Google Maps API. I've got an array of data that I
im fairly new to android development, but i'm having some trouble trying to make
Im fairly new to android dev and I want to build an app that
I am fairly new to Django and I'm curious if some functionality regarding selecting
Fairly new to Java, so I hope the wording makes sense. I have some
Im fairly new to objective C. I am currently trying to implement a split

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.