Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will design, implement, and test a program that implements a simulation of L

ID: 3854762 • Letter: Y

Question

You will design, implement, and test a program that implements a simulation of Langton’s Ant. For a brief explanation consider Wikipedia: https://en.wikipedia.org/wiki/Langton%27s_ant (Links to an external site.)Links to an external site.. Note that it can be considered as a cellular automaton. This means that you have an array or matrix of cells. Each turn or step, the value of each cell may change based upon a simple rule. For the Ant there are two simple rules. The rules are: In a white square, turn right 90o and change the square to black. In a black square, turn left 90o and change the square to white. You should use a blank character for a white space, and use the number sign “#” for the black space. And for the Ant? "@" works fine. (You can use other sign as long as it is easy to see.) Left ? Right? Those are relative directions! How are you going to keep track of the direction the Ant is facing? How will you “remember” what color the current cell occupied by the Ant is or was? You will create an Ant class to help organize, hold, and manipulate this information. Your program will prompt the user to enter the number of rows and columns for the 2D array. You should also prompt the user for the number of steps. If you want to limit the size of the board or number of steps, suggest values to the user. (You are suggested to choose a size that is no larger than the default width 80 when you are developing). Your program should display each step by printing out the whole board. Use functional decomposition to develop functions to validate the input. (You can make a validation class if you would like.) You should start by asking the user for the starting location of the Ant. For testing purposes, always start the Ant at the same location. Then try different locations. Remember that when you create the array dynamically, you need to de-allocate the space to avoid memory leaks. You can use valgrind to check for memory leaks. Then what if the ant meets a wall collision (i.e. ant attempts to step out of the bounds of the array)? Think about the corner case. For example, the ant makes it to the edge of the game board, and according to the rule, it needs to make a turn and end up getting out of the game board. It is your design decision, and make sure it is properly handled. Previously, students tried different actions, such as “skip the step forward step, make another turn and then continue on”, or “wrap the board around so that the ant will appear on the other side”, or “turn the ant around and send it back onto the board”. As long as it makes sense, you are fine. But do not terminate the game before it reaches the designated step.

The array MUST be dynamically allocated, and you may not use any libraries aside from iostream.

**I have read answers to this question on Chegg and elsewhere, but they do not dynamically allocate the array using user input, or they use libraries that are not allowed for this problem.

EDIT: I apologize, but I should have specified that it needs to be in C++.

Explanation / Answer

Langton's Ant :

Langton’ s ant work on two principle
At a white square, turn 90° right, flip the color of the square, move forward one unit
At a black square, turn 90° left, flip the color of the square, move forward one unit

Pyhon:

Langton’s Ant is a turmite governed by simple rules whose outcome is both unpredictable and intresting. The path taken by the ant generates some surprising shapes, never appearing when you would expect them to, but a seemingly random moments. This article describes the rules behind Langton’s Ant, shows some of the images formed and provides a Python programme to simulate the ant.

#************************************************

# Rules of the game

#   1. If the ant is on a black square, it turns

#       right 90 and moves forward one unit

#   2. If the ant is on a white square, it turns

#       left 90 and moves forward one unit

#   3. When the ant leaves a square, it inverts

#       colour

#

# SEE: http://mathworld.wolfram.com/LangtonsAnt.html

#************************************************

import sys, pygame

from pygame.locals import *

import time

dirs = (

        (-1, 0),

        (0, 1),

        (1, 0),

        (0, -1)

        )

cellSize = 12 # size in pixels of the board (4 pixels are used to draw the grid)

numCells = 64 # length of the side of the board

background = 0, 0, 0 # background colour; black here

foreground = 23, 23, 23 # foreground colour; the grid's colour; dark gray here

textcol = 177, 177, 177 # the colour of the step display in the upper left of the screen

antwalk = 44, 88, 44 # the ant's trail; greenish here

antant = 222, 44, 44 # the ant's colour; red here

fps = 1.0 / 40 # time between steps; 1.0 / 40 means 40 steps per second

def main():

    pygame.init()

    size = width, height = numCells * cellSize, numCells * cellSize

    pygame.display.set_caption("Langton's Ant")

    screen = pygame.display.set_mode(size) # Screen is now an object representing the window in which we paint

    screen.fill(background)

    pygame.display.flip() # IMPORTANT: No changes are displayed until this function gets called

    for i in xrange(1, numCells):

        pygame.draw.line(screen, foreground, (i * cellSize, 1), (i * cellSize, numCells * cellSize), 2)

        pygame.draw.line(screen, foreground, (1, i * cellSize), (numCells * cellSize, i * cellSize), 2)

    pygame.display.flip() # IMPORTANT: No changes are displayed until this function gets called

    font = pygame.font.Font(None, 36)

    antx, anty = numCells / 2, numCells / 2

    antdir = 0

    board = [[False] * numCells for e in xrange(numCells)]

    step = 1

    pause = False

    while True:

        for event in pygame.event.get():

                if event.type == QUIT:

                    return

                elif event.type == KEYUP:

                    if event.key == 32: # If space pressed, pause or unpause

                        pause = not pause

                    elif event.key == 115:

                        pygame.image.save(screen, "Step%d.tga" % (step))

        if pause:

            time.sleep(fps)

            continue

        text = font.render("%d " % (step), True, textcol, background)

        screen.blit(text, (10, 10))

         

        if board[antx][anty]:

            board[antx][anty] = False # See rule 3

            screen.fill(background, pygame.Rect(antx * cellSize + 1, anty * cellSize + 1, cellSize - 2, cellSize - 2))

            antdir = (antdir + 1) % 4 # See rule 1

        else:

            board[antx][anty] = True # See rule 3

            screen.fill(antwalk, pygame.Rect(antx * cellSize + 1, anty * cellSize + 1, cellSize - 2, cellSize - 2))

            antdir = (antdir + 3) % 4 # See rule 2

        antx = (antx + dirs[antdir][0]) % numCells

        anty = (anty + dirs[antdir][1]) % numCells

        # The current square (i.e. the ant) is painted a different colour

        screen.fill(antant, pygame.Rect(antx * cellSize + 1, anty * cellSize + 1, cellSize -2, cellSize -2))

        pygame.display.flip() # IMPORTANT: No changes are displayed until this function gets called

        step += 1

        time.sleep(fps)

if __name__ == "__main__":

    main()

Using Java :

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote