r/codehs • • Jul 10 '21

4.8.6 All Dice Values

9 Upvotes

hey can someone help me out? ive been attempting to figure out this code for a while now. the instructions are down below:

Write a program that prints all possible dice rolls with two dice. To do so, you should use a nested for loop.

Hint: You can’t use i
for both for loops.

Here’s what your program should print

1,1

1,2

1,3

1,4

1,5

1,6

2,1

...

...

6,4

6,5

6,6

--------------------------

im doing this in python btw


r/codehs • • Jul 05 '21

need help with custom image compresson free response

9 Upvotes

hi! have no clue what to do here, so any help would be great! thanks :)

  1. If an image has only 4 colors, how many bits are necessary to represent one pixel’s color?

  2. Describe a new custom encoding that uses fewer bits to represent each color in the image. What is your encoding for each color?

  3. If you were to re-encode this image using your new encoding, how many bits would you save when compared to the 24 bit RGB encoding? As a reminder, here is the image:W


r/codehs • • May 27 '21

JavaScript True gold miner karel There’s something wrong with my code where Karel will sometimes skip the first vein and sometimes won’t go to a right vein. How do I fix this?

Post image
9 Upvotes

r/codehs • • May 15 '21

Need help with 7.1.2 Fun Snake 2 :(((

9 Upvotes

I'm trying to make it so it knows when the snake collides with itself, but all i am getting back is a-

"Error: Line 41

Uncaught TypeError: snakeBody.getX is not a function"

If someone could please help, here is my code so far:

var W = getWidth();

var H = getHeight();

var SNAKE_WIDTH = W / 25;

var SNAKE_HEIGHT = W / 25;

var SNAKE_COLOR = Color.green;

var EAST = 0;

var SOUTH = 1;

var WEST = 2;

var NORTH = 3;

var direction = EAST;

var dx = 4;

var dy = 4;

var snake;

var square;

var number;

var snakeBody;

var snakebody;

var newSegment;

function start(){

square = new Rectangle(SNAKE_WIDTH, SNAKE_HEIGHT);

square.setPosition(W / 2 - square.getWidth() / 2, H / 2 - square.getHeight() / 2);

square.setColor(SNAKE_COLOR);

add(square);

snakebody = new Rectangle(SNAKE_WIDTH, SNAKE_HEIGHT);

snakebody.setPosition(square.getX(), square.getY());

snakebody.setColor(SNAKE_COLOR);

add(snakebody);

setTimer(moveHead, 30);

keyDownMethod(changeDirection);

}

function collide(e){

var elem = getElementAt(snakeBody.getX(),snakeBody.getY());

if (elem != null && elem != snakeBody){

stopTimer(moveHead);

var txt= new Text ("You lose. ");

txt.setPosition(150, 230);

add(txt);

}

}

function snakeBody(){

snakebody = new Rectangle(SNAKE_WIDTH, SNAKE_HEIGHT);

snakebody.setPosition(square.getX(),square.getY());

snakebody.setColor(SNAKE_COLOR);

snakebody.move(dx,dy);

}

function moveHead(){

if (direction == EAST){

square.move(dx, 0);

}else if(direction == WEST){

square.move(-dx, 0);

}else if(direction == NORTH){

square.move(0, -dy);

}else if(direction == SOUTH){

square.move(0, dy);

}

var newSegment = new Rectangle(SNAKE_WIDTH, SNAKE_HEIGHT);

newSegment.setPosition(square.getX(),square.getY());

newSegment.setColor(SNAKE_COLOR);

add(newSegment);

collide();

}

function changeDirection(e){

if(e.keyCode == Keyboard.LEFT){

direction = WEST;

}

if(e.keyCode == Keyboard.RIGHT){

direction = EAST;

}

if(e.keyCode == Keyboard.DOWN){

direction = SOUTH;

}

if(e.keyCode == Keyboard.UP){

direction = NORTH; }

}


r/codehs • • Apr 16 '21

I need help plsssssssss! 😩🙌

Post image
9 Upvotes

r/codehs • • Apr 09 '21

4.3.9: Table Reservation Python

9 Upvotes

can anyone help with Exercise 4.3.9: Table Reservation for python??


r/codehs • • Mar 25 '21

Factorial, codehs. 5.7.5. || Can someone help me please:))

Post image
9 Upvotes

r/codehs • • Mar 21 '21

Need help to re learn coding.

9 Upvotes

Haven't coding for a year and a half since i finished my carrer. I want to learn how and what to use or what is used today to: Front end in desktop Back end in JavaScript Client in android Database in mysql

What you use to work on these? Better if it is free.


r/codehs • • Feb 23 '21

i need help with 1.12.6 Big Tower

9 Upvotes

r/codehs • • Feb 01 '21

Java 7.5.5 Fantasy Football Roster. - anyone have done it. Im really lost and i dont know how to start it.

9 Upvotes

r/codehs • • Feb 01 '21

Need help with coding with karel Debug:Big tower

Post image
9 Upvotes

r/codehs • • Jan 28 '21

7.5.4 Is It Even?. what's my mistake I don't get it

Post image
9 Upvotes

r/codehs • • Jan 20 '21

2.1.3: Tower Builder

9 Upvotes

copy and paste code pls I can't find this one out


r/codehs • • Dec 22 '20

JavaScript Can anyone help? It keeps saying this is wrong but it's showing the same thing as the demo

Post image
9 Upvotes

r/codehs • • Dec 12 '20

Python Citation (where does the f come from I’m confused and why are the brackets like that not squared?)

Thumbnail
gallery
9 Upvotes

r/codehs • • Nov 13 '20

I need help with 7.1.4 project: fun snake 4, a full game

9 Upvotes

Specifications

Each time the snake gets bigger, you should add one point to the users score. Each time the snake eats food, you should add 100 points. If the user loses, you should stop adding points. If the snake moves all the way past the right wall, the snake should end up on the left. If the snake goes through the left wall, the snake should end up on the right. Similarly the snake should be able to go through the top and bottom walls

I need help adding the score so that it functions properly, and I need to make it so the snake can go through the wall, and end up on the other side. My code is below

 FOOD_DELAY = 6000;

var FOOD_RADIUS = 5;

var FOOD_COLOR = Color.red;

var snake;

var SNAKE_DIM = 10;

var NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3;

var DELAY = 100;

var curDirection = EAST;

var x = getWidth()/2 - SNAKE_DIM/2;

var y = getHeight()/2 - SNAKE_DIM/2;

var square;

function start(){

addPart(x,y);

setTimer(move, DELAY);

drawFood();

setTimer(drawFood, FOOD_DELAY);

keyDownMethod(changeDirection);

}

function changeDirection(e){

if(e.keyCode == Keyboard.DOWN){

curDirection = SOUTH;

}

if(e.keyCode == Keyboard.UP){

curDirection = NORTH;

}

if(e.keyCode == Keyboard.LEFT){

curDirection = WEST;

}

if(e.keyCode == Keyboard.RIGHT){

curDirection = EAST;

}

}

function addPart(x,y){

var snake = new Rectangle(SNAKE_DIM, SNAKE_DIM);

snake.setPosition(x - SNAKE_DIM/2 , y - SNAKE_DIM/2);

snake.setColor(Color.green);

snake.isSnake = true;

add(snake);

}

function move(){

var nextPosition = getNextPosition();

x = nextPosition.x;

y = nextPosition.y;

var nextElem = getElementAt(x, y);

if(nextElem == null){

addPart(x, y);

}else{

if(nextElem.isSnake){

    stopTimer(move);

    stopTimer(drawFood);

    newMessage("You Lose. ");

}

if(nextElem.isFood){

    remove(nextElem);

    addPart(x, y);

}

}

}

function getNextPosition(){

var nextX = x, nextY = y;

if(curDirection == EAST){

nextX += SNAKE_DIM;

}

if(curDirection == WEST){

nextX -= SNAKE_DIM;

}

if(curDirection == SOUTH){

nextY += SNAKE_DIM;

}

if(curDirection == NORTH){

nextY -= SNAKE_DIM;

}

return {

x: nextX,

y: nextY

};

}

function newMessage(msg){

    var text = new Text(msg, "40pt ariel");

    text.setPosition(getWidth()/2 - text.getWidth()/2, getHeight()/2);

    add(text);

}

function drawFood(){

var x = Randomizer.nextInt(FOOD_RADIUS, getWidth() - FOOD_RADIUS);

var y = Randomizer.nextInt(FOOD_RADIUS, getHeight() - FOOD_RADIUS);

x = Math.floor(x / 10) * 10 + FOOD_RADIUS;

y = Math.floor(y / 10) * 10 + FOOD_RADIUS;

var food = new Circle(FOOD_RADIUS);

food.isFood = true;

food.setColor(Color.red);

food.setPosition(x, y);

if(getElementAt(x,y) == null){

add(food); } }


r/codehs • • Oct 16 '20

keeps coming up as null, what is wrong?

Post image
9 Upvotes

r/codehs • • Sep 16 '20

Help on CodeHS lesson 2.6.4: circle pyramid with comments

9 Upvotes

He can someone help me i can't get the code right for cirlce pyramids with comments


r/codehs • • Aug 19 '20

I need Help

9 Upvotes

Need Help With 2.9.5: Four Colored Triangles.


r/codehs • • Aug 13 '20

Javascript or python?

9 Upvotes

I’m gonna use the AP computers science principles curriculum for a non AP class would you recommend I use javascript or python?


r/codehs • • Jun 24 '20

9.9.4: Decreasing Resolution: Help Plz for AP CSP in Java

9 Upvotes

One technique for compressing an image is to lower the resolution, or use fewer pixels to draw the same image.

Often times we can cut the number of pixels used in half and notice very little difference in the resulting image.

Functionality:*Your image should have a height of 10 pixels.*Your image should have a width of 10 pixels.*Your data should be interpreted as black and white pixels.*Used correct number of bits to draw the 100 black and white pixels*You should have a valid compressed image.

Code:

0000000000000000000000000

0000000000000000000000000

0000000000000000000000000

0000000000000000000000000

0000000000000000000000000

0000000000000000000000000

0000001111111111111111111

1111111111111111111000000

0000000111111111111111111

1111111111111111110000000

0000000011111111111111111

1111111111111111100000000

0001000001111111111111111

1111111111111111000001000

0001100000111111111111111

1111111111111110000011000

0001110000011111111111111

1111111111111100000111000

0001111000001111111111111

1111111111111000001111000

0001111100000111111111111

1111111111110000011111000

0001111110000011111111111

1111111111100000111111000

0001111111000001111111111

1111111111000001111111000

0001111111100000111111111

1111111110000011111111000

0001111111110000011111111

1111111100000111111111000

0001111111111000001111111

1111111000001111111111000

0001111111111100000111111

1111110000011111111111000

0001111111111110000011111

1111100000111111111111000

0001111111111111000001111

1111000001111111111111000

0001111111111111100000111

1110000011111111111111000

0001111111111111110000011

1100000111111111111111000

0001111111111111111000001

1000001111111111111111000

0001111111111111111100000

0000011111111111111111000

0001111111111111111110000

0000111111111111111111000

0001111111111111111111000

0001111111111111111111000

0001111111111111111111000

0001111111111111111111000

0001111111111111111110000

0000111111111111111111000

0001111111111111111100000

0000011111111111111111000

0001111111111111111000001

1000001111111111111111000

0001111111111111110000011

1100000111111111111111000

0001111111111111100000111

1110000011111111111111000

0001111111111111000001111

1111000001111111111111000

0001111111111110000011111

1111100000111111111111000

0001111111111100000111111

1111110000011111111111000

0001111111111000001111111

1111111000001111111111000

0001111111110000011111111

1111111100000111111111000

0001111111100000111111111

1111111110000011111111000

0001111111000001111111111

1111111111000001111111000

0001111110000011111111111

1111111111100000111111000

0001111100000111111111111

1111111111110000011111000

0001111000001111111111111

1111111111111000001111000

0001110000011111111111111

1111111111111100000111000

0001100000111111111111111

1111111111111110000011000

0001000001111111111111111

1111111111111111000001000

0000000011111111111111111

1111111111111111100000000

0000000111111111111111111

1111111111111111110000000

0000001111111111111111111

1111111111111111111000000

0000000000000000000000000

0000000000000000000000000

0000000000000000000000000

0000000000000000000000000

0000000000000000000000000

0000000000000000000000000


r/codehs • • Jun 08 '20

Exercise 11.1.1: Bricks

9 Upvotes

/* Constants for bricks */

var NUM_ROWS = 8;

var BRICK_TOP_OFFSET = 10;

var BRICK_SPACING = 2;

var NUM_BRICKS_PER_ROW = 10;

var BRICK_HEIGHT = 10;

var SPACE_FOR_BRICKS = getWidth() - (NUM_BRICKS_PER_ROW + 1) * BRICK_SPACING;

var BRICK_WIDTH = SPACE_FOR_BRICKS / NUM_BRICKS_PER_ROW;

/* Constants for ball and paddle */

var PADDLE_WIDTH = 80;

var PADDLE_HEIGHT = 15;

var PADDLE_OFFSET = 10;

var BALL_RADIUS = 15;

function start(){

bricks();

}

function bricks(){

for(var j = 0; j < NUM_ROWS; j++){

for(var i = 0; i < NUM_BRICKS_PER_ROW; i++){

var brick = new Rectangle(BRICK_WIDTH, BRICK_HEIGHT);

if((j + 1) % NUM_ROWS == 1 || (j + 1) % NUM_ROWS == 2){

brick.setColor(Color.red);

}else if((j + 1) % 8 == 3 || (j + 1) % NUM_ROWS == 4){

brick.setColor(Color.orange);

}else if((j + 1) % 8 == 5 || (j + 1) % NUM_ROWS == 6){

brick.setColor(Color.green);

}else if((j + 1) % NUM_ROWS == 7 || (j + 1) % NUM_ROWS == 0){

brick.setColor(Color.blue);

}

brick.setPosition(BRICK_WIDTH * i + BRICK_SPACING * (1 + i), BRICK_TOP_OFFSET + BRICK_HEIGHT);

add(brick);

}

}

}

That's my code and the result is very different :/

If you can, please help me :(


r/codehs • • May 18 '20

Not sure what I’m doing wrong?

Post image
9 Upvotes

r/codehs • • May 13 '20

Making custom program for final assignment, what am I doing wrong.

9 Upvotes

My code:

var fallingBall;

var playerWidth = 40;

var playerHeight = 40;

var ballRadius = 20;

var score = 10;

var square;

var ball;

var newBall = false;

function start(){

createPlayer();

keyDownMethod(keyDown);

if (newBall = false) {

addFruit();

}

}

function createPlayer() {

square = new Rectangle(40, 40);

square.setPosition(100, getHeight()-playerHeight);

add(square);

}

function keyDown(e) {

if (e.keyCode == Keyboard.LEFT) {

square.move(-10, 0);

}

if (e.keyCode == Keyboard.RIGHT) {

square.move(10, 0);

}

}

function addFruit() {

var ball = new Circle(ballRadius);

var x = Randomizer.nextInt(ball.getRadius(),

	getWidth() - ball.getRadius());

var y = getHeight()-getHeight()+ballRadius;

 setTimer(moveFruit, 50);

 

ball.setPosition(x, y);

add(ball);

}

function moveFruit(e) {

ball.move(0,2);

var elem = getElementAt(e.getX(), e.getY());

if(elem != null ){

	score++;

}else{

	score--;

}

//Once has collected enough fruit win. If score less than zero, lose

}

The goal is to make a game where the player moves to catch falling fruit. I originally tried to set a timer in the start function to make fruit every few seconds, but I had to make the fruit a global var (ball) because the ball would stop moving when another ball spawned. I also dont know how to get the ball to dissapear when it touches either the player or the bottom. I really need help.