I've been trying to figure out for the life of me why my counter loop isn't running. I have this snippet of my code that will not run even when I qualify the requirements of it being that (counter>=10). It should run the line saying that I lost the Game but it won't do so regardless. I initially thought it was because the counter was within the loop and since it'll keep going it won't ever meet the condition but even then it still won't go. My apologize if its not the best looking code far as spacing goes I'm still new to coding
void draw ()
{
int name=5, strikes=1;
int [][] scores= new int [name][strikes];
int score1=0;
keyPressed();
{
for (int x=0; x<10; x++)
{
first();
if (key == 'r' || key == 'R')
{
secoend();
}
if (key != 'r' || key != 'R')
{
score1++;
} else if (score1>=10)
{
background(255);
String text="You Lost the Game";
text(text, 411, 90);
}
}
}
}
This if-statement is always true, and so it will never enter the else-branch:
if (key != 'r' || key != 'R')
key can only be one thing so either of those is always true
I think you ment:
if (key != 'r' && key != 'R')
Check your if statements, you're always incrementing score1 because if(key != 'r' || key != 'R') will always be true.
Related
So I am trying to code pong in processing and everything is working fine, and I can move the paddles up and down perfectly, however, when you try to move two paddles at the same time, they don't move / it doesn't let you (I am going to make this a 2 player game so that 2 people can play using the same keyboard but with different keys for the different paddles).
I believe this is an issue with using "key" or "keyPressed", because I think it can't detect both or something? but I can't seem to figure out how to fix this or any alternatives. (Keep in mind that I know how to move the paddles, its just that you can't move them both at the same time with the different provided keys like im trying to)
I have two objects so far, "Player1" and "Player2"
keep in mind "y" is the y position which will either go up or down depending on the key pressed, and "speed" is just the speed that the paddle will move.
This is in Player1. Up = w, Down = s
void movement() {
if(keyPressed) {
if(key == 'w' || key == 'W') {
y = y - speed; //goes up
} else if (key == 's' || key == 'S') {
y = y + speed; //goes down
}
}
}
This is in Player2. Up = up arrow key, Down = down arrow key
void movement() {
if (keyPressed) {
if(key == CODED) {
if(keyCode == UP) {
y = y - speed; //goes up
} else if (keyCode == DOWN) {
y = y + speed; //goes down
}
}
}
}
No error messages, just doesn't let you move 2 paddles at the same time, which is something I'd like to do.
You've to use the keyPressed and keyReleased() events. The events are executed once when a key is pressed or released.
Set a state when a key is pressed, respectively reset the state when the key is released:
Boolean player1_up = false;
Boolean player1_down = false;
Boolean player2_up = false;
Boolean player2_down = false;
void keyPressed() {
if (keyCode == UP)
player1_up = true;
else if (keyCode == DOWN)
player1_up = true;
if (key == 'w' || key == 'W')
player2_up = true;
else if (key == 's' || key == 'S')
player2_down = true;
}
void keyReleasd() {
if (keyCode == UP)
player1_up = false;
else if (keyCode == DOWN)
player1_up = false;
if (key == 'w' || key == 'W')
player2_up = false;
else if (key == 's' || key == 'S')
player2_down = false;
}
Use the states player1_up, player1_down, player2_up and player2_down in the movement functions.
i'm doing an easy exercise for school, everything work except for this method. i want to insert some teams in a vector, but the array "serie" can only be A or B, upper or lower. I check with the debug and the while condition doens't work even if serie[i]=a.
public static void popolamento(String squadre[], char serie[], int punti[]) {
Scanner in= new Scanner(System.in);
for (int i=0;i<punti.length;i++) {
System.out.println("How many teams?");
squadre[i]=in.next();
do {
serie[i]=in.next().charAt(0);
System.out.println(serie[i]);
}
while (serie[i]!='a' || serie[i]!='A' || serie[i]!='b' || serie[i]!='B');
punti[i]=in.nextInt();
}
System.out.println("teams entered correctly ");}
The condition
(X != a || X != b || X != c || X != d)
should have been
(X != a && X != b && X != c && X != d)
Such a pattern is very likely an error, as to fail, all terms must fail
When X == u (X != u fails) then X != v holds (different cases assumed), hence always true.
If you read something like this, you know it is in 99.9% an error.
I'm trying to create a maze with Union Find, but am unable to remove walls.
This is what I have got so far.
private void createMaze (int cells, Graphics g) {
s = new int[cells*cells]; //No unions yet setting all to -1
for(int i = 0; i < cells*cells; ++i){
s[i] = -1;
}
g.setColor(Color.yellow);
random = new Random();
while(breaker){
g.setColor(Color.yellow);
int innerWall = random.nextInt(4)+0;
int randomCellX = random.nextInt(cells-1)+0;
int randomCellY = random.nextInt(cells)+0;
if(randomCellX==cells&&innerWall==2||
randomCellX==0&&innerWall==0||
randomCellY==cells-1&&innerWall==3||
randomCellY==0&&innerWall==1){
continue;
}
else{
int location = randomCellX+(randomCellY*cells);
int neighbour = 0;
if(innerWall==0){
neighbour =location-1;
}
else if(innerWall==1){
neighbour =location-cells;
}
else if(innerWall==2){
neighbour =location+1;
}
else if(innerWall==3){
neighbour =location+cells;
}
int locationRoot =find(location);
int neighbourRoot =find(neighbour);
if(locationRoot==neighbourRoot){
breaker = checkIfDone(s);
}
union(location,neighbour);
drawWall(randomCellX,randomCellY,innerWall,g);
}
}
}
If I remove the
if(randomCellX==cells&&innerWall==2||
randomCellX==0&&innerWall==0||
randomCellY==cells-1&&innerWall==3||
randomCellY==0&&innerWall==1){
continue;
}
It removes the lines fine,but when it is added the walls are not removed. The method is called but doesn't do anything.
It seems some logical mistake have been done obviously. But can not spot specifically as you haven't given the explanation of the logic you have done. Only can help showing the path where the problem might be happening.
Since you can not reach the else block anyway, it is obvious that one or more of these conditions in if are always true.
(randomCellX == cells && innerWall == 2)
(randomCellX == 0 && innerWall == 0)
(randomCellY == cells - 1 && innerWall == 3)
(randomCellY == 0 && innerWall == 1)
That's why you are getting true for the if condition and continuing the loop without doing anything. Ensure the conditions are okay.
If these conditions are right, the next suspect might be these lines:
int innerWall = random.nextInt(4)+0;
int randomCellX = random.nextInt(cells-1)+0;
int randomCellY = random.nextInt(cells)+0;
Check whether these random values range are exactly what you want or not. For example: you are taking random values for randomCellX from 0 to cells-2, but for randomCellY the range is 0 to cells-1.
public boolean checkWin() {
if(states[0][0][0] == 1 && states[0][0][1] == 1 && states[0][0][2] ==1 && states[0][0][3] ==1) { // Checks 0th layer, 0th row
return true;
}
else if (states[0][1][0] == 1 && states[0][1][1] == 1 && states[0][1][2] ==1 && states[0][1][3] ==1) { // Checks 0th layer, 1st row
return true;
}
else if (states[0][2][0] == 1 && states[0][2][1] == 1 && states[0][2][2] ==1 && states[0][2][3] ==1) { // Checks 0th layer, 2nd row
return true;
}
else if (states[0][3][0] == 1 && states[0][3][1] == 1 && states[0][3][2] ==1 && states[0][3][3] ==1) { // Checks 0th layer, 3rd row
return true;
}
}
This code is hard coded to check the 0 th layer, and the 4 rows on that layer. I could hard code the rest but of course that would be very time consuming and bad code. When I try and make a loop it stops after three clicks
public boolean checkWin() {
for (int i=0; i<=3; i++) {
if(states[0][0][i] == 1){ // Checks 0th layer, all rows
return true;
}
}
return false;
}
This is how I tried to make the loop, but it doesn't work.
This game is a nice example of how a data driven approach can simplify our code. Consider:
there are 64 cells, that can be empty or contain a token; the number of tokens depends on the number of players. This can be represented as a one dimensional array of 64 elements.
the cells are in 76 rows. This can be represented as a array of 76 rows of 4, each cell containing a subscript for an element in the first array. (In C or C++ you could also store pointers, in Java you can store references).
To check for a winning row, you can then just iterate through the 76 rows, and check if every cell in a row of the first array has the value you assigned to one player or the other.
Your current code checks only for rows in the third dimension. You should check the other dimensions as well:
for(int i = 0;i < 4;i++) {
for(int j = 0;j < 4;j++) {
if(
(states[i][j][0] == 1 && states[i][j][1] == 1 && states[i][j][2] == 1 && states[i][j][3] == 1) ||
(states[i][0][j] == 1 && states[i][1][j] == 1 && states[i][2][j] == 1 && states[i][3][j] == 1) ||
(states[0][i][j] == 1 && states[1][i][j] == 1 && states[2][i][j] == 1 && states[3][i][j] == 1)) {
return true;
}
}
}
You could make it more abstract than this by looping also over the last dimension, but that'd require boolean variables or break/continue statements so I think this is about as clear as the code is going to get.
You need to rewrite you loop like so:
public boolean checkWin() {
for (int layer=0; layer<=3; layer++) {
for (int row=0; row<=3; row++) {
if(states[layer][row][0] == 1){
return true;
}
}
}
return false;
}
This logic checks only for row based wins. Note however that in a TicTacToe, you also need to check for Column based win and diagonal win.
I'm having an issue with a mock game of Tic tac toe. I'm using a two-dimensional array to represent the playing board, and have instantiated it as follows. It's required that I use a char type array. I realize that I shouldn't have to specify that each index is null, as that's the default for char, but I thought I would give it a try.
public TicTacToe2D()
{
board = new char[3][3];
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[i].length; j++)
{
board[j] = null;
}
board[i] = null;
}
}
Here I'm checking for a win condition, seeing if indexes are equal to each other and not null (the default) though I have attempted using ' ' for my array initial value. In that case I got the error: "incompatible types: char cannot be converted to char[]"
public char isWin()
{
//Check for row wins
if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][0] != null)
return true;
if (board[1][0]==board[1][1] && board[1][1]==board[1][2] && board[1][0] != null)
return true;
if (board[2][0]==board[2][1] && board[2][1]==board[2][2] && board[2][0] != null)
return true;
//Check for column wins
if (board[0][0]==board[1][0] && board[1][0]==board[2][0] && board[0][0] != null)
return true;
if (board[0][1]==board[1][1] && board[1][1]==board[2][1] && board[0][1] != null)
return true;
if (board[0][2]==board[1][2] && board[1][2]==board[2][2] && board[0][2] != null)
return true;
//Check for diagonal wins
if (board[0][0]==board[1][1] && board[1][1]==board[2][2] && board[0][0] != null)
return true;
if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[2][0] != 0)
return true;
else return false;
}
When checking if an index is null I get the error "incomparable types: char and "
Any help would be greatly appreciated!
The datatype char is primitive, so it can't be null. But the default value is the null character, \0 (or \u0000). JLS Section 4.12.5 gives default values:
For type char, the default value is the null character, that is, '\u0000'.
Try comparing it to \0 or \u0000 instead of null.
a char is not an object, it's a primitive type.
that means that a char is like an integer, or float, or Boolean, that have a fixed length and its initial value is zero (or false).
As far as I remember char is an 8bit, but I might be wrong. Said all that, a char you can compare to a letter, for example: 'a' or with an actual number, for example 0 or 1.
board is declared as char[][]. It is thus an array of char arrays. So board[i] is a char array (a row of your board). And board[i][j] is a char (the value of a cell of your board).
The default value of each cell of such a 2D array is 0. Not null. A primitive type can't be null.