Coder Social home page Coder Social logo

conways_c's Introduction

Conways in C

Learn some C by building conways game of life

Learning objectives

  • Pointers and pointer syntax
  • Understand the difference between passing values by copy of by reference.
  • Build conways

Target audience

  • You already know how to program in another language.
  • You've never written any c before
  • Bonus if:
    • You've written conways game of life in your language of choice.
    • You're familiar with a test driven development. This exercise has tests that you will make pass. Being able to read the tests will help guide you.

Conways

Rules

  • Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
  • Any live cell with two or three live neighbours lives on to the next generation.
  • Any live cell with more than three live neighbours dies, as if by overpopulation.
  • Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

Conways lingo

  • A cell is either alive or dead. We store that as a bool, true means alive.
  • A board is the entire collection of cells in the game.
  • I use the word ressurectable in the code for a cell that comes back to life (Rule 4 above)

Board data structure

The tests expect a conways board to be a single dimension array of bools like:

bool board [BOARD_SIZE * BOARD_SIZE] = {false};

You're going to have to work out the maths and syntax for treating this like a 2d array:

  int8_t row = 1;
  int8_t col = 2;

  // one option if you want to use index syntax
  bool cell_at_location = board[???something with row, col and BOARD_SIZE??];

  // the option if you want to use pointer syntax
  bool cell_at_location = *(board + ???something with row, col and BOARD_SIZE???);

Build files

Build set ups are painful and you don't need to delve into it now. But you will need to uncomment some lines of the main build file as you progress through the challenge. This is so that you can be sure that any errors you see are actually coming from the code you're working on.

So, go open up the file CMakeLists.txt and go down until you find the lines that look something like this:

# Uncomment each group below as you progress through the challenge

#add_library(is_over_populated is_over_populated.cpp)
#add_executable(is_over_populated_test is_over_populated_test.cpp)
#target_link_libraries(is_over_populated_test is_over_populated gtest_main)

and uncomment the first of the three lines like so:

# Uncomment each group below as you progress through the challenge

add_library(is_over_populated is_over_populated.cpp)
add_executable(is_over_populated_test is_over_populated_test.cpp)
target_link_libraries(is_over_populated_test is_over_populated gtest_main)

Dev cycle

  • Edit the build settings to build the next part of the challenge.
  • Write code.
  • Try and build it with $ make
  • Run the test for the part you're working on, eg $ ./is_over_populated_test
  • Repeat!

Testing

Run all the tests:

$ make test

Revel in the glory that is The Game of Life

./conways

Stretch ideas

  • Pass an argument from the command line to set how large the board should be.
    • You'll have to use malloc to get memory for your boards at run time because we don't know at compile time how big a board is going to be.

FAQ

  • Why are the tests .cpp (c++) files?
    • We're using google test which is written in c++. C++ code can interoperate with C code if you include some stuff in the C header files. You don't need to worry about C++ for this exercise though so don't worry.

conways_c's People

Contributors

pietgeursen avatar

Watchers

James Cloos avatar Suresh Swamy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.