We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So i am making a game of connect four and am trying to use the arrow keys to navigate and drop the tokens in the game. I dont want to use a mousepressed function and am at a loss of how to apply the players input when he presses on the down arrow. the code below is what i have so far:
int redScore= 0;
int blueScore= 0;
int rows = 6;
int cols =7;
Cell[][] ellipseGrid = new Cell[cols][rows];
int[][] gridChecker = new int[cols][rows];
int colCount = 4;
boolean left = false;
color red = color(#FF0505);
color blue = color(#053DFF);
void setup()
{
size(1600,800);
int a;
int b;
for ( a = 0; a<cols; a++) {
for (b = 0; b<rows; b++) {
ellipseGrid[a][b] = new Cell(700+a*80,200+b*80,45,45,255);
}}}
void draw()
{
background(255);
background(0);
scoreboard();
fill(#F9FA03);
rect(660,160,560,480);
for (int a = 0; a < cols; a++) {
for (int b = 0; b < rows; b++) {
fill(255);
ellipseGrid[a][b].display();
indicator();
textSize(40);
text("HI = "+ colCount,500,600);
}}}
void colUpdate()
{
for (int a = 0; a<cols; a++) {
for (int b = 0; b<rows; b++) {
if(colCount =1)
{
}
}}}
void indicator()
{
if (colCount == 1)
{
triangle(700,150,690,110,710,110);
}
if (colCount == 2)
{
triangle(780,150,770,110,790,110);
}
if (colCount == 3)
{
triangle(860,150,850,110,870,110);
}
if (colCount == 4)
{
triangle(940,150,930,110,950,110);
}
if (colCount == 5)
{
triangle(1020,150,1010,110,1030,110);
}
if (colCount == 6)
{
triangle(1100,150,1090,110,1110,110);
}
if (colCount == 7)
{
triangle(1180,150,1170,110,1190,110);
}
}
void keyPressed()
{
if((keyCode == LEFT) && (colCount > 1))
colCount --;
if((keyCode == RIGHT) && (colCount <7))
colCount++;
if((keyCode == DOWN) && (colCount <7 && colCount >1))
{
for (int a = 0; a < cols; a++) {
for (int b = 0; b < rows; b++) {
ellipseGrid[a][b+1].display();
}}}
}
void scoreboard()
{
fill(#F50008);
rect(0,0,1600,400);
fill(#0005F5);
rect(0,400,1600,400);
fill(255);
textSize(53);
text("Player 1 score:"+ redScore ,50,200);
text("Your Turn" ,50, 300);
text("Player 2 score :"+ blueScore, 50, 600);
textSize(15);
text("press z to reset score",1400,100);
//playeroneBoard();
//playertwoBoard();
}
void intro()
{
fill(#1203FF);
textSize(50);
text("Welcome to the", width/2 -350,height/2 -100);
fill(#1203FF);
textSize(50);
text("game of Connect Four",width/2 -300,height/2 );
ConnectFourBoard();
}
void ConnectFourBoard(){ // This is the image that is used in the intro screen of the connect four board
fill(#F6FF03);
rect(width/2-100,height/2+100,220,200); // rectangle for board
for(int i=1;i<rows;i++) {
for(int j=1;j<cols;j++) {
fill(255);
ellipse(688+(i*(210/cols)), 490+(j*(250/rows)), 30, 30);
}}}
class Cell {
// A cell object knows about its location in the grid
// as well as its size with the variables x,y,w,h
float x,y; // x,y location
float w,h; // width and height
color currentcolor;
// Cell Constructor
Cell(float circleX, float circleY, float circleW, float circleH,color icolor) {
x = circleX;
y = circleY;
w = circleW;
h = circleH;
currentcolor = icolor;
}
// Oscillation means increase angle
void display() {
stroke(255);
fill(currentcolor);
ellipse(x,y,w,h);
}
}
Answers
@mzadery --
Please edit and format your code so it is readable / testable by forum members.
edit > highlight code > CTRL+o > save
https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text#latest
thanks, ive never used these forums. Appreciate the help
In Processing, try cleaning up the code with command+T.
First, this code doesn't run due to errors. Have you tried clicking on them in the errors box and trying to fix them? For example:
This gives you an out of bounds exception. Why are you trying to access [b+1]?
This is a long, complicated piece of code. Did the code once work before your latest modifications? Or was the sketch assembled without ever working?
First, get some simpler version to work. Then go to a more complex one. Break up any problem into multiple parts, it makes your work easier.
Hello,
You have a class
Cell
. Good.The basic idea is to let the cell know whether it is filled or not.
When a chip is placed above it (in its column) the first empty cell gets filled with this chip.
I also made the intro work using
state
.I ordered your functions differently.
Best, Chrisir ;-)
thanks chrissir, thats exactly what i was struggling to do
sure! You are welcome!
hey another question if thats possible for anyone. For determinning the win between two players. For horizontal, vertical and diagonal i have an idea of how i want to do it.
This however does now work with errors and was curious how you would go about it. The program as a whole is posted below without the boolean for checking the win of each player
I already answered this with an version of your function
just repeat for
vertical,
for diagonal / and
for diagonal \