We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi
I found tutorials to help me learn the basics of programming, to help me go ahead in my processing project. But due to the difference between bare java and processing, I can't implement some of the code I learned to processing. It seems to me that it is a problem of the program not stopping when asking for the user input
Here it is, can you guys help ?
///DECLARE
int EMPTY = 0;
int YELLOW = 1;
int RED = 2;
//the grid has lines et 7 colonnes
int [][] grid = new int [6][7];
void setup() {
initialise(grid);
display(grid);
}
void draw() {
int playerColor = YELLOW;
boolean won;
//ask for a play
//play
//display the game
//switch player
do {
askandPlay(grid, playerColor);
display (grid);
won = isItWon(grid, playerColor);
//switch color / player
if (playerColor == YELLOW) {
playerColor = RED;
} else {
playerColor = YELLOW;
}
}
while (!won && !full (grid));
if (won) {
// it's inverted because we switched the players
if (playerColor==YELLOW) {
println("player 0 won !");
} else {
println("player X won !");
}
} else {
println("match nul !");
}
//noLoop();
}
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
boolean full (int [][] grid) {
//if we find an empty cell on line 0 the grid is not full
for (int i = 0; i< grid.length; i++) {
if (grid[0][i] == EMPTY) {
return false;
}
}
return true;
}
boolean isItWon(int [][] grid, int playerColor) {
for (int line = 0; line < grid.length; ++line) {
for (int colonne = 0; colonne < grid.length; ++colonne) {
int cellColor = grid [line][colonne];
//check if four are aligned
if (cellColor == playerColor) {
int lineMax = grid.length -4;
int colonneMax = grid[line].length -4;
if (
// diago UP RIGHT
(line >= 3 && colonne <= colonneMax &&
count(grid, line, colonne, -1, +1) >= 4) ||
// HORI RIGHT
(colonne <= colonneMax &&
count(grid, line, colonne, 0, +1) >= 4) ||
// diago DOWN RIGHT
(line <= lineMax && colonne <= colonneMax &&
count(grid, line, colonne, +1, +1) >= 4) ||
// DOWN
(line <= lineMax &&
count(grid, line, colonne, +1, 0) >= 4)
) {
// if yes send true
return true;
}
}
}
}
return false;
}
int count(int [][] grid, int startLine, int startCol, int dirline, int dirCol) {
int counter = 0;
int line = startLine;
int colonne = startCol;
while ( grid[line][colonne] == grid[startLine][startCol] &&
line >= 0 && line < grid.length &&
colonne >=0 && colonne < grid[line].length ) {
++counter;
line = line + dirline;
colonne = colonne + dirCol;
}
return counter;
}
void askandPlay(int [][] grid, int playerColor)
{
boolean valide;
do {
print ("playur ");
if (playerColor == YELLOW) {
print ("X");
} else {
print ("O");
}
println(" : entrez un numéro de colonne");
/////////////////////////////////////////////////////////////////////////////
//////////////////////////////////input problem//////////////////////////////
/////////////////////the program should wait for user input//////////////////
/////////////////////////////////////////////////////////////////////////////
int colonne = key;
//arrays cells start from 0
--colonne;
println (colonne);
valide = play (grid, colonne, playerColor);
if (!valide) {
println("ce coup n'est pas valide");
}
}
while (!valide);
}
boolean play (int [][] grid, int colonne, int couleur) {
//if colonne number is not valide, the play is not
if (grid[0][colonne] != EMPTY) {
return false;
}
//looking for first empty cell
int line = grid.length -1;
while (grid[line][colonne]!= EMPTY) {
--line;
}
//filling the cell
grid[line][colonne] = couleur;
return true;
}
// display 0 for RED, X for YELLOW
void display(int [][] grid)
{
for (int [] line : grid ) {
print( " |" );
for (int cellule : line ) {
if (cellule == EMPTY) {
print (" ");
} else if (cellule == RED) {
print ("O");
} else {
print ("X");
}
print( "|" );
}
println();
}
print ("=");
for (int i = 1; i<= grid[0].length; i++) {
print("=" + i);
}
println("==");
}
void initialise(int [][] grid) {
for (int i= 0; i < grid.length; i++) {
for (int j= 0; j < grid[i].length; j++) {
grid[i][j]=EMPTY;
}
}
}
Answers
Don't just copy and paste code and hope for the best. Try to understand what the original code is doing, and then try to recreate that yourself.
You might think you've found a shortcut by using code you found, but really you're just heading towards a lot of headaches.
to get an user input is not totally easy in processing
processing doesn't wait but runs on and on (draw() is run 60 times per second)
you have to make it so that you get the input while processing runs on and on
this is done by the famous states... I show a simple way here....
;-)
so you have a state waitForInput and in draw() in this state nothing happens (or just text( "Please enter your move...", ... ); )
in function void keyPressed() you monitor whether a key comes in
when it does and it's correct (0..9) you set state back to statePlay in function void keyPressed() and do the move that has been entered
you can add or rename states as you like....
the code gets longer but is very reliable and stable.
;-)
do this only in function void keyPressed() not in normal functions (in normal functions you read in 3-5 keys, in function void keyPressed() only one each time a key is pressed)
dear KevinWorkman I think you are misguided if you think that I just "copy / pasted code and wish for the best" I have been spending a month following a video course and one week on the "connect four" program alone... no code to copy was provided and I followed at least 2 times the making of each single line. I think I did my homework by looking for answers on forums and other processing dedicated pages before posting, as well as trying to implement some keyPressed() solution myself. In short, I've spent more than 15 years doing my autodidacts computer stuff training on the net :]
I thank you very much Chrisir for your answer, It looks like it is precisely what I was looking for. To name it , this very user input question. I am going to study it :D
cheers to you all
great!
Here we go it didn't take me so long but : 1 - my internet connection was broken for 8 days 2 - I got a bug with the games code that I tried to fix... in vain but ! I got the USER INPUT part to work just as I wanted thanks to Chrisir's code here it is:
great!
a few remarks
great work!
;-)
I tried int(myChar) but couldn't get it to work using STATEPLAYWIN for both win and full is a bit of a shortcut :S, I realize this now. Don't have the time to put the graphical board together and I would have like to get the games code to be fixed first
thank you for your remarks