java - Scanner only accepting input once -
recently, i've been working on noughts , crosses game, i've stumbled upon problem can't seem able fix.
whenever type in first time, works , prints out board, however, point onward, stops checking , printing out board.
while(inf){ string temp = input.next(); if(playerturn == 1){ if(amountp1 <= 3){ if(temp.equalsignorecase("00")){ if(board[0][0] == 0){ board[0][0] = 1; showboard(board); playerturn = 2; amountp1++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("01")){ if(board[0][1] == 0){ board[0][1] = 1; showboard(board); playerturn = 2; amountp1++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("02")){ if(board[0][2] == 0){ board[0][2] = 1; showboard(board); playerturn = 2; amountp1++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("10")){ if(board[1][0] == 0){ board[1][0] = 1; showboard(board); playerturn = 2; amountp1++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("11")){ if(board[1][1] == 0){ board[1][1] = 1; showboard(board); playerturn = 2; amountp1++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("12")){ if(board[1][2] == 0){ board[1][2] = 1; showboard(board); playerturn = 2; amountp1++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("20")){ if(board[2][0] == 0){ board[2][0] = 1; showboard(board); playerturn = 2; amountp1++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("21")){ if(board[2][1] == 0){ board[2][1] = 1; showboard(board); playerturn = 2; amountp1++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("22")){ if(board[2][2] == 0){ board[2][2] = 1; showboard(board); playerturn = 2; amountp1++; } else{ system.out.println("this space occupied!"); } } } else if(playerturn == 2){ if(amountp2 <= 3){ if(temp.equalsignorecase("00")){ if(board[0][0] == 0){ board[0][0] = 2; showboard(board); playerturn = 1; amountp2++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("01")){ if(board[0][1] == 0){ board[0][1] = 2; showboard(board); playerturn = 1; amountp2++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("02")){ if(board[0][2] == 0){ board[0][2] = 2; showboard(board); playerturn = 1; amountp2++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("10")){ if(board[1][0] == 0){ board[1][0] = 2; showboard(board); playerturn = 1; amountp2++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("11")){ if(board[1][1] == 0){ board[1][1] = 2; showboard(board); playerturn = 1; amountp2++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("12")){ if(board[1][2] == 0){ board[1][2] = 2; showboard(board); playerturn = 1; amountp2++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("20")){ if(board[2][0] == 0){ board[2][0] = 2; showboard(board); playerturn = 1; amountp2++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("21")){ if(board[2][1] == 0){ board[2][1] = 2; showboard(board); playerturn = 1; amountp2++; } else{ system.out.println("this space occupied!"); } } else if(temp.equalsignorecase("22")){ if(board[2][2] == 0){ board[2][2] = 2; showboard(board); playerturn = 1; amountp2++; } else{ system.out.println("this space occupied!"); } } } } } }
and method printing out board:
public static void showboard(int x[][]){ int rownumber = 1; for(int row = 0 ; row < x.length ; row++){ for(int column = 0 ; column < x[row].length ; column++){ if(x[row][column] == 0){ if(rownumber <= 2){ system.out.print(" e"); rownumber++; } else if(rownumber == 3){ system.out.println(" e"); rownumber = 1; } } else if(x[row][column] == 1){ if(rownumber <= 2){ system.out.print(" x"); rownumber++; } else if(rownumber == 3){ system.out.println(" x"); rownumber = 1; } } else if(x[row][column] == 2){ if(rownumber <= 2){ system.out.print(" o"); rownumber++; } else if(rownumber == 3){ system.out.println(" o"); rownumber = 1; } } } } }
also, aware code big mess , there's problably ton of other, more efficient ways this, since new programmer, chose go functionality on efficiency one.
your else if(playerturn == 2)
line needs pulled out same level if(playerturn == 1)
:
if(playerturn == 1){ if(amountp1 <= 3){ //... } else if(playerturn == 2){ //... } }
should become
if(playerturn == 1){ if(amountp1 <= 3){ //... } } else if(playerturn == 2) { //... }
Comments
Post a Comment