I'm making a connect 4 like game in Java and everything I choose a column to drop the piece it goes where I want it to however, when I want to drop a piece on top of that I, my program throws an error and makes me select another move. Here's what I'm talking about.
Could someone please look over my code and tell me what I'm missing here? Thanks
public static void getMove() {
// --> to do
int row1 = 5, row2 = 5, row3 = 5, row4 = 5, row5 = 5, row6 = 5, col, row = 0;
String tmp;
System.out.println("Select your move");
System.out.println("Column");
System.out.println("hey");
tmp = TextIO.getln();
col = Integer.parseInt(tmp);
if (col == 1){
col--;
row1 = row;
row1--;
}
else if (col == 2){
col--;
row = row2;
}
else if (col == 3){
col--;
row = row3;
}
else if (col == 4){
col--;
row = row4;
}
else if (col == 5){
col--;
row = row5;
}
else{
col--;
row = row6;
}
while(!game.playerClicked(row, col)){
System.out.println("Please select a valid move");
System.out.println("Select your move");
System.out.println("Column");
tmp = TextIO.getln();
col = Integer.parseInt(tmp);
}
//System.out.println(row);
}
public boolean playerClicked (int r, int c) {
if ( /*r < 0 || r > 5 || */ c < 0 || c > 6) return false;
if ( data[r][c] != MARK_NONE) return false;
// valid row, col and cell
if ( gameState == GAME_STATE_O_TURN) {
data[r][c] = MARK_O;
gameState = GAME_STATE_X_TURN;
}
else if ( gameState == GAME_STATE_X_TURN) {
data[r][c] = MARK_X;
gameState = GAME_STATE_O_TURN;
}
updateGame(); // winner?, tie?
return true; // move was successfully made
}
Also, any comments or suggestions are welcomed too.
Thanks