1: 2D Array

  • What are some examples of 2d Arrays
    • A cartegian grid an be represented as a 2D array. Each nested array is the y-value and the indices of the elements within the nested array are the x-values.
  • What is a modern day game that could be classified as a 2D array
    • All video games (Minecraft, for example), use 2D arrays to manage the current framebuffer and display pixels to the computer screen.

How I used 2D Arrays (game example)

  • Describe a 2D array in your own words
    • A 2D array is an array of arrays. Indexing the array uses two pairs of square brackets: [][].

2: Iteration

Robot Game:Finn Carpenter - What is the defenition of iteration in your own words

  • Iteration is the action of looping through a set of items and performing an action on each of them

Iteration Game

  • Link to the game
  • Play the levels (only play the first 2 in class)
    • Levels completed in ticket
  • Explain how the game relates to itertation
    • The game relates to iteration because certain tasks such as moving the robot up, down, left, and right are repeated over and over again.

How I used iteration (game example)

  • What parts of the code use iteration
    • The for loop on line 16 shows iteration because it loops until the looper variable is reached by l.

How I used List to make a game

  • Explain which parts of the code use lists
    • The random.choice uses the word because it selects a random word from the list.
  • Explain what list manipulation is happening in that part
    • The list is manipulated by generating a random number between the list index min/max, selecting a random item, and storing it in word.

Hacks: Your Score/1

General 0.3

  • Copy this noteboook into your personal fastpages
  • Answer all questions

Iteration 0.2 (can get up to 0.23)

  • Get to level 5
    • Take ScreenShots of your name inside the box an put them in your ticket
  • Create a code segment with iteration that does something cool

2D array 0.2 (can get up to 0.23)

  • Explain how the tic tac toe game works
  • Give 3 Examples of games that can be made from 2D arrays

List and Dictionaries 0.2 (can get up to 0.23)

  • Explain the differences between Lists and Dictionaries
  • Make a code block that manipulates either a list or a dictionary

2D array

The given Python code is an implementation of a two-player Tic Tac Toe game on the command line. The game board is represented using a 2D array of size 3x3. The print_board() function prints the current state of the board, while the check_win(player) and check_tie() functions determine if a player has won or if the game is tied, respectively. The play_game() function handles the main game loop, prompting players to enter their moves and updating the board state accordingly. 2D arrays are used extensively in the code to represent and manipulate the game board, with nested loops used to iterate over the rows and columns of the board to print its state and check for wins and ties.

Games that can be made using 2D arrays: chess, crosswords, mazes, battleship, connect-4, minesweeper, reversi, sokoban, snake, tetris

Implementation of a 2D array to create a chess board and move pieces according to it (Link to web app)

export function Move (moves) {


    // initial state up of the board
    var setup = [
        ["wr1","wn1","wb1","wq1","wk1","wb2","wn2","wr2"],
        ["wp1","wp2","wp3","wp4","wp5","wp6","wp7","wp8"],
        ["   ","   ","   ","   ","   ","   ","   ","   "],
        ["   ","   ","   ","   ","   ","   ","   ","   "],
        ["   ","   ","   ","   ","   ","   ","   ","   "],
        ["   ","   ","   ","   ","   ","   ","   ","   "],
        ["bp1","bp2","bp3","bp4","bp5","bp6","bp7","bp8"],
        ["br1","bn1","bb1","bq1","bk1","bb2","bn2","br2"]
    ];



    // creates the value to return and pushed the clone of setup in order to avoid referencing values
    var allBoards = [];
    allBoards.push(clone2DArray(setup));


    // this if statement only creates a list of boards only when a list of moves provided, passing in 'setup' returns only the setup position
    if(moves !== 'setup') {


            // iterates through the number of moves provided
            for (var index = 0; index < moves.length; ++index){

                // declares local vars for the different indexes of the current index
                var move = moves[index];
                var col = move[0];
                var row = move[1];
                var piece = move[2];


                // initializes local vars for the current position of the desired piece [row, col]
                var pos = [];


                // declares the currentBoard as the board in its last state
                var currentBoard = clone2DArray(allBoards[allBoards.length-1])

                // iterates through the rows and collumns in the board
                rows:
                    for (var y = 0; y < 8; ++y){
                        for (var x = 0; x < 8; ++x){


                            // finds the current piece and declares pos to its index, then breaks out of the for loop
                            if (currentBoard[y][x] === piece){


                                // sets the previous position of the piece to a blank space
                                currentBoard[y][x] = "   ";


                                // breaks out of the board for loops when found
                                break rows;
                            }
                        }
                    }


                // sets the desired position to the piece
                currentBoard[row - 1][col - 1] = piece;

                // appends the current baord to the return var
                allBoards.push(currentBoard);
            }
    }


  return allBoards;
}

A 2D array was used to create the squares on a chess board and order the different pieces of it. MUtating methods such as .push() are used to edit it.

List and Dictionaries

Differences between list and dictionary:

  • Lists are collections of a single data type, while dictionaries are collections of key-value pairs, where the keys and values can be of two distinct types
  • List elements are accessed using a numeric index, while dictionary values are accessed using a key
  • Lists are ordered, while dictionaries are unordered (Binary Tree maps can be used to preserve order)
  • Lists are mutable, while dictionaries are mutable too

