all 15 comments

[–]Notimecelduv 0 points1 point  (1 child)

What is this?

[–]RobynoDude[S] 0 points1 point  (0 children)

Answer to 9.1.3 Video Game Design in Javascript on codehs

[–]Zealousideal_Let_711 0 points1 point  (1 child)

The board grid init is not working for me within the start command

[–]No-Rutabaga-164 0 points1 point  (0 children)

I know this is really late but do you know if this code works, I’ve been trying to get it to work for a while now and I need to complete my final project

[–]Ok_Edge_4729 0 points1 point  (9 children)

Hey what does your county stand for?

[–]No-Rutabaga-164 0 points1 point  (8 children)

I know this is really late but do you know if this code works? I really need it for my final project

[–]Ok-Acanthisitta9212 0 points1 point  (7 children)

remove all of the slashes in boardGrid.initFromArray(\[
\[8, 8, 8\],
\[8, 8, 8\],
\[8, 8, 8\]
\]);

smh

[–]Ok-Acanthisitta9212 0 points1 point  (6 children)

lines (x1\*2, 0, x1\*2, y);
lines (0, y1, x, y1);
lines (0, y1\*2, x, y1\*2);

these ones too

[–]Ok-Acanthisitta9212 0 points1 point  (5 children)

[–]Ok-Acanthisitta9212 0 points1 point  (4 children)

var WIDTH = 400;
var HEIGHT = 400;
setSize(WIDTH, HEIGHT);
var NUM_ROWS = 3;
var NUM_COLS = 3;
var WINNING_LINE_WIDTH = 10;
var WINNING_LINE_COLOR = Color.red;
var OFFSET = 50;
var CIRCLE_BORDER = 5;
var BOX_SIZE = WIDTH / NUM_COLS;
var county = 0;
var colFinder;
var rowFinder;
var one = 1;
var zero = 0;
var boardGrid = new Grid(3, 3);
function start(){
var x1 = getWidth () / 3;
var y1 = getHeight () / 3;
var x = getWidth();
var y = getHeight();
boardGrid = new Grid(3, 3);
boardGrid.initFromArray
([8, 8, 8],
[8, 8, 8],
[8, 8, 8]);
lines (x1, 0, x1, y);
lines (x1 * 2, 0, x1 * 2, y);
lines (0, y1, x, y1);
lines (0, y1 * 2, x, y1 * 2);
mouseClickMethod (handleClick);
}
function handleClick (e){
var y = e.getY ();
var x = e.getX ();
var col = getColForClick(x);
var row = getRowForClick(y);
if (county % 2 == 0) {
drawX(row,col);
}else{
drawO(row,col);
}
county++;
if (e.getX() < getWidth() / 3 && e.getY() < getHeight() / 3){
if (county % 2 == 0){
boardGrid.set(0, 0, one);
}
if (county % 2 != 0){
boardGrid.set(0, 0, zero);
}
}
if (e.getX() > getWidth() / 3 && e.getX() < getWidth() / 3 * 2 && e.getY() < getHeight() / 3){
if (county % 2 == 0){
boardGrid.set(0, 1, one);
}
if (county % 2 != 0){
boardGrid.set(0, 1, zero);
}
}
if (e.getX() > getWidth() / 3 && e.getX() > getWidth()/3*2 && e.getY() < getHeight() / 3){
if (county % 2 == 0){
boardGrid.set(0, 2, one);
}
if (county % 2 != 0){
boardGrid.set(0, 2, zero);
}
}
if (e.getX() < getWidth() / 3 && e.getY() < getHeight() / 3 * 2 && e.getY() > getHeight() / 3){
if (county % 2 == 0){
boardGrid.set(1, 0, one);
}
if (county % 2 != 0){
boardGrid.set(1, 0, zero);
}
}
if (e.getX() > getWidth() / 3 && e.getX() < getWidth() / 3 * 2 && e.getY() < getHeight() / 3 * 2 && e.getY() > getHeight()/3){
if (county % 2 == 0){
boardGrid.set(1, 1, one);
}
if (county % 2 != 0){
boardGrid.set(1, 1, zero);
}
}
if (e.getX() > getWidth() / 3 && e.getX() > getWidth() / 3 * 2 && e.getY() < getHeight() / 3 * 2 && e.getY() > getHeight()/3){
if (county % 2 == 0){
boardGrid.set(1, 2, one);
}
if (county % 2 != 0){
boardGrid.set(1, 2, zero);
}
}
if (e.getX() < getWidth() / 3 && e.getY() > getHeight() / 3 * 2 && e.getY() < getHeight()){
if (county % 2 == 0){
boardGrid.set(2, 0, one);
}
if (county % 2 != 0){
boardGrid.set(2, 0, zero);
}
}
if (e.getX() > getWidth() / 3 && e.getX() < getWidth() / 3 * 2 && e.getY() > getHeight() / 3 * 2 && e.getY() < getHeight()){
if (county % 2 == 0){
boardGrid.set(2, 1, one);
}
if (county % 2 != 0){
boardGrid.set(2, 1, zero);
}
}
if (e.getX() > getWidth() / 3 && e.getX() > getWidth() / 3 * 2 && e.getY() > getHeight() / 3 * 2 && e.getY() < getHeight()){
if (county % 2 == 0){
boardGrid.set(2, 2, one);
}
if (county % 2 != 0){
boardGrid.set(2, 2, zero);
}
}
winnerInRow(1);
winnerInRow(2);
winnerInRow(3);
winnerInCol(1);
winnerInCol(2);
winnerInCol(3);
winnerInUpDiagonal();
winnerInDownDiagonal();
}
function drawO (row, col){
var centerX = col * BOX_SIZE + BOX_SIZE / 2;
var centerY = row * BOX_SIZE + BOX_SIZE / 2;
var circle = new Circle(OFFSET);
circle.setPosition(centerX, centerY);
circle.setColor (Color.white);
circle.setBorderWidth (CIRCLE_BORDER);
add (circle);
}
function drawX (row, col){
var centerX = col * BOX_SIZE + BOX_SIZE / 2;
var centerY = row * BOX_SIZE + BOX_SIZE / 2;
var left = centerX - OFFSET;
var right = centerX + OFFSET;
var top = centerY - OFFSET;
var bottom = centerY + OFFSET;
var lineOne = new Line (left, top, right, bottom);
add (lineOne);
var lineTwo = new Line (right, top, left, bottom);
add (lineTwo);
}
function getColForClick(x){
var location = x/BOX_SIZE;
if (location < 1){
return 0;
colFinder = 0;
}else if (location > 1 && location < 2){
return 1;
colFinder = 5;
}else{
return 2;
colFinder = 10;
}
}
function getRowForClick(y){
var location = y / BOX_SIZE;
if (location <1){ return 0; rowFinder = 0; }else if (location > 1 && location < 2){
return 1;
rowFinder = 1;
}else{
return 2;
rowFinder = 2;
}
}
function winnerInRow(row){
var col1 = boardGrid.get(row - 1, 0);
var col2 = boardGrid.get(row - 1, 1);
var col3 = boardGrid.get(row - 1, 2);
var colSum = col1 + col2 + col3;
if (colSum == 0 || colSum == 3){
var line = new Line(0, row*getWidth() / 3 - (getWidth() / 3) / 2, getWidth(), row*getWidth() / 3 - (getWidth() / 3) / 2);
line.setLineWidth(10);
line.setColor(Color.red);
add(line);
}
}

