Changing Number in Cells
in
Programming Questions
•
7 months ago
Hi all,
I'm writing a program for a sudoku board for an intro programming class. I have the board how I want it, but I'm having trouble figuring out how to change the numbers in the cells. I'm using a mousePressed function to increase the numbers by one with each click but the numbers are being drawn over each other making them impossible to read. How can I make it so the number in the cell changes and the old number goes away? Here's my code and thanks for the help.
int x = 0; // initialized x_global at top left corner of the canvas
int y = 0; // initializes y_global at top left corner of the canvas
int s = 35; // sets cells size to 35 pixels (1/9 of canvas size)
color tinto = color(255); // defines color of "tinto"
int cell_x = 0; // initializes cell_x for use in mousePressed function
int cell_y = 0; // inititalized cell_y for use in mousePressed function
int counter = 1;
void setup(){
size(315, 315); //sets canvas size to fit 9 cells of 35 pixels
board(x, y, s, color(255)); // calls board function which contains cell, triple, block, row, cellarray, and line functions
}
void cell(int x, int y, int s, color tinto){ // draws a single cell
fill(tinto);
rect(x, y, s, s);
}
void triple(int x, int y, int s, color tinto){ // uses cell function to draw row of 3 cells
fill( tinto);
for(int i = 0; i<3; i++) {
cell(x+i*s, y, s, tinto);
}
}
void block(int x, int y, int s, color tinto){ // uses triple to make 3 rows of triple
fill(tinto);
for(int i = 0; i < 3; i++){
triple(x, y+i*s, s, tinto);
}
}
void row(int x, int y, int s, color tinto){ // draws 3 blocks to make top row third of board
for(int i = 0; i<3; i++){
block(x+i*s*3, y, s, tinto);
if(tinto == color(180)){ // makes color alternate every time program draws a block
tinto = color(255);
} else {
tinto = color(180);
}
}
}
void cellarray(int x, int y, int s, color tinto){ // draws 3 rows to complete board
fill(tinto);
for( int i = 0; i<3; i++){
row(x, y+i*s*3, s, tinto);
if(tinto == color(180)){ // alternates color every time row is drawn
tinto = color(255);
} else {
tinto = color(180);
}
}
}
void board(int x, int y, int s, color tinto){ // combines all above function to draw complete sudoku board
for(int i = 0; i<3; i++){
cellarray(x, y, s, tinto);
}
strokeWeight(2); // makes lines that define block thicker than the rest of the lines
line(0, 105, 315, 105); // draws lines that separate blocks
line(0, 210, 325, 210);
line(105, 0, 105, 315);
line(210, 0, 210, 315);
}
void mousePressed(){
cell_x = (mouseX-x)/s; // defines cell_x with respect to the x position of the mouse
cell_y = (mouseY-y)/s; // defines cell_y with respect to the y position of the mouse
fill(0);
textSize(32);
text(counter++, x+s*cell_x+s/4, y+s*cell_y+s/1.25);
counter = counter%10;
}
void draw() {
if(mousePressed) {
}
}
1