Implementation of a dictionary to make a quiz (Link to full implementation):

const quiz_container = document.getElementById("quiz-container")

// initialize dictionary
const questions = {
    "What is Iteration defined as?":
        [1, 2, ["(a) Sequence of instructions", "(b) Repetition of a process", "(c) Boolean", "(d) List"]],
    "What is a List defined as?":
        [2, 1, ["(a) Collection of data in a sequence that is a iterable", "(b) Numbers", "(c) Selection between two paths based on solution", "(d) Groceries"]],
    "What does a “FOR LOOP” do? What is the variable, “i”, used for?":
        [3, 1, ["(a) FOR LOOP repeats a function for a set number of times; I is the number of times repeated", "(b) I is a parameter of the function, and for only performs an action if certain circumstances are met", "(c) The FOR LOOP is a function and I the absolute value of the function", "(d) The FOR LOOP controls the order functions are called on outside of the for loop; I is the number of functions it controls"]],
    "How can we add something to the end of a list?":
        [4, 3, ["(a) Use + sign", "(b) The word Add", "(c) Append", "(d) Extend"]],
    "What is Indexing / List Index defined as?":
        [5, 2, ["(a) Contents of List", "(b) The position of an element in a list, starting from 0", "(c) Alphabetical Ordered List", "(d) Measurement of List"]],
    "What does the POP command do?":
        [6, 4, ["(a) Removes a list", "(b) Adds a list", "(c) Adds to the end of List", "(d) Removes the last item from a list"]],
    "What is Base 0 Indexing?":
        [7, 4, ["(a) Binary", "(b) Mutation", "(c) Sequence demarcated with an Index starting from 1", "(d) Seguence demarcated with an Index starting from 0"]],
    "What does a WHILE loop do?":
        [8, 3, ["(a) Iterates over an iterable and for a set amount of time", "(b) Intuitively takes an iterable and manipulates it over a set period using pointers", "(c) Loop over a bound interval by comparing to a conditional", "(d) Crash bitcoin"]],
    "I want to iterate over a list until the user inputs 'quit'. What loop would I use?":
        [9, 2, ["(a) WHILE loops", "(b) FOR loops", "(c) Recursive Loops", "(d) Paradoxical Loops"]],
    "We have the list [['lizards', 'snakes', 'salamanders'], ['sharks', 'whales', 'fish'], ['lions', 'tigers', 'pumas']]. How many loops and of which type(s) would we need in order to iterate through every element in this list most efficiently?":
        [10, 1, ["(a) 2, both FOR loops", "1, a FOR loop", "2, a WHILE and a FOR loop", "2, both WHILE loops"]]
}

for (let question in questions) {

    // question wrapper
    let question_container = document.createElement("div")
    question_container.style.backgroundColor = "#f5f5f5"
    question_container.style.marginTop = "10px"
    question_container.style.marginBottom = "10px"
    question_container.style.padding = "15px"
    question_container.style.borderRadius = "18px"

    quiz_container.appendChild(question_container)

    // key in questions
    let prompt = document.createElement("h3")
    prompt.innerHTML = "#" + questions[question][0] + ") " + question
    prompt.style.marginBottom = "2px"
    prompt.style.marginTop = ""

    question_container.appendChild(prompt)

    // ul for answers
    let answers = document.createElement("ul")
    answers.style.listStyle = "none"
    answers.style.margin = "4px"
    answers.style.paddingLeft = "12px"
    answers.setAttribute("id", "answer-container-" + questions[question][0])

    question_container.appendChild(answers)

    let answer_num = 0

    for (let answer_choice in questions[question][2]) {

        answer_num += 1

        // creates answer choice 
        let answer_option = document.createElement("li")
        answer_option.style = "display:flex; align-items:center;"
        answer_option.style.cursor = "pointer";
        answer_option.style.borderRadius = "8px"
        answer_option.style.backgroundColor = "#e7e5e4"
        answer_option.style.marginTop = "8px"
        answer_option.setAttribute("id", answer_num)


        let answer_text = document.createElement("p")
        answer_text.style.marginLeft = "8px"
        answer_text.innerHTML = questions[question][2][answer_choice]

        answer_option.append(answer_text)

        // adds to DOM
        answers.appendChild(answer_option)

        // passes in question num & answer num
        answer_option.onclick = () => check_answer(questions[question][0], answer_option.id)
    }

}

function check_answer(question_num, answer_num) {

    // gets the answer element
    console.log("answer_num: " + answer_num)
    let answer_container = document.getElementById("answer-container-" + question_num)
    let selected_answer = answer_container.children[answer_num - 1]
    selected_answer.style.transition = "all .5s ease"

    console.log(Object.values(questions)[question_num - 1][1])

    // converts the dict to an array and accesses the answer by question_num
    if (Object.values(questions)[question_num - 1][1] == answer_num) {
        selected_answer.style.backgroundColor = "#bbf7d0";
        selected_answer.style.fontWeight = "bold";
    } else {
        selected_answer.style.textDecoration = "line-through";
        selected_answer.style.backgroundColor = "#fca5a5";
    }

}

I use a dictionary and iterate through it to create quiz elements such as the questions and answers. The value is a list that stores the question number, the index of the correct answer, and each of hte question choices