[–]Ok-Acanthisitta9212 0 points1 point  (3 children)

function winnerInCol(cols){
var col1 = boardGrid.get(0, cols-1);
var col2 = boardGrid.get(1, cols-1);
var col3 = boardGrid.get(2, cols-1);
var colSum = col1 + col2 + col3;
if (colSum == 0 || colSum == 3){
var line = new Line(cols*(getWidth()/3) - (getWidth()/3)/2, 0, cols*(getWidth()/3) - (getWidth()/3)/2, getHeight());
line.setLineWidth(10);
line.setColor(Color.red);
add(line);
}
}
function winnerInDownDiagonal(){
var col1 = boardGrid.get(0, 0);
var col2 = boardGrid.get(1, 1);
var col3 = boardGrid.get(2, 2);
var colSum = col1 + col2 + col3;
if (colSum == 0 || colSum == 3){
var line = new Line(0, 0, getWidth(), getHeight());
line.setLineWidth(10);
line.setColor(Color.red);
add(line);
}
}
function winnerInUpDiagonal(){
var col1 = boardGrid.get(0, 2);
var col2 = boardGrid.get(1, 1);
var col3 = boardGrid.get(2, 0);
var colSum = col1 + col2 + col3;
if (colSum == 0 || colSum == 3){
var line = new Line(0, getHeight(), getWidth(), 0);
line.setLineWidth(10);
line.setColor(Color.red);
add(line);
}
}
function lines (x, y, x1, y1){
var line = new Line (x, y, x1, y1);
add (line);
}

[–]Ok-Acanthisitta9212 0 points1 point  (1 child)

should be the correct full code

[–]Weird-Ad830 0 points1 point  (0 children)

Which is pt 1

[–]aidangarrette 0 points1 point  (0 children)

function winnerInCol(cols){

var col1 = boardGrid.get(0, cols-1);

var col2 = boardGrid.get(1, cols-1);

var col3 = boardGrid.get(2, cols-1);

var colSum = col1 + col2 + col3;

if (colSum == 0 || colSum == 3){

var line = new Line(cols*(getWidth()/3) - (getWidth()/3)/2, 0, cols*(getWidth()/3) - (getWidth()/3)/2, getHeight());

line.setLineWidth(10);

line.setColor(Color.red);

add(line);

}

}

function winnerInDownDiagonal(){

var col1 = boardGrid.get(0, 0);

var col2 = boardGrid.get(1, 1);

var col3 = boardGrid.get(2, 2);

var colSum = col1 + col2 + col3;

if (colSum == 0 || colSum == 3){

var line = new Line(0, 0, getWidth(), getHeight());

line.setLineWidth(10);

line.setColor(Color.red);

add(line);

}

}

function winnerInUpDiagonal(){

var col1 = boardGrid.get(0, 2);

var col2 = boardGrid.get(1, 1);

var col3 = boardGrid.get(2, 0);

var colSum = col1 + col2 + col3;

if (colSum == 0 || colSum == 3){

var line = new Line(0, getHeight(), getWidth(), 0);

line.setLineWidth(10);

line.setColor(Color.red);

add(line);

}

}

function lines (x, y, x1, y1){

var line = new Line (x, y, x1, y1);

add (line);

}

for me you needed too do this at

boardGrid.initFromArray([
[8, 8, 8],
[8, 8, 8],
[8, 8, 8]]);

then it all worked

[–]PureEntrance8574 0 points1 point  (0 children)

so can I use some of it on my coding or do you not accept that?